| 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 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 _typeProvider = new TestTypeProvider(); | 492 _typeProvider = new TestTypeProvider(); |
| 493 _context = new _TestAnalysisContext(); | 493 _context = new _TestAnalysisContext(); |
| 494 _source = new TestSource(); | 494 _source = new TestSource(); |
| 495 } | 495 } |
| 496 | 496 |
| 497 /** | 497 /** |
| 498 * Test an annotation that consists solely of an identifier (and hence | 498 * Test an annotation that consists solely of an identifier (and hence |
| 499 * represents a reference to a compile-time constant variable). | 499 * represents a reference to a compile-time constant variable). |
| 500 */ | 500 */ |
| 501 void test_visitAnnotation_constantVariable() { | 501 void test_visitAnnotation_constantVariable() { |
| 502 _node = AstFactory.annotation(AstFactory.identifier3('x')); | 502 CompilationUnitElement compilationUnitElement = |
| 503 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; |
| 504 ElementFactory.library(_context, 'L').definingCompilationUnit = |
| 505 compilationUnitElement; |
| 506 ElementAnnotationImpl elementAnnotation = |
| 507 new ElementAnnotationImpl(compilationUnitElement); |
| 508 _node = elementAnnotation.annotationAst = AstFactory.annotation( |
| 509 AstFactory.identifier3('x'))..elementAnnotation = elementAnnotation; |
| 503 expect(_findAnnotations(), contains(_node)); | 510 expect(_findAnnotations(), contains(_node)); |
| 504 } | 511 } |
| 505 | 512 |
| 506 /** | 513 /** |
| 507 * Test an annotation that represents the invocation of a constant | 514 * Test an annotation that represents the invocation of a constant |
| 508 * constructor. | 515 * constructor. |
| 509 */ | 516 */ |
| 510 void test_visitAnnotation_invocation() { | 517 void test_visitAnnotation_invocation() { |
| 511 _node = AstFactory.annotation2( | 518 CompilationUnitElement compilationUnitElement = |
| 519 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; |
| 520 ElementFactory.library(_context, 'L').definingCompilationUnit = |
| 521 compilationUnitElement; |
| 522 ElementAnnotationImpl elementAnnotation = |
| 523 new ElementAnnotationImpl(compilationUnitElement); |
| 524 _node = elementAnnotation.annotationAst = AstFactory.annotation2( |
| 525 AstFactory.identifier3('A'), null, AstFactory.argumentList()) |
| 526 ..elementAnnotation = elementAnnotation; |
| 527 expect(_findAnnotations(), contains(_node)); |
| 528 } |
| 529 |
| 530 void test_visitAnnotation_partOf() { |
| 531 // Analyzer ignores annotations on "part of" directives. |
| 532 Annotation annotation = AstFactory.annotation2( |
| 512 AstFactory.identifier3('A'), null, AstFactory.argumentList()); | 533 AstFactory.identifier3('A'), null, AstFactory.argumentList()); |
| 513 expect(_findAnnotations(), contains(_node)); | 534 _node = AstFactory.partOfDirective2( |
| 535 <Annotation>[annotation], AstFactory.libraryIdentifier2(<String>['L'])); |
| 536 expect(_findConstants(), isEmpty); |
| 514 } | 537 } |
| 515 | 538 |
| 516 void test_visitConstructorDeclaration_const() { | 539 void test_visitConstructorDeclaration_const() { |
| 517 ConstructorElement element = _setupConstructorDeclaration("A", true); | 540 ConstructorElement element = _setupConstructorDeclaration("A", true); |
| 518 expect(_findConstants(), contains(element)); | 541 expect(_findConstants(), contains(element)); |
| 519 } | 542 } |
| 520 | 543 |
| 521 void test_visitConstructorDeclaration_nonConst() { | 544 void test_visitConstructorDeclaration_nonConst() { |
| 522 _setupConstructorDeclaration("A", false); | 545 _setupConstructorDeclaration("A", false); |
| 523 expect(_findConstants(), isEmpty); | 546 expect(_findConstants(), isEmpty); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 | 606 |
| 584 void test_visitVariableDeclaration_uninitialized_static_const_inClass() { | 607 void test_visitVariableDeclaration_uninitialized_static_const_inClass() { |
| 585 _setupFieldDeclaration('C', 'f', Keyword.CONST, | 608 _setupFieldDeclaration('C', 'f', Keyword.CONST, |
| 586 isStatic: true, isInitialized: false); | 609 isStatic: true, isInitialized: false); |
| 587 expect(_findConstants(), isEmpty); | 610 expect(_findConstants(), isEmpty); |
| 588 } | 611 } |
| 589 | 612 |
| 590 List<Annotation> _findAnnotations() { | 613 List<Annotation> _findAnnotations() { |
| 591 Set<Annotation> annotations = new Set<Annotation>(); | 614 Set<Annotation> annotations = new Set<Annotation>(); |
| 592 for (ConstantEvaluationTarget target in _findConstants()) { | 615 for (ConstantEvaluationTarget target in _findConstants()) { |
| 593 if (target is ConstantEvaluationTarget_Annotation) { | 616 if (target is ElementAnnotationImpl) { |
| 594 expect(target.context, same(_context)); | 617 expect(target.context, same(_context)); |
| 595 expect(target.source, same(_source)); | 618 expect(target.source, same(_source)); |
| 596 annotations.add(target.annotation); | 619 annotations.add(target.annotationAst); |
| 597 } | 620 } |
| 598 } | 621 } |
| 599 return new List<Annotation>.from(annotations); | 622 return new List<Annotation>.from(annotations); |
| 600 } | 623 } |
| 601 | 624 |
| 602 Set<ConstantEvaluationTarget> _findConstants() { | 625 Set<ConstantEvaluationTarget> _findConstants() { |
| 603 ConstantFinder finder = new ConstantFinder(_context, _source, _source); | 626 ConstantFinder finder = new ConstantFinder(_context, _source, _source); |
| 604 _node.accept(finder); | 627 _node.accept(finder); |
| 605 Set<ConstantEvaluationTarget> constants = finder.constantsToCompute; | 628 Set<ConstantEvaluationTarget> constants = finder.constantsToCompute; |
| 606 expect(constants, isNotNull); | 629 expect(constants, isNotNull); |
| (...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1817 bool _assertValidBool(EvaluationResultImpl result) { | 1840 bool _assertValidBool(EvaluationResultImpl result) { |
| 1818 expect(result.value, isNotNull); | 1841 expect(result.value, isNotNull); |
| 1819 DartObjectImpl value = result.value; | 1842 DartObjectImpl value = result.value; |
| 1820 expect(value.type, typeProvider.boolType); | 1843 expect(value.type, typeProvider.boolType); |
| 1821 bool boolValue = value.toBoolValue(); | 1844 bool boolValue = value.toBoolValue(); |
| 1822 expect(boolValue, isNotNull); | 1845 expect(boolValue, isNotNull); |
| 1823 return boolValue; | 1846 return boolValue; |
| 1824 } | 1847 } |
| 1825 | 1848 |
| 1826 int _assertValidInt(EvaluationResultImpl result) { | 1849 int _assertValidInt(EvaluationResultImpl result) { |
| 1850 expect(result, isNotNull); |
| 1827 expect(result.value, isNotNull); | 1851 expect(result.value, isNotNull); |
| 1828 DartObjectImpl value = result.value; | 1852 DartObjectImpl value = result.value; |
| 1829 expect(value.type, typeProvider.intType); | 1853 expect(value.type, typeProvider.intType); |
| 1830 return value.toIntValue(); | 1854 return value.toIntValue(); |
| 1831 } | 1855 } |
| 1832 | 1856 |
| 1833 void _assertValidNull(EvaluationResultImpl result) { | 1857 void _assertValidNull(EvaluationResultImpl result) { |
| 1834 expect(result.value, isNotNull); | 1858 expect(result.value, isNotNull); |
| 1835 DartObjectImpl value = result.value; | 1859 DartObjectImpl value = result.value; |
| 1836 expect(value.type, typeProvider.nullType); | 1860 expect(value.type, typeProvider.nullType); |
| (...skipping 2669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4506 | 4530 |
| 4507 void _visitNode(AstNode node) { | 4531 void _visitNode(AstNode node) { |
| 4508 node.accept(_createReferenceFinder(_head)); | 4532 node.accept(_createReferenceFinder(_head)); |
| 4509 } | 4533 } |
| 4510 } | 4534 } |
| 4511 | 4535 |
| 4512 class _TestAnalysisContext extends TestAnalysisContext { | 4536 class _TestAnalysisContext extends TestAnalysisContext { |
| 4513 @override | 4537 @override |
| 4514 InternalAnalysisContext getContextFor(Source source) => this; | 4538 InternalAnalysisContext getContextFor(Source source) => this; |
| 4515 } | 4539 } |
| OLD | NEW |