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

Side by Side Diff: pkg/analyzer/test/src/summary/resynthesize_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) 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 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/constant/value.dart'; 10 import 'package:analyzer/dart/constant/value.dart';
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 */ 42 */
43 abstract class AbstractResynthesizeTest extends AbstractSingleUnitTest { 43 abstract class AbstractResynthesizeTest extends AbstractSingleUnitTest {
44 Set<Source> otherLibrarySources = new Set<Source>(); 44 Set<Source> otherLibrarySources = new Set<Source>();
45 45
46 /** 46 /**
47 * Names of variables which have initializers that are not valid constants, 47 * Names of variables which have initializers that are not valid constants,
48 * so they are not resynthesized. 48 * so they are not resynthesized.
49 */ 49 */
50 Set<String> variablesWithNotConstInitializers = new Set<String>(); 50 Set<String> variablesWithNotConstInitializers = new Set<String>();
51 51
52 /**
53 * Tests may set this to `true` to indicate that a missing file at the time of
Brian Wilkerson 2016/08/11 23:49:35 Seems odd to talk about setting a getter to `true`
Paul Berry 2016/08/12 20:56:51 Resolved in an offline discussion. There was a sl
54 * summary resynthesis shouldn't trigger an error.
55 */
56 bool get allowMissingFiles;
57
52 bool get checkPropagatedTypes => true; 58 bool get checkPropagatedTypes => true;
53 59
54 void addLibrary(String uri) { 60 void addLibrary(String uri) {
55 otherLibrarySources.add(context.sourceFactory.forUri(uri)); 61 otherLibrarySources.add(context.sourceFactory.forUri(uri));
56 } 62 }
57 63
58 Source addLibrarySource(String filePath, String contents) { 64 Source addLibrarySource(String filePath, String contents) {
59 Source source = addSource(filePath, contents); 65 Source source = addSource(filePath, contents);
60 otherLibrarySources.add(source); 66 otherLibrarySources.add(source);
61 return source; 67 return source;
(...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 } 1183 }
1178 if (dumpSummaries) { 1184 if (dumpSummaries) {
1179 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) { 1185 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) {
1180 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}'); 1186 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}');
1181 }); 1187 });
1182 linkedSummaries.forEach((String path, LinkedLibrary lib) { 1188 linkedSummaries.forEach((String path, LinkedLibrary lib) {
1183 print('Linked $path: ${JSON.encode(canonicalize(lib))}'); 1189 print('Linked $path: ${JSON.encode(canonicalize(lib))}');
1184 }); 1190 });
1185 } 1191 }
1186 return new TestSummaryResynthesizer( 1192 return new TestSummaryResynthesizer(
1187 null, context, unlinkedSummaries, linkedSummaries); 1193 null, context, unlinkedSummaries, linkedSummaries, allowMissingFiles);
1188 } 1194 }
1189 1195
1190 ElementImpl getActualElement(Element element, String desc) { 1196 ElementImpl getActualElement(Element element, String desc) {
1191 if (element == null) { 1197 if (element == null) {
1192 return null; 1198 return null;
1193 } else if (element is ElementImpl) { 1199 } else if (element is ElementImpl) {
1194 return element; 1200 return element;
1195 } else if (element is ElementHandle) { 1201 } else if (element is ElementHandle) {
1196 Element actualElement = element.actualElement; 1202 Element actualElement = element.actualElement;
1197 // A handle should never point to a member, because if it did, then 1203 // A handle should never point to a member, because if it did, then
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 return element.isSetter; 1330 return element.isSetter;
1325 } 1331 }
1326 return false; 1332 return false;
1327 } else if (modifier == Modifier.STATIC) { 1333 } else if (modifier == Modifier.STATIC) {
1328 if (element is ExecutableElement) { 1334 if (element is ExecutableElement) {
1329 return element.isStatic; 1335 return element.isStatic;
1330 } 1336 }
1331 return false; 1337 return false;
1332 } else if (modifier == Modifier.SYNTHETIC) { 1338 } else if (modifier == Modifier.SYNTHETIC) {
1333 return element.isSynthetic; 1339 return element.isSynthetic;
1334 } else if (modifier == Modifier.URI_EXISTS) {
1335 if (element is ExportElement) {
1336 return element.uriExists;
1337 } else if (element is ImportElement) {
1338 return element.uriExists;
1339 }
1340 return false;
1341 } 1340 }
1342 throw new UnimplementedError( 1341 throw new UnimplementedError(
1343 'Modifier $modifier for ${element?.runtimeType}'); 1342 'Modifier $modifier for ${element?.runtimeType}');
1344 } 1343 }
1345 } 1344 }
1346 1345
1347 @reflectiveTest 1346 @reflectiveTest
1348 class ResynthesizeElementTest extends ResynthesizeTest { 1347 class ResynthesizeElementTest extends ResynthesizeTest {
1349 @override 1348 @override
1349 bool allowMissingFiles = false;
1350
1351 @override
1350 LibraryElementImpl checkLibrary(String text, 1352 LibraryElementImpl checkLibrary(String text,
1351 {bool allowErrors: false, bool dumpSummaries: false}) { 1353 {bool allowErrors: false, bool dumpSummaries: false}) {
1352 Source source = addTestSource(text); 1354 Source source = addTestSource(text);
1353 LibraryElementImpl original = context.computeLibraryElement(source); 1355 LibraryElementImpl original = context.computeLibraryElement(source);
1354 LibraryElementImpl resynthesized = resynthesizeLibraryElement( 1356 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
1355 encodeLibrary(original, 1357 encodeLibrary(original,
1356 allowErrors: allowErrors, dumpSummaries: dumpSummaries), 1358 allowErrors: allowErrors, dumpSummaries: dumpSummaries),
1357 source.uri.toString(), 1359 source.uri.toString(),
1358 original); 1360 original);
1359 checkLibraryElements(original, resynthesized); 1361 checkLibraryElements(original, resynthesized);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1415 } 1417 }
1416 if (dumpSummaries) { 1418 if (dumpSummaries) {
1417 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) { 1419 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) {
1418 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}'); 1420 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}');
1419 }); 1421 });
1420 linkedSummaries.forEach((String path, LinkedLibrary lib) { 1422 linkedSummaries.forEach((String path, LinkedLibrary lib) {
1421 print('Linked $path: ${JSON.encode(canonicalize(lib))}'); 1423 print('Linked $path: ${JSON.encode(canonicalize(lib))}');
1422 }); 1424 });
1423 } 1425 }
1424 return new TestSummaryResynthesizer( 1426 return new TestSummaryResynthesizer(
1425 null, context, unlinkedSummaries, linkedSummaries); 1427 null, context, unlinkedSummaries, linkedSummaries, allowMissingFiles);
1426 } 1428 }
1427 1429
1428 /** 1430 /**
1429 * Resynthesize the library element associated with [uri] using 1431 * Resynthesize the library element associated with [uri] using
1430 * [resynthesizer], and verify that it only had to consult one summary in 1432 * [resynthesizer], and verify that it only had to consult one summary in
1431 * order to do so. [original] is consulted merely to verify that no 1433 * order to do so. [original] is consulted merely to verify that no
1432 * unnecessary resynthesis work was performed. 1434 * unnecessary resynthesis work was performed.
1433 */ 1435 */
1434 LibraryElementImpl resynthesizeLibraryElement( 1436 LibraryElementImpl resynthesizeLibraryElement(
1435 TestSummaryResynthesizer resynthesizer, 1437 TestSummaryResynthesizer resynthesizer,
(...skipping 11 matching lines...) Expand all
1447 LibraryElementImpl original = 1449 LibraryElementImpl original =
1448 context.computeLibraryElement(context.sourceFactory.forUri(uri)); 1450 context.computeLibraryElement(context.sourceFactory.forUri(uri));
1449 LibraryElementImpl resynthesized = resynthesizeLibraryElement( 1451 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
1450 encodeLibraryElement(original), uri, original); 1452 encodeLibraryElement(original), uri, original);
1451 checkLibraryElements(original, resynthesized); 1453 checkLibraryElements(original, resynthesized);
1452 } 1454 }
1453 } 1455 }
1454 1456
1455 @reflectiveTest 1457 @reflectiveTest
1456 abstract class ResynthesizeTest extends AbstractResynthesizeTest { 1458 abstract class ResynthesizeTest extends AbstractResynthesizeTest {
1459 /**
1460 * Tests may set this to `true` to indicate that a missing file at the time of
1461 * summary resynthesis shouldn't trigger an error.
1462 */
1463 void set allowMissingFiles(bool value);
1464
1457 LibraryElementImpl checkLibrary(String text, 1465 LibraryElementImpl checkLibrary(String text,
1458 {bool allowErrors: false, bool dumpSummaries: false}); 1466 {bool allowErrors: false, bool dumpSummaries: false});
1459 1467
1460 /** 1468 /**
1461 * Return a [SummaryResynthesizer] to resynthesize the library with the 1469 * Return a [SummaryResynthesizer] to resynthesize the library with the
1462 * given [librarySource]. 1470 * given [librarySource].
1463 */ 1471 */
1464 SummaryResynthesizer encodeDecodeLibrarySource(Source librarySource); 1472 SummaryResynthesizer encodeDecodeLibrarySource(Source librarySource);
1465 1473
1466 fail_library_hasExtUri() { 1474 fail_library_hasExtUri() {
(...skipping 3002 matching lines...) Expand 10 before | Expand all | Expand 10 after
4469 } 4477 }
4470 4478
4471 test_typedef_type_parameters_f_bound_simple() { 4479 test_typedef_type_parameters_f_bound_simple() {
4472 checkLibrary('typedef U F<T extends U, U>(T t);'); 4480 checkLibrary('typedef U F<T extends U, U>(T t);');
4473 } 4481 }
4474 4482
4475 test_typedefs() { 4483 test_typedefs() {
4476 checkLibrary('f() {} g() {}'); 4484 checkLibrary('f() {} g() {}');
4477 } 4485 }
4478 4486
4487 test_unresolved_export() {
4488 allowMissingFiles = true;
4489 checkLibrary("export 'foo.dart';", allowErrors: true);
4490 }
4491
4492 test_unresolved_import() {
4493 allowMissingFiles = true;
4494 checkLibrary("import 'foo.dart';", allowErrors: true);
4495 }
4496
4497 test_unresolved_part() {
4498 allowMissingFiles = true;
4499 checkLibrary("part 'foo.dart';", allowErrors: true);
4500 }
4501
4479 test_unused_type_parameter() { 4502 test_unused_type_parameter() {
4480 checkLibrary(''' 4503 checkLibrary('''
4481 class C<T> { 4504 class C<T> {
4482 void f() {} 4505 void f() {}
4483 } 4506 }
4484 C<int> c; 4507 C<int> c;
4485 var v = c.f; 4508 var v = c.f;
4486 '''); 4509 ''');
4487 } 4510 }
4488 4511
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
4581 // the library element. 4604 // the library element.
4582 expect(resynthesizer.resynthesisCount, 1); 4605 expect(resynthesizer.resynthesisCount, 1);
4583 expect(result.location, location); 4606 expect(result.location, location);
4584 return result; 4607 return result;
4585 } 4608 }
4586 } 4609 }
4587 4610
4588 class TestSummaryResynthesizer extends SummaryResynthesizer { 4611 class TestSummaryResynthesizer extends SummaryResynthesizer {
4589 final Map<String, UnlinkedUnit> unlinkedSummaries; 4612 final Map<String, UnlinkedUnit> unlinkedSummaries;
4590 final Map<String, LinkedLibrary> linkedSummaries; 4613 final Map<String, LinkedLibrary> linkedSummaries;
4614 final bool allowMissingFiles;
4591 4615
4592 /** 4616 /**
4593 * The set of uris for which unlinked summaries have been requested using 4617 * The set of uris for which unlinked summaries have been requested using
4594 * [getUnlinkedSummary]. 4618 * [getUnlinkedSummary].
4595 */ 4619 */
4596 final Set<String> unlinkedSummariesRequested = new Set<String>(); 4620 final Set<String> unlinkedSummariesRequested = new Set<String>();
4597 4621
4598 /** 4622 /**
4599 * The set of uris for which linked summaries have been requested using 4623 * The set of uris for which linked summaries have been requested using
4600 * [getLinkedSummary]. 4624 * [getLinkedSummary].
4601 */ 4625 */
4602 final Set<String> linkedSummariesRequested = new Set<String>(); 4626 final Set<String> linkedSummariesRequested = new Set<String>();
4603 4627
4604 TestSummaryResynthesizer(SummaryResynthesizer parent, AnalysisContext context, 4628 TestSummaryResynthesizer(SummaryResynthesizer parent, AnalysisContext context,
4605 this.unlinkedSummaries, this.linkedSummaries) 4629 this.unlinkedSummaries, this.linkedSummaries, this.allowMissingFiles)
4606 : super(parent, context, context.typeProvider, context.sourceFactory, 4630 : super(parent, context, context.typeProvider, context.sourceFactory,
4607 context.analysisOptions.strongMode); 4631 context.analysisOptions.strongMode);
4608 4632
4609 @override 4633 @override
4610 LinkedLibrary getLinkedSummary(String uri) { 4634 LinkedLibrary getLinkedSummary(String uri) {
4611 linkedSummariesRequested.add(uri); 4635 linkedSummariesRequested.add(uri);
4612 LinkedLibrary serializedLibrary = linkedSummaries[uri]; 4636 LinkedLibrary serializedLibrary = linkedSummaries[uri];
4613 if (serializedLibrary == null) { 4637 if (serializedLibrary == null && !allowMissingFiles) {
4614 fail('Unexpectedly tried to get linked summary for $uri'); 4638 fail('Unexpectedly tried to get linked summary for $uri');
4615 } 4639 }
4616 return serializedLibrary; 4640 return serializedLibrary;
4617 } 4641 }
4618 4642
4619 @override 4643 @override
4620 UnlinkedUnit getUnlinkedSummary(String uri) { 4644 UnlinkedUnit getUnlinkedSummary(String uri) {
4621 unlinkedSummariesRequested.add(uri); 4645 unlinkedSummariesRequested.add(uri);
4622 UnlinkedUnit serializedUnit = unlinkedSummaries[uri]; 4646 UnlinkedUnit serializedUnit = unlinkedSummaries[uri];
4623 if (serializedUnit == null) { 4647 if (serializedUnit == null && !allowMissingFiles) {
4624 fail('Unexpectedly tried to get unlinked summary for $uri'); 4648 fail('Unexpectedly tried to get unlinked summary for $uri');
4625 } 4649 }
4626 return serializedUnit; 4650 return serializedUnit;
4627 } 4651 }
4628 4652
4629 @override 4653 @override
4630 bool hasLibrarySummary(String uri) { 4654 bool hasLibrarySummary(String uri) {
4631 return true; 4655 return true;
4632 } 4656 }
4633 } 4657 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_strong_test.dart ('k') | pkg/analyzer/test/src/summary/summary_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698