| 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.generated.resolver_test_case; | 5 library analyzer.test.generated.resolver_test_case; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 */ | 513 */ |
| 514 LibraryElementImpl createDefaultTestLibrary() => | 514 LibraryElementImpl createDefaultTestLibrary() => |
| 515 createTestLibrary(AnalysisContextFactory.contextWithCore(), "test"); | 515 createTestLibrary(AnalysisContextFactory.contextWithCore(), "test"); |
| 516 | 516 |
| 517 /** | 517 /** |
| 518 * Create a source object representing a file with the given [fileName] and | 518 * Create a source object representing a file with the given [fileName] and |
| 519 * give it an empty content. Return the source that was created. | 519 * give it an empty content. Return the source that was created. |
| 520 */ | 520 */ |
| 521 FileBasedSource createNamedSource(String fileName) { | 521 FileBasedSource createNamedSource(String fileName) { |
| 522 FileBasedSource source = | 522 FileBasedSource source = |
| 523 new FileBasedSource(FileUtilities2.createFile(fileName)); | 523 new FileBasedSource(FileUtilities2.createFile(fileName)); |
| 524 analysisContext2.setContents(source, ""); | 524 analysisContext2.setContents(source, ""); |
| 525 return source; | 525 return source; |
| 526 } | 526 } |
| 527 | 527 |
| 528 /** | 528 /** |
| 529 * Create a library element that represents a library with the given name cont
aining a single | 529 * Create a library element that represents a library with the given name cont
aining a single |
| 530 * empty compilation unit. | 530 * empty compilation unit. |
| 531 * | 531 * |
| 532 * @param libraryName the name of the library to be created | 532 * @param libraryName the name of the library to be created |
| 533 * @return the library element that was created | 533 * @return the library element that was created |
| 534 */ | 534 */ |
| 535 LibraryElementImpl createTestLibrary( | 535 LibraryElementImpl createTestLibrary( |
| 536 AnalysisContext context, String libraryName, | 536 AnalysisContext context, String libraryName, |
| 537 [List<String> typeNames]) { | 537 [List<String> typeNames]) { |
| 538 String fileName = "$libraryName.dart"; | 538 String fileName = "$libraryName.dart"; |
| 539 FileBasedSource definingCompilationUnitSource = | 539 FileBasedSource definingCompilationUnitSource = createNamedSource(fileName); |
| 540 createNamedSource(fileName); | |
| 541 List<CompilationUnitElement> sourcedCompilationUnits; | 540 List<CompilationUnitElement> sourcedCompilationUnits; |
| 542 if (typeNames == null) { | 541 if (typeNames == null) { |
| 543 sourcedCompilationUnits = CompilationUnitElement.EMPTY_LIST; | 542 sourcedCompilationUnits = CompilationUnitElement.EMPTY_LIST; |
| 544 } else { | 543 } else { |
| 545 int count = typeNames.length; | 544 int count = typeNames.length; |
| 546 sourcedCompilationUnits = new List<CompilationUnitElement>(count); | 545 sourcedCompilationUnits = new List<CompilationUnitElement>(count); |
| 547 for (int i = 0; i < count; i++) { | 546 for (int i = 0; i < count; i++) { |
| 548 String typeName = typeNames[i]; | 547 String typeName = typeNames[i]; |
| 549 ClassElementImpl type = | 548 ClassElementImpl type = |
| 550 new ClassElementImpl.forNode(AstFactory.identifier3(typeName)); | 549 new ClassElementImpl.forNode(AstFactory.identifier3(typeName)); |
| 551 String fileName = "$typeName.dart"; | 550 String fileName = "$typeName.dart"; |
| 552 CompilationUnitElementImpl compilationUnit = | 551 CompilationUnitElementImpl compilationUnit = |
| 553 new CompilationUnitElementImpl(fileName); | 552 new CompilationUnitElementImpl(fileName); |
| 554 compilationUnit.source = createNamedSource(fileName); | 553 compilationUnit.source = createNamedSource(fileName); |
| 555 compilationUnit.librarySource = definingCompilationUnitSource; | 554 compilationUnit.librarySource = definingCompilationUnitSource; |
| 556 compilationUnit.types = <ClassElement>[type]; | 555 compilationUnit.types = <ClassElement>[type]; |
| 557 sourcedCompilationUnits[i] = compilationUnit; | 556 sourcedCompilationUnits[i] = compilationUnit; |
| 558 } | 557 } |
| 559 } | 558 } |
| 560 CompilationUnitElementImpl compilationUnit = | 559 CompilationUnitElementImpl compilationUnit = |
| 561 new CompilationUnitElementImpl(fileName); | 560 new CompilationUnitElementImpl(fileName); |
| 562 compilationUnit.librarySource = | 561 compilationUnit.librarySource = |
| 563 compilationUnit.source = definingCompilationUnitSource; | 562 compilationUnit.source = definingCompilationUnitSource; |
| 564 LibraryElementImpl library = new LibraryElementImpl.forNode( | 563 LibraryElementImpl library = new LibraryElementImpl.forNode( |
| 565 context, AstFactory.libraryIdentifier2([libraryName])); | 564 context, AstFactory.libraryIdentifier2([libraryName])); |
| 566 library.definingCompilationUnit = compilationUnit; | 565 library.definingCompilationUnit = compilationUnit; |
| 567 library.parts = sourcedCompilationUnits; | 566 library.parts = sourcedCompilationUnits; |
| 568 return library; | 567 return library; |
| 569 } | 568 } |
| 570 | 569 |
| 571 /** | 570 /** |
| (...skipping 20 matching lines...) Expand all Loading... |
| 592 // Is there a better exception to throw here? The point is that an | 591 // Is there a better exception to throw here? The point is that an |
| 593 // assertion failure here should be a failure, in both "test_*" and | 592 // assertion failure here should be a failure, in both "test_*" and |
| 594 // "fail_*" tests. However, an assertion failure is success for the | 593 // "fail_*" tests. However, an assertion failure is success for the |
| 595 // purpose of "fail_*" tests, so without catching them here "fail_*" tests | 594 // purpose of "fail_*" tests, so without catching them here "fail_*" tests |
| 596 // can succeed by failing for the wrong reason. | 595 // can succeed by failing for the wrong reason. |
| 597 throw new JavaException("Unexexpected assertion failure: $exception"); | 596 throw new JavaException("Unexexpected assertion failure: $exception"); |
| 598 } | 597 } |
| 599 } | 598 } |
| 600 | 599 |
| 601 Expression findTopLevelConstantExpression( | 600 Expression findTopLevelConstantExpression( |
| 602 CompilationUnit compilationUnit, String name) => | 601 CompilationUnit compilationUnit, String name) => |
| 603 findTopLevelDeclaration(compilationUnit, name).initializer; | 602 findTopLevelDeclaration(compilationUnit, name).initializer; |
| 604 | 603 |
| 605 VariableDeclaration findTopLevelDeclaration( | 604 VariableDeclaration findTopLevelDeclaration( |
| 606 CompilationUnit compilationUnit, String name) { | 605 CompilationUnit compilationUnit, String name) { |
| 607 for (CompilationUnitMember member in compilationUnit.declarations) { | 606 for (CompilationUnitMember member in compilationUnit.declarations) { |
| 608 if (member is TopLevelVariableDeclaration) { | 607 if (member is TopLevelVariableDeclaration) { |
| 609 for (VariableDeclaration variable in member.variables.variables) { | 608 for (VariableDeclaration variable in member.variables.variables) { |
| 610 if (variable.name.name == name) { | 609 if (variable.name.name == name) { |
| 611 return variable; | 610 return variable; |
| 612 } | 611 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 | 648 |
| 650 /** | 649 /** |
| 651 * Return the resolved compilation unit corresponding to the given source in t
he given library. | 650 * Return the resolved compilation unit corresponding to the given source in t
he given library. |
| 652 * | 651 * |
| 653 * @param source the source of the compilation unit to be returned | 652 * @param source the source of the compilation unit to be returned |
| 654 * @param library the library in which the compilation unit is to be resolved | 653 * @param library the library in which the compilation unit is to be resolved |
| 655 * @return the resolved compilation unit | 654 * @return the resolved compilation unit |
| 656 * @throws Exception if the compilation unit could not be resolved | 655 * @throws Exception if the compilation unit could not be resolved |
| 657 */ | 656 */ |
| 658 CompilationUnit resolveCompilationUnit( | 657 CompilationUnit resolveCompilationUnit( |
| 659 Source source, LibraryElement library) => | 658 Source source, LibraryElement library) => |
| 660 analysisContext2.resolveCompilationUnit(source, library); | 659 analysisContext2.resolveCompilationUnit(source, library); |
| 661 | 660 |
| 662 CompilationUnit resolveSource(String sourceText) => | 661 CompilationUnit resolveSource(String sourceText) => |
| 663 resolveSource2("/test.dart", sourceText); | 662 resolveSource2("/test.dart", sourceText); |
| 664 | 663 |
| 665 CompilationUnit resolveSource2(String fileName, String sourceText) { | 664 CompilationUnit resolveSource2(String fileName, String sourceText) { |
| 666 Source source = addNamedSource(fileName, sourceText); | 665 Source source = addNamedSource(fileName, sourceText); |
| 667 LibraryElement library = analysisContext.computeLibraryElement(source); | 666 LibraryElement library = analysisContext.computeLibraryElement(source); |
| 668 return analysisContext.resolveCompilationUnit(source, library); | 667 return analysisContext.resolveCompilationUnit(source, library); |
| 669 } | 668 } |
| 670 | 669 |
| 671 Source resolveSources(List<String> sourceTexts) { | 670 Source resolveSources(List<String> sourceTexts) { |
| 672 for (int i = 0; i < sourceTexts.length; i++) { | 671 for (int i = 0; i < sourceTexts.length; i++) { |
| 673 CompilationUnit unit = | 672 CompilationUnit unit = |
| 674 resolveSource2("/lib${i + 1}.dart", sourceTexts[i]); | 673 resolveSource2("/lib${i + 1}.dart", sourceTexts[i]); |
| 675 // reference the source if this is the last source | 674 // reference the source if this is the last source |
| 676 if (i + 1 == sourceTexts.length) { | 675 if (i + 1 == sourceTexts.length) { |
| 677 return unit.element.source; | 676 return unit.element.source; |
| 678 } | 677 } |
| 679 } | 678 } |
| 680 return null; | 679 return null; |
| 681 } | 680 } |
| 682 | 681 |
| 683 void resolveWithAndWithoutExperimental( | 682 void resolveWithAndWithoutExperimental( |
| 684 List<String> strSources, | 683 List<String> strSources, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 * If [type] is a string, validates that the identifier's static type | 792 * If [type] is a string, validates that the identifier's static type |
| 794 * stringifies to that text. Otherwise, [type] is used directly a [Matcher] | 793 * stringifies to that text. Otherwise, [type] is used directly a [Matcher] |
| 795 * to match the type. | 794 * to match the type. |
| 796 * | 795 * |
| 797 * If [propagatedType] is given, also validate's the identifier's propagated | 796 * If [propagatedType] is given, also validate's the identifier's propagated |
| 798 * type. | 797 * type. |
| 799 */ | 798 */ |
| 800 void expectInitializerType(String name, type, [propagatedType]) { | 799 void expectInitializerType(String name, type, [propagatedType]) { |
| 801 SimpleIdentifier identifier = findIdentifier(name); | 800 SimpleIdentifier identifier = findIdentifier(name); |
| 802 VariableDeclaration declaration = | 801 VariableDeclaration declaration = |
| 803 identifier.getAncestor((node) => node is VariableDeclaration); | 802 identifier.getAncestor((node) => node is VariableDeclaration); |
| 804 Expression initializer = declaration.initializer; | 803 Expression initializer = declaration.initializer; |
| 805 _expectType(initializer.staticType, type); | 804 _expectType(initializer.staticType, type); |
| 806 if (propagatedType != null) { | 805 if (propagatedType != null) { |
| 807 _expectType(initializer.propagatedType, propagatedType); | 806 _expectType(initializer.propagatedType, propagatedType); |
| 808 } | 807 } |
| 809 } | 808 } |
| 810 | 809 |
| 811 SimpleIdentifier findIdentifier(String search) { | 810 SimpleIdentifier findIdentifier(String search) { |
| 812 SimpleIdentifier identifier = EngineTestCase.findNode( | 811 SimpleIdentifier identifier = EngineTestCase.findNode( |
| 813 testUnit, testCode, search, (node) => node is SimpleIdentifier); | 812 testUnit, testCode, search, (node) => node is SimpleIdentifier); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 830 * text. Otherwise, [expected] is used directly a [Matcher] to match the type. | 829 * text. Otherwise, [expected] is used directly a [Matcher] to match the type. |
| 831 */ | 830 */ |
| 832 _expectType(DartType type, expected) { | 831 _expectType(DartType type, expected) { |
| 833 if (expected is String) { | 832 if (expected is String) { |
| 834 expect(type.toString(), expected); | 833 expect(type.toString(), expected); |
| 835 } else { | 834 } else { |
| 836 expect(type, expected); | 835 expect(type, expected); |
| 837 } | 836 } |
| 838 } | 837 } |
| 839 } | 838 } |
| OLD | NEW |