Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: pkg/analyzer/test/src/summary/resynthesize_ast_test.dart

Issue 2235373003: Fix summary handling of unresolved imports, exports, and parts. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.src.summary.resynthesize_ast_test; 5 library analyzer.test.src.summary.resynthesize_ast_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/src/dart/element/element.dart'; 9 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/generated/engine.dart' 10 import 'package:analyzer/src/generated/engine.dart'
11 show AnalysisContext, AnalysisOptionsImpl; 11 show AnalysisContext, AnalysisOptionsImpl;
12 import 'package:analyzer/src/generated/sdk.dart'; 12 import 'package:analyzer/src/generated/sdk.dart';
13 import 'package:analyzer/src/generated/source.dart'; 13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:analyzer/src/summary/format.dart'; 14 import 'package:analyzer/src/summary/format.dart';
15 import 'package:analyzer/src/summary/idl.dart'; 15 import 'package:analyzer/src/summary/idl.dart';
16 import 'package:analyzer/src/summary/link.dart'; 16 import 'package:analyzer/src/summary/link.dart';
17 import 'package:analyzer/src/summary/prelink.dart'; 17 import 'package:analyzer/src/summary/prelink.dart';
18 import 'package:analyzer/src/summary/resynthesize.dart'; 18 import 'package:analyzer/src/summary/resynthesize.dart';
19 import 'package:analyzer/src/summary/summarize_ast.dart'; 19 import 'package:analyzer/src/summary/summarize_ast.dart';
20 import 'package:analyzer/src/summary/summarize_elements.dart' 20 import 'package:analyzer/src/summary/summarize_elements.dart'
21 show PackageBundleAssembler; 21 show PackageBundleAssembler;
22 import 'package:analyzer/task/dart.dart' show PARSED_UNIT; 22 import 'package:analyzer/task/dart.dart' show PARSED_UNIT;
23 import 'package:analyzer/task/general.dart';
23 import 'package:unittest/unittest.dart'; 24 import 'package:unittest/unittest.dart';
24 25
25 import '../../reflective_tests.dart'; 26 import '../../reflective_tests.dart';
26 import '../context/abstract_context.dart'; 27 import '../context/abstract_context.dart';
27 import '../task/strong/inferred_type_test.dart'; 28 import '../task/strong/inferred_type_test.dart';
28 import 'resynthesize_test.dart'; 29 import 'resynthesize_test.dart';
29 import 'summary_common.dart'; 30 import 'summary_common.dart';
30 31
31 main() { 32 main() {
32 groupSep = ' | '; 33 groupSep = ' | ';
33 runReflectiveTests(ResynthesizeAstTest); 34 runReflectiveTests(ResynthesizeAstTest);
34 runReflectiveTests(AstInferredTypeTest); 35 runReflectiveTests(AstInferredTypeTest);
35 } 36 }
36 37
37 @reflectiveTest 38 @reflectiveTest
38 class AstInferredTypeTest extends AbstractResynthesizeTest 39 class AstInferredTypeTest extends AbstractResynthesizeTest
39 with _AstResynthesizeTestMixin, InferredTypeMixin { 40 with _AstResynthesizeTestMixin, InferredTypeMixin {
41 @override
42 bool allowMissingFiles = false;
43
40 bool get checkPropagatedTypes { 44 bool get checkPropagatedTypes {
41 // AST-based summaries do not yet handle propagated types. 45 // AST-based summaries do not yet handle propagated types.
42 // TODO(paulberry): fix this. 46 // TODO(paulberry): fix this.
43 return false; 47 return false;
44 } 48 }
45 49
46 @override 50 @override
47 bool get mayCheckTypesOfLocals => false; 51 bool get mayCheckTypesOfLocals => false;
48 52
49 @override 53 @override
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 LibraryElementImpl original = context.computeLibraryElement(source); 687 LibraryElementImpl original = context.computeLibraryElement(source);
684 checkLibraryElements(original, resynthesized); 688 checkLibraryElements(original, resynthesized);
685 return resynthesized; 689 return resynthesized;
686 } 690 }
687 } 691 }
688 692
689 @reflectiveTest 693 @reflectiveTest
690 class ResynthesizeAstTest extends ResynthesizeTest 694 class ResynthesizeAstTest extends ResynthesizeTest
691 with _AstResynthesizeTestMixin { 695 with _AstResynthesizeTestMixin {
692 @override 696 @override
697 bool allowMissingFiles = false;
698
699 @override
693 bool get checkPropagatedTypes => false; 700 bool get checkPropagatedTypes => false;
694 701
695 @override 702 @override
696 LibraryElementImpl checkLibrary(String text, 703 LibraryElementImpl checkLibrary(String text,
697 {bool allowErrors: false, bool dumpSummaries: false}) { 704 {bool allowErrors: false, bool dumpSummaries: false}) {
698 Source source = addTestSource(text); 705 Source source = addTestSource(text);
699 LibraryElementImpl resynthesized = _encodeDecodeLibraryElement(source); 706 LibraryElementImpl resynthesized = _encodeDecodeLibraryElement(source);
700 LibraryElementImpl original = context.computeLibraryElement(source); 707 LibraryElementImpl original = context.computeLibraryElement(source);
701 checkLibraryElements(original, resynthesized); 708 checkLibraryElements(original, resynthesized);
702 return resynthesized; 709 return resynthesized;
(...skipping 10 matching lines...) Expand all
713 720
714 /** 721 /**
715 * Abstract mixin for serializing ASTs and resynthesizing elements from it. 722 * Abstract mixin for serializing ASTs and resynthesizing elements from it.
716 */ 723 */
717 abstract class _AstResynthesizeTestMixin { 724 abstract class _AstResynthesizeTestMixin {
718 final Set<Source> serializedSources = new Set<Source>(); 725 final Set<Source> serializedSources = new Set<Source>();
719 final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler(); 726 final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler();
720 final Map<String, UnlinkedUnitBuilder> uriToUnit = 727 final Map<String, UnlinkedUnitBuilder> uriToUnit =
721 <String, UnlinkedUnitBuilder>{}; 728 <String, UnlinkedUnitBuilder>{};
722 729
730 bool get allowMissingFiles;
731
723 AnalysisContext get context; 732 AnalysisContext get context;
724 733
725 LibraryElementImpl _encodeDecodeLibraryElement(Source source) { 734 LibraryElementImpl _encodeDecodeLibraryElement(Source source) {
726 SummaryResynthesizer resynthesizer = _encodeLibrary(source); 735 SummaryResynthesizer resynthesizer = _encodeLibrary(source);
727 return resynthesizer.getLibraryElement(source.uri.toString()); 736 return resynthesizer.getLibraryElement(source.uri.toString());
728 } 737 }
729 738
730 TestSummaryResynthesizer _encodeLibrary(Source source) { 739 TestSummaryResynthesizer _encodeLibrary(Source source) {
731 _serializeLibrary(source); 740 _serializeLibrary(source);
732 741
733 PackageBundle bundle = 742 PackageBundle bundle =
734 new PackageBundle.fromBuffer(bundleAssembler.assemble().toBuffer()); 743 new PackageBundle.fromBuffer(bundleAssembler.assemble().toBuffer());
735 744
736 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{}; 745 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{};
737 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) { 746 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) {
738 String uri = bundle.unlinkedUnitUris[i]; 747 String uri = bundle.unlinkedUnitUris[i];
739 unlinkedSummaries[uri] = bundle.unlinkedUnits[i]; 748 unlinkedSummaries[uri] = bundle.unlinkedUnits[i];
740 } 749 }
741 750
742 LinkedLibrary getDependency(String absoluteUri) { 751 LinkedLibrary getDependency(String absoluteUri) {
743 Map<String, LinkedLibrary> sdkLibraries = 752 Map<String, LinkedLibrary> sdkLibraries =
744 SerializedMockSdk.instance.uriToLinkedLibrary; 753 SerializedMockSdk.instance.uriToLinkedLibrary;
745 LinkedLibrary linkedLibrary = sdkLibraries[absoluteUri]; 754 LinkedLibrary linkedLibrary = sdkLibraries[absoluteUri];
746 if (linkedLibrary == null) { 755 if (linkedLibrary == null && !allowMissingFiles) {
747 fail('Linker unexpectedly requested LinkedLibrary for "$absoluteUri".' 756 fail('Linker unexpectedly requested LinkedLibrary for "$absoluteUri".'
748 ' Libraries available: ${sdkLibraries.keys}'); 757 ' Libraries available: ${sdkLibraries.keys}');
749 } 758 }
750 return linkedLibrary; 759 return linkedLibrary;
751 } 760 }
752 761
753 UnlinkedUnit getUnit(String absoluteUri) { 762 UnlinkedUnit getUnit(String absoluteUri) {
754 UnlinkedUnit unit = uriToUnit[absoluteUri] ?? 763 UnlinkedUnit unit = uriToUnit[absoluteUri] ??
755 SerializedMockSdk.instance.uriToUnlinkedUnit[absoluteUri]; 764 SerializedMockSdk.instance.uriToUnlinkedUnit[absoluteUri];
756 if (unit == null) { 765 if (unit == null && !allowMissingFiles) {
757 fail('Linker unexpectedly requested unit for "$absoluteUri".'); 766 fail('Linker unexpectedly requested unit for "$absoluteUri".');
758 } 767 }
759 return unit; 768 return unit;
760 } 769 }
761 770
762 Set<String> nonSdkLibraryUris = context.sources 771 Set<String> nonSdkLibraryUris = context.sources
763 .where((Source source) => 772 .where((Source source) =>
764 !source.isInSystemLibrary && 773 !source.isInSystemLibrary &&
765 context.computeKindOf(source) == SourceKind.LIBRARY) 774 context.computeKindOf(source) == SourceKind.LIBRARY)
766 .map((Source source) => source.uri.toString()) 775 .map((Source source) => source.uri.toString())
767 .toSet(); 776 .toSet();
768 777
769 Map<String, LinkedLibrary> linkedSummaries = link(nonSdkLibraryUris, 778 Map<String, LinkedLibrary> linkedSummaries = link(nonSdkLibraryUris,
770 getDependency, getUnit, context.analysisOptions.strongMode); 779 getDependency, getUnit, context.analysisOptions.strongMode);
771 780
772 return new TestSummaryResynthesizer( 781 return new TestSummaryResynthesizer(
773 null, 782 null,
774 context, 783 context,
775 new Map<String, UnlinkedUnit>() 784 new Map<String, UnlinkedUnit>()
776 ..addAll(SerializedMockSdk.instance.uriToUnlinkedUnit) 785 ..addAll(SerializedMockSdk.instance.uriToUnlinkedUnit)
777 ..addAll(unlinkedSummaries), 786 ..addAll(unlinkedSummaries),
778 new Map<String, LinkedLibrary>() 787 new Map<String, LinkedLibrary>()
779 ..addAll(SerializedMockSdk.instance.uriToLinkedLibrary) 788 ..addAll(SerializedMockSdk.instance.uriToLinkedLibrary)
780 ..addAll(linkedSummaries)); 789 ..addAll(linkedSummaries),
790 allowMissingFiles);
781 } 791 }
782 792
783 UnlinkedUnit _getUnlinkedUnit(Source source) { 793 UnlinkedUnit _getUnlinkedUnit(Source source) {
784 String uriStr = source.uri.toString(); 794 String uriStr = source.uri.toString();
785 { 795 {
786 UnlinkedUnit unlinkedUnitInSdk = 796 UnlinkedUnit unlinkedUnitInSdk =
787 SerializedMockSdk.instance.uriToUnlinkedUnit[uriStr]; 797 SerializedMockSdk.instance.uriToUnlinkedUnit[uriStr];
788 if (unlinkedUnitInSdk != null) { 798 if (unlinkedUnitInSdk != null) {
789 return unlinkedUnitInSdk; 799 return unlinkedUnitInSdk;
790 } 800 }
791 } 801 }
792 return uriToUnit.putIfAbsent(uriStr, () { 802 return uriToUnit.putIfAbsent(uriStr, () {
803 int modificationTime = context.computeResult(source, MODIFICATION_TIME);
804 if (modificationTime < 0) {
805 // Source does not exist.
806 if (!allowMissingFiles) {
807 fail('Unexpectedly tried to get unlinked summary for $source');
808 }
809 return null;
810 }
793 CompilationUnit unit = context.computeResult(source, PARSED_UNIT); 811 CompilationUnit unit = context.computeResult(source, PARSED_UNIT);
794 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit); 812 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit);
795 bundleAssembler.addUnlinkedUnit(source, unlinkedUnit); 813 bundleAssembler.addUnlinkedUnit(source, unlinkedUnit);
796 return unlinkedUnit; 814 return unlinkedUnit;
797 }); 815 });
798 } 816 }
799 817
800 void _serializeLibrary(Source librarySource) { 818 void _serializeLibrary(Source librarySource) {
801 if (librarySource.isInSystemLibrary) { 819 if (librarySource.isInSystemLibrary) {
802 return; 820 return;
(...skipping 10 matching lines...) Expand all
813 '$librarySource (${librarySource.runtimeType})'); 831 '$librarySource (${librarySource.runtimeType})');
814 } 832 }
815 return resolvedSource; 833 return resolvedSource;
816 } 834 }
817 835
818 UnlinkedUnit getPart(String relativeUri) { 836 UnlinkedUnit getPart(String relativeUri) {
819 return _getUnlinkedUnit(resolveRelativeUri(relativeUri)); 837 return _getUnlinkedUnit(resolveRelativeUri(relativeUri));
820 } 838 }
821 839
822 UnlinkedPublicNamespace getImport(String relativeUri) { 840 UnlinkedPublicNamespace getImport(String relativeUri) {
823 return getPart(relativeUri).publicNamespace; 841 return getPart(relativeUri)?.publicNamespace;
824 } 842 }
825 843
826 UnlinkedUnit definingUnit = _getUnlinkedUnit(librarySource); 844 UnlinkedUnit definingUnit = _getUnlinkedUnit(librarySource);
827 LinkedLibraryBuilder linkedLibrary = 845 if (definingUnit != null) {
828 prelink(definingUnit, getPart, getImport); 846 LinkedLibraryBuilder linkedLibrary =
829 linkedLibrary.dependencies.skip(1).forEach((LinkedDependency d) { 847 prelink(definingUnit, getPart, getImport);
830 _serializeLibrary(resolveRelativeUri(d.uri)); 848 linkedLibrary.dependencies.skip(1).forEach((LinkedDependency d) {
831 }); 849 _serializeLibrary(resolveRelativeUri(d.uri));
850 });
851 }
832 } 852 }
833 } 853 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698