| 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.driver; | 5 library analyzer.test.driver; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| 11 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 11 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
| 12 import 'package:analyzer/dart/element/element.dart'; | 12 import 'package:analyzer/dart/element/element.dart'; |
| 13 import 'package:analyzer/dart/element/type.dart'; | 13 import 'package:analyzer/dart/element/type.dart'; |
| 14 import 'package:analyzer/error/error.dart'; | 14 import 'package:analyzer/error/error.dart'; |
| 15 import 'package:analyzer/file_system/file_system.dart'; | 15 import 'package:analyzer/file_system/file_system.dart'; |
| 16 import 'package:analyzer/file_system/memory_file_system.dart'; | 16 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 17 import 'package:analyzer/src/dart/analysis/byte_store.dart'; | 17 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 18 import 'package:analyzer/src/dart/analysis/driver.dart'; | 18 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 19 import 'package:analyzer/src/dart/analysis/file_state.dart'; | 19 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 20 import 'package:analyzer/src/dart/analysis/status.dart'; | 20 import 'package:analyzer/src/dart/analysis/status.dart'; |
| 21 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; | 21 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| 22 import 'package:analyzer/src/dart/constant/evaluation.dart'; |
| 23 import 'package:analyzer/src/dart/element/element.dart'; |
| 22 import 'package:analyzer/src/error/codes.dart'; | 24 import 'package:analyzer/src/error/codes.dart'; |
| 23 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl; | 25 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl; |
| 24 import 'package:analyzer/src/generated/sdk.dart'; | 26 import 'package:analyzer/src/generated/sdk.dart'; |
| 25 import 'package:analyzer/src/generated/source.dart'; | 27 import 'package:analyzer/src/generated/source.dart'; |
| 26 import 'package:analyzer/src/summary/idl.dart'; | 28 import 'package:analyzer/src/summary/idl.dart'; |
| 27 import 'package:convert/convert.dart'; | 29 import 'package:convert/convert.dart'; |
| 28 import 'package:crypto/crypto.dart'; | 30 import 'package:crypto/crypto.dart'; |
| 29 import 'package:test/test.dart'; | 31 import 'package:test/test.dart'; |
| 30 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 32 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 31 import 'package:typed_mock/typed_mock.dart'; | 33 import 'package:typed_mock/typed_mock.dart'; |
| 32 | 34 |
| 35 import '../../../utils.dart'; |
| 33 import '../../context/mock_sdk.dart'; | 36 import '../../context/mock_sdk.dart'; |
| 34 import 'base.dart'; | 37 import 'base.dart'; |
| 35 | 38 |
| 36 main() { | 39 main() { |
| 37 defineReflectiveSuite(() { | 40 defineReflectiveSuite(() { |
| 38 defineReflectiveTests(AnalysisDriverTest); | 41 defineReflectiveTests(AnalysisDriverTest); |
| 39 defineReflectiveTests(AnalysisDriverSchedulerTest); | 42 defineReflectiveTests(AnalysisDriverSchedulerTest); |
| 40 }); | 43 }); |
| 41 } | 44 } |
| 42 | 45 |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 // We get a new result. | 576 // We get a new result. |
| 574 { | 577 { |
| 575 await scheduler.waitForIdle(); | 578 await scheduler.waitForIdle(); |
| 576 expect(allResults, hasLength(1)); | 579 expect(allResults, hasLength(1)); |
| 577 AnalysisResult result = allResults[0]; | 580 AnalysisResult result = allResults[0]; |
| 578 expect(result.path, testFile); | 581 expect(result.path, testFile); |
| 579 expect(_getTopLevelVarType(result.unit, 'V'), 'double'); | 582 expect(_getTopLevelVarType(result.unit, 'V'), 'double'); |
| 580 } | 583 } |
| 581 } | 584 } |
| 582 | 585 |
| 586 test_const_annotation_notConstConstructor() async { |
| 587 addTestFile(''' |
| 588 class A { |
| 589 final int i; |
| 590 A(this.i); |
| 591 } |
| 592 |
| 593 @A(5) |
| 594 class C {} |
| 595 '''); |
| 596 var result = await driver.getResult(testFile); |
| 597 var atD = AstFinder.getClass(result.unit, 'C').metadata[0]; |
| 598 var atDI = atD.elementAnnotation as ElementAnnotationImpl; |
| 599 var value = atDI.evaluationResult.value; |
| 600 // That is illegal. |
| 601 expect(value, isNull); |
| 602 } |
| 603 |
| 604 test_const_annotation_withArgs() async { |
| 605 addTestFile(''' |
| 606 const x = 1; |
| 607 @D(x) class C {} |
| 608 class D { |
| 609 const D(this.value); |
| 610 final value; |
| 611 } |
| 612 '''); |
| 613 var result = await driver.getResult(testFile); |
| 614 var atD = AstFinder.getClass(result.unit, 'C').metadata[0]; |
| 615 var atDI = atD.elementAnnotation as ElementAnnotationImpl; |
| 616 var value = atDI.evaluationResult.value; |
| 617 expect(value, isNotNull); |
| 618 expect(value.type, isNotNull); |
| 619 expect(value.type.name, 'D'); |
| 620 expect(value.fields.keys, ['value']); |
| 621 expect(value.getField('value').toIntValue(), 1); |
| 622 expect(atDI.evaluationResult.errors, isEmpty); |
| 623 } |
| 624 |
| 625 test_const_annotation_withoutArgs() async { |
| 626 addTestFile(''' |
| 627 const x = 1; |
| 628 @x class C {} |
| 629 '''); |
| 630 var result = await driver.getResult(testFile); |
| 631 Annotation at_x = AstFinder.getClass(result.unit, 'C').metadata[0]; |
| 632 expect(at_x.elementAnnotation.constantValue.toIntValue(), 1); |
| 633 } |
| 634 |
| 635 test_const_circular_reference() async { |
| 636 addTestFile(''' |
| 637 const x = y + 1; |
| 638 const y = x + 1; |
| 639 '''); |
| 640 var result = await driver.getResult(testFile); |
| 641 var x = AstFinder.getTopLevelVariableElement(result.unit, 'x') |
| 642 as TopLevelVariableElementImpl; |
| 643 _expectCircularityError(x.evaluationResult); |
| 644 } |
| 645 |
| 646 test_const_dependency_sameUnit() async { |
| 647 addTestFile(''' |
| 648 const x = y + 1; |
| 649 const y = 1; |
| 650 '''); |
| 651 var result = await driver.getResult(testFile); |
| 652 var x = AstFinder.getTopLevelVariableElement(result.unit, 'x'); |
| 653 var y = AstFinder.getTopLevelVariableElement(result.unit, 'y'); |
| 654 expect(x.constantValue.toIntValue(), 2); |
| 655 expect(y.constantValue.toIntValue(), 1); |
| 656 } |
| 657 |
| 658 test_const_externalConstFactory() async { |
| 659 addTestFile(''' |
| 660 const x = const C.foo(); |
| 661 |
| 662 class C extends B { |
| 663 external const factory C.foo(); |
| 664 } |
| 665 |
| 666 class B {} |
| 667 '''); |
| 668 var result = await driver.getResult(testFile); |
| 669 var x = AstFinder.getTopLevelVariableElement(result.unit, 'x'); |
| 670 expect(x.constantValue, isNotNull); |
| 671 } |
| 672 |
| 673 test_const_implicitSuperConstructorInvocation() async { |
| 674 addTestFile(''' |
| 675 class Base {} |
| 676 class Derived extends Base { |
| 677 const Derived(); |
| 678 } |
| 679 const x = const Derived(); |
| 680 '''); |
| 681 var result = await driver.getResult(testFile); |
| 682 var x = AstFinder.getTopLevelVariableElement(result.unit, 'x'); |
| 683 expect(x.constantValue, isNotNull); |
| 684 } |
| 685 |
| 686 test_const_simple_topLevelVariable() async { |
| 687 addTestFile(''' |
| 688 const x = 1; |
| 689 '''); |
| 690 var result = await driver.getResult(testFile); |
| 691 var x = AstFinder.getTopLevelVariableElement(result.unit, 'x'); |
| 692 expect(x.constantValue.toIntValue(), 1); |
| 693 } |
| 694 |
| 583 test_errors_uriDoesNotExist_export() async { | 695 test_errors_uriDoesNotExist_export() async { |
| 584 addTestFile(r''' | 696 addTestFile(r''' |
| 585 export 'foo.dart'; | 697 export 'foo.dart'; |
| 586 '''); | 698 '''); |
| 587 | 699 |
| 588 AnalysisResult result = await driver.getResult(testFile); | 700 AnalysisResult result = await driver.getResult(testFile); |
| 589 List<AnalysisError> errors = result.errors; | 701 List<AnalysisError> errors = result.errors; |
| 590 expect(errors, hasLength(1)); | 702 expect(errors, hasLength(1)); |
| 591 expect(errors[0].errorCode, CompileTimeErrorCode.URI_DOES_NOT_EXIST); | 703 expect(errors[0].errorCode, CompileTimeErrorCode.URI_DOES_NOT_EXIST); |
| 592 } | 704 } |
| (...skipping 1370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1963 expect(expectedFiles, hasLength(expectedIsExported.length)); | 2075 expect(expectedFiles, hasLength(expectedIsExported.length)); |
| 1964 for (int i = 0; i < expectedFiles.length; i++) { | 2076 for (int i = 0; i < expectedFiles.length; i++) { |
| 1965 expect(declarations, | 2077 expect(declarations, |
| 1966 contains(predicate((TopLevelDeclarationInSource declaration) { | 2078 contains(predicate((TopLevelDeclarationInSource declaration) { |
| 1967 return declaration.source.fullName == expectedFiles[i] && | 2079 return declaration.source.fullName == expectedFiles[i] && |
| 1968 declaration.isExported == expectedIsExported[i]; | 2080 declaration.isExported == expectedIsExported[i]; |
| 1969 }))); | 2081 }))); |
| 1970 } | 2082 } |
| 1971 } | 2083 } |
| 1972 | 2084 |
| 2085 void _expectCircularityError(EvaluationResultImpl evaluationResult) { |
| 2086 expect(evaluationResult, isNotNull); |
| 2087 expect(evaluationResult.value, isNull); |
| 2088 expect(evaluationResult.errors, hasLength(1)); |
| 2089 expect(evaluationResult.errors[0].errorCode, |
| 2090 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT); |
| 2091 } |
| 2092 |
| 1973 ClassDeclaration _getClass(CompilationUnit unit, String name) { | 2093 ClassDeclaration _getClass(CompilationUnit unit, String name) { |
| 1974 for (CompilationUnitMember declaration in unit.declarations) { | 2094 for (CompilationUnitMember declaration in unit.declarations) { |
| 1975 if (declaration is ClassDeclaration) { | 2095 if (declaration is ClassDeclaration) { |
| 1976 if (declaration.name.name == name) { | 2096 if (declaration.name.name == name) { |
| 1977 return declaration; | 2097 return declaration; |
| 1978 } | 2098 } |
| 1979 } | 2099 } |
| 1980 } | 2100 } |
| 1981 fail('Cannot find the class $name in\n$unit'); | 2101 fail('Cannot find the class $name in\n$unit'); |
| 1982 return null; | 2102 return null; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2066 * Return the [provider] specific path for the given Posix [path]. | 2186 * Return the [provider] specific path for the given Posix [path]. |
| 2067 */ | 2187 */ |
| 2068 String _p(String path) => provider.convertPath(path); | 2188 String _p(String path) => provider.convertPath(path); |
| 2069 | 2189 |
| 2070 static String _md5(String content) { | 2190 static String _md5(String content) { |
| 2071 return hex.encode(md5.convert(UTF8.encode(content)).bytes); | 2191 return hex.encode(md5.convert(UTF8.encode(content)).bytes); |
| 2072 } | 2192 } |
| 2073 } | 2193 } |
| 2074 | 2194 |
| 2075 class _SourceMock extends TypedMock implements Source {} | 2195 class _SourceMock extends TypedMock implements Source {} |
| OLD | NEW |