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

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

Issue 1653163003: Use LinkedDependency.parts to find part URIs during resynthesis. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 test.src.serialization.elements_test; 5 library test.src.serialization.elements_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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 void addLibrarySource(String filePath, String contents) { 45 void addLibrarySource(String filePath, String contents) {
46 otherLibrarySources.add(addNamedSource(filePath, contents)); 46 otherLibrarySources.add(addNamedSource(filePath, contents));
47 } 47 }
48 48
49 void checkLibrary(String text, {bool allowErrors: false}) { 49 void checkLibrary(String text, {bool allowErrors: false}) {
50 Source source = addSource(text); 50 Source source = addSource(text);
51 LibraryElementImpl original = resolve2(source); 51 LibraryElementImpl original = resolve2(source);
52 LibraryElementImpl resynthesized = resynthesizeLibraryElement( 52 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
53 encodeLibrary(original, allowErrors: allowErrors), 53 encodeLibrary(original, allowErrors: allowErrors),
54 source.uri.toString()); 54 source.uri.toString(),
55 original);
55 checkLibraryElements(original, resynthesized); 56 checkLibraryElements(original, resynthesized);
56 } 57 }
57 58
58 void checkLibraryElements( 59 void checkLibraryElements(
59 LibraryElementImpl original, LibraryElementImpl resynthesized) { 60 LibraryElementImpl original, LibraryElementImpl resynthesized) {
60 compareElements(resynthesized, original, '(library)'); 61 compareElements(resynthesized, original, '(library)');
61 expect(resynthesized.displayName, original.displayName); 62 expect(resynthesized.displayName, original.displayName);
62 expect(original.enclosingElement, isNull); 63 expect(original.enclosingElement, isNull);
63 expect(resynthesized.enclosingElement, isNull); 64 expect(resynthesized.enclosingElement, isNull);
64 expect(resynthesized.hasExtUri, original.hasExtUri); 65 expect(resynthesized.hasExtUri, original.hasExtUri);
(...skipping 29 matching lines...) Expand all
94 // `loadLibrary` functions until after both are created. 95 // `loadLibrary` functions until after both are created.
95 if (original.name != 'dart.core' && original.name != 'dart.async') { 96 if (original.name != 'dart.core' && original.name != 'dart.async') {
96 compareExecutableElements( 97 compareExecutableElements(
97 resynthesized.loadLibraryFunction as ExecutableElementImpl, 98 resynthesized.loadLibraryFunction as ExecutableElementImpl,
98 original.loadLibraryFunction as ExecutableElementImpl, 99 original.loadLibraryFunction as ExecutableElementImpl,
99 '(loadLibraryFunction)'); 100 '(loadLibraryFunction)');
100 } 101 }
101 // TODO(paulberry): test metadata. 102 // TODO(paulberry): test metadata.
102 } 103 }
103 104
105 /**
106 * Verify that the [resynthesizer] didn't do any unnecessary work when
107 * resynthesizing [library].
108 */
109 void checkMinimalResynthesisWork(
110 _TestSummaryResynthesizer resynthesizer, LibraryElement library) {
111 // Check that no other summaries needed to be resynthesized to resynthesize
112 // the library element.
113 expect(resynthesizer.resynthesisCount, 1);
114 // Check that the only linked summary consulted was that for [uri].
115 expect(resynthesizer.linkedSummariesRequested, hasLength(1));
116 expect(resynthesizer.linkedSummariesRequested.first,
117 library.source.uri.toString());
118 // Check that the only unlinked summaries consulted were those for the
119 // library in question.
120 Set<String> expectedCompilationUnitUris =
121 <String>[library.source.uri.toString()].toSet();
122 for (CompilationUnitElement partUnit in library.parts) {
scheglov 2016/02/01 21:45:22 You could use "library.units" and avoid the specia
Paul Berry 2016/02/01 22:32:32 Done.
123 expectedCompilationUnitUris.add(partUnit.source.uri.toString());
124 }
125 for (String requestedUri in resynthesizer.unlinkedSummariesRequested) {
126 expect(expectedCompilationUnitUris, contains(requestedUri));
127 }
128 }
129
104 void compareClassElements( 130 void compareClassElements(
105 ClassElementImpl resynthesized, ClassElementImpl original, String desc) { 131 ClassElementImpl resynthesized, ClassElementImpl original, String desc) {
106 compareElements(resynthesized, original, desc); 132 compareElements(resynthesized, original, desc);
107 expect(resynthesized.fields.length, original.fields.length, 133 expect(resynthesized.fields.length, original.fields.length,
108 reason: '$desc fields.length'); 134 reason: '$desc fields.length');
109 for (int i = 0; i < resynthesized.fields.length; i++) { 135 for (int i = 0; i < resynthesized.fields.length; i++) {
110 String name = original.fields[i].name; 136 String name = original.fields[i].name;
111 compareFieldElements( 137 compareFieldElements(
112 resynthesized.fields[i], original.fields[i], '$desc.field $name'); 138 resynthesized.fields[i], original.fields[i], '$desc.field $name');
113 } 139 }
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 } else { 709 } else {
684 fail('Unexpected type for resynthesized ($desc):' 710 fail('Unexpected type for resynthesized ($desc):'
685 ' ${element.runtimeType}'); 711 ' ${element.runtimeType}');
686 return null; 712 return null;
687 } 713 }
688 } 714 }
689 715
690 /** 716 /**
691 * Resynthesize the library element associated with [uri] using 717 * Resynthesize the library element associated with [uri] using
692 * [resynthesizer], and verify that it only had to consult one summary in 718 * [resynthesizer], and verify that it only had to consult one summary in
693 * order to do so. 719 * order to do so. [original] is consulted merely to verify that no
720 * unnecessary resynthesis work was performed.
694 */ 721 */
695 LibraryElementImpl resynthesizeLibraryElement( 722 LibraryElementImpl resynthesizeLibraryElement(
696 _TestSummaryResynthesizer resynthesizer, String uri) { 723 _TestSummaryResynthesizer resynthesizer,
724 String uri,
725 LibraryElement original) {
697 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri); 726 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri);
698 // Check that no other summaries needed to be resynthesized to resynthesize 727 checkMinimalResynthesisWork(resynthesizer, original);
699 // the library element.
700 expect(resynthesizer.resynthesisCount, 1);
701 return resynthesized; 728 return resynthesized;
702 } 729 }
703 730
704 @override 731 @override
705 void setUp() { 732 void setUp() {
706 super.setUp(); 733 super.setUp();
707 resetWithOptions(options); 734 resetWithOptions(options);
708 } 735 }
709 736
710 test_class_abstract() { 737 test_class_abstract() {
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 * Docs 1336 * Docs
1310 */ 1337 */
1311 C(); 1338 C();
1312 }'''); 1339 }''');
1313 } 1340 }
1314 1341
1315 test_core() { 1342 test_core() {
1316 String uri = 'dart:core'; 1343 String uri = 'dart:core';
1317 LibraryElementImpl original = 1344 LibraryElementImpl original =
1318 resolve2(analysisContext2.sourceFactory.forUri(uri)); 1345 resolve2(analysisContext2.sourceFactory.forUri(uri));
1319 LibraryElementImpl resynthesized = 1346 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
1320 resynthesizeLibraryElement(encodeLibraryElement(original), uri); 1347 encodeLibraryElement(original), uri, original);
1321 checkLibraryElements(original, resynthesized); 1348 checkLibraryElements(original, resynthesized);
1322 } 1349 }
1323 1350
1324 test_enum_documented() { 1351 test_enum_documented() {
1325 checkLibrary(''' 1352 checkLibrary('''
1326 // Extra comment so doc comment offset != 0 1353 // Extra comment so doc comment offset != 0
1327 /** 1354 /**
1328 * Docs 1355 * Docs
1329 */ 1356 */
1330 enum E { v }'''); 1357 enum E { v }''');
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after
2211 2238
2212 /** 2239 /**
2213 * Encode the library containing [original] into a summary and then use 2240 * Encode the library containing [original] into a summary and then use
2214 * [_TestSummaryResynthesizer.getElement] to retrieve just the original 2241 * [_TestSummaryResynthesizer.getElement] to retrieve just the original
2215 * element from the resynthesized summary. 2242 * element from the resynthesized summary.
2216 */ 2243 */
2217 Element validateGetElement(Element original) { 2244 Element validateGetElement(Element original) {
2218 _TestSummaryResynthesizer resynthesizer = encodeLibrary(original.library); 2245 _TestSummaryResynthesizer resynthesizer = encodeLibrary(original.library);
2219 ElementLocationImpl location = original.location; 2246 ElementLocationImpl location = original.location;
2220 Element result = resynthesizer.getElement(location); 2247 Element result = resynthesizer.getElement(location);
2248 checkMinimalResynthesisWork(resynthesizer, original.library);
2221 // Check that no other summaries needed to be resynthesized to resynthesize 2249 // Check that no other summaries needed to be resynthesized to resynthesize
2222 // the library element. 2250 // the library element.
2223 expect(resynthesizer.resynthesisCount, 1); 2251 expect(resynthesizer.resynthesisCount, 1);
2224 expect(result.location, location); 2252 expect(result.location, location);
2225 return result; 2253 return result;
2226 } 2254 }
2227 } 2255 }
2228 2256
2229 class _TestSummaryResynthesizer extends SummaryResynthesizer { 2257 class _TestSummaryResynthesizer extends SummaryResynthesizer {
2230 final Map<String, UnlinkedUnit> unlinkedSummaries; 2258 final Map<String, UnlinkedUnit> unlinkedSummaries;
2231 final Map<String, LinkedLibrary> linkedSummaries; 2259 final Map<String, LinkedLibrary> linkedSummaries;
2232 2260
2261 /**
2262 * The set of uris for which unlinked summaries have been requested using
2263 * [getUnlinkedSummary].
2264 */
2265 final Set<String> unlinkedSummariesRequested = new Set<String>();
2266
2267 /**
2268 * The set of uris for which linked summaries have been requested using
2269 * [getLinkedSummary].
2270 */
2271 final Set<String> linkedSummariesRequested = new Set<String>();
2272
2233 _TestSummaryResynthesizer( 2273 _TestSummaryResynthesizer(
2234 SummaryResynthesizer parent, 2274 SummaryResynthesizer parent,
2235 AnalysisContext context, 2275 AnalysisContext context,
2236 TypeProvider typeProvider, 2276 TypeProvider typeProvider,
2237 SourceFactory sourceFactory, 2277 SourceFactory sourceFactory,
2238 this.unlinkedSummaries, 2278 this.unlinkedSummaries,
2239 this.linkedSummaries, 2279 this.linkedSummaries,
2240 bool strongMode) 2280 bool strongMode)
2241 : super(parent, context, typeProvider, sourceFactory, strongMode); 2281 : super(parent, context, typeProvider, sourceFactory, strongMode);
2242 2282
2243 @override 2283 @override
2244 LinkedLibrary getLinkedSummary(String uri) { 2284 LinkedLibrary getLinkedSummary(String uri) {
2285 linkedSummariesRequested.add(uri);
2245 LinkedLibrary serializedLibrary = linkedSummaries[uri]; 2286 LinkedLibrary serializedLibrary = linkedSummaries[uri];
2246 if (serializedLibrary == null) { 2287 if (serializedLibrary == null) {
2247 fail('Unexpectedly tried to get linked summary for $uri'); 2288 fail('Unexpectedly tried to get linked summary for $uri');
2248 } 2289 }
2249 return serializedLibrary; 2290 return serializedLibrary;
2250 } 2291 }
2251 2292
2252 @override 2293 @override
2253 UnlinkedUnit getUnlinkedSummary(String uri) { 2294 UnlinkedUnit getUnlinkedSummary(String uri) {
2295 unlinkedSummariesRequested.add(uri);
2254 UnlinkedUnit serializedUnit = unlinkedSummaries[uri]; 2296 UnlinkedUnit serializedUnit = unlinkedSummaries[uri];
2255 if (serializedUnit == null) { 2297 if (serializedUnit == null) {
2256 fail('Unexpectedly tried to get unlinked summary for $uri'); 2298 fail('Unexpectedly tried to get unlinked summary for $uri');
2257 } 2299 }
2258 return serializedUnit; 2300 return serializedUnit;
2259 } 2301 }
2260 2302
2261 @override 2303 @override
2262 bool hasLibrarySummary(String uri) { 2304 bool hasLibrarySummary(String uri) {
2263 return true; 2305 return true;
2264 } 2306 }
2265 } 2307 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698