Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.javascript.jscomp.CheckLevel;
20
21 /**
22 * Tests for {@link CheckUnreachableCode}.
23 *
24 */
25 public class CheckUnreachableCodeTest extends CompilerTestCase {
26 @Override
27 protected CompilerPass getProcessor(Compiler compiler) {
28 return new CombinedCompilerPass(compiler,
29 new CheckUnreachableCode(compiler, CheckLevel.ERROR));
30 }
31
32 public void testCorrectSimple() {
33 testSame("var x");
34 testSame("var x = 1");
35 testSame("var x = 1; x = 2;");
36 testSame("if (x) { var x = 1 }");
37 testSame("if (x) { var x = 1 } else { var y = 2 }");
38 testSame("while(x) {}");
39 }
40
41 public void testIncorrectSimple() {
42 assertUnreachable("function f() { return; x=1; }");
43 assertUnreachable("function f() { return; x=1; x=1; }");
44 assertUnreachable("function f() { return; var x = 1; }");
45 }
46
47 public void testCorrectIfReturns() {
48 testSame("function f() { if (x) { return } }");
49 testSame("function f() { if (x) { return } return }");
50 testSame("function f() { if (x) { if (y) { return } } else { return }}");
51 testSame("function f()" +
52 "{ if (x) { if (y) { return } return } else { return }}");
53 }
54
55 public void testInCorrectIfReturns() {
56 assertUnreachable(
57 "function f() { if (x) { return } else { return } return }");
58 }
59
60 public void testCorrectSwitchReturn() {
61 testSame("function f() { switch(x) { default: return; case 1: x++; }}");
62 testSame("function f() {" +
63 "switch(x) { default: return; case 1: x++; } return }");
64 testSame("function f() {" +
65 "switch(x) { default: return; case 1: return; }}");
66 testSame("function f() {" +
67 "switch(x) { case 1: return; } return }");
68 testSame("function f() {" +
69 "switch(x) { case 1: case 2: return; } return }");
70 testSame("function f() {" +
71 "switch(x) { case 1: return; case 2: return; } return }");
72 testSame("function f() {" +
73 "switch(x) { case 1 : return; case 2: return; } return }");
74 }
75
76 public void testInCorrectSwitchReturn() {
77 assertUnreachable("function f() {" +
78 "switch(x) { default: return; case 1: return; } return }");
79 assertUnreachable("function f() {" +
80 "switch(x) { default: return; return; case 1: return; } }");
81 }
82
83 public void testCorrectLoopBreaksAndContinues() {
84 testSame("while(1) { foo(); break }");
85 testSame("while(1) { foo(); continue }");
86 testSame("for(;;) { foo(); break }");
87 testSame("for(;;) { foo(); continue }");
88 testSame("for(;;) { if (x) { break } }");
89 testSame("for(;;) { if (x) { continue } }");
90 testSame("do { foo(); continue} while(1)");
91 }
92
93 public void testInCorrectLoopBreaksAndContinues() {
94 assertUnreachable("while(1) { foo(); break; bar()}");
95 assertUnreachable("while(1) { foo(); continue; bar() }");
96 assertUnreachable("for(;;) { foo(); break; bar() }");
97 assertUnreachable("for(;;) { foo(); continue; bar() }");
98 assertUnreachable("for(;;) { if (x) { break; bar() } }");
99 assertUnreachable("for(;;) { if (x) { continue; bar() } }");
100 assertUnreachable("do { foo(); continue; bar()} while(1)");
101 }
102
103 public void testUncheckedWhileInDo() {
104 assertUnreachable("do { foo(); break} while(1)");
105 }
106
107 public void testUncheckedConditionInFor() {
108 assertUnreachable("for(var x = 0; x < 100; x++) { break };");
109 }
110
111 public void testFunctionDeclaration() {
112 // functions are not in our CFG.
113 testSame("function f() { return; function ff() { }}");
114 }
115
116 public void testVarDeclaration() {
117 assertUnreachable("function f() { return; var x = 1 }");
118 // I think the user should fix this as well.
119 assertUnreachable("function f() { return; var x }");
120 }
121
122 public void testReachableTryCatchFinally() {
123 testSame("try { } finally { }");
124 testSame("try { foo(); } finally bar(); ");
125 testSame("try { foo() } finally { bar() }");
126 testSame("try { foo(); } catch (e) {e()} finally bar(); ");
127 testSame("try { foo() } catch (e) {e()} finally { bar() }");
128 }
129
130 public void testUnreachableCatch() {
131 assertUnreachable("try { var x = 0 } catch (e) { }");
132 }
133
134 public void testSpuriousBreak() {
135 testSame("switch (x) { default: throw x; break; }");
136 }
137
138 public void testInstanceOfThrowsException() {
139 testSame("function f() {try { if (value instanceof type) return true; } " +
140 "catch (e) { }}");
141 }
142
143 public void testFalseCondition() {
144 assertUnreachable("if(false) { }");
145 assertUnreachable("if(0) { }");
146 }
147
148 public void testUnreachableLoop() {
149 assertUnreachable("while(false) {}");
150 }
151
152 public void testInfiniteLoop() {
153 testSame("while (true) { foo(); break; }");
154
155 // TODO(user): Have a infinite loop warning instead.
156 assertUnreachable("while(true) {} foo()");
157 }
158
159 public void testSuppression() {
160 assertUnreachable("if(false) { }");
161
162 testSame(
163 "/** @fileoverview\n" +
164 " * @suppress {uselessCode}\n" +
165 " */\n" +
166 "if(false) { }");
167
168 testSame(
169 "/** @fileoverview\n" +
170 " * @suppress {uselessCode}\n" +
171 " */\n" +
172 "function f() { if(false) { } }");
173
174 testSame(
175 "/**\n" +
176 " * @suppress {uselessCode}\n" +
177 " */\n" +
178 "function f() { if(false) { } }");
179
180 assertUnreachable(
181 "/**\n" +
182 " * @suppress {uselessCode}\n" +
183 " */\n" +
184 "function f() { if(false) { } }\n" +
185 "function g() { if(false) { } }\n");
186
187 testSame(
188 "/**\n" +
189 " * @suppress {uselessCode}\n" +
190 " */\n" +
191 "function f() {\n" +
192 " function g() { if(false) { } }\n" +
193 " if(false) { } }\n");
194
195 assertUnreachable(
196 "function f() {\n" +
197 " /**\n" +
198 " * @suppress {uselessCode}\n" +
199 " */\n" +
200 " function g() { if(false) { } }\n" +
201 " if(false) { } }\n");
202
203 testSame(
204 "function f() {\n" +
205 " /**\n" +
206 " * @suppress {uselessCode}\n" +
207 " */\n" +
208 " function g() { if(false) { } }\n" +
209 "}\n");
210 }
211
212 private void assertUnreachable(String js) {
213 test(js, js, CheckUnreachableCode.UNREACHABLE_CODE);
214 }
215 }