Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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 /**
21 * Tests {@link ConstCheck}.
22 *
23 */
24 public class ConstCheckTest extends CompilerTestCase {
25
26 public ConstCheckTest() {
27 enableNormalize();
28 }
29
30 @Override
31 public CompilerPass getProcessor(Compiler compiler) {
32 return new ConstCheck(compiler);
33 }
34
35 @Override
36 public int getNumRepetitions() {
37 return 1;
38 }
39
40 public void testConstantDefinition1() {
41 testSame("var XYZ = 1;");
42 }
43
44 public void testConstantDefinition2() {
45 testSame("var a$b$XYZ = 1;");
46 }
47
48 public void testConstantInitializedInAnonymousNamespace1() {
49 testSame("var XYZ; (function(){ XYZ = 1; })();");
50 }
51
52 public void testConstantInitializedInAnonymousNamespace2() {
53 testSame("var a$b$XYZ; (function(){ a$b$XYZ = 1; })();");
54 }
55
56 public void testObjectModified() {
57 testSame("var IE = true, XYZ = {a:1,b:1}; if (IE) XYZ['c'] = 1;");
58 }
59
60 public void testObjectPropertyInitializedLate() {
61 testSame("var XYZ = {}; for (var i = 0; i < 10; i++) { XYZ[i] = i; }");
62 }
63
64 public void testObjectRedefined1() {
65 testError("var XYZ = {}; XYZ = 2;");
66 }
67
68 public void testConstantRedefined1() {
69 testError("var XYZ = 1; XYZ = 2;");
70 }
71
72 public void testConstantRedefined2() {
73 testError("var a$b$XYZ = 1; a$b$XYZ = 2;");
74 }
75
76 public void testConstantRedefinedInLocalScope1() {
77 testError("var XYZ = 1; (function(){ XYZ = 2; })();");
78 }
79
80 public void testConstantRedefinedInLocalScope2() {
81 testError("var a$b$XYZ = 1; (function(){ a$b$XYZ = 2; })();");
82 }
83
84 public void testConstantRedefinedInLocalScopeOutOfOrder() {
85 testError("function f() { XYZ = 2; } var XYZ = 1;");
86 }
87
88 public void testConstantPostIncremented1() {
89 testError("var XYZ = 1; XYZ++;");
90 }
91
92 public void testConstantPostIncremented2() {
93 testError("var a$b$XYZ = 1; a$b$XYZ++;");
94 }
95
96 public void testConstantPreIncremented1() {
97 testError("var XYZ = 1; XYZ++;");
98 }
99
100 public void testConstantPreIncremented2() {
101 testError("var a$b$XYZ = 1; a$b$XYZ++;");
102 }
103
104 public void testConstantPostDecremented1() {
105 testError("var XYZ = 1; XYZ--;");
106 }
107
108 public void testConstantPostDecremented2() {
109 testError("var a$b$XYZ = 1; a$b$XYZ--;");
110 }
111
112 public void testConstantPreDecremented1() {
113 testError("var XYZ = 1; XYZ--;");
114 }
115
116 public void testConstantPreDecremented2() {
117 testError("var a$b$XYZ = 1; a$b$XYZ--;");
118 }
119
120 public void testAbbreviatedArithmeticAssignment1() {
121 testError("var XYZ = 1; XYZ += 2;");
122 }
123
124 public void testAbbreviatedArithmeticAssignment2() {
125 testError("var a$b$XYZ = 1; a$b$XYZ %= 2;");
126 }
127
128 public void testAbbreviatedBitAssignment1() {
129 testError("var XYZ = 1; XYZ |= 2;");
130 }
131
132 public void testAbbreviatedBitAssignment2() {
133 testError("var a$b$XYZ = 1; a$b$XYZ &= 2;");
134 }
135
136 public void testAbbreviatedShiftAssignment1() {
137 testError("var XYZ = 1; XYZ >>= 2;");
138 }
139
140 public void testAbbreviatedShiftAssignment2() {
141 testError("var a$b$XYZ = 1; a$b$XYZ <<= 2;");
142 }
143
144 public void testConstAnnotation() {
145 testError("/** @const */ var xyz = 1; xyz = 3;");
146 }
147
148 public void testConstSuppression() {
149 testSame("/**\n" +
150 " * @fileoverview\n" +
151 " * @suppress {const}\n" +
152 " */\n" +
153 "/** @const */ var xyz = 1; xyz = 3;");
154 }
155
156 private void testError(String js) {
157 test(js, null, ConstCheck.CONST_REASSIGNED_VALUE_ERROR);
158 }
159 }