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; |
| 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(String source, Uri uri) |
| 32 : this.incrementalCompiler = makeCompiler(source, uri), |
| 33 super.init(source, uri, null); |
| 34 |
| 35 WebCompilerTestCase(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(String source, Uri mainUri) { |
| 47 Uri libraryRoot = new Uri(scheme: WEB_SCHEME, path: '/sdk/'); |
| 48 Uri packageRoot = new Uri(scheme: WEB_SCHEME, path: '/packages/'); |
| 49 WebInputProvider inputProvider = |
| 50 new WebInputProvider(source, mainUri, libraryRoot, packageRoot); |
| 51 |
| 52 void diagnosticHandler( |
| 53 Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 54 if (uri == null) { |
| 55 print('[$kind] $message'); |
| 56 } else { |
| 57 print('$uri@$begin+${end - begin}: [$kind] $message'); |
| 58 } |
| 59 } |
| 60 |
| 61 return new IncrementalCompiler( |
| 62 libraryRoot: libraryRoot, |
| 63 packageRoot: packageRoot, |
| 64 inputProvider: inputProvider, |
| 65 diagnosticHandler: diagnosticHandler, |
| 66 outputProvider: new OutputProvider()); |
| 67 } |
| 68 } |
| 69 |
| 70 /// An input provider which provides input via [HttpRequest]. |
| 71 /// Includes one in-memory compilation unit [source] which is returned when |
| 72 /// [mainUri] is requested. |
| 73 class WebInputProvider { |
| 74 final String source; |
| 75 |
| 76 final Uri mainUri; |
| 77 |
| 78 final Uri libraryRoot; |
| 79 |
| 80 final Uri packageRoot; |
| 81 |
| 82 final Map<Uri, Future> cachedSources = new Map<Uri, Future>(); |
| 83 |
| 84 WebInputProvider( |
| 85 this.source, this.mainUri, this.libraryRoot, this.packageRoot); |
| 86 |
| 87 Future call(Uri uri) { |
| 88 return cachedSources.putIfAbsent(uri, () { |
| 89 if (uri == mainUri) return new Future.value(source); |
| 90 if (uri.scheme == WEB_SCHEME) { |
| 91 return HttpRequest.getString('/root_dart${uri.path}'); |
| 92 } else { |
| 93 return HttpRequest.getString('$uri'); |
| 94 } |
| 95 }); |
| 96 } |
| 97 } |
| 98 |
| 99 /// Output provider which collect output in [output]. |
| 100 class OutputProvider { |
| 101 final Map<String, String> output = new Map<String, String>(); |
| 102 |
| 103 EventSink<String> call(String name, String extension) { |
| 104 return new StringEventSink((String data) { |
| 105 output['$name.$extension'] = data; |
| 106 }); |
| 107 } |
| 108 |
| 109 String operator[] (String key) => output[key]; |
| 110 } |
| 111 |
| 112 /// Helper class to collect sources. |
| 113 class StringEventSink implements EventSink<String> { |
| 114 List<String> data = <String>[]; |
| 115 |
| 116 final Function onClose; |
| 117 |
| 118 StringEventSink(this.onClose); |
| 119 |
| 120 void add(String event) { |
| 121 if (data == null) throw 'StringEventSink is closed.'; |
| 122 data.add(event); |
| 123 } |
| 124 |
| 125 void addError(errorEvent, [StackTrace stackTrace]) { |
| 126 throw 'addError($errorEvent, $stackTrace)'; |
| 127 } |
| 128 |
| 129 void close() { |
| 130 if (data != null) { |
| 131 onClose(data.join()); |
| 132 data = null; |
| 133 } |
| 134 } |
| 135 } |
OLD | NEW |