Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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 /**
20 * Unit tests for {@link StatementFusion}.
21 *
22 */
23 public class StatementFusionTest extends CompilerTestCase {
24
25 @Override
26 public void setUp() throws Exception {
27 super.setUp();
28 enableLineNumberCheck(true);
29 }
30
31 @Override
32 public CompilerPass getProcessor(final Compiler compiler) {
33 PeepholeOptimizationsPass peepholePass =
34 new PeepholeOptimizationsPass(compiler, new StatementFusion());
35
36 return peepholePass;
37 }
38
39 public void testNothingToDo() {
40 fuseSame("");
41 fuseSame("a");
42 fuseSame("a()");
43 fuseSame("if(a()){}");
44 }
45
46 public void testFoldBlockWithStatements() {
47 fuse("a;b;c", "a,b,c");
48 fuse("a();b();c();", "a(),b(),c()");
49 fuse("a(),b();c(),d()", "a(),b(),c(),d()");
50 fuse("a();b(),c(),d()", "a(),b(),c(),d()");
51 fuse("a(),b(),c();d()", "a(),b(),c(),d()");
52 }
53
54 public void testFoldBlockIntoIf() {
55 fuse("a;b;c;if(x){}", "if(a,b,c,x){}");
56 fuse("a;b;c;if(x,y){}else{}", "if(a,b,c,x,y){}else{}");
57 fuse("a;b;c;if(x,y){}", "if(a,b,c,x,y){}");
58 fuse("a;b;c;if(x,y,z){}", "if(a,b,c,x,y,z){}");
59
60 // Can't fuse if there are statements after the IF.
61 fuseSame("a();if(a()){}a()");
62 }
63
64 public void testFoldBlockReturn() {
65 fuse("a;b;c;return x", "return a,b,c,x");
66 fuse("a;b;c;return x+y", "return a,b,c,x+y");
67
68 // DeadAssignmentElimination would have cleaned it up anyways.
69 fuseSame("a;b;c;return x;a;b;c");
70 }
71
72 public void testFoldBlockThrow() {
73 fuse("a;b;c;throw x", "throw a,b,c,x");
74 fuse("a;b;c;throw x+y", "throw a,b,c,x+y");
75 fuseSame("a;b;c;throw x;a;b;c");
76 }
77
78 public void testFoldSwitch() {
79 fuse("a;b;c;switch(x){}", "switch(a,b,c,x){}");
80 }
81
82 public void testFuseIntoForIn() {
83 fuse("a;b;c;for(x in y){}", "for(x in a,b,c,y){}");
84 fuseSame("a();for(var x = b() in y){}");
85 }
86
87 public void testNoFuseIntoWhile() {
88 fuseSame("a;b;c;while(x){}");
89 }
90
91 public void testNoFuseIntoDo() {
92 fuseSame("a;b;c;do{}while(x)");
93 }
94
95 public void testNoGlobalSchopeChanges() {
96 testSame("a,b,c");
97 }
98
99 public void testNoFunctionBlockChanges() {
100 testSame("function foo() { a,b,c }");
101 }
102
103 private void fuse(String before, String after) {
104 test("function F(){if(CONDITION){" + before + "}}",
105 "function F(){if(CONDITION){" + after + "}}");
106 }
107
108 private void fuseSame(String code) {
109 fuse(code, code);
110 }
111 }