Debian Codegraph @

test/com/google/javascript/jscomp/ControlStructureCheckTest.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 /**
20 * Test for the control structure verification.
21 *
22 */
23 public class ControlStructureCheckTest extends CompilerTestCase {
24 // Rhino parse error message text
25 final String UNLABELED_BREAK =
26 "unlabelled break must be inside loop or switch";
27
28 final String UNEXPECTED_CONTINUE = "continue must be inside loop";
29
30 final String UNEXPECTED_LABLED_CONTINUE =
31 "continue can only use labeles of iteration statements";
32
33 final String UNDEFINED_LABEL = "undefined label";
34
35 @Override
36 public CompilerPass getProcessor(Compiler compiler) {
37 return new ControlStructureCheck(compiler);
38 }
39
40 public void testWhile() {
41 assertNoError("while(1) { break; }");
42 }
43
44 public void testNextedWhile() {
45 assertNoError("while(1) { while(1) { break; } }");
46 }
47
48 public void testBreak() {
49 assertInvalidBreak("break;");
50 }
51
52 public void testContinue() {
53 assertInvalidContinue("continue;");
54 }
55
56 public void testBreakCrossFunction() {
57 assertInvalidBreak("while(1) { function f() { break; } }");
58 }
59
60 public void testBreakCrossFunctionInFor() {
61 assertInvalidBreak("while(1) {for(var f = function () { break; };;) {}}");
62 }
63
64 public void testContinueToSwitch() {
65 assertInvalidContinue("switch(1) {case(1): continue; }");
66 }
67
68 public void testContinueToSwitchWithNoCases() {
69 assertNoError("switch(1){}");
70 }
71
72 public void testContinueToSwitchWithTwoCases() {
73 assertInvalidContinue("switch(1){case(1):break;case(2):continue;}");
74 }
75
76 public void testContinueToSwitchWithDefault() {
77 assertInvalidContinue("switch(1){case(1):break;case(2):default:continue;}");
78 }
79
80 public void testContinueToLabelSwitch() {
81 assertInvalidLabeledContinue(
82 "while(1) {a: switch(1) {case(1): continue a; }}");
83 }
84
85 public void testContinueOutsideSwitch() {
86 assertNoError("b: while(1) { a: switch(1) { case(1): continue b; } }");
87 }
88
89 public void testContinueNotCrossFunction1() {
90 assertNoError("a:switch(1){case(1):function f(){a:while(1){continue a;}}}");
91 }
92
93 public void testContinueNotCrossFunction2() {
94 assertUndefinedLabel(
95 "a:switch(1){case(1):function f(){while(1){continue a;}}}");
96 }
97
98 public void testUseOfWith1() {
99 testSame("with(a){}", ControlStructureCheck.USE_OF_WITH);
100 }
101
102 public void testUseOfWith2() {
103 testSame("/** @suppress {with} */" +
104 "with(a){}");
105 }
106
107 public void testUseOfWith3() {
108 testSame(
109 "function f(expr, context) {\n" +
110 " try {\n" +
111 " /** @suppress{with} */ with (context) {\n" +
112 " return eval('[' + expr + '][0]');\n" +
113 " }\n" +
114 " } catch (e) {\n" +
115 " return null;\n" +
116 " }\n" +
117 "};\n");
118 }
119
120 private void assertNoError(String js) {
121 testSame(js);
122 }
123
124 private void assertInvalidBreak(String js) {
125 testParseError(js, UNLABELED_BREAK);
126 }
127
128 private void assertInvalidContinue(String js) {
129 testParseError(js, UNEXPECTED_CONTINUE);
130 }
131
132 private void assertInvalidLabeledContinue(String js) {
133 testParseError(js, UNEXPECTED_LABLED_CONTINUE);
134 }
135
136 private void assertUndefinedLabel(String js) {
137 testParseError(js, UNDEFINED_LABEL);
138 }
139
140 private void testParseError(String js, String errorText) {
141 Compiler compiler = new Compiler();
142 compiler.parseTestCode(js);
143 assertTrue(compiler.getErrorCount() == 1);
144 String msg = compiler.getErrors()[0].toString();
145 assertTrue(msg.contains(errorText));
146 }
147 }