Debian Codegraph @

test/com/google/javascript/jscomp/FunctionArgumentInjectorTest.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.base.Preconditions;
20 import com.google.common.base.Supplier;
21 import com.google.common.collect.Sets;
22 import com.google.javascript.rhino.Node;
23 import junit.framework.TestCase;
24
25 import java.util.Collections;
26 import java.util.Map;
27 import java.util.Set;
28
29 /**
30 * Inline function tests.
31 * @author [email protected] (John Lenz)
32 */
33 public class FunctionArgumentInjectorTest extends TestCase {
34
35 // TODO(johnlenz): Add unit tests for:
36 // inject
37 // getFunctionCallParameterMap
38
39 private static final Set<String> EMPTY_STRING_SET = Collections.emptySet();
40
41 public void testFindModifiedParameters1() {
42 assertEquals(Sets.newHashSet(),
43 FunctionArgumentInjector.findModifiedParameters(
44 parseFunction("function f(a){ return a==0; }")));
45 }
46
47 public void testFindModifiedParameters2() {
48 assertEquals(Sets.newHashSet(),
49 FunctionArgumentInjector.findModifiedParameters(
50 parseFunction("function f(a){ b=a }")));
51 }
52
53 public void testFindModifiedParameters3() {
54 assertEquals(Sets.newHashSet("a"),
55 FunctionArgumentInjector.findModifiedParameters(
56 parseFunction("function f(a){ a=0 }")));
57 }
58
59 public void testFindModifiedParameters4() {
60 assertEquals(Sets.newHashSet("a", "b"),
61 FunctionArgumentInjector.findModifiedParameters(
62 parseFunction("function f(a,b){ a=0;b=0 }")));
63 }
64
65 public void testFindModifiedParameters5() {
66 assertEquals(Sets.newHashSet("b"),
67 FunctionArgumentInjector.findModifiedParameters(
68 parseFunction("function f(a,b){ a; if (a) b=0 }")));
69 }
70
71 public void testFindModifiedParameters6() {
72 assertEquals(Sets.newHashSet("a", "b"),
73 FunctionArgumentInjector.findModifiedParameters(
74 parseFunction("function f(a,b){ function f(){ a;b; } }")));
75 }
76
77 public void testFindModifiedParameters7() {
78 assertEquals(Sets.newHashSet("b"),
79 FunctionArgumentInjector.findModifiedParameters(
80 parseFunction("function f(a,b){ a; function f(){ b; } }")));
81 }
82
83 public void testFindModifiedParameters8() {
84 assertEquals(Sets.newHashSet("b"),
85 FunctionArgumentInjector.findModifiedParameters(
86 parseFunction(
87 "function f(a,b){ "+
88 "a; function f(){ function g() { b; } } }")));
89 }
90
91 public void testFindModifiedParameters9() {
92 assertEquals(Sets.newHashSet("a", "b"),
93 FunctionArgumentInjector.findModifiedParameters(
94 parseFunction("function f(a,b){ (function(){ a;b; }) }")));
95 }
96
97 public void testFindModifiedParameters10() {
98 assertEquals(Sets.newHashSet("b"),
99 FunctionArgumentInjector.findModifiedParameters(
100 parseFunction("function f(a,b){ a; (function (){ b; }) }")));
101 }
102
103 public void testFindModifiedParameters11() {
104 assertEquals(Sets.newHashSet("b"),
105 FunctionArgumentInjector.findModifiedParameters(
106 parseFunction(
107 "function f(a,b){ "+
108 "a; (function(){ (function () { b; }) }) }")));
109 }
110
111 public void testMaybeAddTempsForCallArguments1() {
112 // Parameters with side-effects must be executed
113 // even if they aren't referenced.
114 testNeededTemps(
115 "function foo(a,b){}; foo(goo(),goo());",
116 "foo",
117 Sets.newHashSet("a", "b"));
118 }
119
120 public void testMaybeAddTempsForCallArguments2() {
121 // Unreferenced parameters without side-effects
122 // can be ignored.
123 testNeededTemps(
124 "function foo(a,b){}; foo(1,2);",
125 "foo",
126 EMPTY_STRING_SET);
127 }
128
129 public void testMaybeAddTempsForCallArguments3() {
130 // Referenced parameters without side-effects
131 // don't need temps.
132 testNeededTemps(
133 "function foo(a,b){a;b;}; foo(x,y);",
134 "foo",
135 EMPTY_STRING_SET);
136 }
137
138 public void testMaybeAddTempsForCallArguments4() {
139 // Parameters referenced after side-effect must
140 // be assigned to temps.
141 testNeededTemps(
142 "function foo(a,b){a;goo();b;}; foo(x,y);",
143 "foo",
144 Sets.newHashSet("b"));
145 }
146
147 public void testMaybeAddTempsForCallArguments5() {
148 // Parameters referenced after out-of-scope side-effect must
149 // be assigned to temps.
150 testNeededTemps(
151 "function foo(a,b){x = b; y = a;}; foo(x,y);",
152 "foo",
153 Sets.newHashSet("a"));
154 }
155
156 public void testMaybeAddTempsForCallArguments6() {
157 // Parameter referenced after a out-of-scope side-effect must
158 // be assigned to a temp.
159 testNeededTemps(
160 "function foo(a){x++;a;}; foo(x);",
161 "foo",
162 Sets.newHashSet("a"));
163 }
164
165 public void testMaybeAddTempsForCallArguments7() {
166 // No temp needed after local side-effects.
167 testNeededTemps(
168 "function foo(a){var c; c=0; a;}; foo(x);",
169 "foo",
170 EMPTY_STRING_SET);
171 }
172
173 public void testMaybeAddTempsForCallArguments8() {
174 // Temp needed for side-effects to object using local name.
175 testNeededTemps(
176 "function foo(a){var c = {}; c.goo=0; a;}; foo(x);",
177 "foo",
178 Sets.newHashSet("a"));
179 }
180
181 public void testMaybeAddTempsForCallArguments9() {
182 // Parameters referenced in a loop with side-effects must
183 // be assigned to temps.
184 testNeededTemps(
185 "function foo(a,b){while(true){a;goo();b;}}; foo(x,y);",
186 "foo",
187 Sets.newHashSet("a", "b"));
188 }
189
190 public void testMaybeAddTempsForCallArguments10() {
191 // No temps for parameters referenced in a loop with no side-effects.
192 testNeededTemps(
193 "function foo(a,b){while(true){a;true;b;}}; foo(x,y);",
194 "foo",
195 EMPTY_STRING_SET);
196 }
197
198 public void testMaybeAddTempsForCallArguments11() {
199 // Parameters referenced in a loop with side-effects must
200 // be assigned to temps.
201 testNeededTemps(
202 "function foo(a,b){do{a;b;}while(goo());}; foo(x,y);",
203 "foo",
204 Sets.newHashSet("a", "b"));
205 }
206
207 public void testMaybeAddTempsForCallArguments12() {
208 // Parameters referenced in a loop with side-effects must
209 // be assigned to temps.
210 testNeededTemps(
211 "function foo(a,b){for(;;){a;b;goo();}}; foo(x,y);",
212 "foo",
213 Sets.newHashSet("a", "b"));
214 }
215
216 public void testMaybeAddTempsForCallArguments13() {
217 // Parameters referenced in a inner loop without side-effects must
218 // be assigned to temps if the outer loop has side-effects.
219 testNeededTemps(
220 "function foo(a,b){for(;;){for(;;){a;b;}goo();}}; foo(x,y);",
221 "foo",
222 Sets.newHashSet("a", "b"));
223 }
224
225 public void testMaybeAddTempsForCallArguments14() {
226 // Parameters referenced in a loop must
227 // be assigned to temps.
228 testNeededTemps(
229 "function foo(a,b){goo();for(;;){a;b;}}; foo(x,y);",
230 "foo",
231 Sets.newHashSet("a", "b"));
232 }
233
234 public void testMaybeAddTempsForCallArguments20() {
235 // A long string referenced more than once should have a temp.
236 testNeededTemps(
237 "function foo(a){a;a;}; foo(\"blah blah\");",
238 "foo",
239 Sets.newHashSet("a"));
240 }
241
242 public void testMaybeAddTempsForCallArguments21() {
243 // A short string referenced once should not have a temp.
244 testNeededTemps(
245 "function foo(a){a;a;}; foo(\"\");",
246 "foo",
247 EMPTY_STRING_SET);
248 }
249
250 public void testMaybeAddTempsForCallArguments22() {
251 // A object literal not referenced.
252 testNeededTemps(
253 "function foo(a){}; foo({x:1});",
254 "foo",
255 EMPTY_STRING_SET);
256 // A object literal referenced, should have a temp.
257 testNeededTemps(
258 "function foo(a){a;}; foo({x:1});",
259 "foo",
260 Sets.newHashSet("a"));
261 // A object literal, referenced more than once, should have a temp.
262 testNeededTemps(
263 "function foo(a){a;a;}; foo({x:1});",
264 "foo",
265 Sets.newHashSet("a"));
266 }
267
268 public void testMaybeAddTempsForCallArguments23() {
269 // A array literal, not referenced.
270 testNeededTemps(
271 "function foo(a){}; foo([1,2]);",
272 "foo",
273 EMPTY_STRING_SET);
274 // A array literal, referenced once, should have a temp.
275 testNeededTemps(
276 "function foo(a){a;}; foo([1,2]);",
277 "foo",
278 Sets.newHashSet("a"));
279 // A array literal, referenced more than once, should have a temp.
280 testNeededTemps(
281 "function foo(a){a;a;}; foo([1,2]);",
282 "foo",
283 Sets.newHashSet("a"));
284 }
285
286 public void testMaybeAddTempsForCallArguments24() {
287 // A regex literal, not referenced.
288 testNeededTemps(
289 "function foo(a){}; foo(/mac/);",
290 "foo",
291 EMPTY_STRING_SET);
292 // A regex literal, referenced once, should have a temp.
293 testNeededTemps(
294 "function foo(a){a;}; foo(/mac/);",
295 "foo",
296 Sets.newHashSet("a"));
297 // A regex literal, referenced more than once, should have a temp.
298 testNeededTemps(
299 "function foo(a){a;a;}; foo(/mac/);",
300 "foo",
301 Sets.newHashSet("a"));
302 }
303
304 public void testMaybeAddTempsForCallArguments25() {
305 // A side-effect-less constructor, not referenced.
306 testNeededTemps(
307 "function foo(a){}; foo(new Date());",
308 "foo",
309 EMPTY_STRING_SET);
310 // A side-effect-less constructor, referenced once, should have a temp.
311 testNeededTemps(
312 "function foo(a){a;}; foo(new Date());",
313 "foo",
314 Sets.newHashSet("a"));
315 // A side-effect-less constructor, referenced more than once, should have
316 // a temp.
317 testNeededTemps(
318 "function foo(a){a;a;}; foo(new Date());",
319 "foo",
320 Sets.newHashSet("a"));
321 }
322
323 public void testMaybeAddTempsForCallArguments26() {
324 // A constructor, not referenced.
325 testNeededTemps(
326 "function foo(a){}; foo(new Bar());",
327 "foo",
328 Sets.newHashSet("a"));
329 // A constructor, referenced once, should have a temp.
330 testNeededTemps(
331 "function foo(a){a;}; foo(new Bar());",
332 "foo",
333 Sets.newHashSet("a"));
334 // A constructor, referenced more than once, should have a temp.
335 testNeededTemps(
336 "function foo(a){a;a;}; foo(new Bar());",
337 "foo",
338 Sets.newHashSet("a"));
339 }
340
341 public void testMaybeAddTempsForCallArguments27() {
342 // Ensure the correct parameter is given a temp, when there is
343 // a this value in the call.
344 testNeededTemps(
345 "function foo(a,b,c){}; foo.call(this,1,goo(),2);",
346 "foo",
347 Sets.newHashSet("b"));
348 }
349
350 public void testMaybeAddTempsForCallArguments28() {
351 // true/false are don't need temps
352 testNeededTemps(
353 "function foo(a){a;a;}; foo(true);",
354 "foo",
355 EMPTY_STRING_SET);
356 }
357
358 public void testMaybeAddTempsForCallArguments29() {
359 // true/false are don't need temps
360 testNeededTemps(
361 "function foo(a){a;a;}; foo(false);",
362 "foo",
363 EMPTY_STRING_SET);
364 }
365
366 public void testMaybeAddTempsForCallArguments30() {
367 // true/false are don't need temps
368 testNeededTemps(
369 "function foo(a){a;a;}; foo(!0);",
370 "foo",
371 EMPTY_STRING_SET);
372 }
373
374 public void testMaybeAddTempsForCallArguments31() {
375 // true/false are don't need temps
376 testNeededTemps(
377 "function foo(a){a;a;}; foo(!1);",
378 "foo",
379 EMPTY_STRING_SET);
380 }
381
382 public void testMaybeAddTempsForCallArguments32() {
383 // void 0 doesn't need a temp
384 testNeededTemps(
385 "function foo(a){a;a;}; foo(void 0);",
386 "foo",
387 EMPTY_STRING_SET);
388 }
389
390 public void testMaybeAddTempsForCallArgumentsInLoops() {
391 // A mutable parameter referenced in loop needs a
392 // temporary.
393 testNeededTemps(
394 "function foo(a){for(;;)a;}; foo(new Bar());",
395 "foo",
396 Sets.newHashSet("a"));
397
398 testNeededTemps(
399 "function foo(a){while(true)a;}; foo(new Bar());",
400 "foo",
401 Sets.newHashSet("a"));
402
403 testNeededTemps(
404 "function foo(a){do{a;}while(true)}; foo(new Bar());",
405 "foo",
406 Sets.newHashSet("a"));
407 }
408
409 private void testNeededTemps(
410 String code, String fnName, Set<String> expectedTemps) {
411 Node n = parse(code);
412 Node fn = findFunction(n, fnName);
413 assertNotNull(fn);
414 Node call = findCall(n, fnName);
415 assertNotNull(call);
416 Map<String, Node> args =
417 FunctionArgumentInjector.getFunctionCallParameterMap(
418 fn, call, getNameSupplier());
419
420 Set<String> actualTemps = Sets.newHashSet();
421 FunctionArgumentInjector.maybeAddTempsForCallArguments(
422 fn, args, actualTemps, new ClosureCodingConvention());
423
424 assertEquals(expectedTemps, actualTemps);
425 }
426
427 private static Supplier<String> getNameSupplier() {
428 return new Supplier<String>() {
429 int i = 0;
430 @Override
431 public String get() {
432 return String.valueOf(i++);
433 }
434 };
435 }
436
437 private static Node findCall(Node n, String name) {
438 if (n.isCall()) {
439 Node callee;
440 if (NodeUtil.isGet(n.getFirstChild())) {
441 callee = n.getFirstChild().getFirstChild();
442 Node prop = callee.getNext();
443 // Only "call" is support at this point.
444 Preconditions.checkArgument(prop.isString() &&
445 prop.getString().equals("call"));
446 } else {
447 callee = n.getFirstChild();
448 }
449
450 if (callee.isName()
451 && callee.getString().equals(name)) {
452 return n;
453 }
454 }
455
456 for (Node c : n.children()) {
457 Node result = findCall(c, name);
458 if (result != null) {
459 return result;
460 }
461 }
462
463 return null;
464 }
465
466 private static Node findFunction(Node n, String name) {
467 if (n.isFunction()) {
468 if (n.getFirstChild().getString().equals(name)) {
469 return n;
470 }
471 }
472
473 for (Node c : n.children()) {
474 Node result = findFunction(c, name);
475 if (result != null) {
476 return result;
477 }
478 }
479
480 return null;
481 }
482
483 private static Node parseFunction(String js) {
484 return parse(js).getFirstChild();
485 }
486
487 private static Node parse(String js) {
488 Compiler compiler = new Compiler();
489 Node n = compiler.parseTestCode(js);
490 assertEquals(0, compiler.getErrorCount());
491 return n;
492 }
493 }