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 // Helpers for writing compiler tests running in browser. | |
6 library trydart.web_compiler_test_case; | |
7 | |
8 import 'dart:async' show | |
9 EventSink, | |
10 Future; | |
11 | |
12 import 'dart:html' show | |
13 HttpRequest; | |
14 | |
15 import '../poi/compiler_test_case.dart' show | |
16 customUri, | |
17 CompilerTestCase; | |
18 | |
19 import 'package:dart2js_incremental/dart2js_incremental.dart' show | |
20 IncrementalCompiler, OutputProvider; | |
21 | |
22 import 'package:compiler/compiler.dart' show | |
23 Diagnostic; | |
24 | |
25 const String WEB_SCHEME = 'org.trydart.web'; | |
26 | |
27 /// A CompilerTestCase which runs in a browser. | |
28 class WebCompilerTestCase extends CompilerTestCase { | |
29 final IncrementalCompiler incrementalCompiler; | |
30 | |
31 WebCompilerTestCase.init(/* Map or String */ source, Uri uri) | |
32 : this.incrementalCompiler = makeCompiler(source, uri), | |
33 super.init(null, uri, null); | |
34 | |
35 WebCompilerTestCase(/* Map or String */ source, [String path]) | |
36 : this.init(source, customUri(path == null ? 'main.dart' : path)); | |
37 | |
38 Future run() { | |
39 return incrementalCompiler.compile(scriptUri).then((success) { | |
40 if (!success) throw 'Compilation failed'; | |
41 OutputProvider outputProvider = incrementalCompiler.outputProvider; | |
42 return outputProvider['.js']; | |
43 }); | |
44 } | |
45 | |
46 static IncrementalCompiler makeCompiler( | |
47 /* Map or String */ source, | |
48 Uri mainUri) { | |
49 Uri libraryRoot = new Uri(scheme: WEB_SCHEME, path: '/sdk/'); | |
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 | |
63 WebInputProvider inputProvider = | |
64 new WebInputProvider(sources, libraryRoot, packageRoot); | |
65 | |
66 void diagnosticHandler( | |
67 Uri uri, int begin, int end, String message, Diagnostic kind) { | |
68 if (uri == null) { | |
69 print('[$kind] $message'); | |
70 } else { | |
71 print('$uri@$begin+${end - begin}: [$kind] $message'); | |
72 } | |
73 } | |
74 | |
75 return new IncrementalCompiler( | |
76 libraryRoot: libraryRoot, | |
77 packageRoot: packageRoot, | |
78 inputProvider: inputProvider, | |
79 diagnosticHandler: diagnosticHandler, | |
80 outputProvider: new OutputProvider()); | |
81 } | |
82 } | |
83 | |
84 /// An input provider which provides input via [HttpRequest]. | |
85 /// Includes one in-memory compilation unit [source] which is returned when | |
86 /// [mainUri] is requested. | |
87 class WebInputProvider { | |
88 final Map<Uri, String> sources; | |
89 | |
90 final Uri libraryRoot; | |
91 | |
92 final Uri packageRoot; | |
93 | |
94 final Map<Uri, Future> cachedSources = new Map<Uri, Future>(); | |
95 | |
96 static final Map<String, Future> cachedRequests = new Map<String, Future>(); | |
97 | |
98 WebInputProvider(this.sources, this.libraryRoot, this.packageRoot); | |
99 | |
100 Future call(Uri uri) { | |
101 return cachedSources.putIfAbsent(uri, () { | |
102 if (sources.containsKey(uri)) return new Future.value(sources[uri]); | |
103 if (uri.scheme == WEB_SCHEME) { | |
104 return cachedHttpRequest('/root_dart${uri.path}'); | |
105 } else { | |
106 return cachedHttpRequest('$uri'); | |
107 } | |
108 }); | |
109 } | |
110 | |
111 static Future cachedHttpRequest(String uri) { | |
112 return cachedRequests.putIfAbsent(uri, () => HttpRequest.getString(uri)); | |
113 } | |
114 } | |
OLD | NEW |