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.src.summary.prelinker_test; | 5 library analyzer.test.src.summary.prelinker_test; |
6 | 6 |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/src/generated/utilities_dart.dart'; |
8 import 'package:analyzer/src/generated/source.dart'; | |
9 import 'package:analyzer/src/generated/source_io.dart'; | |
10 import 'package:analyzer/src/summary/idl.dart'; | 8 import 'package:analyzer/src/summary/idl.dart'; |
11 import 'package:analyzer/src/summary/prelink.dart'; | 9 import 'package:analyzer/src/summary/prelink.dart'; |
12 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
13 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
14 | 12 |
15 import 'summarize_elements_test.dart'; | 13 import 'summarize_ast_test.dart'; |
16 import 'summary_common.dart'; | 14 import 'summary_common.dart'; |
17 | 15 |
18 main() { | 16 main() { |
19 groupSep = ' | '; | 17 groupSep = ' | '; |
20 defineReflectiveTests(PrelinkerTest); | 18 defineReflectiveTests(PrelinkerTest); |
21 } | 19 } |
22 | 20 |
23 /** | 21 /** |
24 * Override of [SummaryTest] which verifies the correctness of the prelinker by | 22 * Override of [SummaryTest] which verifies the correctness of the prelinker by |
25 * creating summaries from the element model, discarding their prelinked | 23 * creating summaries from the element model, discarding their prelinked |
26 * information, and then recreating it using the prelinker. | 24 * information, and then recreating it using the prelinker. |
27 */ | 25 */ |
28 @reflectiveTest | 26 @reflectiveTest |
29 class PrelinkerTest extends SummarizeElementsTest { | 27 class PrelinkerTest extends LinkedSummarizeAstTest { |
30 final Map<String, UnlinkedPublicNamespace> uriToPublicNamespace = | |
31 <String, UnlinkedPublicNamespace>{}; | |
32 | |
33 @override | 28 @override |
34 bool get expectAbsoluteUrisInDependencies => false; | 29 bool get expectAbsoluteUrisInDependencies => false; |
35 | 30 |
36 @override | 31 @override |
37 bool get skipFullyLinkedData => true; | 32 bool get skipFullyLinkedData => true; |
38 | 33 |
39 @override | 34 @override |
40 bool get strongMode => false; | 35 bool get strongMode => false; |
41 | 36 |
42 @override | 37 @override |
43 Source addNamedSource(String filePath, String contents) { | 38 void serializeLibraryText(String text, {bool allowErrors: false}) { |
44 Source source = super.addNamedSource(filePath, contents); | 39 super.serializeLibraryText(text, allowErrors: allowErrors); |
45 uriToPublicNamespace[absUri(filePath)] = | |
46 computePublicNamespaceFromText(contents, source); | |
47 return source; | |
48 } | |
49 | 40 |
50 String resolveToAbsoluteUri(LibraryElement library, String relativeUri) { | |
51 Source resolvedSource = | |
52 context.sourceFactory.resolveUri(library.source, relativeUri); | |
53 if (resolvedSource == null) { | |
54 fail('Failed to resolve relative uri "$relativeUri"'); | |
55 } | |
56 return resolvedSource.uri.toString(); | |
57 } | |
58 | |
59 @override | |
60 void serializeLibraryElement(LibraryElement library) { | |
61 super.serializeLibraryElement(library); | |
62 uriToPublicNamespace[library.source.uri.toString()] = | |
63 unlinkedUnits[0].publicNamespace; | |
64 Map<String, UnlinkedUnit> uriToUnit = <String, UnlinkedUnit>{}; | |
65 expect(unlinkedUnits.length, unitUris.length); | |
66 for (int i = 1; i < unlinkedUnits.length; i++) { | |
67 uriToUnit[unitUris[i]] = unlinkedUnits[i]; | |
68 } | |
69 UnlinkedUnit getPart(String relativeUri) { | 41 UnlinkedUnit getPart(String relativeUri) { |
70 String absoluteUri = resolveToAbsoluteUri(library, relativeUri); | 42 String absoluteUri = |
71 UnlinkedUnit unit = uriToUnit[absoluteUri]; | 43 resolveRelativeUri(linkerInputs.testDartUri, Uri.parse(relativeUri)) |
72 if (unit == null) { | 44 .toString(); |
73 fail('Prelinker unexpectedly requested unit for "$relativeUri"' | 45 return linkerInputs.getUnit(absoluteUri); |
74 ' (resolves to "$absoluteUri").'); | |
75 } | |
76 return unit; | |
77 } | 46 } |
78 | 47 |
79 UnlinkedPublicNamespace getImport(String relativeUri) { | 48 UnlinkedPublicNamespace getImport(String relativeUri) { |
80 String absoluteUri = resolveToAbsoluteUri(library, relativeUri); | 49 return getPart(relativeUri)?.publicNamespace; |
81 UnlinkedPublicNamespace namespace = SerializedMockSdk | |
82 .instance.uriToUnlinkedUnit[absoluteUri]?.publicNamespace; | |
83 if (namespace == null) { | |
84 namespace = uriToPublicNamespace[absoluteUri]; | |
85 } | |
86 if (namespace == null && !allowMissingFiles) { | |
87 fail('Prelinker unexpectedly requested namespace for "$relativeUri"' | |
88 ' (resolves to "$absoluteUri").' | |
89 ' Namespaces available: ${uriToPublicNamespace.keys}'); | |
90 } | |
91 return namespace; | |
92 } | 50 } |
93 | 51 |
94 linked = new LinkedLibrary.fromBuffer(prelink( | 52 linked = new LinkedLibrary.fromBuffer(prelink( |
95 unlinkedUnits[0], getPart, getImport, context.declaredVariables.get) | 53 linkerInputs.unlinkedDefiningUnit, |
96 .toBuffer()); | 54 getPart, |
| 55 getImport, |
| 56 (String declaredVariable) => null).toBuffer()); |
97 validateLinkedLibrary(linked); | 57 validateLinkedLibrary(linked); |
98 } | 58 } |
99 } | 59 } |
OLD | NEW |