Debian Codegraph @

test/com/google/javascript/jscomp/NodeIteratorsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.Lists;
20 import com.google.javascript.rhino.Node;
21 import com.google.javascript.rhino.Token;
22 import com.google.javascript.jscomp.NodeIterators.FunctionlessLocalScope;
23 import com.google.javascript.jscomp.NodeIterators.LocalVarMotion;
24
25 import java.util.Iterator;
26 import java.util.List;
27 import junit.framework.TestCase;
28
29 /**
30 * Tests for NodeIterators.
31 * @author [email protected] (Nick Santos)
32 */
33 public class NodeIteratorsTest extends TestCase {
34
35 // In each test, we find the declaration of "X" in the local scope,
36 // construct a list of all nodes where X is guaranteed to retain its
37 // original value, and compare those nodes against an expected list of
38 // tokens.
39
40 public void testBasic() {
41 testVarMotionWithCode("var X = 3;", Token.VAR, Token.SCRIPT);
42 }
43
44 public void testNamedFunction() {
45 testVarMotionWithCode("var X = 3; function f() {}",
46 Token.VAR, Token.SCRIPT);
47 }
48
49 public void testNamedFunction2() {
50 testVarMotionWithCode("var X = 3; function f() {} var Y;",
51 Token.VAR, Token.NAME, Token.VAR, Token.SCRIPT);
52 }
53
54 public void testFunctionExpression() {
55 testVarMotionWithCode("var X = 3, Y = function() {}; 3;",
56 Token.NAME, Token.VAR, Token.NUMBER, Token.EXPR_RESULT, Token.SCRIPT);
57 }
58
59 public void testFunctionExpression2() {
60 testVarMotionWithCode("var X = 3; var Y = function() {}; 3;",
61 Token.VAR, Token.NAME, Token.VAR, Token.NUMBER,
62 Token.EXPR_RESULT, Token.SCRIPT);
63 }
64
65 public void testHaltAtVarRef() {
66 testVarMotionWithCode("var X, Y = 3; var Z = X;",
67 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
68 }
69
70 public void testHaltAtVarRef2() {
71 testVarMotionWithCode("var X, Y = 3; (function() {})(3, X);",
72 Token.NUMBER, Token.NAME, Token.VAR, Token.NUMBER, Token.NAME);
73 }
74
75 public void testHaltAtVarRef3() {
76 testVarMotionWithCode("var X, Y = 3; X;",
77 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
78 }
79
80 public void testHaltAtSideEffects() {
81 testVarMotionWithCode("var X, Y = 3; var Z = B(3);",
82 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME, Token.NUMBER);
83 }
84
85 public void testHaltAtSideEffects2() {
86 testVarMotionWithCode("var A = 1, X = A, Y = 3; delete A;",
87 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
88 }
89
90 public void testHaltAtSideEffects3() {
91 testVarMotionWithCode("var A = 1, X = A, Y = 3; A++;",
92 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
93 }
94
95 public void testHaltAtSideEffects4() {
96 testVarMotionWithCode("var A = 1, X = A, Y = 3; A--;",
97 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
98 }
99
100 public void testHaltAtSideEffects5() {
101 testVarMotionWithCode("var A = 1, X = A, Y = 3; A = 'a';",
102 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME, Token.STRING);
103 }
104
105 public void testNoHaltReadWhenValueIsImmutable() {
106 testVarMotionWithCode("var X = 1, Y = 3; alert();",
107 Token.NUMBER, Token.NAME, Token.VAR, Token.NAME);
108 }
109
110 public void testHaltReadWhenValueHasSideEffects() {
111 testVarMotionWithCode("var X = f(), Y = 3; alert();",
112 Token.NUMBER, Token.NAME, Token.VAR);
113 }
114
115 public void testCatchBlock() {
116 testVarMotionWithCode("var X = 1; try { 4; } catch (X) {}",
117 Token.VAR, Token.NUMBER, Token.EXPR_RESULT, Token.BLOCK);
118 }
119
120 public void testIfBranch() {
121 testVarMotionWithCode("var X = foo(); if (X) {}",
122 Token.VAR, Token.NAME);
123 }
124
125 /**
126 * Parses the given code, finds the variable X in the global scope, and runs
127 * the VarMotion iterator. Asserts that the iteration order matches the
128 * tokens given.
129 */
130 private void testVarMotionWithCode(String code, int ... expectedTokens) {
131 List<Integer> expectedList = Lists.newArrayList();
132 for (int token : expectedTokens) {
133 expectedList.add(token);
134 }
135 testVarMotionWithCode(code, expectedList);
136 }
137
138 /**
139 * @see #testVarMotionWithCode(String, int ...)
140 */
141 private void testVarMotionWithCode(String code,
142 List<Integer> expectedTokens) {
143 List<Node> ancestors = Lists.newArrayList();
144
145 // Add an empty node to the beginning of the code and start there.
146 Node root = (new Compiler()).parseTestCode(";" + code);
147 for (Node n = root; n != null; n = n.getFirstChild()) {
148 ancestors.add(0, n);
149 }
150
151 FunctionlessLocalScope searchIt = new FunctionlessLocalScope(
152 ancestors.toArray(new Node[ancestors.size()]));
153
154 boolean found = false;
155 while (searchIt.hasNext()) {
156 Node n = searchIt.next();
157 if (n.isName() &&
158 searchIt.currentParent().isVar() &&
159 n.getString().equals("X")) {
160 found = true;
161 break;
162 }
163 }
164
165 assertTrue("Variable X not found! " + root.toStringTree(), found);
166
167 List<Node> currentAncestors = searchIt.currentAncestors();
168 assert(currentAncestors.size() >= 3);
169 Iterator<Node> moveIt = LocalVarMotion.forVar(
170 currentAncestors.get(0),
171 currentAncestors.get(1),
172 currentAncestors.get(2));
173 List<Integer> actualTokens = Lists.newArrayList();
174 while (moveIt.hasNext()) {
175 actualTokens.add(moveIt.next().getType());
176 }
177
178 assertEquals(expectedTokens, actualTokens);
179 }
180 }