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

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

Issue 1828543009: First steps toward generating fully linked summaries from ASTs. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 analyzer.test.src.summary.summary_common; 5 library analyzer.test.src.summary.summary_common;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/dart/scanner/reader.dart'; 10 import 'package:analyzer/src/dart/scanner/reader.dart';
11 import 'package:analyzer/src/dart/scanner/scanner.dart'; 11 import 'package:analyzer/src/dart/scanner/scanner.dart';
12 import 'package:analyzer/src/generated/engine.dart'; 12 import 'package:analyzer/src/generated/engine.dart';
13 import 'package:analyzer/src/generated/error.dart'; 13 import 'package:analyzer/src/generated/error.dart';
14 import 'package:analyzer/src/generated/java_engine_io.dart'; 14 import 'package:analyzer/src/generated/java_engine_io.dart';
15 import 'package:analyzer/src/generated/parser.dart'; 15 import 'package:analyzer/src/generated/parser.dart';
16 import 'package:analyzer/src/generated/source.dart'; 16 import 'package:analyzer/src/generated/source.dart';
17 import 'package:analyzer/src/generated/source_io.dart'; 17 import 'package:analyzer/src/generated/source_io.dart';
18 import 'package:analyzer/src/summary/base.dart'; 18 import 'package:analyzer/src/summary/base.dart';
19 import 'package:analyzer/src/summary/idl.dart'; 19 import 'package:analyzer/src/summary/idl.dart';
20 import 'package:analyzer/src/summary/public_namespace_computer.dart' 20 import 'package:analyzer/src/summary/public_namespace_computer.dart'
21 as public_namespace; 21 as public_namespace;
22 import 'package:analyzer/src/summary/summarize_elements.dart' 22 import 'package:analyzer/src/summary/summarize_elements.dart'
23 as summarize_elements; 23 as summarize_elements;
24 import 'package:unittest/unittest.dart'; 24 import 'package:unittest/unittest.dart';
25 25
26 import '../../generated/analysis_context_factory.dart'; 26 import '../../generated/analysis_context_factory.dart';
27 27
28 /** 28 /**
29 * The public namespaces of the sdk are computed once so that we don't bog
30 * down the test. Structured as a map from absolute URI to the corresponding
31 * public namespace.
32 *
33 * Note: should an exception occur during computation of this variable, it
34 * will silently be set to null to allow other tests to run.
35 */
36 final Map<String, UnlinkedPublicNamespace> sdkPublicNamespace = () {
37 try {
38 AnalysisContext analysisContext = AnalysisContextFactory.contextWithCore();
39 Map<String, UnlinkedPublicNamespace> uriToNamespace =
40 <String, UnlinkedPublicNamespace>{};
41 List<LibraryElement> libraries = [
42 analysisContext.typeProvider.objectType.element.library,
43 analysisContext.typeProvider.futureType.element.library
44 ];
45 for (LibraryElement library in libraries) {
46 summarize_elements.LibrarySerializationResult serializedLibrary =
47 summarize_elements.serializeLibrary(
48 library, analysisContext.typeProvider, false);
49 for (int i = 0; i < serializedLibrary.unlinkedUnits.length; i++) {
50 uriToNamespace[serializedLibrary.unitUris[i]] =
51 new UnlinkedUnit.fromBuffer(
52 serializedLibrary.unlinkedUnits[i].toBuffer())
53 .publicNamespace;
54 }
55 }
56 return uriToNamespace;
57 } catch (_) {
58 return null;
59 }
60 }();
61
62 /**
63 * Convert a summary object (or a portion of one) into a canonical form that 29 * Convert a summary object (or a portion of one) into a canonical form that
64 * can be easily compared using [expect]. If [orderByName] is true, and the 30 * can be easily compared using [expect]. If [orderByName] is true, and the
65 * object is a [List], it is sorted by the `name` field of its elements. 31 * object is a [List], it is sorted by the `name` field of its elements.
66 */ 32 */
67 Object canonicalize(Object obj, {bool orderByName: false}) { 33 Object canonicalize(Object obj, {bool orderByName: false}) {
68 if (obj is SummaryClass) { 34 if (obj is SummaryClass) {
69 Map<String, Object> result = <String, Object>{}; 35 Map<String, Object> result = <String, Object>{};
70 obj.toMap().forEach((String key, Object value) { 36 obj.toMap().forEach((String key, Object value) {
71 bool orderByName = false; 37 bool orderByName = false;
72 if (obj is UnlinkedPublicNamespace && key == 'names') { 38 if (obj is UnlinkedPublicNamespace && key == 'names') {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 public_namespace.computePublicNamespace(unit).toBuffer()); 75 public_namespace.computePublicNamespace(unit).toBuffer());
110 return namespace; 76 return namespace;
111 } 77 }
112 78
113 /** 79 /**
114 * Type of a function that validates an [EntityRef]. 80 * Type of a function that validates an [EntityRef].
115 */ 81 */
116 typedef void _EntityRefValidator(EntityRef entityRef); 82 typedef void _EntityRefValidator(EntityRef entityRef);
117 83
118 /** 84 /**
85 * [SerializedMockSdk] is a singleton class representing the result of
86 * serializing the mock SDK to summaries. It is computed once and then shared
87 * among test invocations so that we don't bog down the tests.
88 *
89 * Note: should an exception occur during computation of [instance], it will
90 * silently be set to null to allow other tests to complete quickly.
91 */
92 class SerializedMockSdk {
93 static final SerializedMockSdk instance = _serializeMockSdk();
94
95 final Map<String, UnlinkedUnit> uriToUnlinkedUnit;
96
97 final Map<String, LinkedLibrary> uriToLinkedLibrary;
98 SerializedMockSdk._(this.uriToUnlinkedUnit, this.uriToLinkedLibrary);
99
100 static SerializedMockSdk _serializeMockSdk() {
101 try {
102 AnalysisContext analysisContext =
103 AnalysisContextFactory.contextWithCore();
104 Map<String, UnlinkedUnit> uriToUnlinkedUnit = <String, UnlinkedUnit>{};
105 Map<String, LinkedLibrary> uriToLinkedLibrary = <String, LinkedLibrary>{};
106 List<LibraryElement> libraries = [
107 analysisContext.typeProvider.objectType.element.library,
108 analysisContext.typeProvider.futureType.element.library
109 ];
110 for (LibraryElement library in libraries) {
111 summarize_elements.LibrarySerializationResult serializedLibrary =
112 summarize_elements.serializeLibrary(
113 library, analysisContext.typeProvider, false);
114 uriToLinkedLibrary[library.source.uri.toString()] =
115 new LinkedLibrary.fromBuffer(serializedLibrary.linked.toBuffer());
116 for (int i = 0; i < serializedLibrary.unlinkedUnits.length; i++) {
117 uriToUnlinkedUnit[serializedLibrary.unitUris[i]] =
118 new UnlinkedUnit.fromBuffer(
119 serializedLibrary.unlinkedUnits[i].toBuffer());
120 }
121 }
122 return new SerializedMockSdk._(uriToUnlinkedUnit, uriToLinkedLibrary);
123 } catch (_) {
124 return null;
125 }
126 }
127 }
128
129 /**
119 * Base class containing most summary tests. This allows summary tests to be 130 * Base class containing most summary tests. This allows summary tests to be
120 * re-used to exercise all the different ways in which summaries can be 131 * re-used to exercise all the different ways in which summaries can be
121 * generated (e.g. direct from the AST, from the element model, from a 132 * generated (e.g. direct from the AST, from the element model, from a
122 * "relinking" process, etc.) 133 * "relinking" process, etc.)
123 */ 134 */
124 abstract class SummaryTest { 135 abstract class SummaryTest {
125 /** 136 /**
126 * A test will set this to `true` if it contains `import`, `export`, or 137 * A test will set this to `true` if it contains `import`, `export`, or
127 * `part` declarations that deliberately refer to non-existent files. 138 * `part` declarations that deliberately refer to non-existent files.
128 */ 139 */
(...skipping 7412 matching lines...) Expand 10 before | Expand all | Expand 10 after
7541 class _PrefixExpectation { 7552 class _PrefixExpectation {
7542 final ReferenceKind kind; 7553 final ReferenceKind kind;
7543 final String name; 7554 final String name;
7544 final String absoluteUri; 7555 final String absoluteUri;
7545 final String relativeUri; 7556 final String relativeUri;
7546 final int numTypeParameters; 7557 final int numTypeParameters;
7547 7558
7548 _PrefixExpectation(this.kind, this.name, 7559 _PrefixExpectation(this.kind, this.name,
7549 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0}); 7560 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0});
7550 } 7561 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698