OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Helpers for writing compiler tests running in browser. | 5 // Helpers for writing compiler tests running in browser. |
6 library trydart.web_compiler_test_case; | 6 library trydart.web_compiler_test_case; |
7 | 7 |
8 import 'dart:async' show | 8 import 'dart:async' show |
9 EventSink, | 9 EventSink, |
10 Future; | 10 Future; |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 import 'package:compiler/compiler.dart' show | 22 import 'package:compiler/compiler.dart' show |
23 Diagnostic; | 23 Diagnostic; |
24 | 24 |
25 const String WEB_SCHEME = 'org.trydart.web'; | 25 const String WEB_SCHEME = 'org.trydart.web'; |
26 | 26 |
27 /// A CompilerTestCase which runs in a browser. | 27 /// A CompilerTestCase which runs in a browser. |
28 class WebCompilerTestCase extends CompilerTestCase { | 28 class WebCompilerTestCase extends CompilerTestCase { |
29 final IncrementalCompiler incrementalCompiler; | 29 final IncrementalCompiler incrementalCompiler; |
30 | 30 |
31 WebCompilerTestCase.init(String source, Uri uri) | 31 WebCompilerTestCase.init(/* Map or String */ source, Uri uri) |
32 : this.incrementalCompiler = makeCompiler(source, uri), | 32 : this.incrementalCompiler = makeCompiler(source, uri), |
33 super.init(source, uri, null); | 33 super.init(null, uri, null); |
34 | 34 |
35 WebCompilerTestCase(String source, [String path]) | 35 WebCompilerTestCase(/* Map or String */ source, [String path]) |
36 : this.init(source, customUri(path == null ? 'main.dart' : path)); | 36 : this.init(source, customUri(path == null ? 'main.dart' : path)); |
37 | 37 |
38 Future run() { | 38 Future run() { |
39 return incrementalCompiler.compile(scriptUri).then((success) { | 39 return incrementalCompiler.compile(scriptUri).then((success) { |
40 if (!success) throw 'Compilation failed'; | 40 if (!success) throw 'Compilation failed'; |
41 OutputProvider outputProvider = incrementalCompiler.outputProvider; | 41 OutputProvider outputProvider = incrementalCompiler.outputProvider; |
42 return outputProvider['.js']; | 42 return outputProvider['.js']; |
43 }); | 43 }); |
44 } | 44 } |
45 | 45 |
46 static IncrementalCompiler makeCompiler(String source, Uri mainUri) { | 46 static IncrementalCompiler makeCompiler( |
| 47 /* Map or String */ source, |
| 48 Uri mainUri) { |
47 Uri libraryRoot = new Uri(scheme: WEB_SCHEME, path: '/sdk/'); | 49 Uri libraryRoot = new Uri(scheme: WEB_SCHEME, path: '/sdk/'); |
48 Uri packageRoot = new Uri(scheme: WEB_SCHEME, path: '/packages/'); | 50 Uri packageRoot = new Uri(scheme: WEB_SCHEME, path: '/packages/'); |
| 51 |
| 52 Map<Uri, String> sources = <Uri, String>{}; |
| 53 if (source is String) { |
| 54 sources[mainUri] = source; |
| 55 } else if (source is Map) { |
| 56 source.forEach((String name, String code) { |
| 57 sources[mainUri.resolve(name)] = code; |
| 58 }); |
| 59 } else { |
| 60 throw new ArgumentError("[source] should be a String or a Map"); |
| 61 } |
| 62 |
49 WebInputProvider inputProvider = | 63 WebInputProvider inputProvider = |
50 new WebInputProvider(source, mainUri, libraryRoot, packageRoot); | 64 new WebInputProvider(sources, libraryRoot, packageRoot); |
51 | 65 |
52 void diagnosticHandler( | 66 void diagnosticHandler( |
53 Uri uri, int begin, int end, String message, Diagnostic kind) { | 67 Uri uri, int begin, int end, String message, Diagnostic kind) { |
54 if (uri == null) { | 68 if (uri == null) { |
55 print('[$kind] $message'); | 69 print('[$kind] $message'); |
56 } else { | 70 } else { |
57 print('$uri@$begin+${end - begin}: [$kind] $message'); | 71 print('$uri@$begin+${end - begin}: [$kind] $message'); |
58 } | 72 } |
59 } | 73 } |
60 | 74 |
61 return new IncrementalCompiler( | 75 return new IncrementalCompiler( |
62 libraryRoot: libraryRoot, | 76 libraryRoot: libraryRoot, |
63 packageRoot: packageRoot, | 77 packageRoot: packageRoot, |
64 inputProvider: inputProvider, | 78 inputProvider: inputProvider, |
65 diagnosticHandler: diagnosticHandler, | 79 diagnosticHandler: diagnosticHandler, |
66 outputProvider: new OutputProvider()); | 80 outputProvider: new OutputProvider()); |
67 } | 81 } |
68 } | 82 } |
69 | 83 |
70 /// An input provider which provides input via [HttpRequest]. | 84 /// An input provider which provides input via [HttpRequest]. |
71 /// Includes one in-memory compilation unit [source] which is returned when | 85 /// Includes one in-memory compilation unit [source] which is returned when |
72 /// [mainUri] is requested. | 86 /// [mainUri] is requested. |
73 class WebInputProvider { | 87 class WebInputProvider { |
74 final String source; | 88 final Map<Uri, String> sources; |
75 | |
76 final Uri mainUri; | |
77 | 89 |
78 final Uri libraryRoot; | 90 final Uri libraryRoot; |
79 | 91 |
80 final Uri packageRoot; | 92 final Uri packageRoot; |
81 | 93 |
82 final Map<Uri, Future> cachedSources = new Map<Uri, Future>(); | 94 final Map<Uri, Future> cachedSources = new Map<Uri, Future>(); |
83 | 95 |
84 static final Map<String, Future> cachedRequests = new Map<String, Future>(); | 96 static final Map<String, Future> cachedRequests = new Map<String, Future>(); |
85 | 97 |
86 WebInputProvider( | 98 WebInputProvider(this.sources, this.libraryRoot, this.packageRoot); |
87 this.source, this.mainUri, this.libraryRoot, this.packageRoot); | |
88 | 99 |
89 Future call(Uri uri) { | 100 Future call(Uri uri) { |
90 return cachedSources.putIfAbsent(uri, () { | 101 return cachedSources.putIfAbsent(uri, () { |
91 if (uri == mainUri) return new Future.value(source); | 102 if (sources.containsKey(uri)) return new Future.value(sources[uri]); |
92 if (uri.scheme == WEB_SCHEME) { | 103 if (uri.scheme == WEB_SCHEME) { |
93 return cachedHttpRequest('/root_dart${uri.path}'); | 104 return cachedHttpRequest('/root_dart${uri.path}'); |
94 } else { | 105 } else { |
95 return cachedHttpRequest('$uri'); | 106 return cachedHttpRequest('$uri'); |
96 } | 107 } |
97 }); | 108 }); |
98 } | 109 } |
99 | 110 |
100 static Future cachedHttpRequest(String uri) { | 111 static Future cachedHttpRequest(String uri) { |
101 return cachedRequests.putIfAbsent(uri, () => HttpRequest.getString(uri)); | 112 return cachedRequests.putIfAbsent(uri, () => HttpRequest.getString(uri)); |
(...skipping 30 matching lines...) Expand all Loading... |
132 throw 'addError($errorEvent, $stackTrace)'; | 143 throw 'addError($errorEvent, $stackTrace)'; |
133 } | 144 } |
134 | 145 |
135 void close() { | 146 void close() { |
136 if (data != null) { | 147 if (data != null) { |
137 onClose(data.join()); | 148 onClose(data.join()); |
138 data = null; | 149 data = null; |
139 } | 150 } |
140 } | 151 } |
141 } | 152 } |
OLD | NEW |