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 // Helper class for writing compiler tests. | |
6 library trydart.compiler_test_case; | |
7 | |
8 import 'dart:async' show | |
9 Future; | |
10 | |
11 export 'dart:async' show | |
12 Future; | |
13 | |
14 import 'package:async_helper/async_helper.dart' show | |
15 asyncTest; | |
16 | |
17 import '../../compiler/dart2js/mock_compiler.dart' show | |
18 MockCompiler, | |
19 compilerFor; | |
20 | |
21 export 'package:expect/expect.dart' show | |
22 Expect; | |
23 | |
24 import 'package:compiler/src/elements/elements.dart' show | |
25 LibraryElement; | |
26 | |
27 export 'package:compiler/src/elements/elements.dart' show | |
28 LibraryElement; | |
29 | |
30 const String CONSTANT_CLASS = 'class Constant { const Constant(); }'; | |
31 | |
32 const String SCHEME = 'org.trydart.compiler-test-case'; | |
33 | |
34 Uri customUri(String path) => Uri.parse('$SCHEME:/$path'); | |
35 | |
36 Future runTests(List<CompilerTestCase> tests) { | |
37 asyncTest(() => Future.forEach(tests, runTest)); | |
38 } | |
39 | |
40 Future runTest(CompilerTestCase test) { | |
41 print('\n{{{\n$test\n\n=== Test Output:\n'); | |
42 return test.run().then((_) { | |
43 print('}}}'); | |
44 }); | |
45 } | |
46 | |
47 abstract class CompilerTestCase { | |
48 final String source; | |
49 | |
50 final Uri scriptUri; | |
51 | |
52 final MockCompiler compiler; | |
53 | |
54 CompilerTestCase.init(this.source, this.scriptUri, this.compiler); | |
55 | |
56 CompilerTestCase.intermediate(String source, Uri scriptUri) | |
57 : this.init(source, scriptUri, compilerFor(source, scriptUri)); | |
58 | |
59 CompilerTestCase(String source, [String path]) | |
60 : this.intermediate(source, customUri(path == null ? 'main.dart' : path)); | |
61 | |
62 Future<LibraryElement> loadMainApp() { | |
63 return compiler.libraryLoader.loadLibrary(scriptUri) | |
64 .then((LibraryElement library) { | |
65 if (compiler.mainApp == null) { | |
66 compiler.mainApp = library; | |
67 } else if (compiler.mainApp != library) { | |
68 throw | |
69 "Inconsistent use of compiler" | |
70 " (${compiler.mainApp} != $library)."; | |
71 } | |
72 return library; | |
73 }); | |
74 } | |
75 | |
76 Future run(); | |
77 | |
78 /// Returns a future for the mainApp after running the compiler. | |
79 Future<LibraryElement> compile() { | |
80 return loadMainApp().then((LibraryElement library) { | |
81 return compiler.run(scriptUri).then((_) => library); | |
82 }); | |
83 } | |
84 | |
85 String toString() => source; | |
86 } | |
OLD | NEW |