Chromium Code Reviews| 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 dart2js.serialization_test_helper; | 5 library dart2js.serialization_test_helper; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'package:compiler/src/common/resolution.dart'; | 8 import 'package:compiler/src/common/resolution.dart'; |
| 9 import 'package:compiler/src/constants/expressions.dart'; | 9 import 'package:compiler/src/constants/expressions.dart'; |
| 10 import 'package:compiler/src/constants/values.dart'; | |
| 10 import 'package:compiler/src/dart_types.dart'; | 11 import 'package:compiler/src/dart_types.dart'; |
| 11 import 'package:compiler/src/compiler.dart'; | 12 import 'package:compiler/src/compiler.dart'; |
| 12 import 'package:compiler/src/elements/elements.dart'; | 13 import 'package:compiler/src/elements/elements.dart'; |
| 13 import 'package:compiler/src/serialization/equivalence.dart'; | 14 import 'package:compiler/src/serialization/equivalence.dart'; |
| 14 import 'package:compiler/src/tree/nodes.dart'; | 15 import 'package:compiler/src/tree/nodes.dart'; |
| 15 import 'package:expect/expect.dart'; | 16 import 'package:expect/expect.dart'; |
| 16 | 17 |
| 17 Check currentCheck; | 18 Check currentCheck; |
| 18 | 19 |
| 19 class Check { | 20 class Check { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 @override | 81 @override |
| 81 bool testSets( | 82 bool testSets( |
| 82 var object1, var object2, String property, | 83 var object1, var object2, String property, |
| 83 Iterable set1, Iterable set2, | 84 Iterable set1, Iterable set2, |
| 84 [bool elementEquivalence(a, b) = equality]) { | 85 [bool elementEquivalence(a, b) = equality]) { |
| 85 return checkSetEquivalence( | 86 return checkSetEquivalence( |
| 86 object1, object2,property, set1, set2, elementEquivalence); | 87 object1, object2,property, set1, set2, elementEquivalence); |
| 87 } | 88 } |
| 88 | 89 |
| 89 @override | 90 @override |
| 91 bool testMaps( | |
| 92 var object1, var object2, String property, Map map1, Map map2, | |
| 93 [bool keyEquivalence(a, b) = equality, | |
| 94 bool valueEquivalence(a, b) = equality]) { | |
| 95 return checkMapEquivalence(object1, object2, property, | |
| 96 map1, map2, keyEquivalence, valueEquivalence); | |
| 97 } | |
| 98 | |
| 99 @override | |
| 90 bool testElements( | 100 bool testElements( |
| 91 Object object1, Object object2, String property, | 101 Object object1, Object object2, String property, |
| 92 Element element1, Element element2) { | 102 Element element1, Element element2) { |
| 93 return checkElementIdentities( | 103 return checkElementIdentities( |
| 94 object1, object2, property, element1, element2); | 104 object1, object2, property, element1, element2); |
| 95 } | 105 } |
| 96 | 106 |
| 97 @override | 107 @override |
| 98 bool testTypes( | 108 bool testTypes( |
| 99 Object object1, Object object2, String property, | 109 Object object1, Object object2, String property, |
| 100 DartType type1, DartType type2) { | 110 DartType type1, DartType type2) { |
| 101 return checkTypes(object1, object2, property, type1, type2); | 111 return checkTypes(object1, object2, property, type1, type2); |
| 102 } | 112 } |
| 103 | 113 |
| 104 @override | 114 @override |
| 105 bool testConstants( | 115 bool testConstants( |
| 106 Object object1, Object object2, String property, | 116 Object object1, Object object2, String property, |
| 107 ConstantExpression exp1, ConstantExpression exp2) { | 117 ConstantExpression exp1, ConstantExpression exp2) { |
| 108 return checkConstants(object1, object2, property, exp1, exp2); | 118 return checkConstants(object1, object2, property, exp1, exp2); |
| 109 } | 119 } |
| 110 | 120 |
| 111 @override | 121 @override |
| 122 bool testConstantValues(Object object1, Object object2, String property, | |
| 123 ConstantValue value1, ConstantValue value2) { | |
| 124 return areConstantValuesEquivalent(value1, value2); | |
| 125 } | |
| 126 | |
| 127 @override | |
| 112 bool testTypeLists( | 128 bool testTypeLists( |
| 113 Object object1, Object object2, String property, | 129 Object object1, Object object2, String property, |
| 114 List<DartType> list1, List<DartType> list2) { | 130 List<DartType> list1, List<DartType> list2) { |
| 115 return checkTypeLists(object1, object2, property, list1, list2); | 131 return checkTypeLists(object1, object2, property, list1, list2); |
| 116 } | 132 } |
| 117 | 133 |
| 118 @override | 134 @override |
| 119 bool testConstantLists( | 135 bool testConstantLists( |
| 120 Object object1, Object object2, String property, | 136 Object object1, Object object2, String property, |
| 121 List<ConstantExpression> list1, | 137 List<ConstantExpression> list1, |
| 122 List<ConstantExpression> list2) { | 138 List<ConstantExpression> list2) { |
| 123 return checkConstantLists(object1, object2, property, list1, list2); | 139 return checkConstantLists(object1, object2, property, list1, list2); |
| 124 } | 140 } |
| 125 | 141 |
| 126 @override | 142 @override |
| 143 bool testConstantValueLists(Object object1, Object object2, String property, | |
| 144 List<ConstantValue> list1, List<ConstantValue> list2) { | |
| 145 return checkConstantValueLists(object1, object2, property, list1, list2); | |
| 146 } | |
| 147 | |
| 148 @override | |
| 127 bool testNodes(Object object1, Object object2, String property, | 149 bool testNodes(Object object1, Object object2, String property, |
| 128 Node node1, Node node2) { | 150 Node node1, Node node2) { |
| 129 return new NodeEquivalenceVisitor(this).testNodes( | 151 return new NodeEquivalenceVisitor(this).testNodes( |
| 130 object1, object2, property, node1, node2); | 152 object1, object2, property, node1, node2); |
| 131 } | 153 } |
| 154 | |
|
Harry Terkelsen
2016/08/01 17:42:23
nit: unnecessary blank lines
Johnni Winther
2016/08/08 11:38:33
Done.
| |
| 155 | |
| 132 } | 156 } |
| 133 | 157 |
| 134 /// Check that the values [property] of [object1] and [object2], [value1] and | 158 /// Check that the values [property] of [object1] and [object2], [value1] and |
| 135 /// [value2] respectively, are equal and throw otherwise. | 159 /// [value2] respectively, are equal and throw otherwise. |
| 136 bool check(var object1, var object2, String property, var value1, var value2, | 160 bool check(var object1, var object2, String property, var value1, var value2, |
| 137 [bool equivalence(a, b) = equality]) { | 161 [bool equivalence(a, b) = equality]) { |
| 138 currentCheck = new Check( | 162 currentCheck = new Check( |
| 139 currentCheck, object1, object2, property, value1, value2); | 163 currentCheck, object1, object2, property, value1, value2); |
| 140 if (!equivalence(value1, value2)) { | 164 if (!equivalence(value1, value2)) { |
| 141 throw currentCheck; | 165 throw currentCheck; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 String message = | 263 String message = |
| 240 "Set mismatch for `$property` on $object1 vs $object2: \n" | 264 "Set mismatch for `$property` on $object1 vs $object2: \n" |
| 241 "Common:\n ${common.join('\n ')}\n" | 265 "Common:\n ${common.join('\n ')}\n" |
| 242 "Unfound:\n ${unfound.join('\n ')}\n" | 266 "Unfound:\n ${unfound.join('\n ')}\n" |
| 243 "Extra: \n ${remaining.join('\n ')}"; | 267 "Extra: \n ${remaining.join('\n ')}"; |
| 244 throw message; | 268 throw message; |
| 245 } | 269 } |
| 246 return true; | 270 return true; |
| 247 } | 271 } |
| 248 | 272 |
| 273 /// Check equivalence of the two iterables, [set1] and [set1], as sets using | |
| 274 /// [elementEquivalence] to compute the pair-wise equivalence. | |
| 275 /// | |
| 276 /// Uses [object1], [object2] and [property] to provide context for failures. | |
| 277 bool checkMapEquivalence( | |
| 278 var object1, | |
| 279 var object2, | |
| 280 String property, | |
| 281 Map map1, | |
| 282 Map map2, | |
| 283 bool sameKey(a, b), | |
| 284 bool sameValue(a, b)) { | |
| 285 List<List> common = <List>[]; | |
| 286 List unfound = []; | |
| 287 Set remaining = | |
| 288 computeSetDifference(map1.keys, map2.keys, common, unfound, | |
| 289 sameElement: sameKey); | |
| 290 if (unfound.isNotEmpty || remaining.isNotEmpty) { | |
| 291 String message = | |
| 292 "Map key mismatch for `$property` on $object1 vs $object2: \n" | |
| 293 "Common:\n ${common.join('\n ')}\n" | |
| 294 "Unfound:\n ${unfound.join('\n ')}\n" | |
| 295 "Extra: \n ${remaining.join('\n ')}"; | |
| 296 throw message; | |
| 297 } | |
| 298 for (List pair in common) { | |
| 299 check(object1, object2, 'Map value for `$property`', | |
| 300 map1[pair[0]], map2[pair[1]], sameValue); | |
| 301 } | |
| 302 return true; | |
| 303 } | |
| 304 | |
| 249 /// Checks the equivalence of the identity (but not properties) of [element1] | 305 /// Checks the equivalence of the identity (but not properties) of [element1] |
| 250 /// and [element2]. | 306 /// and [element2]. |
| 251 /// | 307 /// |
| 252 /// Uses [object1], [object2] and [property] to provide context for failures. | 308 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 253 bool checkElementIdentities( | 309 bool checkElementIdentities( |
| 254 Object object1, Object object2, String property, | 310 Object object1, Object object2, String property, |
| 255 Element element1, Element element2) { | 311 Element element1, Element element2) { |
| 256 if (identical(element1, element2)) return true; | 312 if (identical(element1, element2)) return true; |
| 257 return check(object1, object2, | 313 return check(object1, object2, |
| 258 property, element1, element2, areElementsEquivalent); | 314 property, element1, element2, areElementsEquivalent); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 ConstantExpression exp1, ConstantExpression exp2) { | 359 ConstantExpression exp1, ConstantExpression exp2) { |
| 304 if (identical(exp1, exp2)) return true; | 360 if (identical(exp1, exp2)) return true; |
| 305 if (exp1 == null || exp2 == null) { | 361 if (exp1 == null || exp2 == null) { |
| 306 return check(object1, object2, property, exp1, exp2); | 362 return check(object1, object2, property, exp1, exp2); |
| 307 } else { | 363 } else { |
| 308 return check(object1, object2, property, exp1, exp2, | 364 return check(object1, object2, property, exp1, exp2, |
| 309 (a, b) => const ConstantEquivalence(const CheckStrategy()).visit(a, b)); | 365 (a, b) => const ConstantEquivalence(const CheckStrategy()).visit(a, b)); |
| 310 } | 366 } |
| 311 } | 367 } |
| 312 | 368 |
| 313 /// Checks the pair-wise equivalence of the contants in [list1] and [list2]. | 369 /// Checks the equivalence of [value1] and [value2]. |
| 370 /// | |
| 371 /// Uses [object1], [object2] and [property] to provide context for failures. | |
| 372 bool checkConstantValues( | |
| 373 Object object1, Object object2, String property, | |
| 374 ConstantValue value1, ConstantValue value2) { | |
| 375 if (identical(value1, value2)) return true; | |
| 376 if (value1 == null || value2 == null) { | |
| 377 return check(object1, object2, property, value1, value2); | |
| 378 } else { | |
| 379 return check(object1, object2, property, value1, value2, | |
| 380 (a, b) => const ConstantValueEquivalence( | |
| 381 const CheckStrategy()).visit(a, b)); | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 /// Checks the pair-wise equivalence of the constants in [list1] and [list2]. | |
| 314 /// | 386 /// |
| 315 /// Uses [object1], [object2] and [property] to provide context for failures. | 387 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 316 bool checkConstantLists( | 388 bool checkConstantLists( |
| 317 Object object1, Object object2, String property, | 389 Object object1, Object object2, String property, |
| 318 List<ConstantExpression> list1, | 390 List<ConstantExpression> list1, |
| 319 List<ConstantExpression> list2) { | 391 List<ConstantExpression> list2) { |
| 320 return checkListEquivalence( | 392 return checkListEquivalence( |
| 321 object1, object2, property, | 393 object1, object2, property, |
| 322 list1, list2, checkConstants); | 394 list1, list2, checkConstants); |
| 323 } | 395 } |
| 324 | 396 |
| 397 /// Checks the pair-wise equivalence of the constants values in [list1] and | |
| 398 /// [list2]. | |
| 399 /// | |
| 400 /// Uses [object1], [object2] and [property] to provide context for failures. | |
| 401 bool checkConstantValueLists( | |
| 402 Object object1, Object object2, String property, | |
| 403 List<ConstantValue> list1, | |
| 404 List<ConstantValue> list2) { | |
| 405 return checkListEquivalence( | |
| 406 object1, object2, property, | |
| 407 list1, list2, checkConstantValues); | |
| 408 } | |
| 325 | 409 |
| 326 /// Check member property equivalence between all members common to [compiler1] | 410 /// Check member property equivalence between all members common to [compiler1] |
| 327 /// and [compiler2]. | 411 /// and [compiler2]. |
| 328 void checkLoadedLibraryMembers( | 412 void checkLoadedLibraryMembers( |
| 329 Compiler compiler1, | 413 Compiler compiler1, |
| 330 Compiler compiler2, | 414 Compiler compiler2, |
| 331 bool hasProperty(Element member1), | 415 bool hasProperty(Element member1), |
| 332 void checkMemberProperties(Compiler compiler1, Element member1, | 416 void checkMemberProperties(Compiler compiler1, Element member1, |
| 333 Compiler compiler2, Element member2, | 417 Compiler compiler2, Element member2, |
| 334 {bool verbose}), | 418 {bool verbose}), |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 } | 515 } |
| 432 | 516 |
| 433 void checkSets( | 517 void checkSets( |
| 434 Iterable set1, | 518 Iterable set1, |
| 435 Iterable set2, | 519 Iterable set2, |
| 436 String messagePrefix, | 520 String messagePrefix, |
| 437 bool sameElement(a, b), | 521 bool sameElement(a, b), |
| 438 {bool failOnUnfound: true, | 522 {bool failOnUnfound: true, |
| 439 bool failOnExtra: true, | 523 bool failOnExtra: true, |
| 440 bool verbose: false, | 524 bool verbose: false, |
| 441 void onSameElement(a, b)}) { | 525 void onSameElement(a, b), |
| 526 void onUnfoundElement(a), | |
| 527 void onExtraElement(b), | |
| 528 String elementToString(key): defaultToString}) { | |
| 442 List<List> common = <List>[]; | 529 List<List> common = <List>[]; |
| 443 List unfound = []; | 530 List unfound = []; |
| 444 Set remaining = computeSetDifference( | 531 Set remaining = computeSetDifference( |
| 445 set1, set2, common, unfound, | 532 set1, set2, common, unfound, |
| 446 sameElement: sameElement, | 533 sameElement: sameElement, |
| 447 checkElements: onSameElement); | 534 checkElements: onSameElement); |
| 535 if (onUnfoundElement != null) { | |
| 536 unfound.forEach(onUnfoundElement); | |
| 537 } | |
| 538 if (onExtraElement != null) { | |
| 539 remaining.forEach(onExtraElement); | |
| 540 } | |
| 448 StringBuffer sb = new StringBuffer(); | 541 StringBuffer sb = new StringBuffer(); |
| 449 sb.write("$messagePrefix:"); | 542 sb.write("$messagePrefix:"); |
| 450 if (verbose) { | 543 if (verbose) { |
| 451 sb.write("\n Common:\n ${common.join('\n ')}"); | 544 sb.write("\n Common: \n"); |
| 545 for (List pair in common) { | |
| 546 var element1 = pair[0]; | |
| 547 var element2 = pair[1]; | |
| 548 sb.write(" [${elementToString(element1)}," | |
| 549 "${elementToString(element2)}]\n"); | |
| 550 } | |
| 452 } | 551 } |
| 453 if (unfound.isNotEmpty || verbose) { | 552 if (unfound.isNotEmpty || verbose) { |
| 454 sb.write("\n Unfound:\n ${unfound.join('\n ')}"); | 553 sb.write("\n Unfound:\n ${unfound.map(elementToString).join('\n ')}"); |
| 455 } | 554 } |
| 456 if (remaining.isNotEmpty || verbose) { | 555 if (remaining.isNotEmpty || verbose) { |
| 457 sb.write("\n Extra: \n ${remaining.join('\n ')}"); | 556 sb.write("\n Extra: \n ${remaining.map(elementToString).join('\n ')}"); |
| 458 } | 557 } |
| 459 String message = sb.toString(); | 558 String message = sb.toString(); |
| 460 if (unfound.isNotEmpty || remaining.isNotEmpty) { | 559 if (unfound.isNotEmpty || remaining.isNotEmpty) { |
| 461 | |
| 462 if ((failOnUnfound && unfound.isNotEmpty) || | 560 if ((failOnUnfound && unfound.isNotEmpty) || |
| 463 (failOnExtra && remaining.isNotEmpty)) { | 561 (failOnExtra && remaining.isNotEmpty)) { |
| 464 Expect.fail(message); | 562 Expect.fail(message); |
| 465 } else { | 563 } else { |
| 466 print(message); | 564 print(message); |
| 467 } | 565 } |
| 468 } else if (verbose) { | 566 } else if (verbose) { |
| 469 print(message); | 567 print(message); |
| 470 } | 568 } |
| 471 } | 569 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 | 680 |
| 583 if (resolvedAst1 == null || resolvedAst2 == null) return; | 681 if (resolvedAst1 == null || resolvedAst2 == null) return; |
| 584 | 682 |
| 585 if (verbose) { | 683 if (verbose) { |
| 586 print('Checking resolved asts for $member1 vs $member2'); | 684 print('Checking resolved asts for $member1 vs $member2'); |
| 587 } | 685 } |
| 588 | 686 |
| 589 testResolvedAstEquivalence( | 687 testResolvedAstEquivalence( |
| 590 resolvedAst1, resolvedAst2, const CheckStrategy()); | 688 resolvedAst1, resolvedAst2, const CheckStrategy()); |
| 591 } | 689 } |
| OLD | NEW |