Debian Codegraph @

test/com/google/javascript/rhino/NodeTest.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Nick Santos
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License Version 2 or later (the "GPL"), in which
29 * case the provisions of the GPL are applicable instead of those above. If
30 * you wish to allow use of your version of this file only under the terms of
31 * the GPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replacing
33 * them with the notice and other provisions required by the GPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the GPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 package com.google.javascript.rhino;
40
41 import com.google.javascript.rhino.Node.NodeMismatch;
42 import com.google.javascript.rhino.jstype.JSTypeNative;
43 import com.google.javascript.rhino.jstype.JSTypeRegistry;
44 import com.google.javascript.rhino.testing.TestErrorReporter;
45
46 import junit.framework.TestCase;
47
48 public class NodeTest extends TestCase {
49 public void testMergeExtractNormal() throws Exception {
50 testMergeExtract(5, 6);
51 testMergeExtract(456, 3423);
52 testMergeExtract(0, 0);
53 }
54
55 public void testMergeExtractErroneous() throws Exception {
56 assertEquals(-1, Node.mergeLineCharNo(-5, 90));
57 assertEquals(-1, Node.mergeLineCharNo(0, -1));
58 assertEquals(-1, Node.extractLineno(-1));
59 assertEquals(-1, Node.extractCharno(-1));
60 }
61
62 public void testMergeOverflowGraciously() throws Exception {
63 int linecharno = Node.mergeLineCharNo(89, 4096);
64 assertEquals(89, Node.extractLineno(linecharno));
65 assertEquals(4095, Node.extractCharno(linecharno));
66 }
67
68 public void testCheckTreeEqualsImplSame() {
69 Node node1 = new Node(1, new Node(2));
70 Node node2 = new Node(1, new Node(2));
71 assertEquals(null, node1.checkTreeEqualsImpl(node2));
72 }
73
74 public void testCheckTreeEqualsImplDifferentType() {
75 Node node1 = new Node(1, new Node(2));
76 Node node2 = new Node(2, new Node(2));
77 assertEquals(new NodeMismatch(node1, node2),
78 node1.checkTreeEqualsImpl(node2));
79 }
80
81 public void testCheckTreeEqualsImplDifferentChildCount() {
82 Node node1 = new Node(1, new Node(2));
83 Node node2 = new Node(1);
84 assertEquals(new NodeMismatch(node1, node2),
85 node1.checkTreeEqualsImpl(node2));
86 }
87
88 public void testCheckTreeEqualsImplDifferentChild() {
89 Node child1 = new Node(1);
90 Node child2 = new Node(2);
91 Node node1 = new Node(1, child1);
92 Node node2 = new Node(1, child2);
93 assertEquals(new NodeMismatch(child1, child2),
94 node1.checkTreeEqualsImpl(node2));
95 }
96
97 public void testCheckTreeEqualsSame() {
98 Node node1 = new Node(1);
99 assertEquals(null, node1.checkTreeEquals(node1));
100 }
101
102 public void testCheckTreeEqualsStringDifferent() {
103 Node node1 = new Node(Token.ADD);
104 Node node2 = new Node(Token.SUB);
105 assertNotNull(node1.checkTreeEquals(node2));
106 }
107
108 public void testCheckTreeEqualsBooleanSame() {
109 Node node1 = new Node(1);
110 assertEquals(true, node1.isEquivalentTo(node1));
111 }
112
113 public void testCheckTreeEqualsBooleanDifferent() {
114 Node node1 = new Node(1);
115 Node node2 = new Node(2);
116 assertEquals(false, node1.isEquivalentTo(node2));
117 }
118
119 public void testCheckTreeEqualsSlashVDifferent() {
120 Node node1 = Node.newString("\u000B");
121 node1.putBooleanProp(Node.SLASH_V, true);
122 Node node2 = Node.newString("\u000B");
123 assertEquals(false, node1.isEquivalentTo(node2));
124 }
125
126 public void testCheckTreeEqualsImplDifferentIncProp() {
127 Node node1 = new Node(Token.INC);
128 node1.putIntProp(Node.INCRDECR_PROP, 1);
129 Node node2 = new Node(Token.INC);
130 assertNotNull(node1.checkTreeEqualsImpl(node2));
131 }
132
133 public void testCheckTreeTypeAwareEqualsSame() {
134 TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
135 JSTypeRegistry registry = new JSTypeRegistry(testErrorReporter);
136 Node node1 = Node.newString(Token.NAME, "f");
137 node1.setJSType(registry.getNativeType(JSTypeNative.NUMBER_TYPE));
138 Node node2 = Node.newString(Token.NAME, "f");
139 node2.setJSType(registry.getNativeType(JSTypeNative.NUMBER_TYPE));
140 assertTrue(node1.isEquivalentToTyped(node2));
141 }
142
143 public void testCheckTreeTypeAwareEqualsSameNull() {
144 Node node1 = Node.newString(Token.NAME, "f");
145 Node node2 = Node.newString(Token.NAME, "f");
146 assertTrue(node1.isEquivalentToTyped(node2));
147 }
148
149 public void testCheckTreeTypeAwareEqualsDifferent() {
150 TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
151 JSTypeRegistry registry = new JSTypeRegistry(testErrorReporter);
152 Node node1 = Node.newString(Token.NAME, "f");
153 node1.setJSType(registry.getNativeType(JSTypeNative.NUMBER_TYPE));
154 Node node2 = Node.newString(Token.NAME, "f");
155 node2.setJSType(registry.getNativeType(JSTypeNative.STRING_TYPE));
156 assertFalse(node1.isEquivalentToTyped(node2));
157 }
158
159 public void testCheckTreeTypeAwareEqualsDifferentNull() {
160 TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
161 JSTypeRegistry registry = new JSTypeRegistry(testErrorReporter);
162 Node node1 = Node.newString(Token.NAME, "f");
163 node1.setJSType(registry.getNativeType(JSTypeNative.NUMBER_TYPE));
164 Node node2 = Node.newString(Token.NAME, "f");
165 assertFalse(node1.isEquivalentToTyped(node2));
166 }
167
168 public void testVarArgs1() {
169 assertFalse(new Node(1).isVarArgs());
170 }
171
172 public void testVarArgs2() {
173 Node n = new Node(1);
174 n.setVarArgs(false);
175 assertFalse(n.isVarArgs());
176 }
177
178 public void testVarArgs3() {
179 Node n = new Node(1);
180 n.setVarArgs(true);
181 assertTrue(n.isVarArgs());
182 }
183
184 private void testMergeExtract(int lineno, int charno) {
185 int linecharno = Node.mergeLineCharNo(lineno, charno);
186 assertEquals(lineno, Node.extractLineno(linecharno));
187 assertEquals(charno, Node.extractCharno(linecharno));
188 }
189
190 public void testFileLevelJSDocAppender() {
191 Node n = new Node(1);
192 Node.FileLevelJsDocBuilder builder = n.getJsDocBuilderForNode();
193 builder.append("foo");
194 builder.append("bar");
195 assertEquals("foobar", n.getJSDocInfo().getLicense());
196 }
197
198 public void testIsQualifiedName() {
199 assertTrue(IR.name("a").isQualifiedName());
200 assertTrue(IR.name("$").isQualifiedName());
201 assertTrue(IR.name("_").isQualifiedName());
202 assertTrue(IR.getprop(IR.name("a"),IR.string("b")).isQualifiedName());
203 assertTrue(IR.getprop(IR.thisNode(),IR.string("b")).isQualifiedName());
204 assertFalse(IR.number(0).isQualifiedName());
205 assertFalse(IR.arraylit().isQualifiedName());
206 assertFalse(IR.objectlit().isQualifiedName());
207 assertFalse(IR.string("").isQualifiedName());
208 assertFalse(IR.getelem(IR.name("a"),IR.string("b")).isQualifiedName());
209 assertFalse( // a[b].c
210 IR.getprop(
211 IR.getelem(IR.name("a"),IR.string("b")),
212 IR.string("c"))
213 .isQualifiedName());
214 assertFalse( // a.b[c]
215 IR.getelem(
216 IR.getprop(IR.name("a"),IR.string("b")),
217 IR.string("c"))
218 .isQualifiedName());
219 assertFalse(IR.call(IR.name("a")).isQualifiedName());
220 assertFalse( // a().b
221 IR.getprop(
222 IR.call(IR.name("a")),
223 IR.string("b"))
224 .isQualifiedName());
225 assertFalse( // (a.b)()
226 IR.call(
227 IR.getprop(IR.name("a"),IR.string("b")))
228 .isQualifiedName());
229 assertFalse(IR.string("a").isQualifiedName());
230 assertFalse(IR.regexp(IR.string("x")).isQualifiedName());
231 assertFalse(new Node(Token.INC, IR.name("x")).isQualifiedName());
232 }
233
234 public void testCloneAnnontations() {
235 Node n = getVarRef("a");
236 assertFalse(n.getBooleanProp(Node.IS_CONSTANT_NAME));
237 n.putBooleanProp(Node.IS_CONSTANT_NAME, true);
238 assertTrue(n.getBooleanProp(Node.IS_CONSTANT_NAME));
239
240 Node nodeClone = n.cloneNode();
241 assertTrue(nodeClone.getBooleanProp(Node.IS_CONSTANT_NAME));
242 }
243
244 public void testSharedProps1() {
245 Node n = getVarRef("A");
246 n.putIntProp(Node.SIDE_EFFECT_FLAGS, 5);
247 Node m = new Node(Token.TRUE);
248 m.clonePropsFrom(n);
249 assertEquals(m.getPropListHeadForTesting(), n.getPropListHeadForTesting());
250 assertEquals(5, n.getIntProp(Node.SIDE_EFFECT_FLAGS));
251 assertEquals(5, m.getIntProp(Node.SIDE_EFFECT_FLAGS));
252 }
253
254 public void testSharedProps2() {
255 Node n = getVarRef("A");
256 n.putIntProp(Node.SIDE_EFFECT_FLAGS, 5);
257 Node m = new Node(Token.TRUE);
258 m.clonePropsFrom(n);
259
260 n.putIntProp(Node.SIDE_EFFECT_FLAGS, 6);
261 assertEquals(6, n.getIntProp(Node.SIDE_EFFECT_FLAGS));
262 assertEquals(5, m.getIntProp(Node.SIDE_EFFECT_FLAGS));
263 assertFalse(
264 m.getPropListHeadForTesting() == n.getPropListHeadForTesting());
265
266 m.putIntProp(Node.SIDE_EFFECT_FLAGS, 7);
267 assertEquals(6, n.getIntProp(Node.SIDE_EFFECT_FLAGS));
268 assertEquals(7, m.getIntProp(Node.SIDE_EFFECT_FLAGS));
269 }
270
271 public void testSharedProps3() {
272 Node n = getVarRef("A");
273 n.putIntProp(Node.SIDE_EFFECT_FLAGS, 2);
274 n.putIntProp(Node.INCRDECR_PROP, 3);
275 Node m = new Node(Token.TRUE);
276 m.clonePropsFrom(n);
277
278 n.putIntProp(Node.SIDE_EFFECT_FLAGS, 4);
279 assertEquals(4, n.getIntProp(Node.SIDE_EFFECT_FLAGS));
280 assertEquals(2, m.getIntProp(Node.SIDE_EFFECT_FLAGS));
281 }
282
283 public void testBooleanProp() {
284 Node n = getVarRef("a");
285
286 n.putBooleanProp(Node.IS_CONSTANT_NAME, false);
287
288 assertNull(n.lookupProperty(Node.IS_CONSTANT_NAME));
289 assertFalse(n.getBooleanProp(Node.IS_CONSTANT_NAME));
290
291 n.putBooleanProp(Node.IS_CONSTANT_NAME, true);
292
293 assertNotNull(n.lookupProperty(Node.IS_CONSTANT_NAME));
294 assertTrue(n.getBooleanProp(Node.IS_CONSTANT_NAME));
295
296 n.putBooleanProp(Node.IS_CONSTANT_NAME, false);
297
298 assertNull(n.lookupProperty(Node.IS_CONSTANT_NAME));
299 assertFalse(n.getBooleanProp(Node.IS_CONSTANT_NAME));
300 }
301
302 // Verify that annotations on cloned nodes are properly handled.
303 public void testCloneAnnontations2() {
304 Node n = getVarRef("a");
305 n.putBooleanProp(Node.IS_CONSTANT_NAME, true);
306 n.putBooleanProp(Node.IS_DISPATCHER, true);
307 assertTrue(n.getBooleanProp(Node.IS_CONSTANT_NAME));
308 assertTrue(n.getBooleanProp(Node.IS_DISPATCHER));
309
310 Node nodeClone = n.cloneNode();
311 assertTrue(nodeClone.getBooleanProp(Node.IS_CONSTANT_NAME));
312 assertTrue(nodeClone.getBooleanProp(Node.IS_DISPATCHER));
313
314 n.putBooleanProp(Node.IS_DISPATCHER, false);
315 assertTrue(n.getBooleanProp(Node.IS_CONSTANT_NAME));
316 assertFalse(n.getBooleanProp(Node.IS_DISPATCHER));
317
318 assertTrue(nodeClone.getBooleanProp(Node.IS_CONSTANT_NAME));
319 assertTrue(nodeClone.getBooleanProp(Node.IS_DISPATCHER));
320 }
321
322 public void testGetIndexOfChild() {
323 Node assign = getAssignExpr("b","c");
324 assertEquals(2, assign.getChildCount());
325
326 Node firstChild = assign.getFirstChild();
327 Node secondChild = firstChild.getNext();
328 assertNotNull(secondChild);
329
330 assertEquals(0, assign.getIndexOfChild(firstChild));
331 assertEquals(1, assign.getIndexOfChild(secondChild));
332 assertEquals(-1, assign.getIndexOfChild(assign));
333 }
334
335 public void testCopyInformationFrom() {
336 Node assign = getAssignExpr("b","c");
337 assign.setSourceEncodedPosition(99);
338 assign.setSourceFileForTesting("foo.js");
339
340 Node lhs = assign.getFirstChild();
341 lhs.copyInformationFrom(assign);
342 assertEquals(99, lhs.getSourcePosition());
343 assertEquals("foo.js", lhs.getSourceFileName());
344
345 assign.setSourceEncodedPosition(101);
346 assign.setSourceFileForTesting("bar.js");
347 lhs.copyInformationFrom(assign);
348 assertEquals(99, lhs.getSourcePosition());
349 assertEquals("foo.js", lhs.getSourceFileName());
350 }
351
352 public void testUseSourceInfoIfMissingFrom() {
353 Node assign = getAssignExpr("b","c");
354 assign.setSourceEncodedPosition(99);
355 assign.setSourceFileForTesting("foo.js");
356
357 Node lhs = assign.getFirstChild();
358 lhs.useSourceInfoIfMissingFrom(assign);
359 assertEquals(99, lhs.getSourcePosition());
360 assertEquals("foo.js", lhs.getSourceFileName());
361
362 assign.setSourceEncodedPosition(101);
363 assign.setSourceFileForTesting("bar.js");
364 lhs.useSourceInfoIfMissingFrom(assign);
365 assertEquals(99, lhs.getSourcePosition());
366 assertEquals("foo.js", lhs.getSourceFileName());
367 }
368
369 public void testUseSourceInfoFrom() {
370 Node assign = getAssignExpr("b","c");
371 assign.setSourceEncodedPosition(99);
372 assign.setSourceFileForTesting("foo.js");
373
374 Node lhs = assign.getFirstChild();
375 lhs.useSourceInfoFrom(assign);
376 assertEquals(99, lhs.getSourcePosition());
377 assertEquals("foo.js", lhs.getSourceFileName());
378
379 assign.setSourceEncodedPosition(101);
380 assign.setSourceFileForTesting("bar.js");
381 lhs.useSourceInfoFrom(assign);
382 assertEquals(101, lhs.getSourcePosition());
383 assertEquals("bar.js", lhs.getSourceFileName());
384 }
385
386 public void testInvalidSourceOffset() {
387 Node string = Node.newString("a");
388
389 string.setSourceEncodedPosition(-1);
390 assertTrue(string.getSourceOffset() < 0);
391
392 string.setSourceFileForTesting("foo.js");
393 assertTrue(string.getSourceOffset() < 0);
394 }
395
396 public void testQualifiedName() {
397 assertNull(IR.name("").getQualifiedName());
398 assertEquals("a", IR.name("a").getQualifiedName());
399 assertEquals(
400 "a.b", IR.getprop(IR.name("a"), IR.string("b")).getQualifiedName());
401 assertEquals(
402 "this.b", IR.getprop(IR.thisNode(), IR.string("b")).getQualifiedName());
403 assertNull(
404 IR.getprop(IR.call(IR.name("a")), IR.string("b")).getQualifiedName());
405 }
406
407 private static Node getVarRef(String name) {
408 return Node.newString(Token.NAME, name);
409 }
410
411 private static Node getAssignExpr(String name1, String name2) {
412 return new Node(Token.ASSIGN, getVarRef(name1), getVarRef(name2));
413 }
414 }