Debian Codegraph @

test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.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
18 package com.google.javascript.jscomp;
19
20 import com.google.common.base.Preconditions;
21 import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
22 import com.google.javascript.jscomp.NodeTraversal.Callback;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.Token;
25
26 import junit.framework.TestCase;
27
28 /**
29 * @author [email protected] (John Lenz)
30 */
31 public class FunctionToBlockMutatorTest extends TestCase {
32
33 public void testMutateNoReturnWithoutResultAssignment() {
34 helperMutate(
35 "function foo(){}; foo();",
36 "{}",
37 "foo");
38 }
39
40 public void testMutateNoReturnWithResultAssignment() {
41 helperMutate(
42 "function foo(){}; var result = foo();",
43 "{result = void 0}",
44 "foo", true, false);
45 }
46
47
48 public void testMutateNoValueReturnWithoutResultAssignment() {
49 helperMutate(
50 "function foo(){return;}; foo();",
51 "{}",
52 "foo", null);
53 }
54
55 public void testMutateNoValueReturnWithResultAssignment() {
56 helperMutate(
57 "function foo(){return;}; var result = foo();",
58 "{result = void 0}",
59 "foo");
60 }
61
62 public void testMutateValueReturnWithoutResultAssignment() {
63 helperMutate(
64 "function foo(){return true;}; foo();",
65 "{true;}",
66 "foo", null);
67 }
68
69 public void testMutateValueReturnWithResultAssignment() {
70 helperMutate(
71 "function foo(){return true;}; var x=foo();",
72 "{x=true}",
73 "foo", "x", true, false);
74 }
75
76 public void testMutateWithMultipleReturns() {
77 helperMutate(
78 "function foo(){ if (0) {return 0} else {return 1} };" +
79 "var result=foo();",
80 "{" +
81 "JSCompiler_inline_label_foo_0:{" +
82 "if(0) {" +
83 "result=0; break JSCompiler_inline_label_foo_0" +
84 "} else {" +
85 "result=1; break JSCompiler_inline_label_foo_0" +
86 "} result=void 0" +
87 "}" +
88 "}",
89 "foo", true, false);
90 }
91
92 public void testMutateWithParameters1() {
93 // Simple call with useless parameter
94 helperMutate(
95 "function foo(a){return true;}; foo(x);",
96 "{true}",
97 "foo", null);
98 }
99
100 public void testMutateWithParameters2() {
101 // Simple call with parameter
102 helperMutate(
103 "function foo(a){return x;}; foo(x);",
104 "{x}",
105 "foo", null);
106 }
107
108 public void testMutateWithParameters3() {
109 // Parameter has side-effects.
110 helperMutate(
111 "function foo(a){return a;}; " +
112 "function x() { foo(x++); }",
113 "{var a$$inline_0 = x++; a$$inline_0}",
114 "foo", null);
115 }
116
117 public void testMutate8() {
118 // Parameter has side-effects.
119 helperMutate(
120 "function foo(a){return a+a;}; foo(x++);",
121 "{var a$$inline_0 = x++;" +
122 "a$$inline_0 + a$$inline_0;}",
123 "foo", null);
124 }
125
126 public void testMutateInitializeUninitializedVars1() {
127 helperMutate(
128 "function foo(a){var b;return a;}; foo(1);",
129 "{var b$$inline_1=void 0;1}",
130 "foo", null, false, true);
131 }
132
133 public void testMutateInitializeUninitializedVars2() {
134 helperMutate(
135 "function foo(a){for(var b in c)return a;}; foo(1);",
136 "{JSCompiler_inline_label_foo_2:" +
137 "{" +
138 "for(var b$$inline_1 in c){" +
139 "1;break JSCompiler_inline_label_foo_2" +
140 "}" +
141 "}" +
142 "}",
143 "foo", null);
144 }
145
146 public void testMutateCallInLoopVars1() {
147 // baseline: outside a loop, the constant remains constant.
148 boolean callInLoop = false;
149 helperMutate(
150 "function foo(a){var B = bar(); a;}; foo(1);",
151 "{var B$$inline_1=bar(); 1;}",
152 "foo", null, false, callInLoop);
153 // ... in a loop, the constant-ness is removed.
154 // TODO(johnlenz): update this test to look for the const annotation.
155 callInLoop = true;
156 helperMutate(
157 "function foo(a){var B = bar(); a;}; foo(1);",
158 "{var B$$inline_1 = bar(); 1;}",
159 "foo", null, false, callInLoop);
160 }
161
162 public void testMutateFunctionDefinition() {
163 // function declarations are rewritten as function
164 // expressions
165 helperMutate(
166 "function foo(a){function g(){}}; foo(1);",
167 "{var g$$inline_1=function(){};}",
168 "foo", null);
169 }
170
171 public void helperMutate(
172 String code, final String expectedResult, final String fnName) {
173 helperMutate(code, expectedResult, fnName, false, false);
174 }
175
176 public void helperMutate(
177 String code, final String expectedResult, final String fnName,
178 final boolean needsDefaultResult,
179 final boolean isCallInLoop) {
180 helperMutate(code, expectedResult, fnName,
181 "result", needsDefaultResult, isCallInLoop);
182 }
183
184 public void helperMutate(
185 String code, final String expectedResult, final String fnName,
186 final String resultName) {
187 helperMutate(code, expectedResult, fnName, resultName, false, false);
188 }
189
190 private void validateSourceInfo(Compiler compiler, Node subtree) {
191 (new LineNumberCheck(compiler)).setCheckSubTree(subtree);
192 // Source information problems are reported as compiler errors.
193 if (compiler.getErrorCount() != 0) {
194 String msg = "Error encountered: ";
195 for (JSError err : compiler.getErrors()) {
196 msg += err.toString() + "\n";
197 }
198 assertTrue(msg, compiler.getErrorCount() == 0);
199 }
200 }
201
202 public void helperMutate(
203 String code, final String expectedResult, final String fnName,
204 final String resultName,
205 final boolean needsDefaultResult,
206 final boolean isCallInLoop) {
207 final Compiler compiler = new Compiler();
208 final FunctionToBlockMutator mutator = new FunctionToBlockMutator(
209 compiler, compiler.getUniqueNameIdSupplier());
210 Node expectedRoot = parse(compiler, expectedResult);
211 Preconditions.checkState(compiler.getErrorCount() == 0);
212 final Node expected = expectedRoot.getFirstChild();
213 final Node tree = parse(compiler, code);
214 Preconditions.checkState(compiler.getErrorCount() == 0);
215
216 Node externsRoot = new Node(Token.EMPTY);
217 Node mainRoot = tree;
218 MarkNoSideEffectCalls mark = new MarkNoSideEffectCalls(compiler);
219 mark.process(externsRoot, mainRoot);
220
221 final Node fnNode = findFunction(tree, fnName);
222
223 // Fake precondition.
224 compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);
225
226 // inline tester
227 Method tester = new Method() {
228 @Override
229 public boolean call(NodeTraversal t, Node n, Node parent) {
230
231 Node result = mutator.mutate(
232 fnName, fnNode, n, resultName,
233 needsDefaultResult, isCallInLoop);
234 validateSourceInfo(compiler, result);
235 String explanation = expected.checkTreeEquals(result);
236 assertNull("\nExpected: " + compiler.toSource(expected) +
237 "\nResult: " + compiler.toSource(result) +
238 "\n" + explanation, explanation);
239 return true;
240 }
241 };
242
243 compiler.resetUniqueNameId();
244 TestCallback test = new TestCallback(fnName, tester);
245 NodeTraversal.traverse(compiler, tree, test);
246 }
247
248 interface Method {
249 boolean call(NodeTraversal t, Node n, Node parent);
250 }
251
252 class TestCallback implements Callback {
253
254 private final String callname;
255 private final Method method;
256 private boolean complete = false;
257
258 TestCallback(String callname, Method method) {
259 this.callname = callname;
260 this.method = method;
261 }
262
263 @Override
264 public boolean shouldTraverse(
265 NodeTraversal nodeTraversal, Node n, Node parent) {
266 return !complete;
267 }
268
269 @Override
270 public void visit(NodeTraversal t, Node n, Node parent) {
271 if (n.isCall()) {
272 Node first = n.getFirstChild();
273 if (first.isName() &&
274 first.getString().equals(callname)) {
275 complete = method.call(t, n, parent);
276 }
277 }
278
279 if (parent == null) {
280 assertTrue(complete);
281 }
282 }
283 }
284
285 private static Node findFunction(Node n, String name) {
286 if (n.isFunction()) {
287 if (n.getFirstChild().getString().equals(name)) {
288 return n;
289 }
290 }
291
292 for (Node c : n.children()) {
293 Node result = findFunction(c, name);
294 if (result != null) {
295 return result;
296 }
297 }
298
299 return null;
300 }
301
302 private static Node parse(Compiler compiler, String js) {
303 Node n = compiler.parseTestCode(js);
304 assertEquals(0, compiler.getErrorCount());
305 return n;
306 }
307 }