OLD | NEW |
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 Loading... |
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 |
| 54 * summary resynthesis shouldn't trigger an error. |
| 55 */ |
| 56 bool allowMissingFiles = false; |
| 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 Loading... |
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 Loading... |
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 |
1350 LibraryElementImpl checkLibrary(String text, | 1349 LibraryElementImpl checkLibrary(String text, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 } | 1414 } |
1416 if (dumpSummaries) { | 1415 if (dumpSummaries) { |
1417 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) { | 1416 unlinkedSummaries.forEach((String path, UnlinkedUnit unit) { |
1418 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}'); | 1417 print('Unlinked $path: ${JSON.encode(canonicalize(unit))}'); |
1419 }); | 1418 }); |
1420 linkedSummaries.forEach((String path, LinkedLibrary lib) { | 1419 linkedSummaries.forEach((String path, LinkedLibrary lib) { |
1421 print('Linked $path: ${JSON.encode(canonicalize(lib))}'); | 1420 print('Linked $path: ${JSON.encode(canonicalize(lib))}'); |
1422 }); | 1421 }); |
1423 } | 1422 } |
1424 return new TestSummaryResynthesizer( | 1423 return new TestSummaryResynthesizer( |
1425 null, context, unlinkedSummaries, linkedSummaries); | 1424 null, context, unlinkedSummaries, linkedSummaries, allowMissingFiles); |
1426 } | 1425 } |
1427 | 1426 |
1428 /** | 1427 /** |
1429 * Resynthesize the library element associated with [uri] using | 1428 * Resynthesize the library element associated with [uri] using |
1430 * [resynthesizer], and verify that it only had to consult one summary in | 1429 * [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 | 1430 * order to do so. [original] is consulted merely to verify that no |
1432 * unnecessary resynthesis work was performed. | 1431 * unnecessary resynthesis work was performed. |
1433 */ | 1432 */ |
1434 LibraryElementImpl resynthesizeLibraryElement( | 1433 LibraryElementImpl resynthesizeLibraryElement( |
1435 TestSummaryResynthesizer resynthesizer, | 1434 TestSummaryResynthesizer resynthesizer, |
(...skipping 3033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4469 } | 4468 } |
4470 | 4469 |
4471 test_typedef_type_parameters_f_bound_simple() { | 4470 test_typedef_type_parameters_f_bound_simple() { |
4472 checkLibrary('typedef U F<T extends U, U>(T t);'); | 4471 checkLibrary('typedef U F<T extends U, U>(T t);'); |
4473 } | 4472 } |
4474 | 4473 |
4475 test_typedefs() { | 4474 test_typedefs() { |
4476 checkLibrary('f() {} g() {}'); | 4475 checkLibrary('f() {} g() {}'); |
4477 } | 4476 } |
4478 | 4477 |
| 4478 test_unresolved_export() { |
| 4479 allowMissingFiles = true; |
| 4480 checkLibrary("export 'foo.dart';", allowErrors: true); |
| 4481 } |
| 4482 |
| 4483 test_unresolved_import() { |
| 4484 allowMissingFiles = true; |
| 4485 checkLibrary("import 'foo.dart';", allowErrors: true); |
| 4486 } |
| 4487 |
| 4488 test_unresolved_part() { |
| 4489 allowMissingFiles = true; |
| 4490 checkLibrary("part 'foo.dart';", allowErrors: true); |
| 4491 } |
| 4492 |
4479 test_unused_type_parameter() { | 4493 test_unused_type_parameter() { |
4480 checkLibrary(''' | 4494 checkLibrary(''' |
4481 class C<T> { | 4495 class C<T> { |
4482 void f() {} | 4496 void f() {} |
4483 } | 4497 } |
4484 C<int> c; | 4498 C<int> c; |
4485 var v = c.f; | 4499 var v = c.f; |
4486 '''); | 4500 '''); |
4487 } | 4501 } |
4488 | 4502 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4581 // the library element. | 4595 // the library element. |
4582 expect(resynthesizer.resynthesisCount, 1); | 4596 expect(resynthesizer.resynthesisCount, 1); |
4583 expect(result.location, location); | 4597 expect(result.location, location); |
4584 return result; | 4598 return result; |
4585 } | 4599 } |
4586 } | 4600 } |
4587 | 4601 |
4588 class TestSummaryResynthesizer extends SummaryResynthesizer { | 4602 class TestSummaryResynthesizer extends SummaryResynthesizer { |
4589 final Map<String, UnlinkedUnit> unlinkedSummaries; | 4603 final Map<String, UnlinkedUnit> unlinkedSummaries; |
4590 final Map<String, LinkedLibrary> linkedSummaries; | 4604 final Map<String, LinkedLibrary> linkedSummaries; |
| 4605 final bool allowMissingFiles; |
4591 | 4606 |
4592 /** | 4607 /** |
4593 * The set of uris for which unlinked summaries have been requested using | 4608 * The set of uris for which unlinked summaries have been requested using |
4594 * [getUnlinkedSummary]. | 4609 * [getUnlinkedSummary]. |
4595 */ | 4610 */ |
4596 final Set<String> unlinkedSummariesRequested = new Set<String>(); | 4611 final Set<String> unlinkedSummariesRequested = new Set<String>(); |
4597 | 4612 |
4598 /** | 4613 /** |
4599 * The set of uris for which linked summaries have been requested using | 4614 * The set of uris for which linked summaries have been requested using |
4600 * [getLinkedSummary]. | 4615 * [getLinkedSummary]. |
4601 */ | 4616 */ |
4602 final Set<String> linkedSummariesRequested = new Set<String>(); | 4617 final Set<String> linkedSummariesRequested = new Set<String>(); |
4603 | 4618 |
4604 TestSummaryResynthesizer(SummaryResynthesizer parent, AnalysisContext context, | 4619 TestSummaryResynthesizer(SummaryResynthesizer parent, AnalysisContext context, |
4605 this.unlinkedSummaries, this.linkedSummaries) | 4620 this.unlinkedSummaries, this.linkedSummaries, this.allowMissingFiles) |
4606 : super(parent, context, context.typeProvider, context.sourceFactory, | 4621 : super(parent, context, context.typeProvider, context.sourceFactory, |
4607 context.analysisOptions.strongMode); | 4622 context.analysisOptions.strongMode); |
4608 | 4623 |
4609 @override | 4624 @override |
4610 LinkedLibrary getLinkedSummary(String uri) { | 4625 LinkedLibrary getLinkedSummary(String uri) { |
4611 linkedSummariesRequested.add(uri); | 4626 linkedSummariesRequested.add(uri); |
4612 LinkedLibrary serializedLibrary = linkedSummaries[uri]; | 4627 LinkedLibrary serializedLibrary = linkedSummaries[uri]; |
4613 if (serializedLibrary == null) { | 4628 if (serializedLibrary == null && !allowMissingFiles) { |
4614 fail('Unexpectedly tried to get linked summary for $uri'); | 4629 fail('Unexpectedly tried to get linked summary for $uri'); |
4615 } | 4630 } |
4616 return serializedLibrary; | 4631 return serializedLibrary; |
4617 } | 4632 } |
4618 | 4633 |
4619 @override | 4634 @override |
4620 UnlinkedUnit getUnlinkedSummary(String uri) { | 4635 UnlinkedUnit getUnlinkedSummary(String uri) { |
4621 unlinkedSummariesRequested.add(uri); | 4636 unlinkedSummariesRequested.add(uri); |
4622 UnlinkedUnit serializedUnit = unlinkedSummaries[uri]; | 4637 UnlinkedUnit serializedUnit = unlinkedSummaries[uri]; |
4623 if (serializedUnit == null) { | 4638 if (serializedUnit == null && !allowMissingFiles) { |
4624 fail('Unexpectedly tried to get unlinked summary for $uri'); | 4639 fail('Unexpectedly tried to get unlinked summary for $uri'); |
4625 } | 4640 } |
4626 return serializedUnit; | 4641 return serializedUnit; |
4627 } | 4642 } |
4628 | 4643 |
4629 @override | 4644 @override |
4630 bool hasLibrarySummary(String uri) { | 4645 bool hasLibrarySummary(String uri) { |
4631 return true; | 4646 return true; |
4632 } | 4647 } |
4633 } | 4648 } |
OLD | NEW |