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