OLD | NEW |
1 // Copyright (c) 2013, 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 library polymer.test.build.common; | 5 /// Utilities for creating unit tests of Barback transformers. |
| 6 library code_transformers.src.test_harness; |
6 | 7 |
7 import 'dart:async'; | 8 import 'dart:async'; |
8 | 9 |
9 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
10 import 'package:stack_trace/stack_trace.dart'; | 11 import 'package:stack_trace/stack_trace.dart'; |
11 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
12 | 13 |
13 String idToString(AssetId id) => '${id.package}|${id.path}'; | 14 String idToString(AssetId id) => '${id.package}|${id.path}'; |
14 AssetId idFromString(String s) { | 15 AssetId idFromString(String s) { |
15 int index = s.indexOf('|'); | 16 int index = s.indexOf('|'); |
16 return new AssetId(s.substring(0, index), s.substring(index + 1)); | 17 return new AssetId(s.substring(0, index), s.substring(index + 1)); |
17 } | 18 } |
18 | 19 |
19 String _removeTrailingWhitespace(String str) => | 20 String _removeTrailingWhitespace(String str) => |
20 str.splitMapJoin('\n', | 21 str.splitMapJoin('\n', |
21 onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), '')); | 22 onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), '')); |
22 | 23 |
23 /** | 24 /// A helper package provider that has files stored in memory, also wraps |
24 * A helper package provider that has files stored in memory, also wraps | 25 /// [Barback] to simply our tests. |
25 * [Barback] to simply our tests. | |
26 */ | |
27 class TestHelper implements PackageProvider { | 26 class TestHelper implements PackageProvider { |
28 /** | 27 |
29 * Maps from an asset string identifier of the form 'package|path' to the | 28 /// Maps from an asset string identifier of the form 'package|path' to the |
30 * file contents. | 29 /// file contents. |
31 */ | |
32 final Map<String, String> files; | 30 final Map<String, String> files; |
33 final Iterable<String> packages; | 31 final Iterable<String> packages; |
34 final List<String> messages; | 32 final List<String> messages; |
35 int messagesSeen = 0; | 33 int messagesSeen = 0; |
36 bool errorSeen = false; | 34 bool errorSeen = false; |
37 | 35 |
38 Barback barback; | 36 Barback barback; |
39 var errorSubscription; | 37 var errorSubscription; |
40 var resultSubscription; | 38 var resultSubscription; |
41 var logSubscription; | 39 var logSubscription; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 expect('$msg$spanInfo', messages[messagesSeen++]); | 79 expect('$msg$spanInfo', messages[messagesSeen++]); |
82 }); | 80 }); |
83 } | 81 } |
84 | 82 |
85 void tearDown() { | 83 void tearDown() { |
86 errorSubscription.cancel(); | 84 errorSubscription.cancel(); |
87 resultSubscription.cancel(); | 85 resultSubscription.cancel(); |
88 logSubscription.cancel(); | 86 logSubscription.cancel(); |
89 } | 87 } |
90 | 88 |
91 /** | 89 /// Tells barback which files have changed, and thus anything that depends on |
92 * Tells barback which files have changed, and thus anything that depends on | 90 /// it on should be computed. By default mark all the input files. |
93 * it on should be computed. By default mark all the input files. | |
94 */ | |
95 void run([Iterable<String> paths]) { | 91 void run([Iterable<String> paths]) { |
96 if (paths == null) paths = files.keys; | 92 if (paths == null) paths = files.keys; |
97 barback.updateSources(paths.map(idFromString)); | 93 barback.updateSources(paths.map(idFromString)); |
98 } | 94 } |
99 | 95 |
100 Future<String> operator [](String assetString){ | 96 Future<String> operator [](String assetString){ |
101 return barback.getAssetById(idFromString(assetString)) | 97 return barback.getAssetById(idFromString(assetString)) |
102 .then((asset) => asset.readAsString()); | 98 .then((asset) => asset.readAsString()); |
103 } | 99 } |
104 | 100 |
(...skipping 14 matching lines...) Expand all Loading... |
119 }); | 115 }); |
120 return Future.wait(futures); | 116 return Future.wait(futures); |
121 }).then((_) { | 117 }).then((_) { |
122 // We only check messages when an expectation is provided. | 118 // We only check messages when an expectation is provided. |
123 if (messages == null) return; | 119 if (messages == null) return; |
124 expect(messages.length, messagesSeen, | 120 expect(messages.length, messagesSeen, |
125 reason: 'less messages than expected'); | 121 reason: 'less messages than expected'); |
126 }); | 122 }); |
127 } | 123 } |
128 } | 124 } |
129 | |
130 testPhases(String testName, List<List<Transformer>> phases, | |
131 Map<String, String> inputFiles, Map<String, String> expectedFiles, | |
132 [List<String> expectedMessages]) { | |
133 test(testName, () { | |
134 var helper = new TestHelper(phases, inputFiles, expectedMessages)..run(); | |
135 return helper.checkAll(expectedFiles).then((_) => helper.tearDown()); | |
136 }); | |
137 } | |
138 | |
139 const WEB_COMPONENTS_TAG = | |
140 '<script src="packages/web_components/platform.js"></script>\n' | |
141 '<script src="packages/web_components/dart_support.js"></script>\n'; | |
142 | |
143 const INTEROP_TAG = '<script src="packages/browser/interop.js"></script>\n'; | |
144 const DART_JS_TAG = '<script src="packages/browser/dart.js"></script>'; | |
145 | |
OLD | NEW |