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

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

Issue 1870133002: Refactor serialization test files. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fix 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 dart2js.serialization_test; 5 library dart2js.serialization_test;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'memory_compiler.dart'; 8 import 'memory_compiler.dart';
9 import 'package:async_helper/async_helper.dart'; 9 import 'package:async_helper/async_helper.dart';
10 import 'package:compiler/src/commandline_options.dart'; 10 import 'package:compiler/src/commandline_options.dart';
11 import 'package:compiler/src/constants/constructors.dart'; 11 import 'package:compiler/src/constants/constructors.dart';
12 import 'package:compiler/src/constants/expressions.dart'; 12 import 'package:compiler/src/constants/expressions.dart';
13 import 'package:compiler/src/dart_types.dart'; 13 import 'package:compiler/src/dart_types.dart';
14 import 'package:compiler/src/compiler.dart'; 14 import 'package:compiler/src/compiler.dart';
15 import 'package:compiler/src/diagnostics/invariant.dart'; 15 import 'package:compiler/src/diagnostics/invariant.dart';
16 import 'package:compiler/src/elements/elements.dart'; 16 import 'package:compiler/src/elements/elements.dart';
17 import 'package:compiler/src/elements/visitor.dart'; 17 import 'package:compiler/src/elements/visitor.dart';
18 import 'package:compiler/src/ordered_typeset.dart'; 18 import 'package:compiler/src/ordered_typeset.dart';
19 import 'package:compiler/src/serialization/element_serialization.dart'; 19 import 'package:compiler/src/serialization/element_serialization.dart';
20 import 'package:compiler/src/serialization/equivalence.dart'; 20 import 'package:compiler/src/serialization/equivalence.dart';
21 import 'package:compiler/src/serialization/json_serializer.dart'; 21 import 'package:compiler/src/serialization/json_serializer.dart';
22 import 'package:compiler/src/serialization/serialization.dart'; 22 import 'package:compiler/src/serialization/serialization.dart';
23 import 'serialization_test_helper.dart';
23 24
24 main(List<String> arguments) { 25 main(List<String> arguments) {
25 // Ensure that we can print out constant expressions. 26 // Ensure that we can print out constant expressions.
26 DEBUG_MODE = true; 27 DEBUG_MODE = true;
27 28
28 Uri entryPoint; 29 Uri entryPoint;
29 String outPath; 30 String outPath;
30 bool prettyPrint = false; 31 bool prettyPrint = false;
31 for (String arg in arguments) { 32 for (String arg in arguments) {
32 if (arg.startsWith('--')) { 33 if (arg.startsWith('--')) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 126
126 /// Check the equivalence of [element1] and [element2] and their properties. 127 /// Check the equivalence of [element1] and [element2] and their properties.
127 /// 128 ///
128 /// Uses [object1], [object2] and [property] to provide context for failures. 129 /// Uses [object1], [object2] and [property] to provide context for failures.
129 checkElementProperties( 130 checkElementProperties(
130 Object object1, object2, String property, 131 Object object1, object2, String property,
131 Element element1, Element element2) { 132 Element element1, Element element2) {
132 const ElementPropertyEquivalence().visit(element1, element2); 133 const ElementPropertyEquivalence().visit(element1, element2);
133 } 134 }
134 135
135 /// Check the equivalence of the two lists of elements, [list1] and [list2].
136 ///
137 /// Uses [object1], [object2] and [property] to provide context for failures.
138 checkElementLists(Object object1, Object object2, String property,
139 Iterable<Element> list1, Iterable<Element> list2) {
140 checkListEquivalence(object1, object2, property,
141 list1, list2, checkElementProperties);
142 }
143
144 /// Check equivalence of the two lists, [list1] and [list2], using
145 /// [checkEquivalence] to check the pair-wise equivalence.
146 ///
147 /// Uses [object1], [object2] and [property] to provide context for failures.
148 bool checkListEquivalence(
149 Object object1, Object object2, String property,
150 Iterable list1, Iterable list2,
151 void checkEquivalence(o1, o2, property, a, b)) {
152 for (int i = 0; i < list1.length && i < list2.length; i++) {
153 checkEquivalence(
154 object1, object2, property,
155 list1.elementAt(i), list2.elementAt(i));
156 }
157 for (int i = list1.length; i < list2.length; i++) {
158 throw
159 'Missing equivalent for element '
160 '#$i ${list2.elementAt(i)} in `${property}` on $object2.\n'
161 '`${property}` on $object1:\n ${list1.join('\n ')}\n'
162 '`${property}` on $object2:\n ${list2.join('\n ')}';
163 }
164 for (int i = list2.length; i < list1.length; i++) {
165 throw
166 'Missing equivalent for element '
167 '#$i ${list1.elementAt(i)} in `${property}` on $object1.\n'
168 '`${property}` on $object1:\n ${list1.join('\n ')}\n'
169 '`${property}` on $object2:\n ${list2.join('\n ')}';
170 }
171 return true;
172 }
173
174 /// Computes the set difference between [set1] and [set2] using
175 /// [elementEquivalence] to determine element equivalence.
176 ///
177 /// Elements both in [set1] and [set2] are added to [common], elements in [set1]
178 /// but not in [set2] are added to [unfound], and the set of elements in [set2]
179 /// but not in [set1] are returned.
180 Set computeSetDifference(
181 Iterable set1,
182 Iterable set2,
183 List common,
184 List unfound,
185 [bool sameElement(a, b) = equality]) {
186 // TODO(johnniwinther): Avoid the quadratic cost here. Some ideas:
187 // - convert each set to a list and sort it first, then compare by walking
188 // both lists in parallel
189 // - map each element to a canonical object, create a map containing those
190 // mappings, use the mapped sets to compare (then operations like
191 // set.difference would work)
192 Set remaining = set2.toSet();
193 for (var element1 in set1) {
194 bool found = false;
195 for (var element2 in remaining) {
196 if (sameElement(element1, element2)) {
197 found = true;
198 remaining.remove(element2);
199 break;
200 }
201 }
202 if (found) {
203 common.add(element1);
204 } else {
205 unfound.add(element1);
206 }
207 }
208 return remaining;
209 }
210
211 /// Check equivalence of the two iterables, [set1] and [set1], as sets using
212 /// [elementEquivalence] to compute the pair-wise equivalence.
213 ///
214 /// Uses [object1], [object2] and [property] to provide context for failures.
215 bool checkSetEquivalence(
216 var object1,
217 var object2,
218 String property,
219 Iterable set1,
220 Iterable set2,
221 bool sameElement(a, b)) {
222 List common = [];
223 List unfound = [];
224 Set remaining =
225 computeSetDifference(set1, set2, common, unfound, sameElement);
226 if (unfound.isNotEmpty || remaining.isNotEmpty) {
227 String message =
228 "Set mismatch for `$property` on $object1 vs $object2: \n"
229 "Common:\n ${common.join('\n ')}\n"
230 "Unfound:\n ${unfound.join('\n ')}\n"
231 "Extra: \n ${remaining.join('\n ')}";
232 throw message;
233 }
234 return true;
235 }
236
237 /// Checks the equivalence of the identity (but not properties) of [element1]
238 /// and [element2].
239 ///
240 /// Uses [object1], [object2] and [property] to provide context for failures.
241 bool checkElementIdentities(
242 Object object1, Object object2, String property,
243 Element element1, Element element2) {
244 if (identical(element1, element2)) return true;
245 if (element1 == null || element2 == null) {
246 return check(object1, object2, property, element1, element2);
247 } else {
248 return const ElementIdentityEquivalence(const CheckStrategy())
249 .visit(element1, element2);
250 }
251 }
252
253 /// Checks the pair-wise equivalence of the identity (but not properties) of the
254 /// elements in [list] and [list2].
255 ///
256 /// Uses [object1], [object2] and [property] to provide context for failures.
257 bool checkElementListIdentities(
258 Object object1, Object object2, String property,
259 Iterable<Element> list1, Iterable<Element> list2) {
260 return checkListEquivalence(
261 object1, object2, property,
262 list1, list2, checkElementIdentities);
263 }
264
265 /// Checks the equivalence of [type1] and [type2].
266 ///
267 /// Uses [object1], [object2] and [property] to provide context for failures.
268 bool checkTypes(
269 Object object1, Object object2, String property,
270 DartType type1, DartType type2) {
271 if (identical(type1, type2)) return true;
272 if (type1 == null || type2 == null) {
273 return check(object1, object2, property, type1, type2);
274 } else {
275 return const TypeEquivalence(const CheckStrategy()).visit(type1, type2);
276 }
277 }
278
279 /// Checks the pair-wise equivalence of the types in [list1] and [list2].
280 ///
281 /// Uses [object1], [object2] and [property] to provide context for failures.
282 bool checkTypeLists(
283 Object object1, Object object2, String property,
284 List<DartType> list1, List<DartType> list2) {
285 return checkListEquivalence(
286 object1, object2, property, list1, list2, checkTypes);
287 }
288
289 /// Checks the equivalence of [exp1] and [exp2].
290 ///
291 /// Uses [object1], [object2] and [property] to provide context for failures.
292 bool checkConstants(
293 Object object1, Object object2, String property,
294 ConstantExpression exp1, ConstantExpression exp2) {
295 if (identical(exp1, exp2)) return true;
296 if (exp1 == null || exp2 == null) {
297 return check(object1, object2, property, exp1, exp2);
298 } else {
299 return const ConstantEquivalence(const CheckStrategy()).visit(exp1, exp2);
300 }
301 }
302
303 /// Checks the pair-wise equivalence of the contants in [list1] and [list2].
304 ///
305 /// Uses [object1], [object2] and [property] to provide context for failures.
306 bool checkConstantLists(
307 Object object1, Object object2, String property,
308 List<ConstantExpression> list1,
309 List<ConstantExpression> list2) {
310 return checkListEquivalence(
311 object1, object2, property,
312 list1, list2, checkConstants);
313 }
314
315
316 /// Strategy for checking equivalence.
317 ///
318 /// Use this strategy to fail early with contextual information in the event of
319 /// inequivalence.
320 class CheckStrategy implements TestStrategy {
321 const CheckStrategy();
322
323 @override
324 bool test(var object1, var object2, String property, var value1, var value2) {
325 return check(object1, object2, property, value1, value2);
326 }
327
328 @override
329 bool testLists(
330 Object object1, Object object2, String property,
331 List list1, List list2,
332 [bool elementEquivalence(a, b) = equality]) {
333 return checkListEquivalence(
334 object1, object2, property, list1, list2,
335 (o1, o2, p, v1, v2) {
336 if (!elementEquivalence(v1, v2)) {
337 throw "$o1.$p = '${v1}' <> "
338 "$o2.$p = '${v2}'";
339 }
340 return true;
341 });
342 }
343
344 @override
345 bool testSets(
346 var object1, var object2, String property,
347 Iterable set1, Iterable set2,
348 [bool elementEquivalence(a, b) = equality]) {
349 return checkSetEquivalence(
350 object1, object2,property, set1, set2, elementEquivalence);
351 }
352
353 @override
354 bool testElements(
355 Object object1, Object object2, String property,
356 Element element1, Element element2) {
357 return checkElementIdentities(
358 object1, object2, property, element1, element2);
359 }
360
361 @override
362 bool testTypes(
363 Object object1, Object object2, String property,
364 DartType type1, DartType type2) {
365 return checkTypes(object1, object2, property, type1, type2);
366 }
367
368 @override
369 bool testConstants(
370 Object object1, Object object2, String property,
371 ConstantExpression exp1, ConstantExpression exp2) {
372 return checkConstants(object1, object2, property, exp1, exp2);
373 }
374
375 @override
376 bool testTypeLists(
377 Object object1, Object object2, String property,
378 List<DartType> list1, List<DartType> list2) {
379 return checkTypeLists(object1, object2, property, list1, list2);
380 }
381
382 @override
383 bool testConstantLists(
384 Object object1, Object object2, String property,
385 List<ConstantExpression> list1,
386 List<ConstantExpression> list2) {
387 return checkConstantLists(object1, object2, property, list1, list2);
388 }
389 }
390
391 /// Checks the equivalence of [constructor1] and [constructor2]. 136 /// Checks the equivalence of [constructor1] and [constructor2].
392 void constantConstructorEquivalence(ConstantConstructor constructor1, 137 void constantConstructorEquivalence(ConstantConstructor constructor1,
393 ConstantConstructor constructor2) { 138 ConstantConstructor constructor2) {
394 const ConstantConstructorEquivalence().visit(constructor1, constructor2); 139 const ConstantConstructorEquivalence().visit(constructor1, constructor2);
395 } 140 }
396 141
397 /// Visitor that checks the equivalence of [ConstantConstructor]s. 142 /// Visitor that checks the equivalence of [ConstantConstructor]s.
398 class ConstantConstructorEquivalence 143 class ConstantConstructorEquivalence
399 extends ConstantConstructorVisitor<dynamic, ConstantConstructor> { 144 extends ConstantConstructorVisitor<dynamic, ConstantConstructor> {
400 const ConstantConstructorEquivalence(); 145 const ConstantConstructorEquivalence();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 constructor1, constructor2, 'defaultValue[$k]', 215 constructor1, constructor2, 'defaultValue[$k]',
471 v, constructor2.defaultValues[k]); 216 v, constructor2.defaultValues[k]);
472 }); 217 });
473 checkConstants( 218 checkConstants(
474 constructor1, constructor2, 'thisConstructorInvocation', 219 constructor1, constructor2, 'thisConstructorInvocation',
475 constructor1.thisConstructorInvocation, 220 constructor1.thisConstructorInvocation,
476 constructor2.thisConstructorInvocation); 221 constructor2.thisConstructorInvocation);
477 } 222 }
478 } 223 }
479 224
480 /// Check that the values [property] of [object1] and [object2], [value1] and 225 /// Check the equivalence of the two lists of elements, [list1] and [list2].
481 /// [value2] respectively, are equal and throw otherwise. 226 ///
482 bool check(var object1, var object2, String property, var value1, value2) { 227 /// Uses [object1], [object2] and [property] to provide context for failures.
483 if (value1 != value2) { 228 checkElementLists(Object object1, Object object2, String property,
484 throw "$object1.$property = '${value1}' <> " 229 Iterable<Element> list1, Iterable<Element> list2) {
485 "$object2.$property = '${value2}'"; 230 checkListEquivalence(object1, object2, property,
486 } 231 list1, list2, checkElementProperties);
487 return true;
488 } 232 }
489 233
490 /// Visitor that checks for equivalence of [Element] properties. 234 /// Visitor that checks for equivalence of [Element] properties.
491 class ElementPropertyEquivalence extends BaseElementVisitor<dynamic, Element> { 235 class ElementPropertyEquivalence extends BaseElementVisitor<dynamic, Element> {
492 const ElementPropertyEquivalence(); 236 const ElementPropertyEquivalence();
493 237
494 void visit(Element element1, Element element2) { 238 void visit(Element element1, Element element2) {
495 if (element1 == null && element2 == null) return; 239 if (element1 == null && element2 == null) return;
496 element1 = element1.declaration; 240 element1 = element1.declaration;
497 element2 = element2.declaration; 241 element2 = element2.declaration;
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 void visitPrefixElement(PrefixElement element1, PrefixElement element2) { 632 void visitPrefixElement(PrefixElement element1, PrefixElement element2) {
889 check( 633 check(
890 element1, element2, 'isDeferred', 634 element1, element2, 'isDeferred',
891 element1.isDeferred, element2.isDeferred); 635 element1.isDeferred, element2.isDeferred);
892 checkElementIdentities( 636 checkElementIdentities(
893 element1, element2, 'importedLibrary', 637 element1, element2, 'importedLibrary',
894 element1.deferredImport, element2.deferredImport); 638 element1.deferredImport, element2.deferredImport);
895 // TODO(johnniwinther): Check members. 639 // TODO(johnniwinther): Check members.
896 } 640 }
897 } 641 }
OLDNEW
« no previous file with comments | « tests/compiler/dart2js/serialization_model_test.dart ('k') | tests/compiler/dart2js/serialization_test_data.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698