Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
|
floitsch
2011/10/14 22:46:15
Please add tests where the variables are in the wr
zundel
2011/10/17 02:56:02
I added a disabled test with a TODO
| |
| 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, | |
| 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 int {}", | |
| 115 "class double {}", | |
| 116 "class num {}", | |
| 117 "class A {", | |
| 118 " static final BOP1 = 0x80 & 2.0;", | |
| 119 " static final BOP2 = 2.0 & 0x80;", | |
| 120 "}"), | |
| 121 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT, | |
| 122 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT); | |
| 123 | |
| 124 resolveAndTest(Joiner.on("\n").join( | |
| 125 "class Object {}", | |
| 126 "class bool {}", | |
| 127 "class int {}", | |
| 128 "class double {}", | |
| 129 "class num {}", | |
| 130 "class A {", | |
| 131 " static bool foo() { return true; }", | |
| 132 "}", | |
| 133 " class B {", | |
| 134 " static final BOP3 = 45 && true;", | |
| 135 " static final BOP4 = true || 45;", | |
| 136 " static final BOP5 = true && A.foo();", | |
| 137 " static final BOP6 = A.foo() && false;", | |
| 138 "}"), | |
| 139 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 140 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 141 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 142 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 143 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 144 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); | |
| 145 | |
| 146 resolveAndTest(Joiner.on("\n").join( | |
| 147 "class Object {}", | |
| 148 "class bool {}", | |
| 149 "class int {}", | |
| 150 "class double {}", | |
| 151 "class num {}", | |
| 152 "class A {", | |
| 153 " static Object foo() { return true; }", | |
| 154 "}", | |
| 155 "class B {", | |
| 156 " const B();", | |
| 157 " static final OBJECT_LIT = const B();", | |
| 158 " static final INT_LIT = 1;", | |
| 159 " static final STRING_LIT = \"true\";", | |
| 160 " static final BOP1 = STRING_LIT && true;", | |
| 161 " static final BOP2 = false || STRING_LIT;", | |
| 162 " static final BOP3 = 59 == OBJECT_LIT;", | |
| 163 " static final BOP4 = OBJECT_LIT != 59;", | |
| 164 " static final BOP5 = INT_LIT === OBJECT_LIT;", | |
| 165 " static final BOP6 = OBJECT_LIT !== true;", | |
| 166 "}"), | |
| 167 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 168 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 169 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
| 170 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
| 171 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, | |
| 172 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL); | |
| 173 | |
| 174 resolveAndTest(Joiner.on("\n").join( | |
| 175 "class Object {}", | |
| 176 "class A {", | |
| 177 " static final INT_LIT = 5;", | |
| 178 " static final INT_LIT_REF = INT_LIT;", | |
| 179 " static final DOUBLE_LIT = 1.5;", | |
| 180 " static final BOOL_LIT = true;", | |
| 181 " // Multiple binary expresions", | |
| 182 " static final BOP1 = 1 * INT_LIT / 3 + INT_LIT + 9;", | |
| 183 " // Parenthized expression", | |
| 184 " static final BOP2 = ( 1 > 2 );", | |
| 185 " static final BOP3 = (1 * 2) + 3;", | |
| 186 " static final BOP4 = 3 + (1 * 2);", | |
| 187 "}")); | |
| 188 | |
| 189 // Negative Tests | |
| 190 resolveAndTest(Joiner.on("\n").join( | |
| 191 "class Object {}", | |
| 192 "class A {", | |
| 193 " static final INT_LIT = 5;", | |
| 194 " static final DOUBLE_LIT = 1.5;", | |
| 195 " const A();", | |
| 196 " static final OBJECT_LIT = const A();", | |
| 197 " // Multiple binary expresions", | |
| 198 " static final BOP1_0 = 0 + 1 + OBJECT_LIT;", | |
| 199 " static final BOP1_1 = 0 + OBJECT_LIT + 1;", | |
| 200 " static final BOP1_2 = OBJECT_LIT + 3 + 9;", | |
| 201 "}"), | |
| 202 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 203 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 204 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 205 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 206 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
| 207 | |
| 208 resolveAndTest(Joiner.on("\n").join( | |
| 209 "class Object {}", | |
| 210 "class A {", | |
| 211 " static final INT_LIT = 5;", | |
| 212 " static final DOUBLE_LIT = 1.5;", | |
| 213 " const A();", | |
| 214 " static final OBJECT_LIT = new A();", | |
| 215 " // Multiple binary expresions", | |
| 216 " static final PP0 = 0 - (1 + OBJECT_LIT);", | |
| 217 " static final PP1 = 0 + (OBJECT_LIT + 1);", | |
| 218 " static final PP2 = (OBJECT_LIT + 3) + 9;", | |
| 219 " static final PP3 = (OBJECT_LIT) + 3 + 9;", | |
| 220 " static final PP4 = (OBJECT_LIT + 3 + 9);", | |
| 221 " static final PP5 = OBJECT_LIT + (3 + 9);", | |
| 222 "}"), | |
| 223 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 224 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 225 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 226 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 227 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 228 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 229 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 230 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 231 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 232 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 233 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, | |
| 234 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); | |
| 235 } | |
| 236 | |
| 237 public void testConstantUnaryExpression() { | |
| 238 resolveAndTest(Joiner.on("\n").join( | |
| 239 "class Object {}", | |
| 240 "class A {", | |
| 241 " // Unary expression", | |
| 242 " static final BOOL_LIT = true;", | |
| 243 " static final INT_LIT = 123;", | |
| 244 " static final DOUBLE_LIT = 12.3;", | |
| 245 " static final UOP1_0 = !BOOL_LIT;", | |
| 246 " static final UOP1_1 = BOOL_LIT || !true;", | |
| 247 " static final UOP1_2 = !BOOL_LIT || true;", | |
| 248 " static final UOP1_3 = !(BOOL_LIT && true);", | |
| 249 " static final UOP2_0 = ~0xf0;", | |
| 250 " static final UOP2_1 = ~INT_LIT;", | |
| 251 " static final UOP2_2 = ~INT_LIT & 123;", | |
| 252 " static final UOP2_3 = ~(INT_LIT | 0xff);", | |
| 253 " static final UOP3_0 = -0xf0;", | |
| 254 " static final UOP3_1 = -INT_LIT;", | |
| 255 " static final UOP3_2 = -INT_LIT + 123;", | |
| 256 " static final UOP3_3 = -(INT_LIT * 0xff);", | |
| 257 " static final UOP3_4 = -0xf0;", | |
| 258 " static final UOP3_5 = -DOUBLE_LIT;", | |
| 259 " static final UOP3_6 = -DOUBLE_LIT + 123;", | |
| 260 " static final UOP3_7 = -(DOUBLE_LIT * 0xff);", | |
| 261 "}")); | |
| 262 | |
| 263 resolveAndTest(Joiner.on("\n").join( | |
| 264 "class Object {}", | |
| 265 "class int {}", | |
| 266 "class A {", | |
| 267 " // Unary expression", | |
| 268 " static final BOOL_LIT = true;", | |
| 269 " static int foo() { return 3; }", | |
| 270 " static final UOP1 = !5;", | |
| 271 " static final UOP2 = !foo();", | |
| 272 " static final UOP3 = !(5);", | |
| 273 " static final UOP4 = !(foo());", | |
| 274 "}"), | |
| 275 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 276 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 277 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 278 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, | |
| 279 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 280 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); | |
| 281 } | |
| 282 | |
| 283 public void testConstantConstructorAssign() { | |
| 284 | |
| 285 resolveAndTest(Joiner.on("\n").join( | |
| 286 "class Object {}", | |
| 287 "class A {", | |
| 288 " const A();", | |
| 289 "}", | |
| 290 "class B {", | |
| 291 " static final a = const A();", // Constant constructor | |
| 292 "}")); | |
| 293 | |
| 294 // Negative tests | |
| 295 resolveAndTest(Joiner.on("\n").join( | |
| 296 "class Object {}", | |
| 297 "class A {", | |
| 298 " const A();", | |
| 299 " static final a = new A();", // Error: not a constant constructor | |
| 300 "}"), | |
| 301 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 302 } | |
| 303 | |
| 304 public void testConstantLiteralAssign() { | |
| 305 | |
| 306 resolveAndTest(Joiner.on("\n").join( | |
| 307 "class Object {}", | |
| 308 "class A {", | |
| 309 " static final b = true;", | |
| 310 " static final s = \"apple\";", // string literal | |
| 311 " static final i = 1;", // integer literal | |
| 312 " static final d = 3.3;", // double literal | |
| 313 " static final h = 0xf;", // hex literal | |
| 314 " static final n = null;", // null | |
| 315 "}")); | |
| 316 | |
| 317 // Negative tests | |
| 318 resolveAndTest(Joiner.on("\n").join( | |
| 319 "class Object {}", | |
| 320 "class A {", | |
| 321 " foo() { return \"Eve\";}", | |
| 322 " static final person = \"earthling\";", | |
| 323 " static final s = \"Hello ${foo()}!\";", | |
| 324 "}"), | |
| 325 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 326 } | |
| 327 | |
| 328 public void testConstantTypedLiteralAssign() { | |
| 329 resolveAndTest(Joiner.on("\n").join( | |
| 330 "class Object {}", | |
| 331 "class List<T> {}", | |
| 332 "class Map<K,V> {}", | |
| 333 "class A {", | |
| 334 " static final aList = const[1, 2, 3];", // array literal | |
| 335 " static final map = const { \"1\": \"one\", \"2\": \"banana\" };", // map literal | |
| 336 " static final val = aList[2];", | |
| 337 "}")); | |
| 338 | |
| 339 // Negative tests, on literals that are not compile time constants. | |
| 340 resolveAndTest(Joiner.on("\n").join( | |
| 341 "class Object {}", | |
| 342 "class List<T> {}", | |
| 343 "class A {", | |
| 344 " // array literal not const", | |
| 345 " static final aList= [1, 2, 3];", | |
| 346 "}"), | |
| 347 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 348 | |
| 349 resolveAndTest(Joiner.on("\n").join( | |
| 350 "class Object {}", | |
| 351 "class List<T> {}", | |
| 352 "class A {", | |
| 353 " static foo() { return 1; }", | |
| 354 " // const array literal contains non-const member", | |
| 355 " static final aList = const [foo(), 2, 3];", | |
| 356 "}"), | |
| 357 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 358 | |
| 359 resolveAndTest(Joiner.on("\n").join( | |
| 360 "class Object {}", | |
| 361 "class Map<K,V> {}", | |
| 362 "class A {", | |
| 363 " // map literal is not const", | |
| 364 " static final aMap = { \"1\": \"one\", \"2\": \"banana\" };", | |
| 365 "}"), | |
| 366 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 367 | |
| 368 resolveAndTest(Joiner.on("\n").join( | |
| 369 "class Object {}", | |
| 370 "class String {}", | |
| 371 "class Map<K,V> {}", | |
| 372 "class A {", | |
| 373 " static String foo() { return \"one\"; }", | |
| 374 " static final String s = \"apple\";", | |
| 375 " // map literal contains non-const member", | |
| 376 " static final map = const { \"1\":foo(), \"2\": \"banana\" };", | |
| 377 " static final stringInterp = \"It was that woman who gave me the ${s}\ ";", | |
| 378 "}"), | |
| 379 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, | |
| 380 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 381 } | |
| 382 | |
| 383 public void testConstantVariableAssign() { | |
| 384 resolveAndTest(Joiner.on("\n").join( | |
| 385 "class Object {}", | |
| 386 "class A {", | |
| 387 " static final a = 1;", | |
| 388 "}", | |
| 389 "class B {", | |
| 390 " static final i = 1;", | |
| 391 " static final j = i;", // variable that is a compile-time constant | |
| 392 " static final k = A.a;", // variable that is a compile-time constant | |
| 393 "}")); | |
| 394 | |
| 395 // Negative tests | |
| 396 resolveAndTest(Joiner.on("\n").join( | |
| 397 "class Object {}", | |
| 398 "class A {", | |
| 399 " static foo() {return 1;}", | |
| 400 " static final i = foo();", // Error: not a constant integer | |
| 401 "}"), | |
| 402 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); | |
| 403 | |
| 404 resolveAndTest(Joiner.on("\n").join( | |
| 405 "class Object {}", | |
| 406 "class A {", | |
| 407 " static final foo;", | |
| 408 "}"), | |
| 409 DartCompilerErrorCode.STATIC_FINAL_REQUIRES_VALUE); | |
| 410 } | |
| 411 } | |
| OLD | NEW |