OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 // Helper to test compilation equivalence between source and .dill based | 5 // Helper to test compilation equivalence between source and .dill based |
6 // compilation. | 6 // compilation. |
7 library dart2js.kernel.compile_from_dill_test_helper; | 7 library dart2js.kernel.compile_from_dill_test_helper; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
11 | 11 |
12 import 'package:compiler/compiler_new.dart'; | 12 import 'package:compiler/compiler_new.dart'; |
13 import 'package:compiler/src/commandline_options.dart'; | 13 import 'package:compiler/src/commandline_options.dart'; |
14 import 'package:compiler/src/common.dart'; | 14 import 'package:compiler/src/common.dart'; |
15 import 'package:compiler/src/compiler.dart'; | 15 import 'package:compiler/src/compiler.dart'; |
16 import 'package:compiler/src/elements/elements.dart'; | 16 import 'package:compiler/src/elements/elements.dart'; |
17 import 'package:compiler/src/elements/types.dart'; | 17 import 'package:compiler/src/elements/types.dart'; |
18 import 'package:compiler/src/kernel/element_map.dart'; | 18 import 'package:compiler/src/kernel/element_map.dart'; |
19 import 'package:compiler/src/kernel/kernel_strategy.dart'; | 19 import 'package:compiler/src/kernel/kernel_strategy.dart'; |
20 import 'package:compiler/src/serialization/equivalence.dart'; | 20 import 'package:compiler/src/serialization/equivalence.dart'; |
21 import 'package:compiler/src/resolution/enum_creator.dart'; | 21 import 'package:compiler/src/resolution/enum_creator.dart'; |
22 import 'package:compiler/src/universe/world_builder.dart'; | 22 import 'package:compiler/src/universe/world_builder.dart'; |
23 import 'package:compiler/src/world.dart'; | 23 import 'package:compiler/src/world.dart'; |
24 import 'package:expect/expect.dart'; | 24 import 'package:expect/expect.dart'; |
25 import '../memory_compiler.dart'; | 25 import '../memory_compiler.dart'; |
26 import '../equivalence/check_functions.dart'; | 26 import '../equivalence/check_functions.dart'; |
27 import '../equivalence/check_helpers.dart'; | 27 import '../equivalence/check_helpers.dart'; |
28 import '../serialization/helper.dart'; | |
28 import 'test_helpers.dart'; | 29 import 'test_helpers.dart'; |
29 | 30 |
30 import 'compiler_helper.dart'; | 31 import 'compiler_helper.dart'; |
31 | 32 |
32 const SOURCE = const { | 33 class Test { |
33 'main.dart': ''' | 34 final Uri uri; |
35 final Map<String, String> sources; | |
36 final bool expectIdenticalOutput; | |
37 | |
38 const Test(this.sources, {this.expectIdenticalOutput: true}) : uri = null; | |
39 | |
40 Test.fromUri(this.uri, {this.expectIdenticalOutput: true}) | |
41 : sources = const {}; | |
42 | |
43 Uri get entryPoint => uri ?? Uri.parse('memory:main.dart'); | |
44 | |
45 String toString() => uri != null ? '$uri' : sources.values.first; | |
46 } | |
47 | |
48 const List<Test> TESTS = const <Test>[ | |
49 const Test(const { | |
50 'main.dart': ''' | |
34 import 'dart:html'; | 51 import 'dart:html'; |
35 | 52 |
36 foo({named}) => 1; | 53 foo({named}) => 1; |
37 bar(a) => !a; | 54 bar(a) => !a; |
38 class Class { | 55 class Class { |
39 var field; | 56 var field; |
40 static var staticField; | 57 static var staticField; |
41 | 58 |
42 Class(); | 59 Class(); |
43 Class.named(this.field); | 60 Class.named(this.field); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 x = i; | 97 x = i; |
81 if (i == 7) break; | 98 if (i == 7) break; |
82 i++; | 99 i++; |
83 } | 100 } |
84 for (var v in [3, 5]) { | 101 for (var v in [3, 5]) { |
85 if (v == 5) continue; | 102 if (v == 5) continue; |
86 x = v; | 103 x = v; |
87 if (v == 7) break; | 104 if (v == 7) break; |
88 } | 105 } |
89 do { | 106 do { |
90 // TODO(johnniwinther): Support js ast equivalence to handle label name | |
91 // mismatches. Enable the continue test: | |
92 //if (i == 5) continue; | |
Siggi Cherem (dart-lang)
2017/06/30 22:24:01
did you mean to keep the `if (i == 5) continue;` s
Johnni Winther
2017/07/03 08:01:02
No. It requires labels which is why it is move the
| |
93 x = i; | 107 x = i; |
94 if (i == 7) break; | 108 if (i == 7) break; |
95 i++; | 109 i++; |
96 } while (i < 10); | 110 } while (i < 10); |
97 switch (x) { | 111 switch (x) { |
98 case 0: | 112 case 0: |
99 x = 7; | 113 x = 7; |
100 break; | 114 break; |
101 case 1: | 115 case 1: |
102 x = 9; | 116 x = 9; |
103 break; | 117 break; |
104 default: | 118 default: |
105 x = 11; | 119 x = 11; |
106 break; | 120 break; |
107 } | 121 } |
108 x = toplevel; | 122 x = toplevel; |
109 print(x); | 123 print(x); |
110 return x; | 124 return x; |
111 } | 125 } |
112 ''' | 126 ''' |
113 }; | 127 }), |
128 const Test(const { | |
129 'main.dart': ''' | |
130 | |
131 main() { | |
132 var x; | |
133 int i = 0; | |
134 do { | |
135 if (i == 5) continue; | |
136 x = i; | |
137 if (i == 7) break; | |
138 i++; | |
139 } while (i < 10); | |
140 print(x); | |
141 } | |
142 ''' | |
143 }, expectIdenticalOutput: false), | |
144 ]; | |
114 | 145 |
115 enum ResultKind { crashes, errors, warnings, success, failure } | 146 enum ResultKind { crashes, errors, warnings, success, failure } |
116 | 147 |
117 const List<String> commonOptions = const <String>[ | 148 const List<String> commonOptions = const <String>[ |
118 Flags.disableTypeInference, | 149 Flags.disableTypeInference, |
119 Flags.disableInlining, | 150 Flags.disableInlining, |
120 Flags.enableAssertMessage | 151 Flags.enableAssertMessage |
121 ]; | 152 ]; |
122 | 153 |
154 Future runTests(List<String> args, | |
155 {bool skipWarnings: false, | |
156 bool skipErrors: false, | |
157 List<String> options: const <String>[]}) async { | |
158 Arguments arguments = new Arguments.from(args); | |
159 List<Test> tests; | |
160 if (arguments.uri != null) { | |
161 tests = <Test>[new Test.fromUri(arguments.uri)]; | |
162 } else { | |
163 tests = TESTS; | |
164 } | |
165 for (Test test in tests) { | |
166 if (test.uri != null) { | |
167 print('--- running test uri ${test.uri} -------------------------------'); | |
168 } else { | |
169 print( | |
170 '--- running test code -------------------------------------------'); | |
171 print(test.sources.values.first); | |
172 print('----------------------------------------------------------------'); | |
173 } | |
174 await runTest(test.entryPoint, test.sources, | |
175 verbose: arguments.verbose, | |
176 skipWarnings: skipWarnings, | |
177 skipErrors: skipErrors, | |
178 options: options, | |
179 expectIdenticalOutput: test.expectIdenticalOutput); | |
180 } | |
181 } | |
182 | |
123 Future<ResultKind> runTest( | 183 Future<ResultKind> runTest( |
124 Uri entryPoint, Map<String, String> memorySourceFiles, | 184 Uri entryPoint, Map<String, String> memorySourceFiles, |
125 {bool skipWarnings: false, | 185 {bool skipWarnings: false, |
126 bool skipErrors: false, | 186 bool skipErrors: false, |
127 bool verbose: false, | 187 bool verbose: false, |
128 List<String> options: const <String>[]}) async { | 188 List<String> options: const <String>[], |
189 bool expectIdenticalOutput: true}) async { | |
129 enableDebugMode(); | 190 enableDebugMode(); |
130 EnumCreator.matchKernelRepresentationForTesting = true; | 191 EnumCreator.matchKernelRepresentationForTesting = true; |
131 Elements.usePatchedDart2jsSdkSorting = true; | 192 Elements.usePatchedDart2jsSdkSorting = true; |
132 | 193 |
133 Directory dir = await Directory.systemTemp.createTemp('dart2js-with-dill'); | 194 Directory dir = await Directory.systemTemp.createTemp('dart2js-with-dill'); |
134 print('--- create temp directory $dir -------------------------------'); | 195 print('--- create temp directory $dir -------------------------------'); |
135 memorySourceFiles.forEach((String name, String source) { | 196 memorySourceFiles.forEach((String name, String source) { |
136 new File.fromUri(dir.uri.resolve(name)).writeAsStringSync(source); | 197 new File.fromUri(dir.uri.resolve(name)).writeAsStringSync(source); |
137 }); | 198 }); |
138 entryPoint = dir.uri.resolve(entryPoint.path); | 199 entryPoint = dir.uri.resolve(entryPoint.path); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 elementFilter: elementFilter, | 274 elementFilter: elementFilter, |
214 verbose: verbose); | 275 verbose: verbose); |
215 | 276 |
216 checkEmitters(compiler1.backend.emitter, compiler2.backend.emitter, | 277 checkEmitters(compiler1.backend.emitter, compiler2.backend.emitter, |
217 elementEquivalence: (a, b) => equivalence.entityEquivalence(a, b), | 278 elementEquivalence: (a, b) => equivalence.entityEquivalence(a, b), |
218 typeEquivalence: (DartType a, DartType b) { | 279 typeEquivalence: (DartType a, DartType b) { |
219 return equivalence.typeEquivalence(unalias(a), b); | 280 return equivalence.typeEquivalence(unalias(a), b); |
220 }, | 281 }, |
221 verbose: verbose); | 282 verbose: verbose); |
222 | 283 |
223 print('--- checking output------- -----------------------------------------'); | 284 checkGeneratedCode(compiler1.backend, compiler2.backend, |
224 collector1.outputMap | 285 elementEquivalence: (a, b) => equivalence.entityEquivalence(a, b)); |
225 .forEach((OutputType outputType, Map<String, BufferedOutputSink> map1) { | 286 |
226 if (outputType == OutputType.sourceMap) { | 287 if (expectIdenticalOutput) { |
227 // TODO(johnniwinther): Support source map from .dill. | 288 print('--- checking output------- ---------------------------------------'); |
228 return; | 289 collector1.outputMap |
229 } | 290 .forEach((OutputType outputType, Map<String, BufferedOutputSink> map1) { |
230 Map<String, BufferedOutputSink> map2 = collector2.outputMap[outputType]; | 291 if (outputType == OutputType.sourceMap) { |
231 checkSets(map1.keys, map2.keys, 'output', equality); | 292 // TODO(johnniwinther): Support source map from .dill. |
232 map1.forEach((String name, BufferedOutputSink output1) { | 293 return; |
233 BufferedOutputSink output2 = map2[name]; | 294 } |
234 Expect.stringEquals(output1.text, output2.text); | 295 Map<String, BufferedOutputSink> map2 = collector2.outputMap[outputType]; |
296 checkSets(map1.keys, map2.keys, 'output', equality); | |
297 map1.forEach((String name, BufferedOutputSink output1) { | |
298 BufferedOutputSink output2 = map2[name]; | |
299 Expect.stringEquals(output1.text, output2.text); | |
300 }); | |
235 }); | 301 }); |
236 }); | 302 } |
237 return ResultKind.success; | 303 return ResultKind.success; |
238 } | 304 } |
OLD | NEW |