| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.test.generated.all_the_rest_test; | 5 library analyzer.test.generated.all_the_rest_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/type.dart'; | 9 import 'package:analyzer/dart/element/type.dart'; |
| 10 import 'package:analyzer/file_system/physical_file_system.dart'; | 10 import 'package:analyzer/file_system/physical_file_system.dart'; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 import 'package:analyzer/src/generated/testing/token_factory.dart'; | 28 import 'package:analyzer/src/generated/testing/token_factory.dart'; |
| 29 import 'package:analyzer/src/generated/utilities_collection.dart'; | 29 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 30 import 'package:analyzer/src/generated/utilities_dart.dart'; | 30 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 31 import 'package:analyzer/src/task/dart.dart'; | 31 import 'package:analyzer/src/task/dart.dart'; |
| 32 import 'package:path/path.dart'; | 32 import 'package:path/path.dart'; |
| 33 import 'package:source_span/source_span.dart'; | 33 import 'package:source_span/source_span.dart'; |
| 34 import 'package:unittest/unittest.dart'; | 34 import 'package:unittest/unittest.dart'; |
| 35 | 35 |
| 36 import '../reflective_tests.dart'; | 36 import '../reflective_tests.dart'; |
| 37 import '../utils.dart'; | 37 import '../utils.dart'; |
| 38 import 'engine_test.dart'; | |
| 39 import 'parser_test.dart'; | 38 import 'parser_test.dart'; |
| 40 import 'resolver_test.dart'; | 39 import 'resolver_test.dart'; |
| 41 import 'test_support.dart'; | 40 import 'test_support.dart'; |
| 42 | 41 |
| 43 main() { | 42 main() { |
| 44 initializeTestEnvironment(); | 43 initializeTestEnvironment(); |
| 45 runReflectiveTests(ConstantEvaluatorTest); | |
| 46 runReflectiveTests(ConstantFinderTest); | |
| 47 runReflectiveTests(ConstantValueComputerTest); | |
| 48 runReflectiveTests(ConstantVisitorTest); | |
| 49 runReflectiveTests(ContentCacheTest); | 44 runReflectiveTests(ContentCacheTest); |
| 50 runReflectiveTests(CustomUriResolverTest); | 45 runReflectiveTests(CustomUriResolverTest); |
| 51 runReflectiveTests(DartObjectImplTest); | 46 runReflectiveTests(DartObjectImplTest); |
| 52 runReflectiveTests(DartUriResolverTest); | 47 runReflectiveTests(DartUriResolverTest); |
| 53 runReflectiveTests(DeclaredVariablesTest); | 48 runReflectiveTests(DeclaredVariablesTest); |
| 54 runReflectiveTests(DirectoryBasedDartSdkTest); | 49 runReflectiveTests(DirectoryBasedDartSdkTest); |
| 55 runReflectiveTests(DirectoryBasedSourceContainerTest); | 50 runReflectiveTests(DirectoryBasedSourceContainerTest); |
| 56 runReflectiveTests(ElementBuilderTest); | 51 runReflectiveTests(ElementBuilderTest); |
| 57 runReflectiveTests(ElementLocatorTest); | 52 runReflectiveTests(ElementLocatorTest); |
| 58 runReflectiveTests(EnumMemberBuilderTest); | 53 runReflectiveTests(EnumMemberBuilderTest); |
| 59 runReflectiveTests(ErrorReporterTest); | 54 runReflectiveTests(ErrorReporterTest); |
| 60 runReflectiveTests(ErrorSeverityTest); | 55 runReflectiveTests(ErrorSeverityTest); |
| 61 runReflectiveTests(ExitDetectorTest); | 56 runReflectiveTests(ExitDetectorTest); |
| 62 runReflectiveTests(ExitDetectorTest2); | 57 runReflectiveTests(ExitDetectorTest2); |
| 63 runReflectiveTests(FileBasedSourceTest); | 58 runReflectiveTests(FileBasedSourceTest); |
| 64 runReflectiveTests(FileUriResolverTest); | 59 runReflectiveTests(FileUriResolverTest); |
| 65 runReflectiveTests(ReferenceFinderTest); | 60 runReflectiveTests(ReferenceFinderTest); |
| 66 runReflectiveTests(SDKLibrariesReaderTest); | 61 runReflectiveTests(SDKLibrariesReaderTest); |
| 67 runReflectiveTests(UriKindTest); | 62 runReflectiveTests(UriKindTest); |
| 68 } | 63 } |
| 69 | 64 |
| 70 /** | |
| 71 * Implementation of [ConstantEvaluationValidator] used during unit tests; | |
| 72 * verifies that any nodes referenced during constant evaluation are present in | |
| 73 * the dependency graph. | |
| 74 */ | |
| 75 class ConstantEvaluationValidator_ForTest | |
| 76 implements ConstantEvaluationValidator { | |
| 77 ConstantValueComputer computer; | |
| 78 | |
| 79 ConstantEvaluationTarget _nodeBeingEvaluated; | |
| 80 | |
| 81 @override | |
| 82 void beforeComputeValue(ConstantEvaluationTarget constant) { | |
| 83 _nodeBeingEvaluated = constant; | |
| 84 } | |
| 85 | |
| 86 @override | |
| 87 void beforeGetConstantInitializers(ConstructorElement constructor) { | |
| 88 // Make sure we properly recorded the dependency. | |
| 89 expect( | |
| 90 computer.referenceGraph.containsPath(_nodeBeingEvaluated, constructor), | |
| 91 isTrue); | |
| 92 } | |
| 93 | |
| 94 @override | |
| 95 void beforeGetEvaluationResult(ConstantEvaluationTarget constant) { | |
| 96 // Make sure we properly recorded the dependency. | |
| 97 expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, constant), | |
| 98 isTrue); | |
| 99 } | |
| 100 | |
| 101 @override | |
| 102 void beforeGetFieldEvaluationResult(FieldElementImpl field) { | |
| 103 // Make sure we properly recorded the dependency. | |
| 104 expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, field), | |
| 105 isTrue); | |
| 106 } | |
| 107 | |
| 108 @override | |
| 109 void beforeGetParameterDefault(ParameterElement parameter) { | |
| 110 // Make sure we properly recorded the dependency. | |
| 111 expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, parameter), | |
| 112 isTrue); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 @reflectiveTest | |
| 117 class ConstantEvaluatorTest extends ResolverTestCase { | |
| 118 void fail_constructor() { | |
| 119 EvaluationResult result = _getExpressionValue("?"); | |
| 120 expect(result.isValid, isTrue); | |
| 121 DartObject value = result.value; | |
| 122 expect(value, null); | |
| 123 } | |
| 124 | |
| 125 void fail_identifier_class() { | |
| 126 EvaluationResult result = _getExpressionValue("?"); | |
| 127 expect(result.isValid, isTrue); | |
| 128 DartObject value = result.value; | |
| 129 expect(value, null); | |
| 130 } | |
| 131 | |
| 132 void fail_identifier_function() { | |
| 133 EvaluationResult result = _getExpressionValue("?"); | |
| 134 expect(result.isValid, isTrue); | |
| 135 DartObject value = result.value; | |
| 136 expect(value, null); | |
| 137 } | |
| 138 | |
| 139 void fail_identifier_static() { | |
| 140 EvaluationResult result = _getExpressionValue("?"); | |
| 141 expect(result.isValid, isTrue); | |
| 142 DartObject value = result.value; | |
| 143 expect(value, null); | |
| 144 } | |
| 145 | |
| 146 void fail_identifier_staticMethod() { | |
| 147 EvaluationResult result = _getExpressionValue("?"); | |
| 148 expect(result.isValid, isTrue); | |
| 149 DartObject value = result.value; | |
| 150 expect(value, null); | |
| 151 } | |
| 152 | |
| 153 void fail_identifier_topLevel() { | |
| 154 EvaluationResult result = _getExpressionValue("?"); | |
| 155 expect(result.isValid, isTrue); | |
| 156 DartObject value = result.value; | |
| 157 expect(value, null); | |
| 158 } | |
| 159 | |
| 160 void fail_identifier_typeParameter() { | |
| 161 EvaluationResult result = _getExpressionValue("?"); | |
| 162 expect(result.isValid, isTrue); | |
| 163 DartObject value = result.value; | |
| 164 expect(value, null); | |
| 165 } | |
| 166 | |
| 167 void fail_prefixedIdentifier_invalid() { | |
| 168 EvaluationResult result = _getExpressionValue("?"); | |
| 169 expect(result.isValid, isTrue); | |
| 170 DartObject value = result.value; | |
| 171 expect(value, null); | |
| 172 } | |
| 173 | |
| 174 void fail_prefixedIdentifier_valid() { | |
| 175 EvaluationResult result = _getExpressionValue("?"); | |
| 176 expect(result.isValid, isTrue); | |
| 177 DartObject value = result.value; | |
| 178 expect(value, null); | |
| 179 } | |
| 180 | |
| 181 void fail_propertyAccess_invalid() { | |
| 182 EvaluationResult result = _getExpressionValue("?"); | |
| 183 expect(result.isValid, isTrue); | |
| 184 DartObject value = result.value; | |
| 185 expect(value, null); | |
| 186 } | |
| 187 | |
| 188 void fail_propertyAccess_valid() { | |
| 189 EvaluationResult result = _getExpressionValue("?"); | |
| 190 expect(result.isValid, isTrue); | |
| 191 DartObject value = result.value; | |
| 192 expect(value, null); | |
| 193 } | |
| 194 | |
| 195 void fail_simpleIdentifier_invalid() { | |
| 196 EvaluationResult result = _getExpressionValue("?"); | |
| 197 expect(result.isValid, isTrue); | |
| 198 DartObject value = result.value; | |
| 199 expect(value, null); | |
| 200 } | |
| 201 | |
| 202 void fail_simpleIdentifier_valid() { | |
| 203 EvaluationResult result = _getExpressionValue("?"); | |
| 204 expect(result.isValid, isTrue); | |
| 205 DartObject value = result.value; | |
| 206 expect(value, null); | |
| 207 } | |
| 208 | |
| 209 void test_bitAnd_int_int() { | |
| 210 _assertValue3(74 & 42, "74 & 42"); | |
| 211 } | |
| 212 | |
| 213 void test_bitNot() { | |
| 214 _assertValue3(~42, "~42"); | |
| 215 } | |
| 216 | |
| 217 void test_bitOr_int_int() { | |
| 218 _assertValue3(74 | 42, "74 | 42"); | |
| 219 } | |
| 220 | |
| 221 void test_bitXor_int_int() { | |
| 222 _assertValue3(74 ^ 42, "74 ^ 42"); | |
| 223 } | |
| 224 | |
| 225 void test_divide_double_double() { | |
| 226 _assertValue2(3.2 / 2.3, "3.2 / 2.3"); | |
| 227 } | |
| 228 | |
| 229 void test_divide_double_double_byZero() { | |
| 230 EvaluationResult result = _getExpressionValue("3.2 / 0.0"); | |
| 231 expect(result.isValid, isTrue); | |
| 232 DartObject value = result.value; | |
| 233 expect(value.type.name, "double"); | |
| 234 expect(value.toDoubleValue().isInfinite, isTrue); | |
| 235 } | |
| 236 | |
| 237 void test_divide_int_int() { | |
| 238 _assertValue2(1.5, "3 / 2"); | |
| 239 } | |
| 240 | |
| 241 void test_divide_int_int_byZero() { | |
| 242 EvaluationResult result = _getExpressionValue("3 / 0"); | |
| 243 expect(result.isValid, isTrue); | |
| 244 } | |
| 245 | |
| 246 void test_equal_boolean_boolean() { | |
| 247 _assertValue(false, "true == false"); | |
| 248 } | |
| 249 | |
| 250 void test_equal_int_int() { | |
| 251 _assertValue(false, "2 == 3"); | |
| 252 } | |
| 253 | |
| 254 void test_equal_invalidLeft() { | |
| 255 EvaluationResult result = _getExpressionValue("a == 3"); | |
| 256 expect(result.isValid, isFalse); | |
| 257 } | |
| 258 | |
| 259 void test_equal_invalidRight() { | |
| 260 EvaluationResult result = _getExpressionValue("2 == a"); | |
| 261 expect(result.isValid, isFalse); | |
| 262 } | |
| 263 | |
| 264 void test_equal_string_string() { | |
| 265 _assertValue(false, "'a' == 'b'"); | |
| 266 } | |
| 267 | |
| 268 void test_greaterThan_int_int() { | |
| 269 _assertValue(false, "2 > 3"); | |
| 270 } | |
| 271 | |
| 272 void test_greaterThanOrEqual_int_int() { | |
| 273 _assertValue(false, "2 >= 3"); | |
| 274 } | |
| 275 | |
| 276 void test_leftShift_int_int() { | |
| 277 _assertValue3(64, "16 << 2"); | |
| 278 } | |
| 279 | |
| 280 void test_lessThan_int_int() { | |
| 281 _assertValue(true, "2 < 3"); | |
| 282 } | |
| 283 | |
| 284 void test_lessThanOrEqual_int_int() { | |
| 285 _assertValue(true, "2 <= 3"); | |
| 286 } | |
| 287 | |
| 288 void test_literal_boolean_false() { | |
| 289 _assertValue(false, "false"); | |
| 290 } | |
| 291 | |
| 292 void test_literal_boolean_true() { | |
| 293 _assertValue(true, "true"); | |
| 294 } | |
| 295 | |
| 296 void test_literal_list() { | |
| 297 EvaluationResult result = _getExpressionValue("const ['a', 'b', 'c']"); | |
| 298 expect(result.isValid, isTrue); | |
| 299 } | |
| 300 | |
| 301 void test_literal_map() { | |
| 302 EvaluationResult result = | |
| 303 _getExpressionValue("const {'a' : 'm', 'b' : 'n', 'c' : 'o'}"); | |
| 304 expect(result.isValid, isTrue); | |
| 305 } | |
| 306 | |
| 307 void test_literal_null() { | |
| 308 EvaluationResult result = _getExpressionValue("null"); | |
| 309 expect(result.isValid, isTrue); | |
| 310 DartObject value = result.value; | |
| 311 expect(value.isNull, isTrue); | |
| 312 } | |
| 313 | |
| 314 void test_literal_number_double() { | |
| 315 _assertValue2(3.45, "3.45"); | |
| 316 } | |
| 317 | |
| 318 void test_literal_number_integer() { | |
| 319 _assertValue3(42, "42"); | |
| 320 } | |
| 321 | |
| 322 void test_literal_string_adjacent() { | |
| 323 _assertValue4("abcdef", "'abc' 'def'"); | |
| 324 } | |
| 325 | |
| 326 void test_literal_string_interpolation_invalid() { | |
| 327 EvaluationResult result = _getExpressionValue("'a\${f()}c'"); | |
| 328 expect(result.isValid, isFalse); | |
| 329 } | |
| 330 | |
| 331 void test_literal_string_interpolation_valid() { | |
| 332 _assertValue4("a3c", "'a\${3}c'"); | |
| 333 } | |
| 334 | |
| 335 void test_literal_string_simple() { | |
| 336 _assertValue4("abc", "'abc'"); | |
| 337 } | |
| 338 | |
| 339 void test_logicalAnd() { | |
| 340 _assertValue(false, "true && false"); | |
| 341 } | |
| 342 | |
| 343 void test_logicalNot() { | |
| 344 _assertValue(false, "!true"); | |
| 345 } | |
| 346 | |
| 347 void test_logicalOr() { | |
| 348 _assertValue(true, "true || false"); | |
| 349 } | |
| 350 | |
| 351 void test_minus_double_double() { | |
| 352 _assertValue2(3.2 - 2.3, "3.2 - 2.3"); | |
| 353 } | |
| 354 | |
| 355 void test_minus_int_int() { | |
| 356 _assertValue3(1, "3 - 2"); | |
| 357 } | |
| 358 | |
| 359 void test_negated_boolean() { | |
| 360 EvaluationResult result = _getExpressionValue("-true"); | |
| 361 expect(result.isValid, isFalse); | |
| 362 } | |
| 363 | |
| 364 void test_negated_double() { | |
| 365 _assertValue2(-42.3, "-42.3"); | |
| 366 } | |
| 367 | |
| 368 void test_negated_integer() { | |
| 369 _assertValue3(-42, "-42"); | |
| 370 } | |
| 371 | |
| 372 void test_notEqual_boolean_boolean() { | |
| 373 _assertValue(true, "true != false"); | |
| 374 } | |
| 375 | |
| 376 void test_notEqual_int_int() { | |
| 377 _assertValue(true, "2 != 3"); | |
| 378 } | |
| 379 | |
| 380 void test_notEqual_invalidLeft() { | |
| 381 EvaluationResult result = _getExpressionValue("a != 3"); | |
| 382 expect(result.isValid, isFalse); | |
| 383 } | |
| 384 | |
| 385 void test_notEqual_invalidRight() { | |
| 386 EvaluationResult result = _getExpressionValue("2 != a"); | |
| 387 expect(result.isValid, isFalse); | |
| 388 } | |
| 389 | |
| 390 void test_notEqual_string_string() { | |
| 391 _assertValue(true, "'a' != 'b'"); | |
| 392 } | |
| 393 | |
| 394 void test_parenthesizedExpression() { | |
| 395 _assertValue4("a", "('a')"); | |
| 396 } | |
| 397 | |
| 398 void test_plus_double_double() { | |
| 399 _assertValue2(2.3 + 3.2, "2.3 + 3.2"); | |
| 400 } | |
| 401 | |
| 402 void test_plus_int_int() { | |
| 403 _assertValue3(5, "2 + 3"); | |
| 404 } | |
| 405 | |
| 406 void test_plus_string_string() { | |
| 407 _assertValue4("ab", "'a' + 'b'"); | |
| 408 } | |
| 409 | |
| 410 void test_remainder_double_double() { | |
| 411 _assertValue2(3.2 % 2.3, "3.2 % 2.3"); | |
| 412 } | |
| 413 | |
| 414 void test_remainder_int_int() { | |
| 415 _assertValue3(2, "8 % 3"); | |
| 416 } | |
| 417 | |
| 418 void test_rightShift() { | |
| 419 _assertValue3(16, "64 >> 2"); | |
| 420 } | |
| 421 | |
| 422 void test_stringLength_complex() { | |
| 423 _assertValue3(6, "('qwe' + 'rty').length"); | |
| 424 } | |
| 425 | |
| 426 void test_stringLength_simple() { | |
| 427 _assertValue3(6, "'Dvorak'.length"); | |
| 428 } | |
| 429 | |
| 430 void test_times_double_double() { | |
| 431 _assertValue2(2.3 * 3.2, "2.3 * 3.2"); | |
| 432 } | |
| 433 | |
| 434 void test_times_int_int() { | |
| 435 _assertValue3(6, "2 * 3"); | |
| 436 } | |
| 437 | |
| 438 void test_truncatingDivide_double_double() { | |
| 439 _assertValue3(1, "3.2 ~/ 2.3"); | |
| 440 } | |
| 441 | |
| 442 void test_truncatingDivide_int_int() { | |
| 443 _assertValue3(3, "10 ~/ 3"); | |
| 444 } | |
| 445 | |
| 446 void _assertValue(bool expectedValue, String contents) { | |
| 447 EvaluationResult result = _getExpressionValue(contents); | |
| 448 DartObject value = result.value; | |
| 449 expect(value.type.name, "bool"); | |
| 450 expect(value.toBoolValue(), expectedValue); | |
| 451 } | |
| 452 | |
| 453 void _assertValue2(double expectedValue, String contents) { | |
| 454 EvaluationResult result = _getExpressionValue(contents); | |
| 455 expect(result.isValid, isTrue); | |
| 456 DartObject value = result.value; | |
| 457 expect(value.type.name, "double"); | |
| 458 expect(value.toDoubleValue(), expectedValue); | |
| 459 } | |
| 460 | |
| 461 void _assertValue3(int expectedValue, String contents) { | |
| 462 EvaluationResult result = _getExpressionValue(contents); | |
| 463 expect(result.isValid, isTrue); | |
| 464 DartObject value = result.value; | |
| 465 expect(value.type.name, "int"); | |
| 466 expect(value.toIntValue(), expectedValue); | |
| 467 } | |
| 468 | |
| 469 void _assertValue4(String expectedValue, String contents) { | |
| 470 EvaluationResult result = _getExpressionValue(contents); | |
| 471 DartObject value = result.value; | |
| 472 expect(value, isNotNull); | |
| 473 ParameterizedType type = value.type; | |
| 474 expect(type, isNotNull); | |
| 475 expect(type.name, "String"); | |
| 476 expect(value.toStringValue(), expectedValue); | |
| 477 } | |
| 478 | |
| 479 EvaluationResult _getExpressionValue(String contents) { | |
| 480 Source source = addSource("var x = $contents;"); | |
| 481 LibraryElement library = resolve2(source); | |
| 482 CompilationUnit unit = | |
| 483 analysisContext.resolveCompilationUnit(source, library); | |
| 484 expect(unit, isNotNull); | |
| 485 NodeList<CompilationUnitMember> declarations = unit.declarations; | |
| 486 expect(declarations, hasLength(1)); | |
| 487 CompilationUnitMember declaration = declarations[0]; | |
| 488 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration, | |
| 489 TopLevelVariableDeclaration, declaration); | |
| 490 NodeList<VariableDeclaration> variables = | |
| 491 (declaration as TopLevelVariableDeclaration).variables.variables; | |
| 492 expect(variables, hasLength(1)); | |
| 493 ConstantEvaluator evaluator = new ConstantEvaluator( | |
| 494 source, analysisContext.typeProvider, | |
| 495 typeSystem: analysisContext.typeSystem); | |
| 496 return evaluator.evaluate(variables[0].initializer); | |
| 497 } | |
| 498 } | |
| 499 | |
| 500 @reflectiveTest | |
| 501 class ConstantFinderTest { | |
| 502 AstNode _node; | |
| 503 TypeProvider _typeProvider; | |
| 504 AnalysisContext _context; | |
| 505 Source _source; | |
| 506 | |
| 507 void setUp() { | |
| 508 _typeProvider = new TestTypeProvider(); | |
| 509 _context = new TestAnalysisContext_ConstantFinderTest(); | |
| 510 _source = new TestSource(); | |
| 511 } | |
| 512 | |
| 513 /** | |
| 514 * Test an annotation that consists solely of an identifier (and hence | |
| 515 * represents a reference to a compile-time constant variable). | |
| 516 */ | |
| 517 void test_visitAnnotation_constantVariable() { | |
| 518 _node = AstFactory.annotation(AstFactory.identifier3('x')); | |
| 519 expect(_findAnnotations(), contains(_node)); | |
| 520 } | |
| 521 | |
| 522 /** | |
| 523 * Test an annotation that represents the invocation of a constant | |
| 524 * constructor. | |
| 525 */ | |
| 526 void test_visitAnnotation_invocation() { | |
| 527 _node = AstFactory.annotation2( | |
| 528 AstFactory.identifier3('A'), null, AstFactory.argumentList()); | |
| 529 expect(_findAnnotations(), contains(_node)); | |
| 530 } | |
| 531 | |
| 532 void test_visitConstructorDeclaration_const() { | |
| 533 ConstructorElement element = _setupConstructorDeclaration("A", true); | |
| 534 expect(_findConstants(), contains(element)); | |
| 535 } | |
| 536 | |
| 537 void test_visitConstructorDeclaration_nonConst() { | |
| 538 _setupConstructorDeclaration("A", false); | |
| 539 expect(_findConstants(), isEmpty); | |
| 540 } | |
| 541 | |
| 542 void test_visitVariableDeclaration_const() { | |
| 543 VariableElement element = _setupVariableDeclaration("v", true, true); | |
| 544 expect(_findConstants(), contains(element)); | |
| 545 } | |
| 546 | |
| 547 void test_visitVariableDeclaration_final_inClass() { | |
| 548 _setupFieldDeclaration('C', 'f', Keyword.FINAL); | |
| 549 expect(_findConstants(), isEmpty); | |
| 550 } | |
| 551 | |
| 552 void test_visitVariableDeclaration_final_inClassWithConstConstructor() { | |
| 553 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL, | |
| 554 hasConstConstructor: true); | |
| 555 expect(_findConstants(), contains(field.element)); | |
| 556 } | |
| 557 | |
| 558 void test_visitVariableDeclaration_final_outsideClass() { | |
| 559 _setupVariableDeclaration('v', false, true, isFinal: true); | |
| 560 expect(_findConstants(), isEmpty); | |
| 561 } | |
| 562 | |
| 563 void test_visitVariableDeclaration_noInitializer() { | |
| 564 _setupVariableDeclaration("v", true, false); | |
| 565 expect(_findConstants(), isEmpty); | |
| 566 } | |
| 567 | |
| 568 void test_visitVariableDeclaration_nonConst() { | |
| 569 _setupVariableDeclaration("v", false, true); | |
| 570 expect(_findConstants(), isEmpty); | |
| 571 } | |
| 572 | |
| 573 void test_visitVariableDeclaration_static_const_inClass() { | |
| 574 VariableDeclaration field = | |
| 575 _setupFieldDeclaration('C', 'f', Keyword.CONST, isStatic: true); | |
| 576 expect(_findConstants(), contains(field.element)); | |
| 577 } | |
| 578 | |
| 579 void | |
| 580 test_visitVariableDeclaration_static_const_inClassWithConstConstructor() { | |
| 581 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.CONST, | |
| 582 isStatic: true, hasConstConstructor: true); | |
| 583 expect(_findConstants(), contains(field.element)); | |
| 584 } | |
| 585 | |
| 586 void | |
| 587 test_visitVariableDeclaration_static_final_inClassWithConstConstructor() { | |
| 588 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL, | |
| 589 isStatic: true, hasConstConstructor: true); | |
| 590 expect(_findConstants(), isNot(contains(field.element))); | |
| 591 } | |
| 592 | |
| 593 void | |
| 594 test_visitVariableDeclaration_uninitialized_final_inClassWithConstConstruc
tor() { | |
| 595 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL, | |
| 596 isInitialized: false, hasConstConstructor: true); | |
| 597 expect(_findConstants(), isNot(contains(field.element))); | |
| 598 } | |
| 599 | |
| 600 void test_visitVariableDeclaration_uninitialized_static_const_inClass() { | |
| 601 _setupFieldDeclaration('C', 'f', Keyword.CONST, | |
| 602 isStatic: true, isInitialized: false); | |
| 603 expect(_findConstants(), isEmpty); | |
| 604 } | |
| 605 | |
| 606 List<Annotation> _findAnnotations() { | |
| 607 Set<Annotation> annotations = new Set<Annotation>(); | |
| 608 for (ConstantEvaluationTarget target in _findConstants()) { | |
| 609 if (target is ConstantEvaluationTarget_Annotation) { | |
| 610 expect(target.context, same(_context)); | |
| 611 expect(target.source, same(_source)); | |
| 612 annotations.add(target.annotation); | |
| 613 } | |
| 614 } | |
| 615 return new List<Annotation>.from(annotations); | |
| 616 } | |
| 617 | |
| 618 Set<ConstantEvaluationTarget> _findConstants() { | |
| 619 ConstantFinder finder = new ConstantFinder(_context, _source, _source); | |
| 620 _node.accept(finder); | |
| 621 Set<ConstantEvaluationTarget> constants = finder.constantsToCompute; | |
| 622 expect(constants, isNotNull); | |
| 623 return constants; | |
| 624 } | |
| 625 | |
| 626 ConstructorElement _setupConstructorDeclaration(String name, bool isConst) { | |
| 627 Keyword constKeyword = isConst ? Keyword.CONST : null; | |
| 628 ConstructorDeclaration constructorDeclaration = | |
| 629 AstFactory.constructorDeclaration2( | |
| 630 constKeyword, | |
| 631 null, | |
| 632 null, | |
| 633 name, | |
| 634 AstFactory.formalParameterList(), | |
| 635 null, | |
| 636 AstFactory.blockFunctionBody2()); | |
| 637 ClassElement classElement = ElementFactory.classElement2(name); | |
| 638 ConstructorElement element = | |
| 639 ElementFactory.constructorElement(classElement, name, isConst); | |
| 640 constructorDeclaration.element = element; | |
| 641 _node = constructorDeclaration; | |
| 642 return element; | |
| 643 } | |
| 644 | |
| 645 VariableDeclaration _setupFieldDeclaration( | |
| 646 String className, String fieldName, Keyword keyword, | |
| 647 {bool isInitialized: true, | |
| 648 bool isStatic: false, | |
| 649 bool hasConstConstructor: false}) { | |
| 650 VariableDeclaration variableDeclaration = isInitialized | |
| 651 ? AstFactory.variableDeclaration2(fieldName, AstFactory.integer(0)) | |
| 652 : AstFactory.variableDeclaration(fieldName); | |
| 653 VariableElement fieldElement = ElementFactory.fieldElement( | |
| 654 fieldName, | |
| 655 isStatic, | |
| 656 keyword == Keyword.FINAL, | |
| 657 keyword == Keyword.CONST, | |
| 658 _typeProvider.intType); | |
| 659 variableDeclaration.name.staticElement = fieldElement; | |
| 660 FieldDeclaration fieldDeclaration = AstFactory.fieldDeclaration2( | |
| 661 isStatic, keyword, <VariableDeclaration>[variableDeclaration]); | |
| 662 ClassDeclaration classDeclaration = | |
| 663 AstFactory.classDeclaration(null, className, null, null, null, null); | |
| 664 classDeclaration.members.add(fieldDeclaration); | |
| 665 _node = classDeclaration; | |
| 666 ClassElementImpl classElement = ElementFactory.classElement2(className); | |
| 667 classElement.fields = <FieldElement>[fieldElement]; | |
| 668 classDeclaration.name.staticElement = classElement; | |
| 669 if (hasConstConstructor) { | |
| 670 ConstructorDeclaration constructorDeclaration = | |
| 671 AstFactory.constructorDeclaration2( | |
| 672 Keyword.CONST, | |
| 673 null, | |
| 674 AstFactory.identifier3(className), | |
| 675 null, | |
| 676 AstFactory.formalParameterList(), | |
| 677 null, | |
| 678 AstFactory.blockFunctionBody2()); | |
| 679 classDeclaration.members.add(constructorDeclaration); | |
| 680 ConstructorElement constructorElement = | |
| 681 ElementFactory.constructorElement(classElement, '', true); | |
| 682 constructorDeclaration.element = constructorElement; | |
| 683 classElement.constructors = <ConstructorElement>[constructorElement]; | |
| 684 } else { | |
| 685 classElement.constructors = ConstructorElement.EMPTY_LIST; | |
| 686 } | |
| 687 return variableDeclaration; | |
| 688 } | |
| 689 | |
| 690 VariableElement _setupVariableDeclaration( | |
| 691 String name, bool isConst, bool isInitialized, | |
| 692 {isFinal: false}) { | |
| 693 VariableDeclaration variableDeclaration = isInitialized | |
| 694 ? AstFactory.variableDeclaration2(name, AstFactory.integer(0)) | |
| 695 : AstFactory.variableDeclaration(name); | |
| 696 SimpleIdentifier identifier = variableDeclaration.name; | |
| 697 VariableElement element = ElementFactory.localVariableElement(identifier); | |
| 698 identifier.staticElement = element; | |
| 699 Keyword keyword = isConst ? Keyword.CONST : isFinal ? Keyword.FINAL : null; | |
| 700 AstFactory.variableDeclarationList2(keyword, [variableDeclaration]); | |
| 701 _node = variableDeclaration; | |
| 702 return element; | |
| 703 } | |
| 704 } | |
| 705 | |
| 706 @reflectiveTest | |
| 707 class ConstantValueComputerTest extends ResolverTestCase { | |
| 708 void test_annotation_constConstructor() { | |
| 709 CompilationUnit compilationUnit = resolveSource(r''' | |
| 710 class A { | |
| 711 final int i; | |
| 712 const A(this.i); | |
| 713 } | |
| 714 | |
| 715 class C { | |
| 716 @A(5) | |
| 717 f() {} | |
| 718 } | |
| 719 '''); | |
| 720 EvaluationResultImpl result = | |
| 721 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 722 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A'); | |
| 723 _assertIntField(annotationFields, 'i', 5); | |
| 724 } | |
| 725 | |
| 726 void test_annotation_constConstructor_named() { | |
| 727 CompilationUnit compilationUnit = resolveSource(r''' | |
| 728 class A { | |
| 729 final int i; | |
| 730 const A.named(this.i); | |
| 731 } | |
| 732 | |
| 733 class C { | |
| 734 @A.named(5) | |
| 735 f() {} | |
| 736 } | |
| 737 '''); | |
| 738 EvaluationResultImpl result = | |
| 739 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 740 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A'); | |
| 741 _assertIntField(annotationFields, 'i', 5); | |
| 742 } | |
| 743 | |
| 744 void test_annotation_constConstructor_noArgs() { | |
| 745 // Failing to pass arguments to an annotation which is a constant | |
| 746 // constructor is illegal, but shouldn't crash analysis. | |
| 747 CompilationUnit compilationUnit = resolveSource(r''' | |
| 748 class A { | |
| 749 final int i; | |
| 750 const A(this.i); | |
| 751 } | |
| 752 | |
| 753 class C { | |
| 754 @A | |
| 755 f() {} | |
| 756 } | |
| 757 '''); | |
| 758 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 759 } | |
| 760 | |
| 761 void test_annotation_constConstructor_noArgs_named() { | |
| 762 // Failing to pass arguments to an annotation which is a constant | |
| 763 // constructor is illegal, but shouldn't crash analysis. | |
| 764 CompilationUnit compilationUnit = resolveSource(r''' | |
| 765 class A { | |
| 766 final int i; | |
| 767 const A.named(this.i); | |
| 768 } | |
| 769 | |
| 770 class C { | |
| 771 @A.named | |
| 772 f() {} | |
| 773 } | |
| 774 '''); | |
| 775 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 776 } | |
| 777 | |
| 778 void test_annotation_nonConstConstructor() { | |
| 779 // Calling a non-const constructor from an annotation that is illegal, but | |
| 780 // shouldn't crash analysis. | |
| 781 CompilationUnit compilationUnit = resolveSource(r''' | |
| 782 class A { | |
| 783 final int i; | |
| 784 A(this.i); | |
| 785 } | |
| 786 | |
| 787 class C { | |
| 788 @A(5) | |
| 789 f() {} | |
| 790 } | |
| 791 '''); | |
| 792 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 793 } | |
| 794 | |
| 795 void test_annotation_staticConst() { | |
| 796 CompilationUnit compilationUnit = resolveSource(r''' | |
| 797 class C { | |
| 798 static const int i = 5; | |
| 799 | |
| 800 @i | |
| 801 f() {} | |
| 802 } | |
| 803 '''); | |
| 804 EvaluationResultImpl result = | |
| 805 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 806 expect(_assertValidInt(result), 5); | |
| 807 } | |
| 808 | |
| 809 void test_annotation_staticConst_args() { | |
| 810 // Applying arguments to an annotation that is a static const is | |
| 811 // illegal, but shouldn't crash analysis. | |
| 812 CompilationUnit compilationUnit = resolveSource(r''' | |
| 813 class C { | |
| 814 static const int i = 5; | |
| 815 | |
| 816 @i(1) | |
| 817 f() {} | |
| 818 } | |
| 819 '''); | |
| 820 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 821 } | |
| 822 | |
| 823 void test_annotation_staticConst_otherClass() { | |
| 824 CompilationUnit compilationUnit = resolveSource(r''' | |
| 825 class A { | |
| 826 static const int i = 5; | |
| 827 } | |
| 828 | |
| 829 class C { | |
| 830 @A.i | |
| 831 f() {} | |
| 832 } | |
| 833 '''); | |
| 834 EvaluationResultImpl result = | |
| 835 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 836 expect(_assertValidInt(result), 5); | |
| 837 } | |
| 838 | |
| 839 void test_annotation_staticConst_otherClass_args() { | |
| 840 // Applying arguments to an annotation that is a static const is | |
| 841 // illegal, but shouldn't crash analysis. | |
| 842 CompilationUnit compilationUnit = resolveSource(r''' | |
| 843 class A { | |
| 844 static const int i = 5; | |
| 845 } | |
| 846 | |
| 847 class C { | |
| 848 @A.i(1) | |
| 849 f() {} | |
| 850 } | |
| 851 '''); | |
| 852 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 853 } | |
| 854 | |
| 855 void test_annotation_toplevelVariable() { | |
| 856 CompilationUnit compilationUnit = resolveSource(r''' | |
| 857 const int i = 5; | |
| 858 class C { | |
| 859 @i | |
| 860 f() {} | |
| 861 } | |
| 862 '''); | |
| 863 EvaluationResultImpl result = | |
| 864 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 865 expect(_assertValidInt(result), 5); | |
| 866 } | |
| 867 | |
| 868 void test_annotation_toplevelVariable_args() { | |
| 869 // Applying arguments to an annotation that is a toplevel variable is | |
| 870 // illegal, but shouldn't crash analysis. | |
| 871 CompilationUnit compilationUnit = resolveSource(r''' | |
| 872 const int i = 5; | |
| 873 class C { | |
| 874 @i(1) | |
| 875 f() {} | |
| 876 } | |
| 877 '''); | |
| 878 _evaluateAnnotation(compilationUnit, "C", "f"); | |
| 879 } | |
| 880 | |
| 881 void test_computeValues_cycle() { | |
| 882 TestLogger logger = new TestLogger(); | |
| 883 AnalysisEngine.instance.logger = logger; | |
| 884 try { | |
| 885 Source librarySource = addSource(r''' | |
| 886 const int a = c; | |
| 887 const int b = a; | |
| 888 const int c = b;'''); | |
| 889 LibraryElement libraryElement = resolve2(librarySource); | |
| 890 CompilationUnit unit = | |
| 891 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | |
| 892 analysisContext.computeErrors(librarySource); | |
| 893 expect(unit, isNotNull); | |
| 894 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 895 computer.add(unit, librarySource, librarySource); | |
| 896 computer.computeValues(); | |
| 897 NodeList<CompilationUnitMember> members = unit.declarations; | |
| 898 expect(members, hasLength(3)); | |
| 899 _validate(false, (members[0] as TopLevelVariableDeclaration).variables); | |
| 900 _validate(false, (members[1] as TopLevelVariableDeclaration).variables); | |
| 901 _validate(false, (members[2] as TopLevelVariableDeclaration).variables); | |
| 902 } finally { | |
| 903 AnalysisEngine.instance.logger = Logger.NULL; | |
| 904 } | |
| 905 } | |
| 906 | |
| 907 void test_computeValues_dependentVariables() { | |
| 908 Source librarySource = addSource(r''' | |
| 909 const int b = a; | |
| 910 const int a = 0;'''); | |
| 911 LibraryElement libraryElement = resolve2(librarySource); | |
| 912 CompilationUnit unit = | |
| 913 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | |
| 914 expect(unit, isNotNull); | |
| 915 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 916 computer.add(unit, librarySource, librarySource); | |
| 917 computer.computeValues(); | |
| 918 NodeList<CompilationUnitMember> members = unit.declarations; | |
| 919 expect(members, hasLength(2)); | |
| 920 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); | |
| 921 _validate(true, (members[1] as TopLevelVariableDeclaration).variables); | |
| 922 } | |
| 923 | |
| 924 void test_computeValues_empty() { | |
| 925 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 926 computer.computeValues(); | |
| 927 } | |
| 928 | |
| 929 void test_computeValues_multipleSources() { | |
| 930 Source librarySource = addNamedSource( | |
| 931 "/lib.dart", | |
| 932 r''' | |
| 933 library lib; | |
| 934 part 'part.dart'; | |
| 935 const int c = b; | |
| 936 const int a = 0;'''); | |
| 937 Source partSource = addNamedSource( | |
| 938 "/part.dart", | |
| 939 r''' | |
| 940 part of lib; | |
| 941 const int b = a; | |
| 942 const int d = c;'''); | |
| 943 LibraryElement libraryElement = resolve2(librarySource); | |
| 944 CompilationUnit libraryUnit = | |
| 945 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | |
| 946 expect(libraryUnit, isNotNull); | |
| 947 CompilationUnit partUnit = | |
| 948 analysisContext.resolveCompilationUnit(partSource, libraryElement); | |
| 949 expect(partUnit, isNotNull); | |
| 950 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 951 computer.add(libraryUnit, librarySource, librarySource); | |
| 952 computer.add(partUnit, partSource, librarySource); | |
| 953 computer.computeValues(); | |
| 954 NodeList<CompilationUnitMember> libraryMembers = libraryUnit.declarations; | |
| 955 expect(libraryMembers, hasLength(2)); | |
| 956 _validate( | |
| 957 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables); | |
| 958 _validate( | |
| 959 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables); | |
| 960 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations; | |
| 961 expect(partMembers, hasLength(2)); | |
| 962 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables); | |
| 963 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables); | |
| 964 } | |
| 965 | |
| 966 void test_computeValues_singleVariable() { | |
| 967 Source librarySource = addSource("const int a = 0;"); | |
| 968 LibraryElement libraryElement = resolve2(librarySource); | |
| 969 CompilationUnit unit = | |
| 970 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | |
| 971 expect(unit, isNotNull); | |
| 972 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 973 computer.add(unit, librarySource, librarySource); | |
| 974 computer.computeValues(); | |
| 975 NodeList<CompilationUnitMember> members = unit.declarations; | |
| 976 expect(members, hasLength(1)); | |
| 977 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); | |
| 978 } | |
| 979 | |
| 980 void test_computeValues_value_depends_on_enum() { | |
| 981 Source librarySource = addSource(''' | |
| 982 enum E { id0, id1 } | |
| 983 const E e = E.id0; | |
| 984 '''); | |
| 985 LibraryElement libraryElement = resolve2(librarySource); | |
| 986 CompilationUnit unit = | |
| 987 analysisContext.resolveCompilationUnit(librarySource, libraryElement); | |
| 988 expect(unit, isNotNull); | |
| 989 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 990 computer.add(unit, librarySource, librarySource); | |
| 991 computer.computeValues(); | |
| 992 TopLevelVariableDeclaration declaration = unit.declarations | |
| 993 .firstWhere((member) => member is TopLevelVariableDeclaration); | |
| 994 _validate(true, declaration.variables); | |
| 995 } | |
| 996 | |
| 997 void test_dependencyOnConstructor() { | |
| 998 // x depends on "const A()" | |
| 999 _assertProperDependencies(r''' | |
| 1000 class A { | |
| 1001 const A(); | |
| 1002 } | |
| 1003 const x = const A();'''); | |
| 1004 } | |
| 1005 | |
| 1006 void test_dependencyOnConstructorArgument() { | |
| 1007 // "const A(x)" depends on x | |
| 1008 _assertProperDependencies(r''' | |
| 1009 class A { | |
| 1010 const A(this.next); | |
| 1011 final A next; | |
| 1012 } | |
| 1013 const A x = const A(null); | |
| 1014 const A y = const A(x);'''); | |
| 1015 } | |
| 1016 | |
| 1017 void test_dependencyOnConstructorArgument_unresolvedConstructor() { | |
| 1018 // "const A.a(x)" depends on x even if the constructor A.a can't be found. | |
| 1019 _assertProperDependencies( | |
| 1020 r''' | |
| 1021 class A { | |
| 1022 } | |
| 1023 const int x = 1; | |
| 1024 const A y = const A.a(x);''', | |
| 1025 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]); | |
| 1026 } | |
| 1027 | |
| 1028 void test_dependencyOnConstructorInitializer() { | |
| 1029 // "const A()" depends on x | |
| 1030 _assertProperDependencies(r''' | |
| 1031 const int x = 1; | |
| 1032 class A { | |
| 1033 const A() : v = x; | |
| 1034 final int v; | |
| 1035 }'''); | |
| 1036 } | |
| 1037 | |
| 1038 void test_dependencyOnExplicitSuperConstructor() { | |
| 1039 // b depends on B() depends on A() | |
| 1040 _assertProperDependencies(r''' | |
| 1041 class A { | |
| 1042 const A(this.x); | |
| 1043 final int x; | |
| 1044 } | |
| 1045 class B extends A { | |
| 1046 const B() : super(5); | |
| 1047 } | |
| 1048 const B b = const B();'''); | |
| 1049 } | |
| 1050 | |
| 1051 void test_dependencyOnExplicitSuperConstructorParameters() { | |
| 1052 // b depends on B() depends on i | |
| 1053 _assertProperDependencies(r''' | |
| 1054 class A { | |
| 1055 const A(this.x); | |
| 1056 final int x; | |
| 1057 } | |
| 1058 class B extends A { | |
| 1059 const B() : super(i); | |
| 1060 } | |
| 1061 const B b = const B(); | |
| 1062 const int i = 5;'''); | |
| 1063 } | |
| 1064 | |
| 1065 void test_dependencyOnFactoryRedirect() { | |
| 1066 // a depends on A.foo() depends on A.bar() | |
| 1067 _assertProperDependencies(r''' | |
| 1068 const A a = const A.foo(); | |
| 1069 class A { | |
| 1070 factory const A.foo() = A.bar; | |
| 1071 const A.bar(); | |
| 1072 }'''); | |
| 1073 } | |
| 1074 | |
| 1075 void test_dependencyOnFactoryRedirectWithTypeParams() { | |
| 1076 _assertProperDependencies(r''' | |
| 1077 class A { | |
| 1078 const factory A(var a) = B<int>; | |
| 1079 } | |
| 1080 | |
| 1081 class B<T> implements A { | |
| 1082 final T x; | |
| 1083 const B(this.x); | |
| 1084 } | |
| 1085 | |
| 1086 const A a = const A(10);'''); | |
| 1087 } | |
| 1088 | |
| 1089 void test_dependencyOnImplicitSuperConstructor() { | |
| 1090 // b depends on B() depends on A() | |
| 1091 _assertProperDependencies(r''' | |
| 1092 class A { | |
| 1093 const A() : x = 5; | |
| 1094 final int x; | |
| 1095 } | |
| 1096 class B extends A { | |
| 1097 const B(); | |
| 1098 } | |
| 1099 const B b = const B();'''); | |
| 1100 } | |
| 1101 | |
| 1102 void test_dependencyOnInitializedFinal() { | |
| 1103 // a depends on A() depends on A.x | |
| 1104 _assertProperDependencies(''' | |
| 1105 class A { | |
| 1106 const A(); | |
| 1107 final int x = 1; | |
| 1108 } | |
| 1109 const A a = const A(); | |
| 1110 '''); | |
| 1111 } | |
| 1112 | |
| 1113 void test_dependencyOnInitializedNonStaticConst() { | |
| 1114 // Even though non-static consts are not allowed by the language, we need | |
| 1115 // to handle them for error recovery purposes. | |
| 1116 // a depends on A() depends on A.x | |
| 1117 _assertProperDependencies( | |
| 1118 ''' | |
| 1119 class A { | |
| 1120 const A(); | |
| 1121 const int x = 1; | |
| 1122 } | |
| 1123 const A a = const A(); | |
| 1124 ''', | |
| 1125 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]); | |
| 1126 } | |
| 1127 | |
| 1128 void test_dependencyOnNonFactoryRedirect() { | |
| 1129 // a depends on A.foo() depends on A.bar() | |
| 1130 _assertProperDependencies(r''' | |
| 1131 const A a = const A.foo(); | |
| 1132 class A { | |
| 1133 const A.foo() : this.bar(); | |
| 1134 const A.bar(); | |
| 1135 }'''); | |
| 1136 } | |
| 1137 | |
| 1138 void test_dependencyOnNonFactoryRedirect_arg() { | |
| 1139 // a depends on A.foo() depends on b | |
| 1140 _assertProperDependencies(r''' | |
| 1141 const A a = const A.foo(); | |
| 1142 const int b = 1; | |
| 1143 class A { | |
| 1144 const A.foo() : this.bar(b); | |
| 1145 const A.bar(x) : y = x; | |
| 1146 final int y; | |
| 1147 }'''); | |
| 1148 } | |
| 1149 | |
| 1150 void test_dependencyOnNonFactoryRedirect_defaultValue() { | |
| 1151 // a depends on A.foo() depends on A.bar() depends on b | |
| 1152 _assertProperDependencies(r''' | |
| 1153 const A a = const A.foo(); | |
| 1154 const int b = 1; | |
| 1155 class A { | |
| 1156 const A.foo() : this.bar(); | |
| 1157 const A.bar([x = b]) : y = x; | |
| 1158 final int y; | |
| 1159 }'''); | |
| 1160 } | |
| 1161 | |
| 1162 void test_dependencyOnNonFactoryRedirect_toMissing() { | |
| 1163 // a depends on A.foo() which depends on nothing, since A.bar() is | |
| 1164 // missing. | |
| 1165 _assertProperDependencies( | |
| 1166 r''' | |
| 1167 const A a = const A.foo(); | |
| 1168 class A { | |
| 1169 const A.foo() : this.bar(); | |
| 1170 }''', | |
| 1171 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]); | |
| 1172 } | |
| 1173 | |
| 1174 void test_dependencyOnNonFactoryRedirect_toNonConst() { | |
| 1175 // a depends on A.foo() which depends on nothing, since A.bar() is | |
| 1176 // non-const. | |
| 1177 _assertProperDependencies(r''' | |
| 1178 const A a = const A.foo(); | |
| 1179 class A { | |
| 1180 const A.foo() : this.bar(); | |
| 1181 A.bar(); | |
| 1182 }'''); | |
| 1183 } | |
| 1184 | |
| 1185 void test_dependencyOnNonFactoryRedirect_unnamed() { | |
| 1186 // a depends on A.foo() depends on A() | |
| 1187 _assertProperDependencies(r''' | |
| 1188 const A a = const A.foo(); | |
| 1189 class A { | |
| 1190 const A.foo() : this(); | |
| 1191 const A(); | |
| 1192 }'''); | |
| 1193 } | |
| 1194 | |
| 1195 void test_dependencyOnOptionalParameterDefault() { | |
| 1196 // a depends on A() depends on B() | |
| 1197 _assertProperDependencies(r''' | |
| 1198 class A { | |
| 1199 const A([x = const B()]) : b = x; | |
| 1200 final B b; | |
| 1201 } | |
| 1202 class B { | |
| 1203 const B(); | |
| 1204 } | |
| 1205 const A a = const A();'''); | |
| 1206 } | |
| 1207 | |
| 1208 void test_dependencyOnVariable() { | |
| 1209 // x depends on y | |
| 1210 _assertProperDependencies(r''' | |
| 1211 const x = y + 1; | |
| 1212 const y = 2;'''); | |
| 1213 } | |
| 1214 | |
| 1215 void test_final_initialized_at_declaration() { | |
| 1216 CompilationUnit compilationUnit = resolveSource(''' | |
| 1217 class A { | |
| 1218 final int i = 123; | |
| 1219 const A(); | |
| 1220 } | |
| 1221 | |
| 1222 const A a = const A(); | |
| 1223 '''); | |
| 1224 EvaluationResultImpl result = | |
| 1225 _evaluateTopLevelVariable(compilationUnit, 'a'); | |
| 1226 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1227 expect(fields, hasLength(1)); | |
| 1228 _assertIntField(fields, "i", 123); | |
| 1229 } | |
| 1230 | |
| 1231 void test_fromEnvironment_bool_default_false() { | |
| 1232 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false); | |
| 1233 } | |
| 1234 | |
| 1235 void test_fromEnvironment_bool_default_overridden() { | |
| 1236 expect( | |
| 1237 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false); | |
| 1238 } | |
| 1239 | |
| 1240 void test_fromEnvironment_bool_default_parseError() { | |
| 1241 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", "true")), | |
| 1242 true); | |
| 1243 } | |
| 1244 | |
| 1245 void test_fromEnvironment_bool_default_true() { | |
| 1246 expect(_assertValidBool(_check_fromEnvironment_bool(null, "true")), true); | |
| 1247 } | |
| 1248 | |
| 1249 void test_fromEnvironment_bool_false() { | |
| 1250 expect(_assertValidBool(_check_fromEnvironment_bool("false", null)), false); | |
| 1251 } | |
| 1252 | |
| 1253 void test_fromEnvironment_bool_parseError() { | |
| 1254 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", null)), | |
| 1255 false); | |
| 1256 } | |
| 1257 | |
| 1258 void test_fromEnvironment_bool_true() { | |
| 1259 expect(_assertValidBool(_check_fromEnvironment_bool("true", null)), true); | |
| 1260 } | |
| 1261 | |
| 1262 void test_fromEnvironment_bool_undeclared() { | |
| 1263 _assertValidUnknown(_check_fromEnvironment_bool(null, null)); | |
| 1264 } | |
| 1265 | |
| 1266 void test_fromEnvironment_int_default_overridden() { | |
| 1267 expect(_assertValidInt(_check_fromEnvironment_int("234", "123")), 234); | |
| 1268 } | |
| 1269 | |
| 1270 void test_fromEnvironment_int_default_parseError() { | |
| 1271 expect( | |
| 1272 _assertValidInt(_check_fromEnvironment_int("parseError", "123")), 123); | |
| 1273 } | |
| 1274 | |
| 1275 void test_fromEnvironment_int_default_undeclared() { | |
| 1276 expect(_assertValidInt(_check_fromEnvironment_int(null, "123")), 123); | |
| 1277 } | |
| 1278 | |
| 1279 void test_fromEnvironment_int_ok() { | |
| 1280 expect(_assertValidInt(_check_fromEnvironment_int("234", null)), 234); | |
| 1281 } | |
| 1282 | |
| 1283 void test_fromEnvironment_int_parseError() { | |
| 1284 _assertValidNull(_check_fromEnvironment_int("parseError", null)); | |
| 1285 } | |
| 1286 | |
| 1287 void test_fromEnvironment_int_parseError_nullDefault() { | |
| 1288 _assertValidNull(_check_fromEnvironment_int("parseError", "null")); | |
| 1289 } | |
| 1290 | |
| 1291 void test_fromEnvironment_int_undeclared() { | |
| 1292 _assertValidUnknown(_check_fromEnvironment_int(null, null)); | |
| 1293 } | |
| 1294 | |
| 1295 void test_fromEnvironment_int_undeclared_nullDefault() { | |
| 1296 _assertValidNull(_check_fromEnvironment_int(null, "null")); | |
| 1297 } | |
| 1298 | |
| 1299 void test_fromEnvironment_string_default_overridden() { | |
| 1300 expect(_assertValidString(_check_fromEnvironment_string("abc", "'def'")), | |
| 1301 "abc"); | |
| 1302 } | |
| 1303 | |
| 1304 void test_fromEnvironment_string_default_undeclared() { | |
| 1305 expect(_assertValidString(_check_fromEnvironment_string(null, "'def'")), | |
| 1306 "def"); | |
| 1307 } | |
| 1308 | |
| 1309 void test_fromEnvironment_string_empty() { | |
| 1310 expect(_assertValidString(_check_fromEnvironment_string("", null)), ""); | |
| 1311 } | |
| 1312 | |
| 1313 void test_fromEnvironment_string_ok() { | |
| 1314 expect( | |
| 1315 _assertValidString(_check_fromEnvironment_string("abc", null)), "abc"); | |
| 1316 } | |
| 1317 | |
| 1318 void test_fromEnvironment_string_undeclared() { | |
| 1319 _assertValidUnknown(_check_fromEnvironment_string(null, null)); | |
| 1320 } | |
| 1321 | |
| 1322 void test_fromEnvironment_string_undeclared_nullDefault() { | |
| 1323 _assertValidNull(_check_fromEnvironment_string(null, "null")); | |
| 1324 } | |
| 1325 | |
| 1326 void test_instanceCreationExpression_computedField() { | |
| 1327 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1328 const foo = const A(4, 5); | |
| 1329 class A { | |
| 1330 const A(int i, int j) : k = 2 * i + j; | |
| 1331 final int k; | |
| 1332 }'''); | |
| 1333 EvaluationResultImpl result = | |
| 1334 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1335 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1336 expect(fields, hasLength(1)); | |
| 1337 _assertIntField(fields, "k", 13); | |
| 1338 } | |
| 1339 | |
| 1340 void | |
| 1341 test_instanceCreationExpression_computedField_namedOptionalWithDefault() { | |
| 1342 _checkInstanceCreationOptionalParams(false, true, true); | |
| 1343 } | |
| 1344 | |
| 1345 void | |
| 1346 test_instanceCreationExpression_computedField_namedOptionalWithoutDefault(
) { | |
| 1347 _checkInstanceCreationOptionalParams(false, true, false); | |
| 1348 } | |
| 1349 | |
| 1350 void | |
| 1351 test_instanceCreationExpression_computedField_unnamedOptionalWithDefault()
{ | |
| 1352 _checkInstanceCreationOptionalParams(false, false, true); | |
| 1353 } | |
| 1354 | |
| 1355 void | |
| 1356 test_instanceCreationExpression_computedField_unnamedOptionalWithoutDefaul
t() { | |
| 1357 _checkInstanceCreationOptionalParams(false, false, false); | |
| 1358 } | |
| 1359 | |
| 1360 void test_instanceCreationExpression_computedField_usesConstConstructor() { | |
| 1361 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1362 const foo = const A(3); | |
| 1363 class A { | |
| 1364 const A(int i) : b = const B(4); | |
| 1365 final int b; | |
| 1366 } | |
| 1367 class B { | |
| 1368 const B(this.k); | |
| 1369 final int k; | |
| 1370 }'''); | |
| 1371 EvaluationResultImpl result = | |
| 1372 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1373 Map<String, DartObjectImpl> fieldsOfA = _assertType(result, "A"); | |
| 1374 expect(fieldsOfA, hasLength(1)); | |
| 1375 Map<String, DartObjectImpl> fieldsOfB = | |
| 1376 _assertFieldType(fieldsOfA, "b", "B"); | |
| 1377 expect(fieldsOfB, hasLength(1)); | |
| 1378 _assertIntField(fieldsOfB, "k", 4); | |
| 1379 } | |
| 1380 | |
| 1381 void test_instanceCreationExpression_computedField_usesStaticConst() { | |
| 1382 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1383 const foo = const A(3); | |
| 1384 class A { | |
| 1385 const A(int i) : k = i + B.bar; | |
| 1386 final int k; | |
| 1387 } | |
| 1388 class B { | |
| 1389 static const bar = 4; | |
| 1390 }'''); | |
| 1391 EvaluationResultImpl result = | |
| 1392 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1393 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1394 expect(fields, hasLength(1)); | |
| 1395 _assertIntField(fields, "k", 7); | |
| 1396 } | |
| 1397 | |
| 1398 void test_instanceCreationExpression_computedField_usesToplevelConst() { | |
| 1399 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1400 const foo = const A(3); | |
| 1401 const bar = 4; | |
| 1402 class A { | |
| 1403 const A(int i) : k = i + bar; | |
| 1404 final int k; | |
| 1405 }'''); | |
| 1406 EvaluationResultImpl result = | |
| 1407 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1408 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1409 expect(fields, hasLength(1)); | |
| 1410 _assertIntField(fields, "k", 7); | |
| 1411 } | |
| 1412 | |
| 1413 void test_instanceCreationExpression_explicitSuper() { | |
| 1414 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1415 const foo = const B(4, 5); | |
| 1416 class A { | |
| 1417 const A(this.x); | |
| 1418 final int x; | |
| 1419 } | |
| 1420 class B extends A { | |
| 1421 const B(int x, this.y) : super(x * 2); | |
| 1422 final int y; | |
| 1423 }'''); | |
| 1424 EvaluationResultImpl result = | |
| 1425 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1426 Map<String, DartObjectImpl> fields = _assertType(result, "B"); | |
| 1427 expect(fields, hasLength(2)); | |
| 1428 _assertIntField(fields, "y", 5); | |
| 1429 Map<String, DartObjectImpl> superclassFields = | |
| 1430 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A"); | |
| 1431 expect(superclassFields, hasLength(1)); | |
| 1432 _assertIntField(superclassFields, "x", 8); | |
| 1433 } | |
| 1434 | |
| 1435 void test_instanceCreationExpression_fieldFormalParameter() { | |
| 1436 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1437 const foo = const A(42); | |
| 1438 class A { | |
| 1439 int x; | |
| 1440 const A(this.x) | |
| 1441 }'''); | |
| 1442 EvaluationResultImpl result = | |
| 1443 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1444 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1445 expect(fields, hasLength(1)); | |
| 1446 _assertIntField(fields, "x", 42); | |
| 1447 } | |
| 1448 | |
| 1449 void | |
| 1450 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithDefa
ult() { | |
| 1451 _checkInstanceCreationOptionalParams(true, true, true); | |
| 1452 } | |
| 1453 | |
| 1454 void | |
| 1455 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithoutD
efault() { | |
| 1456 _checkInstanceCreationOptionalParams(true, true, false); | |
| 1457 } | |
| 1458 | |
| 1459 void | |
| 1460 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithDe
fault() { | |
| 1461 _checkInstanceCreationOptionalParams(true, false, true); | |
| 1462 } | |
| 1463 | |
| 1464 void | |
| 1465 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithou
tDefault() { | |
| 1466 _checkInstanceCreationOptionalParams(true, false, false); | |
| 1467 } | |
| 1468 | |
| 1469 void test_instanceCreationExpression_implicitSuper() { | |
| 1470 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1471 const foo = const B(4); | |
| 1472 class A { | |
| 1473 const A() : x = 3; | |
| 1474 final int x; | |
| 1475 } | |
| 1476 class B extends A { | |
| 1477 const B(this.y); | |
| 1478 final int y; | |
| 1479 }'''); | |
| 1480 EvaluationResultImpl result = | |
| 1481 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1482 Map<String, DartObjectImpl> fields = _assertType(result, "B"); | |
| 1483 expect(fields, hasLength(2)); | |
| 1484 _assertIntField(fields, "y", 4); | |
| 1485 Map<String, DartObjectImpl> superclassFields = | |
| 1486 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A"); | |
| 1487 expect(superclassFields, hasLength(1)); | |
| 1488 _assertIntField(superclassFields, "x", 3); | |
| 1489 } | |
| 1490 | |
| 1491 void test_instanceCreationExpression_nonFactoryRedirect() { | |
| 1492 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1493 const foo = const A.a1(); | |
| 1494 class A { | |
| 1495 const A.a1() : this.a2(); | |
| 1496 const A.a2() : x = 5; | |
| 1497 final int x; | |
| 1498 }'''); | |
| 1499 Map<String, DartObjectImpl> aFields = | |
| 1500 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1501 _assertIntField(aFields, 'x', 5); | |
| 1502 } | |
| 1503 | |
| 1504 void test_instanceCreationExpression_nonFactoryRedirect_arg() { | |
| 1505 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1506 const foo = const A.a1(1); | |
| 1507 class A { | |
| 1508 const A.a1(x) : this.a2(x + 100); | |
| 1509 const A.a2(x) : y = x + 10; | |
| 1510 final int y; | |
| 1511 }'''); | |
| 1512 Map<String, DartObjectImpl> aFields = | |
| 1513 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1514 _assertIntField(aFields, 'y', 111); | |
| 1515 } | |
| 1516 | |
| 1517 void test_instanceCreationExpression_nonFactoryRedirect_cycle() { | |
| 1518 // It is an error to have a cycle in non-factory redirects; however, we | |
| 1519 // need to make sure that even if the error occurs, attempting to evaluate | |
| 1520 // the constant will terminate. | |
| 1521 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1522 const foo = const A(); | |
| 1523 class A { | |
| 1524 const A() : this.b(); | |
| 1525 const A.b() : this(); | |
| 1526 }'''); | |
| 1527 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | |
| 1528 } | |
| 1529 | |
| 1530 void test_instanceCreationExpression_nonFactoryRedirect_defaultArg() { | |
| 1531 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1532 const foo = const A.a1(); | |
| 1533 class A { | |
| 1534 const A.a1() : this.a2(); | |
| 1535 const A.a2([x = 100]) : y = x + 10; | |
| 1536 final int y; | |
| 1537 }'''); | |
| 1538 Map<String, DartObjectImpl> aFields = | |
| 1539 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1540 _assertIntField(aFields, 'y', 110); | |
| 1541 } | |
| 1542 | |
| 1543 void test_instanceCreationExpression_nonFactoryRedirect_toMissing() { | |
| 1544 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1545 const foo = const A.a1(); | |
| 1546 class A { | |
| 1547 const A.a1() : this.a2(); | |
| 1548 }'''); | |
| 1549 // We don't care what value foo evaluates to (since there is a compile | |
| 1550 // error), but we shouldn't crash, and we should figure | |
| 1551 // out that it evaluates to an instance of class A. | |
| 1552 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1553 } | |
| 1554 | |
| 1555 void test_instanceCreationExpression_nonFactoryRedirect_toNonConst() { | |
| 1556 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1557 const foo = const A.a1(); | |
| 1558 class A { | |
| 1559 const A.a1() : this.a2(); | |
| 1560 A.a2(); | |
| 1561 }'''); | |
| 1562 // We don't care what value foo evaluates to (since there is a compile | |
| 1563 // error), but we shouldn't crash, and we should figure | |
| 1564 // out that it evaluates to an instance of class A. | |
| 1565 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1566 } | |
| 1567 | |
| 1568 void test_instanceCreationExpression_nonFactoryRedirect_unnamed() { | |
| 1569 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1570 const foo = const A.a1(); | |
| 1571 class A { | |
| 1572 const A.a1() : this(); | |
| 1573 const A() : x = 5; | |
| 1574 final int x; | |
| 1575 }'''); | |
| 1576 Map<String, DartObjectImpl> aFields = | |
| 1577 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); | |
| 1578 _assertIntField(aFields, 'x', 5); | |
| 1579 } | |
| 1580 | |
| 1581 void test_instanceCreationExpression_redirect() { | |
| 1582 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1583 const foo = const A(); | |
| 1584 class A { | |
| 1585 const factory A() = B; | |
| 1586 } | |
| 1587 class B implements A { | |
| 1588 const B(); | |
| 1589 }'''); | |
| 1590 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "B"); | |
| 1591 } | |
| 1592 | |
| 1593 void test_instanceCreationExpression_redirect_cycle() { | |
| 1594 // It is an error to have a cycle in factory redirects; however, we need | |
| 1595 // to make sure that even if the error occurs, attempting to evaluate the | |
| 1596 // constant will terminate. | |
| 1597 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1598 const foo = const A(); | |
| 1599 class A { | |
| 1600 const factory A() = A.b; | |
| 1601 const factory A.b() = A; | |
| 1602 }'''); | |
| 1603 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | |
| 1604 } | |
| 1605 | |
| 1606 void test_instanceCreationExpression_redirect_extern() { | |
| 1607 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1608 const foo = const A(); | |
| 1609 class A { | |
| 1610 external const factory A(); | |
| 1611 }'''); | |
| 1612 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | |
| 1613 } | |
| 1614 | |
| 1615 void test_instanceCreationExpression_redirect_nonConst() { | |
| 1616 // It is an error for a const factory constructor redirect to a non-const | |
| 1617 // constructor; however, we need to make sure that even if the error | |
| 1618 // attempting to evaluate the constant won't cause a crash. | |
| 1619 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1620 const foo = const A(); | |
| 1621 class A { | |
| 1622 const factory A() = A.b; | |
| 1623 A.b(); | |
| 1624 }'''); | |
| 1625 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | |
| 1626 } | |
| 1627 | |
| 1628 void test_instanceCreationExpression_redirectWithTypeParams() { | |
| 1629 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1630 class A { | |
| 1631 const factory A(var a) = B<int>; | |
| 1632 } | |
| 1633 | |
| 1634 class B<T> implements A { | |
| 1635 final T x; | |
| 1636 const B(this.x); | |
| 1637 } | |
| 1638 | |
| 1639 const A a = const A(10);'''); | |
| 1640 EvaluationResultImpl result = | |
| 1641 _evaluateTopLevelVariable(compilationUnit, "a"); | |
| 1642 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>"); | |
| 1643 expect(fields, hasLength(1)); | |
| 1644 _assertIntField(fields, "x", 10); | |
| 1645 } | |
| 1646 | |
| 1647 void test_instanceCreationExpression_redirectWithTypeSubstitution() { | |
| 1648 // To evaluate the redirection of A<int>, | |
| 1649 // A's template argument (T=int) must be substituted | |
| 1650 // into B's template argument (B<U> where U=T) to get B<int>. | |
| 1651 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1652 class A<T> { | |
| 1653 const factory A(var a) = B<T>; | |
| 1654 } | |
| 1655 | |
| 1656 class B<U> implements A { | |
| 1657 final U x; | |
| 1658 const B(this.x); | |
| 1659 } | |
| 1660 | |
| 1661 const A<int> a = const A<int>(10);'''); | |
| 1662 EvaluationResultImpl result = | |
| 1663 _evaluateTopLevelVariable(compilationUnit, "a"); | |
| 1664 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>"); | |
| 1665 expect(fields, hasLength(1)); | |
| 1666 _assertIntField(fields, "x", 10); | |
| 1667 } | |
| 1668 | |
| 1669 void test_instanceCreationExpression_symbol() { | |
| 1670 CompilationUnit compilationUnit = | |
| 1671 resolveSource("const foo = const Symbol('a');"); | |
| 1672 EvaluationResultImpl evaluationResult = | |
| 1673 _evaluateTopLevelVariable(compilationUnit, "foo"); | |
| 1674 expect(evaluationResult.value, isNotNull); | |
| 1675 DartObjectImpl value = evaluationResult.value; | |
| 1676 expect(value.type, typeProvider.symbolType); | |
| 1677 expect(value.toSymbolValue(), "a"); | |
| 1678 } | |
| 1679 | |
| 1680 void test_instanceCreationExpression_withSupertypeParams_explicit() { | |
| 1681 _checkInstanceCreation_withSupertypeParams(true); | |
| 1682 } | |
| 1683 | |
| 1684 void test_instanceCreationExpression_withSupertypeParams_implicit() { | |
| 1685 _checkInstanceCreation_withSupertypeParams(false); | |
| 1686 } | |
| 1687 | |
| 1688 void test_instanceCreationExpression_withTypeParams() { | |
| 1689 CompilationUnit compilationUnit = resolveSource(r''' | |
| 1690 class C<E> { | |
| 1691 const C(); | |
| 1692 } | |
| 1693 const c_int = const C<int>(); | |
| 1694 const c_num = const C<num>();'''); | |
| 1695 EvaluationResultImpl c_int = | |
| 1696 _evaluateTopLevelVariable(compilationUnit, "c_int"); | |
| 1697 _assertType(c_int, "C<int>"); | |
| 1698 DartObjectImpl c_int_value = c_int.value; | |
| 1699 EvaluationResultImpl c_num = | |
| 1700 _evaluateTopLevelVariable(compilationUnit, "c_num"); | |
| 1701 _assertType(c_num, "C<num>"); | |
| 1702 DartObjectImpl c_num_value = c_num.value; | |
| 1703 expect(c_int_value == c_num_value, isFalse); | |
| 1704 } | |
| 1705 | |
| 1706 void test_isValidSymbol() { | |
| 1707 expect(ConstantEvaluationEngine.isValidPublicSymbol(""), isTrue); | |
| 1708 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo"), isTrue); | |
| 1709 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar"), isTrue); | |
| 1710 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$"), isTrue); | |
| 1711 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$bar"), isTrue); | |
| 1712 expect(ConstantEvaluationEngine.isValidPublicSymbol("iff"), isTrue); | |
| 1713 expect(ConstantEvaluationEngine.isValidPublicSymbol("gif"), isTrue); | |
| 1714 expect(ConstantEvaluationEngine.isValidPublicSymbol("if\$"), isTrue); | |
| 1715 expect(ConstantEvaluationEngine.isValidPublicSymbol("\$if"), isTrue); | |
| 1716 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo="), isTrue); | |
| 1717 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar="), isTrue); | |
| 1718 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.+"), isTrue); | |
| 1719 expect(ConstantEvaluationEngine.isValidPublicSymbol("void"), isTrue); | |
| 1720 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo"), isFalse); | |
| 1721 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo.bar"), isFalse); | |
| 1722 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo._bar"), isFalse); | |
| 1723 expect(ConstantEvaluationEngine.isValidPublicSymbol("if"), isFalse); | |
| 1724 expect(ConstantEvaluationEngine.isValidPublicSymbol("if.foo"), isFalse); | |
| 1725 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.if"), isFalse); | |
| 1726 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo=.bar"), isFalse); | |
| 1727 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo."), isFalse); | |
| 1728 expect(ConstantEvaluationEngine.isValidPublicSymbol("+.foo"), isFalse); | |
| 1729 expect(ConstantEvaluationEngine.isValidPublicSymbol("void.foo"), isFalse); | |
| 1730 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.void"), isFalse); | |
| 1731 } | |
| 1732 | |
| 1733 void test_length_of_improperly_typed_string_expression() { | |
| 1734 // Since type annotations are ignored in unchecked mode, the improper | |
| 1735 // types on s1 and s2 shouldn't prevent us from evaluating i to | |
| 1736 // 'alpha'.length. | |
| 1737 CompilationUnit compilationUnit = resolveSource(''' | |
| 1738 const int s1 = 'alpha'; | |
| 1739 const int s2 = 'beta'; | |
| 1740 const int i = (true ? s1 : s2).length; | |
| 1741 '''); | |
| 1742 ConstTopLevelVariableElementImpl element = | |
| 1743 findTopLevelDeclaration(compilationUnit, 'i').element; | |
| 1744 EvaluationResultImpl result = element.evaluationResult; | |
| 1745 expect(_assertValidInt(result), 5); | |
| 1746 } | |
| 1747 | |
| 1748 void test_length_of_improperly_typed_string_identifier() { | |
| 1749 // Since type annotations are ignored in unchecked mode, the improper type | |
| 1750 // on s shouldn't prevent us from evaluating i to 'alpha'.length. | |
| 1751 CompilationUnit compilationUnit = resolveSource(''' | |
| 1752 const int s = 'alpha'; | |
| 1753 const int i = s.length; | |
| 1754 '''); | |
| 1755 ConstTopLevelVariableElementImpl element = | |
| 1756 findTopLevelDeclaration(compilationUnit, 'i').element; | |
| 1757 EvaluationResultImpl result = element.evaluationResult; | |
| 1758 expect(_assertValidInt(result), 5); | |
| 1759 } | |
| 1760 | |
| 1761 void test_non_static_const_initialized_at_declaration() { | |
| 1762 // Even though non-static consts are not allowed by the language, we need | |
| 1763 // to handle them for error recovery purposes. | |
| 1764 CompilationUnit compilationUnit = resolveSource(''' | |
| 1765 class A { | |
| 1766 const int i = 123; | |
| 1767 const A(); | |
| 1768 } | |
| 1769 | |
| 1770 const A a = const A(); | |
| 1771 '''); | |
| 1772 EvaluationResultImpl result = | |
| 1773 _evaluateTopLevelVariable(compilationUnit, 'a'); | |
| 1774 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | |
| 1775 expect(fields, hasLength(1)); | |
| 1776 _assertIntField(fields, "i", 123); | |
| 1777 } | |
| 1778 | |
| 1779 void test_symbolLiteral_void() { | |
| 1780 CompilationUnit compilationUnit = | |
| 1781 resolveSource("const voidSymbol = #void;"); | |
| 1782 VariableDeclaration voidSymbol = | |
| 1783 findTopLevelDeclaration(compilationUnit, "voidSymbol"); | |
| 1784 EvaluationResultImpl voidSymbolResult = | |
| 1785 (voidSymbol.element as VariableElementImpl).evaluationResult; | |
| 1786 DartObjectImpl value = voidSymbolResult.value; | |
| 1787 expect(value.type, typeProvider.symbolType); | |
| 1788 expect(value.toSymbolValue(), "void"); | |
| 1789 } | |
| 1790 | |
| 1791 Map<String, DartObjectImpl> _assertFieldType( | |
| 1792 Map<String, DartObjectImpl> fields, | |
| 1793 String fieldName, | |
| 1794 String expectedType) { | |
| 1795 DartObjectImpl field = fields[fieldName]; | |
| 1796 expect(field.type.displayName, expectedType); | |
| 1797 return field.fields; | |
| 1798 } | |
| 1799 | |
| 1800 void _assertIntField( | |
| 1801 Map<String, DartObjectImpl> fields, String fieldName, int expectedValue) { | |
| 1802 DartObjectImpl field = fields[fieldName]; | |
| 1803 expect(field.type.name, "int"); | |
| 1804 expect(field.toIntValue(), expectedValue); | |
| 1805 } | |
| 1806 | |
| 1807 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) { | |
| 1808 DartObjectImpl field = fields[fieldName]; | |
| 1809 expect(field.isNull, isTrue); | |
| 1810 } | |
| 1811 | |
| 1812 void _assertProperDependencies(String sourceText, | |
| 1813 [List<ErrorCode> expectedErrorCodes = ErrorCode.EMPTY_LIST]) { | |
| 1814 Source source = addSource(sourceText); | |
| 1815 LibraryElement element = resolve2(source); | |
| 1816 CompilationUnit unit = | |
| 1817 analysisContext.resolveCompilationUnit(source, element); | |
| 1818 expect(unit, isNotNull); | |
| 1819 ConstantValueComputer computer = _makeConstantValueComputer(); | |
| 1820 computer.add(unit, source, source); | |
| 1821 computer.computeValues(); | |
| 1822 assertErrors(source, expectedErrorCodes); | |
| 1823 } | |
| 1824 | |
| 1825 Map<String, DartObjectImpl> _assertType( | |
| 1826 EvaluationResultImpl result, String typeName) { | |
| 1827 expect(result.value, isNotNull); | |
| 1828 DartObjectImpl value = result.value; | |
| 1829 expect(value.type.displayName, typeName); | |
| 1830 return value.fields; | |
| 1831 } | |
| 1832 | |
| 1833 bool _assertValidBool(EvaluationResultImpl result) { | |
| 1834 expect(result.value, isNotNull); | |
| 1835 DartObjectImpl value = result.value; | |
| 1836 expect(value.type, typeProvider.boolType); | |
| 1837 bool boolValue = value.toBoolValue(); | |
| 1838 expect(boolValue, isNotNull); | |
| 1839 return boolValue; | |
| 1840 } | |
| 1841 | |
| 1842 int _assertValidInt(EvaluationResultImpl result) { | |
| 1843 expect(result.value, isNotNull); | |
| 1844 DartObjectImpl value = result.value; | |
| 1845 expect(value.type, typeProvider.intType); | |
| 1846 return value.toIntValue(); | |
| 1847 } | |
| 1848 | |
| 1849 void _assertValidNull(EvaluationResultImpl result) { | |
| 1850 expect(result.value, isNotNull); | |
| 1851 DartObjectImpl value = result.value; | |
| 1852 expect(value.type, typeProvider.nullType); | |
| 1853 } | |
| 1854 | |
| 1855 String _assertValidString(EvaluationResultImpl result) { | |
| 1856 expect(result.value, isNotNull); | |
| 1857 DartObjectImpl value = result.value; | |
| 1858 expect(value.type, typeProvider.stringType); | |
| 1859 return value.toStringValue(); | |
| 1860 } | |
| 1861 | |
| 1862 void _assertValidUnknown(EvaluationResultImpl result) { | |
| 1863 expect(result.value, isNotNull); | |
| 1864 DartObjectImpl value = result.value; | |
| 1865 expect(value.isUnknown, isTrue); | |
| 1866 } | |
| 1867 | |
| 1868 EvaluationResultImpl _check_fromEnvironment_bool( | |
| 1869 String valueInEnvironment, String defaultExpr) { | |
| 1870 String envVarName = "x"; | |
| 1871 String varName = "foo"; | |
| 1872 if (valueInEnvironment != null) { | |
| 1873 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment); | |
| 1874 } | |
| 1875 String defaultArg = | |
| 1876 defaultExpr == null ? "" : ", defaultValue: $defaultExpr"; | |
| 1877 CompilationUnit compilationUnit = resolveSource( | |
| 1878 "const $varName = const bool.fromEnvironment('$envVarName'$defaultArg);"
); | |
| 1879 return _evaluateTopLevelVariable(compilationUnit, varName); | |
| 1880 } | |
| 1881 | |
| 1882 EvaluationResultImpl _check_fromEnvironment_int( | |
| 1883 String valueInEnvironment, String defaultExpr) { | |
| 1884 String envVarName = "x"; | |
| 1885 String varName = "foo"; | |
| 1886 if (valueInEnvironment != null) { | |
| 1887 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment); | |
| 1888 } | |
| 1889 String defaultArg = | |
| 1890 defaultExpr == null ? "" : ", defaultValue: $defaultExpr"; | |
| 1891 CompilationUnit compilationUnit = resolveSource( | |
| 1892 "const $varName = const int.fromEnvironment('$envVarName'$defaultArg);")
; | |
| 1893 return _evaluateTopLevelVariable(compilationUnit, varName); | |
| 1894 } | |
| 1895 | |
| 1896 EvaluationResultImpl _check_fromEnvironment_string( | |
| 1897 String valueInEnvironment, String defaultExpr) { | |
| 1898 String envVarName = "x"; | |
| 1899 String varName = "foo"; | |
| 1900 if (valueInEnvironment != null) { | |
| 1901 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment); | |
| 1902 } | |
| 1903 String defaultArg = | |
| 1904 defaultExpr == null ? "" : ", defaultValue: $defaultExpr"; | |
| 1905 CompilationUnit compilationUnit = resolveSource( | |
| 1906 "const $varName = const String.fromEnvironment('$envVarName'$defaultArg)
;"); | |
| 1907 return _evaluateTopLevelVariable(compilationUnit, varName); | |
| 1908 } | |
| 1909 | |
| 1910 void _checkInstanceCreation_withSupertypeParams(bool isExplicit) { | |
| 1911 String superCall = isExplicit ? " : super()" : ""; | |
| 1912 CompilationUnit compilationUnit = resolveSource(""" | |
| 1913 class A<T> { | |
| 1914 const A(); | |
| 1915 } | |
| 1916 class B<T, U> extends A<T> { | |
| 1917 const B()$superCall; | |
| 1918 } | |
| 1919 class C<T, U> extends A<U> { | |
| 1920 const C()$superCall; | |
| 1921 } | |
| 1922 const b_int_num = const B<int, num>(); | |
| 1923 const c_int_num = const C<int, num>();"""); | |
| 1924 EvaluationResultImpl b_int_num = | |
| 1925 _evaluateTopLevelVariable(compilationUnit, "b_int_num"); | |
| 1926 Map<String, DartObjectImpl> b_int_num_fields = | |
| 1927 _assertType(b_int_num, "B<int, num>"); | |
| 1928 _assertFieldType(b_int_num_fields, GenericState.SUPERCLASS_FIELD, "A<int>"); | |
| 1929 EvaluationResultImpl c_int_num = | |
| 1930 _evaluateTopLevelVariable(compilationUnit, "c_int_num"); | |
| 1931 Map<String, DartObjectImpl> c_int_num_fields = | |
| 1932 _assertType(c_int_num, "C<int, num>"); | |
| 1933 _assertFieldType(c_int_num_fields, GenericState.SUPERCLASS_FIELD, "A<num>"); | |
| 1934 } | |
| 1935 | |
| 1936 void _checkInstanceCreationOptionalParams( | |
| 1937 bool isFieldFormal, bool isNamed, bool hasDefault) { | |
| 1938 String fieldName = "j"; | |
| 1939 String paramName = isFieldFormal ? fieldName : "i"; | |
| 1940 String formalParam = | |
| 1941 "${isFieldFormal ? "this." : "int "}$paramName${hasDefault ? " = 3" : ""
}"; | |
| 1942 CompilationUnit compilationUnit = resolveSource(""" | |
| 1943 const x = const A(); | |
| 1944 const y = const A(${isNamed ? '$paramName: ' : ''}10); | |
| 1945 class A { | |
| 1946 const A(${isNamed ? "{$formalParam}" : "[$formalParam]"})${isFieldFormal ? ""
: " : $fieldName = $paramName"}; | |
| 1947 final int $fieldName; | |
| 1948 }"""); | |
| 1949 EvaluationResultImpl x = _evaluateTopLevelVariable(compilationUnit, "x"); | |
| 1950 Map<String, DartObjectImpl> fieldsOfX = _assertType(x, "A"); | |
| 1951 expect(fieldsOfX, hasLength(1)); | |
| 1952 if (hasDefault) { | |
| 1953 _assertIntField(fieldsOfX, fieldName, 3); | |
| 1954 } else { | |
| 1955 _assertNullField(fieldsOfX, fieldName); | |
| 1956 } | |
| 1957 EvaluationResultImpl y = _evaluateTopLevelVariable(compilationUnit, "y"); | |
| 1958 Map<String, DartObjectImpl> fieldsOfY = _assertType(y, "A"); | |
| 1959 expect(fieldsOfY, hasLength(1)); | |
| 1960 _assertIntField(fieldsOfY, fieldName, 10); | |
| 1961 } | |
| 1962 | |
| 1963 /** | |
| 1964 * Search [compilationUnit] for a class named [className], containing a | |
| 1965 * method [methodName], with exactly one annotation. Return the constant | |
| 1966 * value of the annotation. | |
| 1967 */ | |
| 1968 EvaluationResultImpl _evaluateAnnotation( | |
| 1969 CompilationUnit compilationUnit, String className, String memberName) { | |
| 1970 for (CompilationUnitMember member in compilationUnit.declarations) { | |
| 1971 if (member is ClassDeclaration && member.name.name == className) { | |
| 1972 for (ClassMember classMember in member.members) { | |
| 1973 if (classMember is MethodDeclaration && | |
| 1974 classMember.name.name == memberName) { | |
| 1975 expect(classMember.metadata, hasLength(1)); | |
| 1976 ElementAnnotationImpl elementAnnotation = | |
| 1977 classMember.metadata[0].elementAnnotation; | |
| 1978 return elementAnnotation.evaluationResult; | |
| 1979 } | |
| 1980 } | |
| 1981 } | |
| 1982 } | |
| 1983 fail('Class member not found'); | |
| 1984 return null; | |
| 1985 } | |
| 1986 | |
| 1987 EvaluationResultImpl _evaluateTopLevelVariable( | |
| 1988 CompilationUnit compilationUnit, String name) { | |
| 1989 VariableDeclaration varDecl = | |
| 1990 findTopLevelDeclaration(compilationUnit, name); | |
| 1991 ConstTopLevelVariableElementImpl varElement = varDecl.element; | |
| 1992 return varElement.evaluationResult; | |
| 1993 } | |
| 1994 | |
| 1995 ConstantValueComputer _makeConstantValueComputer() { | |
| 1996 ConstantEvaluationValidator_ForTest validator = | |
| 1997 new ConstantEvaluationValidator_ForTest(); | |
| 1998 validator.computer = new ConstantValueComputer( | |
| 1999 analysisContext2, | |
| 2000 analysisContext2.typeProvider, | |
| 2001 analysisContext2.declaredVariables, | |
| 2002 validator, | |
| 2003 analysisContext2.typeSystem); | |
| 2004 return validator.computer; | |
| 2005 } | |
| 2006 | |
| 2007 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) { | |
| 2008 for (VariableDeclaration declaration in declarationList.variables) { | |
| 2009 VariableElementImpl element = declaration.element as VariableElementImpl; | |
| 2010 expect(element, isNotNull); | |
| 2011 EvaluationResultImpl result = element.evaluationResult; | |
| 2012 if (shouldBeValid) { | |
| 2013 expect(result.value, isNotNull); | |
| 2014 } else { | |
| 2015 expect(result.value, isNull); | |
| 2016 } | |
| 2017 } | |
| 2018 } | |
| 2019 } | |
| 2020 | |
| 2021 @reflectiveTest | |
| 2022 class ConstantVisitorTest extends ResolverTestCase { | |
| 2023 void test_visitBinaryExpression_questionQuestion_notNull_notNull() { | |
| 2024 Expression left = AstFactory.string2('a'); | |
| 2025 Expression right = AstFactory.string2('b'); | |
| 2026 Expression expression = | |
| 2027 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right); | |
| 2028 | |
| 2029 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2030 ErrorReporter errorReporter = | |
| 2031 new ErrorReporter(errorListener, _dummySource()); | |
| 2032 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2033 expect(result, isNotNull); | |
| 2034 expect(result.isNull, isFalse); | |
| 2035 expect(result.toStringValue(), 'a'); | |
| 2036 errorListener.assertNoErrors(); | |
| 2037 } | |
| 2038 | |
| 2039 void test_visitBinaryExpression_questionQuestion_null_notNull() { | |
| 2040 Expression left = AstFactory.nullLiteral(); | |
| 2041 Expression right = AstFactory.string2('b'); | |
| 2042 Expression expression = | |
| 2043 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right); | |
| 2044 | |
| 2045 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2046 ErrorReporter errorReporter = | |
| 2047 new ErrorReporter(errorListener, _dummySource()); | |
| 2048 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2049 expect(result, isNotNull); | |
| 2050 expect(result.isNull, isFalse); | |
| 2051 expect(result.toStringValue(), 'b'); | |
| 2052 errorListener.assertNoErrors(); | |
| 2053 } | |
| 2054 | |
| 2055 void test_visitBinaryExpression_questionQuestion_null_null() { | |
| 2056 Expression left = AstFactory.nullLiteral(); | |
| 2057 Expression right = AstFactory.nullLiteral(); | |
| 2058 Expression expression = | |
| 2059 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right); | |
| 2060 | |
| 2061 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2062 ErrorReporter errorReporter = | |
| 2063 new ErrorReporter(errorListener, _dummySource()); | |
| 2064 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2065 expect(result, isNotNull); | |
| 2066 expect(result.isNull, isTrue); | |
| 2067 errorListener.assertNoErrors(); | |
| 2068 } | |
| 2069 | |
| 2070 void test_visitConditionalExpression_false() { | |
| 2071 Expression thenExpression = AstFactory.integer(1); | |
| 2072 Expression elseExpression = AstFactory.integer(0); | |
| 2073 ConditionalExpression expression = AstFactory.conditionalExpression( | |
| 2074 AstFactory.booleanLiteral(false), thenExpression, elseExpression); | |
| 2075 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2076 ErrorReporter errorReporter = | |
| 2077 new ErrorReporter(errorListener, _dummySource()); | |
| 2078 _assertValue(0, _evaluate(expression, errorReporter)); | |
| 2079 errorListener.assertNoErrors(); | |
| 2080 } | |
| 2081 | |
| 2082 void test_visitConditionalExpression_nonBooleanCondition() { | |
| 2083 Expression thenExpression = AstFactory.integer(1); | |
| 2084 Expression elseExpression = AstFactory.integer(0); | |
| 2085 NullLiteral conditionExpression = AstFactory.nullLiteral(); | |
| 2086 ConditionalExpression expression = AstFactory.conditionalExpression( | |
| 2087 conditionExpression, thenExpression, elseExpression); | |
| 2088 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2089 ErrorReporter errorReporter = | |
| 2090 new ErrorReporter(errorListener, _dummySource()); | |
| 2091 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2092 expect(result, isNull); | |
| 2093 errorListener | |
| 2094 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]); | |
| 2095 } | |
| 2096 | |
| 2097 void test_visitConditionalExpression_nonConstantElse() { | |
| 2098 Expression thenExpression = AstFactory.integer(1); | |
| 2099 Expression elseExpression = AstFactory.identifier3("x"); | |
| 2100 ConditionalExpression expression = AstFactory.conditionalExpression( | |
| 2101 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | |
| 2102 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2103 ErrorReporter errorReporter = | |
| 2104 new ErrorReporter(errorListener, _dummySource()); | |
| 2105 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2106 expect(result, isNull); | |
| 2107 errorListener | |
| 2108 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); | |
| 2109 } | |
| 2110 | |
| 2111 void test_visitConditionalExpression_nonConstantThen() { | |
| 2112 Expression thenExpression = AstFactory.identifier3("x"); | |
| 2113 Expression elseExpression = AstFactory.integer(0); | |
| 2114 ConditionalExpression expression = AstFactory.conditionalExpression( | |
| 2115 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | |
| 2116 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2117 ErrorReporter errorReporter = | |
| 2118 new ErrorReporter(errorListener, _dummySource()); | |
| 2119 DartObjectImpl result = _evaluate(expression, errorReporter); | |
| 2120 expect(result, isNull); | |
| 2121 errorListener | |
| 2122 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); | |
| 2123 } | |
| 2124 | |
| 2125 void test_visitConditionalExpression_true() { | |
| 2126 Expression thenExpression = AstFactory.integer(1); | |
| 2127 Expression elseExpression = AstFactory.integer(0); | |
| 2128 ConditionalExpression expression = AstFactory.conditionalExpression( | |
| 2129 AstFactory.booleanLiteral(true), thenExpression, elseExpression); | |
| 2130 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2131 ErrorReporter errorReporter = | |
| 2132 new ErrorReporter(errorListener, _dummySource()); | |
| 2133 _assertValue(1, _evaluate(expression, errorReporter)); | |
| 2134 errorListener.assertNoErrors(); | |
| 2135 } | |
| 2136 | |
| 2137 void test_visitSimpleIdentifier_className() { | |
| 2138 CompilationUnit compilationUnit = resolveSource(''' | |
| 2139 const a = C; | |
| 2140 class C {} | |
| 2141 '''); | |
| 2142 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); | |
| 2143 expect(result.type, typeProvider.typeType); | |
| 2144 expect(result.toTypeValue().name, 'C'); | |
| 2145 } | |
| 2146 | |
| 2147 void test_visitSimpleIdentifier_dynamic() { | |
| 2148 CompilationUnit compilationUnit = resolveSource(''' | |
| 2149 const a = dynamic; | |
| 2150 '''); | |
| 2151 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); | |
| 2152 expect(result.type, typeProvider.typeType); | |
| 2153 expect(result.toTypeValue(), typeProvider.dynamicType); | |
| 2154 } | |
| 2155 | |
| 2156 void test_visitSimpleIdentifier_inEnvironment() { | |
| 2157 CompilationUnit compilationUnit = resolveSource(r''' | |
| 2158 const a = b; | |
| 2159 const b = 3;'''); | |
| 2160 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>(); | |
| 2161 DartObjectImpl six = | |
| 2162 new DartObjectImpl(typeProvider.intType, new IntState(6)); | |
| 2163 environment["b"] = six; | |
| 2164 _assertValue(6, _evaluateConstant(compilationUnit, "a", environment)); | |
| 2165 } | |
| 2166 | |
| 2167 void test_visitSimpleIdentifier_notInEnvironment() { | |
| 2168 CompilationUnit compilationUnit = resolveSource(r''' | |
| 2169 const a = b; | |
| 2170 const b = 3;'''); | |
| 2171 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>(); | |
| 2172 DartObjectImpl six = | |
| 2173 new DartObjectImpl(typeProvider.intType, new IntState(6)); | |
| 2174 environment["c"] = six; | |
| 2175 _assertValue(3, _evaluateConstant(compilationUnit, "a", environment)); | |
| 2176 } | |
| 2177 | |
| 2178 void test_visitSimpleIdentifier_withoutEnvironment() { | |
| 2179 CompilationUnit compilationUnit = resolveSource(r''' | |
| 2180 const a = b; | |
| 2181 const b = 3;'''); | |
| 2182 _assertValue(3, _evaluateConstant(compilationUnit, "a", null)); | |
| 2183 } | |
| 2184 | |
| 2185 void _assertValue(int expectedValue, DartObjectImpl result) { | |
| 2186 expect(result, isNotNull); | |
| 2187 expect(result.type.name, "int"); | |
| 2188 expect(result.toIntValue(), expectedValue); | |
| 2189 } | |
| 2190 | |
| 2191 NonExistingSource _dummySource() { | |
| 2192 String path = '/test.dart'; | |
| 2193 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI); | |
| 2194 } | |
| 2195 | |
| 2196 DartObjectImpl _evaluate(Expression expression, ErrorReporter errorReporter) { | |
| 2197 return expression.accept(new ConstantVisitor( | |
| 2198 new ConstantEvaluationEngine( | |
| 2199 new TestTypeProvider(), new DeclaredVariables(), | |
| 2200 typeSystem: new TypeSystemImpl()), | |
| 2201 errorReporter)); | |
| 2202 } | |
| 2203 | |
| 2204 DartObjectImpl _evaluateConstant(CompilationUnit compilationUnit, String name, | |
| 2205 Map<String, DartObjectImpl> lexicalEnvironment) { | |
| 2206 Source source = compilationUnit.element.source; | |
| 2207 Expression expression = | |
| 2208 findTopLevelConstantExpression(compilationUnit, name); | |
| 2209 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 2210 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); | |
| 2211 DartObjectImpl result = expression.accept(new ConstantVisitor( | |
| 2212 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(), | |
| 2213 typeSystem: typeSystem), | |
| 2214 errorReporter, | |
| 2215 lexicalEnvironment: lexicalEnvironment)); | |
| 2216 errorListener.assertNoErrors(); | |
| 2217 return result; | |
| 2218 } | |
| 2219 } | |
| 2220 | |
| 2221 @reflectiveTest | 65 @reflectiveTest |
| 2222 class ContentCacheTest { | 66 class ContentCacheTest { |
| 2223 void test_setContents() { | 67 void test_setContents() { |
| 2224 Source source = new TestSource(); | 68 Source source = new TestSource(); |
| 2225 ContentCache cache = new ContentCache(); | 69 ContentCache cache = new ContentCache(); |
| 2226 expect(cache.getContents(source), isNull); | 70 expect(cache.getContents(source), isNull); |
| 2227 expect(cache.getModificationStamp(source), isNull); | 71 expect(cache.getModificationStamp(source), isNull); |
| 2228 String contents = "library lib;"; | 72 String contents = "library lib;"; |
| 2229 expect(cache.setContents(source, contents), isNull); | 73 expect(cache.setContents(source, contents), isNull); |
| 2230 expect(cache.getContents(source), contents); | 74 expect(cache.getContents(source), contents); |
| (...skipping 6025 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8256 expect(second.category, "Server"); | 6100 expect(second.category, "Server"); |
| 8257 expect(second.path, "second/second.dart"); | 6101 expect(second.path, "second/second.dart"); |
| 8258 expect(second.shortName, "dart:second"); | 6102 expect(second.shortName, "dart:second"); |
| 8259 expect(second.isDart2JsLibrary, false); | 6103 expect(second.isDart2JsLibrary, false); |
| 8260 expect(second.isDocumented, false); | 6104 expect(second.isDocumented, false); |
| 8261 expect(second.isImplementation, true); | 6105 expect(second.isImplementation, true); |
| 8262 expect(second.isVmLibrary, false); | 6106 expect(second.isVmLibrary, false); |
| 8263 } | 6107 } |
| 8264 } | 6108 } |
| 8265 | 6109 |
| 8266 class TestAnalysisContext_ConstantFinderTest extends TestAnalysisContext { | |
| 8267 bool invoked = false; | |
| 8268 TestAnalysisContext_ConstantFinderTest(); | |
| 8269 | |
| 8270 @override | |
| 8271 InternalAnalysisContext getContextFor(Source source) { | |
| 8272 return this; | |
| 8273 } | |
| 8274 } | |
| 8275 | |
| 8276 @reflectiveTest | 6110 @reflectiveTest |
| 8277 class UriKindTest { | 6111 class UriKindTest { |
| 8278 void test_fromEncoding() { | 6112 void test_fromEncoding() { |
| 8279 expect(UriKind.fromEncoding(0x64), same(UriKind.DART_URI)); | 6113 expect(UriKind.fromEncoding(0x64), same(UriKind.DART_URI)); |
| 8280 expect(UriKind.fromEncoding(0x66), same(UriKind.FILE_URI)); | 6114 expect(UriKind.fromEncoding(0x66), same(UriKind.FILE_URI)); |
| 8281 expect(UriKind.fromEncoding(0x70), same(UriKind.PACKAGE_URI)); | 6115 expect(UriKind.fromEncoding(0x70), same(UriKind.PACKAGE_URI)); |
| 8282 expect(UriKind.fromEncoding(0x58), same(null)); | 6116 expect(UriKind.fromEncoding(0x58), same(null)); |
| 8283 } | 6117 } |
| 8284 | 6118 |
| 8285 void test_getEncoding() { | 6119 void test_getEncoding() { |
| 8286 expect(UriKind.DART_URI.encoding, 0x64); | 6120 expect(UriKind.DART_URI.encoding, 0x64); |
| 8287 expect(UriKind.FILE_URI.encoding, 0x66); | 6121 expect(UriKind.FILE_URI.encoding, 0x66); |
| 8288 expect(UriKind.PACKAGE_URI.encoding, 0x70); | 6122 expect(UriKind.PACKAGE_URI.encoding, 0x70); |
| 8289 } | 6123 } |
| 8290 } | 6124 } |
| OLD | NEW |