| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.constant_test; | 5 library analyzer.test.constant_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/src/dart/element/element.dart'; | 10 import 'package:analyzer/src/dart/element/element.dart'; |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 277 |
| 278 void test_literal_list() { | 278 void test_literal_list() { |
| 279 EvaluationResult result = _getExpressionValue("const ['a', 'b', 'c']"); | 279 EvaluationResult result = _getExpressionValue("const ['a', 'b', 'c']"); |
| 280 expect(result.isValid, isTrue); | 280 expect(result.isValid, isTrue); |
| 281 } | 281 } |
| 282 | 282 |
| 283 void test_literal_map() { | 283 void test_literal_map() { |
| 284 EvaluationResult result = | 284 EvaluationResult result = |
| 285 _getExpressionValue("const {'a' : 'm', 'b' : 'n', 'c' : 'o'}"); | 285 _getExpressionValue("const {'a' : 'm', 'b' : 'n', 'c' : 'o'}"); |
| 286 expect(result.isValid, isTrue); | 286 expect(result.isValid, isTrue); |
| 287 Map<DartObject, DartObject> map = result.value.toMapValue(); |
| 288 expect(map.keys.map((k) => k.toStringValue()), ['a', 'b', 'c']); |
| 287 } | 289 } |
| 288 | 290 |
| 289 void test_literal_null() { | 291 void test_literal_null() { |
| 290 EvaluationResult result = _getExpressionValue("null"); | 292 EvaluationResult result = _getExpressionValue("null"); |
| 291 expect(result.isValid, isTrue); | 293 expect(result.isValid, isTrue); |
| 292 DartObject value = result.value; | 294 DartObject value = result.value; |
| 293 expect(value.isNull, isTrue); | 295 expect(value.isNull, isTrue); |
| 294 } | 296 } |
| 295 | 297 |
| 296 void test_literal_number_double() { | 298 void test_literal_number_double() { |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 827 } | 829 } |
| 828 | 830 |
| 829 class C { | 831 class C { |
| 830 @A.i(1) | 832 @A.i(1) |
| 831 f() {} | 833 f() {} |
| 832 } | 834 } |
| 833 '''); | 835 '''); |
| 834 _evaluateAnnotation(compilationUnit, "C", "f"); | 836 _evaluateAnnotation(compilationUnit, "C", "f"); |
| 835 } | 837 } |
| 836 | 838 |
| 837 void test_annotation_toplevelVariable() { | 839 void test_annotation_topLevelVariable() { |
| 838 CompilationUnit compilationUnit = resolveSource(r''' | 840 CompilationUnit compilationUnit = resolveSource(r''' |
| 839 const int i = 5; | 841 const int i = 5; |
| 840 class C { | 842 class C { |
| 841 @i | 843 @i |
| 842 f() {} | 844 f() {} |
| 843 } | 845 } |
| 844 '''); | 846 '''); |
| 845 EvaluationResultImpl result = | 847 EvaluationResultImpl result = |
| 846 _evaluateAnnotation(compilationUnit, "C", "f"); | 848 _evaluateAnnotation(compilationUnit, "C", "f"); |
| 847 expect(_assertValidInt(result), 5); | 849 expect(_assertValidInt(result), 5); |
| 848 } | 850 } |
| 849 | 851 |
| 850 void test_annotation_toplevelVariable_args() { | 852 void test_annotation_topLevelVariable_args() { |
| 851 // Applying arguments to an annotation that is a toplevel variable is | 853 // Applying arguments to an annotation that is a top-level variable is |
| 852 // illegal, but shouldn't crash analysis. | 854 // illegal, but shouldn't crash analysis. |
| 853 CompilationUnit compilationUnit = resolveSource(r''' | 855 CompilationUnit compilationUnit = resolveSource(r''' |
| 854 const int i = 5; | 856 const int i = 5; |
| 855 class C { | 857 class C { |
| 856 @i(1) | 858 @i(1) |
| 857 f() {} | 859 f() {} |
| 858 } | 860 } |
| 859 '''); | 861 '''); |
| 860 _evaluateAnnotation(compilationUnit, "C", "f"); | 862 _evaluateAnnotation(compilationUnit, "C", "f"); |
| 861 } | 863 } |
| (...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1370 class B { | 1372 class B { |
| 1371 static const bar = 4; | 1373 static const bar = 4; |
| 1372 }'''); | 1374 }'''); |
| 1373 EvaluationResultImpl result = | 1375 EvaluationResultImpl result = |
| 1374 _evaluateTopLevelVariable(compilationUnit, "foo"); | 1376 _evaluateTopLevelVariable(compilationUnit, "foo"); |
| 1375 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | 1377 Map<String, DartObjectImpl> fields = _assertType(result, "A"); |
| 1376 expect(fields, hasLength(1)); | 1378 expect(fields, hasLength(1)); |
| 1377 _assertIntField(fields, "k", 7); | 1379 _assertIntField(fields, "k", 7); |
| 1378 } | 1380 } |
| 1379 | 1381 |
| 1380 void test_instanceCreationExpression_computedField_usesToplevelConst() { | 1382 void test_instanceCreationExpression_computedField_usesTopLevelConst() { |
| 1381 CompilationUnit compilationUnit = resolveSource(r''' | 1383 CompilationUnit compilationUnit = resolveSource(r''' |
| 1382 const foo = const A(3); | 1384 const foo = const A(3); |
| 1383 const bar = 4; | 1385 const bar = 4; |
| 1384 class A { | 1386 class A { |
| 1385 const A(int i) : k = i + bar; | 1387 const A(int i) : k = i + bar; |
| 1386 final int k; | 1388 final int k; |
| 1387 }'''); | 1389 }'''); |
| 1388 EvaluationResultImpl result = | 1390 EvaluationResultImpl result = |
| 1389 _evaluateTopLevelVariable(compilationUnit, "foo"); | 1391 _evaluateTopLevelVariable(compilationUnit, "foo"); |
| 1390 Map<String, DartObjectImpl> fields = _assertType(result, "A"); | 1392 Map<String, DartObjectImpl> fields = _assertType(result, "A"); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1578 // constant will terminate. | 1580 // constant will terminate. |
| 1579 CompilationUnit compilationUnit = resolveSource(r''' | 1581 CompilationUnit compilationUnit = resolveSource(r''' |
| 1580 const foo = const A(); | 1582 const foo = const A(); |
| 1581 class A { | 1583 class A { |
| 1582 const factory A() = A.b; | 1584 const factory A() = A.b; |
| 1583 const factory A.b() = A; | 1585 const factory A.b() = A; |
| 1584 }'''); | 1586 }'''); |
| 1585 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | 1587 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); |
| 1586 } | 1588 } |
| 1587 | 1589 |
| 1588 void test_instanceCreationExpression_redirect_extern() { | 1590 void test_instanceCreationExpression_redirect_external() { |
| 1589 CompilationUnit compilationUnit = resolveSource(r''' | 1591 CompilationUnit compilationUnit = resolveSource(r''' |
| 1590 const foo = const A(); | 1592 const foo = const A(); |
| 1591 class A { | 1593 class A { |
| 1592 external const factory A(); | 1594 external const factory A(); |
| 1593 }'''); | 1595 }'''); |
| 1594 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); | 1596 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); |
| 1595 } | 1597 } |
| 1596 | 1598 |
| 1597 void test_instanceCreationExpression_redirect_nonConst() { | 1599 void test_instanceCreationExpression_redirect_nonConst() { |
| 1598 // It is an error for a const factory constructor redirect to a non-const | 1600 // It is an error for a const factory constructor redirect to a non-const |
| (...skipping 2905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4504 | 4506 |
| 4505 void _visitNode(AstNode node) { | 4507 void _visitNode(AstNode node) { |
| 4506 node.accept(_createReferenceFinder(_head)); | 4508 node.accept(_createReferenceFinder(_head)); |
| 4507 } | 4509 } |
| 4508 } | 4510 } |
| 4509 | 4511 |
| 4510 class _TestAnalysisContext extends TestAnalysisContext { | 4512 class _TestAnalysisContext extends TestAnalysisContext { |
| 4511 @override | 4513 @override |
| 4512 InternalAnalysisContext getContextFor(Source source) => this; | 4514 InternalAnalysisContext getContextFor(Source source) => this; |
| 4513 } | 4515 } |
| OLD | NEW |