OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 package com.google.dart.compiler.resolver; | |
6 | |
7 import com.google.common.base.Joiner; | |
8 import com.google.dart.compiler.DartCompilerErrorCode; | |
9 | |
10 /** | |
11 * Tests the code in {@link CompileTimeConstantVisitor} | |
12 */ | |
13 public class CompileTimeConstantTest extends ResolverTestCase { | |
14 | |
15 public void testConstantBinaryExpression() { | |
16 resolveAndTest(Joiner.on("\n").join( | |
17 "class Object {}", | |
18 "class A {", | |
19 " static final INT_LIT = 5;", | |
20 " static final INT_LIT_REF = INT_LIT;", | |
21 " static final DOUBLE_LIT = 1.5;", | |
22 " static final BOOL_LIT = true;", | |
23 " static final STRING_LIT = \"Hello\";", | |
24 " static final BOP1_0 = INT_LIT + 1;", | |
25 " static final BOP1_1 = 1 + INT_LIT;", | |
26 " static final BOP1_2 = INT_LIT - 1;", | |
27 " static final BOP1_3 = 1 - INT_LIT;", | |
28 " static final BOP1_4 = INT_LIT * 1;", | |
29 " static final BOP1_5 = 1 * INT_LIT;", | |
30 " static final BOP1_6 = INT_LIT / 1;", | |
31 " static final BOP1_7 = 1 / INT_LIT;", | |
32 " static final BOP2_0 = DOUBLE_LIT + 1.5;", | |
33 " static final BOP2_1 = 1.5 + DOUBLE_LIT;", | |
34 " static final BOP2_2 = DOUBLE_LIT - 1.5;", | |
35 " static final BOP2_3 = 1.5 - DOUBLE_LIT;", | |
36 " static final BOP2_4 = DOUBLE_LIT * 1.5;", | |
37 " static final BOP2_5 = 1.5 * DOUBLE_LIT;", | |
38 " static final BOP2_6 = DOUBLE_LIT / 1.5;", | |
39 " static final BOP2_7 = 1.5 / DOUBLE_LIT;", | |
40 " static final BOP3_0 = 2 < INT_LIT;", | |
41 " static final BOP3_1 = INT_LIT < 2;", | |
42 " static final BOP3_2 = 2 > INT_LIT;", | |
43 " static final BOP3_3 = INT_LIT > 2;", | |
44 " static final BOP3_4 = 2 < DOUBLE_LIT;", | |
45 " static final BOP3_5 = DOUBLE_LIT < 2;", | |
46 " static final BOP3_6 = 2 > DOUBLE_LIT;", | |
47 " static final BOP3_7 = DOUBLE_LIT > 2;", | |
48 " static final BOP3_8 = 2 <= INT_LIT;", | |
49 " static final BOP3_9 = INT_LIT <= 2;", | |
50 " static final BOP3_10 = 2 >= INT_LIT;", | |
51 " static final BOP3_11 = INT_LIT >= 2;", | |
52 " static final BOP3_12 = 2.0 <= DOUBLE_LIT;", | |
53 " static final BOP3_13 = DOUBLE_LIT <= 2.0;", | |
54 " static final BOP3_14 = 2.0 >= DOUBLE_LIT;", | |
55 " static final BOP3_15 = DOUBLE_LIT >= 2;", | |
56 " static final BOP4_0 = 5 % INT_LIT;", | |
57 " static final BOP4_1 = INT_LIT % 5;", | |
58 " static final BOP4_2 = 5.0 % DOUBLE_LIT;", | |
59 " static final BOP4_3 = DOUBLE_LIT % 5.0;", | |
60 " static final BOP5_0 = 0x80 & 0x04;", | |
61 " static final BOP5_1 = 0x80 | 0x04;", | |
62 " static final BOP5_2 = 0x80 << 0x04;", | |
63 " static final BOP5_3 = 0x80 >> 0x04;", | |
64 " static final BOP5_4 = 0x80 ~/ 0x04;", | |
65 " static final BOP5_5 = 0x80 ^ 0x04;", | |
66 " static final BOP6 = BOOL_LIT && true;", | |
67 " static final BOP7 = false || BOOL_LIT;", | |
68 " static final BOP8 = STRING_LIT == \"World!\";", | |
69 " static final BOP9 = \"Hello\" != STRING_LIT;", | |
70 " static final BOP10 = INT_LIT === INT_LIT_REF;", | |
71 " static final BOP11 = BOOL_LIT !== true;", | |
72 "}")); | |
73 | |
74 resolveAndTest(Joiner.on("\n").join( | |
75 "class Object {}", | |
76 "class int {}", | |
77 "class A {", | |
78 " static int foo() { return 1; }", | |
79 "}", | |
80 "class B {", | |
81 " static final BOP1 = A.foo() * 1;", | |
82 " static final BOP2 = 1 * A.foo();", | |
83 "}"), | |
84 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
85 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
ngeoffray
2011/10/14 09:26:56
It looks like the error messages are duplicated, r
zundel
2011/10/14 10:04:21
What's happening is that first, A.foo() isn't a co
ngeoffray
2011/10/14 10:33:42
I understand why you're getting both errors, accor
| |
86 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
87 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
88 | |
89 resolveAndTest(Joiner.on("\n").join( | |
90 "class Object {}", | |
91 "class int {}", | |
92 "class String {}", | |
93 "class A {", | |
94 " static int foo() { return 1; }", | |
95 " static String bar() { return \"1\"; }", | |
96 "}", | |
97 "class B {", | |
98 " static final BOP1 = 2 < A.foo();", | |
99 " static final BOP2 = A.foo() < 2;", | |
100 " static final BOP3 = 2 < A.bar();", | |
101 " static final BOP4 = A.bar() < 2;", | |
102 "}"), | |
103 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
104 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
105 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
106 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
107 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
108 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
109 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
110 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
111 | |
112 resolveAndTest(Joiner.on("\n").join( | |
113 "class Object {}", | |
114 "class bool {}", | |
115 "class int {}", | |
116 "class double {}", | |
117 "class num {}", | |
118 "class A {", | |
119 " static bool foo() { return true; }", | |
120 "}", | |
121 "class B {", | |
122 " static final BOP1 = 0x80 & 2.0;", | |
123 " static final BOP2 = 2.0 & 0x80;", | |
124 " static final BOP3 = 45 && true;", | |
125 " static final BOP4 = true || 45;", | |
126 " static final BOP5 = true && A.foo();", | |
127 " static final BOP6 = A.foo() && false;", | |
128 "}"), | |
129 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT, | |
130 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT, | |
131 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
132 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
133 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
134 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
135 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
136 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); | |
137 | |
138 resolveAndTest(Joiner.on("\n").join( | |
139 "class Object {}", | |
140 "class bool {}", | |
141 "class int {}", | |
142 "class double {}", | |
143 "class num {}", | |
144 "class A {", | |
145 " static Object foo() { return true; }", | |
146 "}", | |
147 "class B {", | |
148 " const B();", | |
149 " static final OBJECT_LIT = new B();", | |
150 " static final INT_LIT = 1;", | |
151 " static final STRING_LIT = \"true\";", | |
152 " static final BOP1 = STRING_LIT && true;", | |
153 " static final BOP2 = false || STRING_LIT;", | |
154 " static final BOP3 = 59 == OBJECT_LIT;", | |
155 " static final BOP4 = OBJECT_LIT != 59;", | |
156 " static final BOP5 = INT_LIT === OBJECT_LIT;", | |
157 " static final BOP6 = OBJECT_LIT !== true;", | |
158 "}"), | |
159 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
160 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
161 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
162 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
163 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
164 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL); | |
165 | |
166 resolveAndTest(Joiner.on("\n").join( | |
167 "class Object {}", | |
168 "class A {", | |
169 " static final INT_LIT = 5;", | |
170 " static final INT_LIT_REF = INT_LIT;", | |
171 " static final DOUBLE_LIT = 1.5;", | |
172 " static final BOOL_LIT = true;", | |
173 " // Multiple binary expresions", | |
174 " static final BOP1 = 1 * INT_LIT / 3 + INT_LIT + 9;", | |
175 " // Parenthized expression", | |
176 " static final BOP2 = ( 1 > 2 );", | |
177 " static final BOP3 = (1 * 2) + 3;", | |
178 " static final BOP4 = 3 + (1 * 2);", | |
179 "}")); | |
180 | |
181 // Negative Tests | |
182 resolveAndTest(Joiner.on("\n").join( | |
183 "class Object {}", | |
184 "class A {", | |
185 " static final INT_LIT = 5;", | |
186 " static final DOUBLE_LIT = 1.5;", | |
187 " const A();", | |
188 " static final OBJECT_LIT = new A();", | |
189 " // Multiple binary expresions", | |
190 " static final BOP1_0 = 0 + 1 + OBJECT_LIT;", | |
191 " static final BOP1_1 = 0 + OBJECT_LIT + 1;", | |
192 " static final BOP1_2 = OBJECT_LIT + 3 + 9;", | |
193 "}"), | |
194 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
195 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
196 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
197 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
198 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
199 | |
200 resolveAndTest(Joiner.on("\n").join( | |
201 "class Object {}", | |
202 "class A {", | |
203 " static final INT_LIT = 5;", | |
204 " static final DOUBLE_LIT = 1.5;", | |
205 " const A();", | |
206 " static final OBJECT_LIT = new A();", | |
207 " // Multiple binary expresions", | |
208 " static final PP0 = 0 - (1 + OBJECT_LIT);", | |
209 " static final PP1 = 0 + (OBJECT_LIT + 1);", | |
210 " static final PP2 = (OBJECT_LIT + 3) + 9;", | |
211 " static final PP3 = (OBJECT_LIT) + 3 + 9;", | |
212 " static final PP4 = (OBJECT_LIT + 3 + 9);", | |
213 " static final PP5 = OBJECT_LIT + (3 + 9);", | |
214 "}"), | |
215 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
216 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
217 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
218 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
219 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
220 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
221 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
222 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
223 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
224 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
225 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
226 } | |
227 | |
228 public void testConstantUnaryExpression() { | |
229 resolveAndTest(Joiner.on("\n").join( | |
230 "class Object {}", | |
231 "class A {", | |
232 " // Unary expression", | |
233 " static final BOOL_LIT = true;", | |
234 " static final INT_LIT = 123;", | |
235 " static final DOUBLE_LIT = 12.3;", | |
236 " static final UOP1_0 = !BOOL_LIT;", | |
237 " static final UOP1_1 = BOOL_LIT || !true;", | |
238 " static final UOP1_2 = !BOOL_LIT || true;", | |
239 " static final UOP1_3 = !(BOOL_LIT && true);", | |
240 " static final UOP2_0 = ~0xf0;", | |
241 " static final UOP2_1 = ~INT_LIT;", | |
242 " static final UOP2_2 = ~INT_LIT & 123;", | |
243 " static final UOP2_3 = ~(INT_LIT | 0xff);", | |
244 " static final UOP3_0 = -0xf0;", | |
245 " static final UOP3_1 = -INT_LIT;", | |
246 " static final UOP3_2 = -INT_LIT + 123;", | |
247 " static final UOP3_3 = -(INT_LIT * 0xff);", | |
248 " static final UOP3_4 = -0xf0;", | |
249 " static final UOP3_5 = -DOUBLE_LIT;", | |
250 " static final UOP3_6 = -DOUBLE_LIT + 123;", | |
251 " static final UOP3_7 = -(DOUBLE_LIT * 0xff);", | |
252 "}")); | |
253 | |
254 resolveAndTest(Joiner.on("\n").join( | |
255 "class Object {}", | |
256 "class int {}", | |
257 "class A {", | |
258 " // Unary expression", | |
259 " static final BOOL_LIT = true;", | |
260 " static int foo() { return 3; }", | |
261 " static final UOP1 = !5;", | |
262 " static final UOP2 = !foo();", | |
263 " static final UOP3 = !(5);", | |
264 " static final UOP4 = !(foo());", | |
265 "}"), | |
266 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
267 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
268 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
269 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
270 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
271 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); | |
272 } | |
273 | |
274 public void testConstantConstructorAssign() { | |
275 | |
276 resolveAndTest(Joiner.on("\n").join( | |
277 "class Object {}", | |
278 "class A {", | |
279 " const A();", | |
280 " static final a = new A();", // Constant constructor | |
281 "}")); | |
282 | |
283 // Negative tests | |
284 resolveAndTest(Joiner.on("\n").join( | |
285 "class Object {}", | |
286 "class A {", | |
287 " A() {}", | |
288 " static final a = new A();", // Error: not a constant constructor | |
289 "}"), | |
290 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
291 } | |
292 | |
293 public void testConstantLiteralAssign() { | |
294 | |
295 resolveAndTest(Joiner.on("\n").join( | |
296 "class Object {}", | |
297 "class A {", | |
298 " static final b = true;", | |
299 " static final s = \"apple\";", // string literal | |
300 " static final i = 1;", // integer literal | |
301 " static final d = 3.3;", // double literal | |
302 " static final h = 0xf;", // hex literal | |
303 " static final n = null;", // null | |
304 "}")); | |
305 | |
306 // Tegative tests | |
307 resolveAndTest(Joiner.on("\n").join( | |
308 "class Object {}", | |
309 "class A {", | |
310 " static final person = \"earthling\";", | |
311 " static final s = \"Hello ${person}!\";", | |
312 "}"), | |
313 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
314 } | |
315 | |
316 public void testConstantTypedLiteralAssign() { | |
317 resolveAndTest(Joiner.on("\n").join( | |
318 "class Object {}", | |
319 "class List<T> {}", | |
320 "class Map<K,V> {}", | |
321 "class A {", | |
322 " static final aList = const[1, 2, 3];", // array literal | |
323 " static final map = const { \"1\": \"one\", \"2\": \"banana\" };", // map literal | |
324 "}")); | |
325 | |
326 // Negative tests, on literals that are not compile time constants. | |
327 resolveAndTest(Joiner.on("\n").join( | |
328 "class Object {}", | |
329 "class List<T> {}", | |
330 "class A {", | |
331 " // array literal not const", | |
332 " static final aList= [1, 2, 3];", | |
333 "}"), | |
334 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
335 | |
336 resolveAndTest(Joiner.on("\n").join( | |
337 "class Object {}", | |
338 "class List<T> {}", | |
339 "class A {", | |
340 " static foo() { return 1; }", | |
341 " // const array literal contains non-const member", | |
342 " static final aList = const [foo(), 2, 3];", | |
343 "}"), | |
344 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
345 | |
346 resolveAndTest(Joiner.on("\n").join( | |
347 "class Object {}", | |
348 "class Map<K,V> {}", | |
349 "class A {", | |
350 " // map literal is not const", | |
351 " static final aMap = { \"1\": \"one\", \"2\": \"banana\" };", | |
352 "}"), | |
353 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
354 | |
355 resolveAndTest(Joiner.on("\n").join( | |
356 "class Object {}", | |
357 "class String {}", | |
358 "class Map<K,V> {}", | |
359 "class A {", | |
360 " static String foo() { return \"one\"; }", | |
361 " // map literal contains non-const member", | |
362 " static final map = const { \"1\":foo(), \"2\": \"banana\" };", | |
363 "}"), | |
364 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
365 } | |
366 | |
367 public void testConstantVariableAssign() { | |
368 resolveAndTest(Joiner.on("\n").join( | |
369 "class Object {}", | |
370 "class A {", | |
371 " static final i = 1;", | |
372 " static final j = i;", // variable that is a compile-time constant | |
373 "}")); | |
374 | |
375 // Negative tests | |
376 resolveAndTest(Joiner.on("\n").join( | |
377 "class Object {}", | |
378 "class A {", | |
379 " static foo() {return 1;}", | |
380 " static final i = foo();", // Error: not a constant integer | |
381 "}"), | |
382 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
383 } | |
384 } | |
OLD | NEW |