Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 /// Test of element diff. | |
| 6 library trydart.diff_test; | |
| 7 | |
| 8 import 'dart:async' show | |
| 9 Future; | |
| 10 | |
| 11 import 'package:expect/expect.dart' show | |
| 12 Expect; | |
| 13 | |
| 14 import 'package:async_helper/async_helper.dart' show | |
| 15 asyncTest; | |
| 16 | |
| 17 import 'package:compiler/implementation/dart2jslib.dart' show | |
| 18 Compiler, | |
| 19 Script; | |
| 20 | |
| 21 import 'package:compiler/implementation/source_file.dart' show | |
| 22 StringSourceFile; | |
| 23 | |
| 24 import 'package:compiler/implementation/elements/elements.dart' show | |
| 25 Element, | |
| 26 LibraryElement; | |
| 27 | |
| 28 import 'package:try/poi/diff.dart' show | |
| 29 Difference, | |
| 30 computeDifference; | |
| 31 | |
| 32 import '../../compiler/dart2js/compiler_helper.dart' show | |
| 33 MockCompiler, | |
| 34 compilerFor; | |
| 35 | |
| 36 final TEST_DATA = [ | |
| 37 { | |
| 38 'beforeSource': 'main() {}', | |
| 39 'afterSource': 'main() { var x; }', | |
| 40 'expectations': [['main', 'main']], | |
| 41 }, | |
| 42 { | |
| 43 'beforeSource': 'main() {}', | |
| 44 'afterSource': 'main() { /* ignored */ }', | |
| 45 'expectations': [], | |
| 46 }, | |
| 47 { | |
| 48 'beforeSource': 'main() {}', | |
| 49 'afterSource': 'main() { }', | |
| 50 'expectations': [], | |
| 51 }, | |
| 52 { | |
| 53 'beforeSource': 'var i; main() {}', | |
| 54 'afterSource': 'main() { } var i;', | |
| 55 'expectations': [], | |
|
Johnni Winther
2014/08/26 12:31:03
Nice capability!
ahe
2014/08/26 13:01:33
Thanks for noticing :-)
| |
| 56 }, | |
| 57 { | |
| 58 'beforeSource': 'main() {}', | |
| 59 'afterSource': '', | |
| 60 'expectations': [['main', null]], | |
| 61 }, | |
| 62 { | |
| 63 'beforeSource': '', | |
| 64 'afterSource': 'main() {}', | |
| 65 'expectations': [[null, 'main']], | |
| 66 }, | |
| 67 ]; | |
| 68 | |
| 69 const String SCHEME = 'org.trydart.diff-test'; | |
| 70 | |
| 71 Uri customUri(String path) => Uri.parse('$SCHEME://$path'); | |
| 72 | |
| 73 Future<List<Difference>> testDifference( | |
| 74 String beforeSource, | |
| 75 String afterSource) { | |
| 76 Uri scriptUri = customUri('main.dart'); | |
| 77 MockCompiler compiler = compilerFor(beforeSource, scriptUri); | |
| 78 | |
| 79 Future<LibraryElement> future = compiler.libraryLoader.loadLibrary(scriptUri); | |
| 80 return future.then((LibraryElement library) { | |
| 81 Script sourceScript = new Script( | |
| 82 scriptUri, scriptUri, new StringSourceFile('$scriptUri', afterSource)); | |
| 83 var dartPrivacyIsBroken = compiler.libraryLoader; | |
| 84 LibraryElement newLibrary = dartPrivacyIsBroken.createLibrarySync( | |
| 85 null, sourceScript, scriptUri); | |
| 86 return computeDifference(library, newLibrary); | |
| 87 }); | |
| 88 } | |
| 89 | |
| 90 Future testData(Map data) { | |
| 91 String beforeSource = data['beforeSource']; | |
| 92 String afterSource = data['afterSource']; | |
| 93 List expectations = data['expectations']; | |
| 94 | |
| 95 validate(List<Difference> differences) { | |
| 96 return checkExpectations(expectations, differences); | |
| 97 } | |
| 98 | |
| 99 return testDifference(beforeSource, afterSource).then(validate); | |
| 100 } | |
| 101 | |
| 102 String elementNameOrNull(Element element) { | |
| 103 return element == null ? null : element.name; | |
| 104 } | |
| 105 | |
| 106 checkExpectations(List expectations, List<Difference> differences) { | |
| 107 Iterator iterator = expectations.iterator; | |
| 108 for (Difference difference in differences) { | |
| 109 Expect.isTrue(iterator.moveNext()); | |
| 110 List expectation = iterator.current; | |
| 111 String expectedBeforeName = expectation[0]; | |
| 112 String expectedAfterName = expectation[1]; | |
| 113 Expect.stringEquals( | |
| 114 expectedBeforeName, elementNameOrNull(difference.before)); | |
| 115 Expect.stringEquals(expectedAfterName, elementNameOrNull(difference.after)); | |
| 116 print(difference); | |
| 117 } | |
| 118 Expect.isFalse(iterator.moveNext()); | |
| 119 } | |
| 120 | |
| 121 void main() { | |
| 122 asyncTest(() => Future.forEach(TEST_DATA, testData)); | |
| 123 } | |
| OLD | NEW |