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

Side by Side Diff: tests/compiler/dart2js/serialization_analysis_test.dart

Issue 1192103002: Support serialization of the compiler backbone. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart2js.serialization_analysis_test;
6
7 import 'dart:async';
8 import 'package:async_helper/async_helper.dart';
9 import 'package:expect/expect.dart';
10 import 'package:compiler/src/elements/elements.dart';
11 import 'package:compiler/src/serialization/serialization.dart';
12 import 'package:compiler/src/serialization/json_serializer.dart';
13 import 'package:compiler/src/serialization/task.dart';
14 import 'package:compiler/src/dart2jslib.dart';
15 import 'package:compiler/src/filenames.dart';
16 import 'memory_compiler.dart';
17
18 const List<Test> TESTS = const <Test>[
19 const Test(const {
20 'main.dart': 'main() => print("Hello World");'
21 }),
22 const Test(const {
floitsch 2015/06/26 20:54:06 empty lines between tests. Here and in the rest of
Johnni Winther 2015/07/03 12:13:45 Done.
23 'main.dart': 'main() => print("Hello World", 0);'
24 },
25 expectedWarningCount: 1,
26 expectedInfoCount: 1),
27
28 const Test(const {
29 'main.dart': r'''
30 main() {
31 String text = "Hello World";
32 print('$text');
33 }'''
34 }),
35 const Test(const {
36 'main.dart': r'''
37 main() {
38 String text = "Hello World";
39 print('$text', text);
40 }'''
41 },
42 expectedWarningCount: 1,
43 expectedInfoCount: 1),
44
45 const Test(const {
46 'main.dart': r'''
47 main(List<String> arguments) {
48 print(arguments);
49 }'''
50 }),
51 const Test(const {
52 'main.dart': r'''
53 main(List<String> arguments) {
54 for (int i = 0; i < arguments.length; i++) {
55 print(arguments[i]);
56 }
57 }'''
58 }),
59 const Test(const {
60 'main.dart': r'''
61 main(List<String> arguments) {
62 for (String argument in arguments) {
63 print(argument);
64 }
65 }'''
66 }),
67
68 const Test(const {
69 'main.dart': r'''
70 class Class {}
71 main() {
72 print(new Class());
73 }'''
74 }),
75
76 const Test(const {
77 'main.dart': r'''
78 class Class implements Function {}
79 main() {
80 print(new Class());
81 }'''
82 },
83 expectedWarningCount: 1),
84 const Test(const {
85 'main.dart': r'''
86 class Class implements Function {
87 call() {}
88 }
89 main() {
90 print(new Class()());
91 }'''
92 }),
93
94 const Test(const {
95 'main.dart': r'''
96 class Class implements Comparable<Class> {
97 int compareTo(Class other) => 0;
98 }
99 main() {
100 print(new Class());
101 }'''
102 }),
103 const Test(const {
104 'main.dart': r'''
105 class Class implements Comparable<Class, Class> {
106 int compareTo(other) => 0;
107 }
108 main() {
109 print(new Class());
110 }'''
111 },
112 expectedWarningCount: 1),
113 const Test(const {
114 'main.dart': r'''
115 class Class implements Comparable<Class> {
116 int compareTo(String other) => 0;
117 }
118 main() {
119 print(new Class().compareTo(null));
120 }'''
121 },
122 expectedWarningCount: 1,
123 expectedInfoCount: 1),
124 const Test(const {
125 'main.dart': r'''
126 class Class implements Comparable {
127 bool compareTo(a, b) => true;
128 }
129 main() {
130 print(new Class().compareTo(null, null));
131 }'''
132 },
133 expectedWarningCount: 1,
134 expectedInfoCount: 1),
135 ];
136
137 main(List<String> arguments) {
138 asyncTest(() async {
139 String serializedData = await serializeDartCore();
140
141 if (arguments.isNotEmpty) {
142 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last));
143 await analyze(serializedData, entryPoint, null);
144 } else {
145 Uri entryPoint = Uri.parse('memory:main.dart');
146 for (Test test in TESTS) {
147 await analyze(serializedData, entryPoint, test);
148 }
149 }
150 });
151 }
152
153 class Test {
154 final Map sourceFiles;
155 final int expectedErrorCount;
156 final int expectedWarningCount;
157 final int expectedHintCount;
158 final int expectedInfoCount;
159
160 const Test(this.sourceFiles, {
161 this.expectedErrorCount: 0,
162 this.expectedWarningCount: 0,
163 this.expectedHintCount: 0,
164 this.expectedInfoCount: 0});
165 }
166
167 Future analyze(String serializedData, Uri entryPoint, Test test) async {
168 Deserializer deserializer = new Deserializer.fromText(
169 serializedData, const JsonSerializationDecoder());
170 DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
171 Compiler compiler = compilerFor(
172 test != null ? test.sourceFiles : const {},
173 options: ['--analyze-only', '--output-type=dart'],
174 diagnosticHandler: diagnosticCollector);
175 compiler.serialization.deserializer = new _DeserializerSystem(deserializer);
176 await compiler.runCompiler(entryPoint);
177 if (test != null) {
178 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length,
179 "Unexpected error count.");
180 Expect.equals(
181 test.expectedWarningCount,
182 diagnosticCollector.warnings.length,
183 "Unexpected warning count.");
184 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length,
185 "Unexpected hint count.");
186 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length,
187 "Unexpected info count.");
188 }
189 }
190
191 Future<String> serializeDartCore() async {
192 Compiler compiler = compilerFor({},
193 options: ['--analyze-all', '--output-type=dart']);
194 await compiler.runCompiler(Uri.parse('dart:core'));
195 return serialize(compiler.libraryLoader.libraries);
196 }
197
198 String serialize(Iterable<LibraryElement> libraries) {
199 Serializer serializer = new Serializer(const JsonSerializationEncoder());
200 for (LibraryElement library in libraries) {
201 serializer.serialize(library);
202 }
203 return serializer.toText();
204 }
205
206 class _DeserializerSystem extends DeserializerSystem {
207 final Deserializer _deserializer;
208 final List<LibraryElement> deserializedLibraries = <LibraryElement>[];
209
210 _DeserializerSystem(this._deserializer);
211
212 LibraryElement readLibrary(Uri resolvedUri) {
213 LibraryElement library = _deserializer.lookupLibrary(resolvedUri);
214 if (library != null) {
215 deserializedLibraries.add(library);
216 }
217 return library;
218 }
219
220 @override
221 WorldImpact getWorldImpact(Element element) {
222 return const WorldImpact();
223 }
224
225 @override
226 bool isDeserialized(Element element) {
227 return deserializedLibraries.contains(element.library);
228 }
229 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698