OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library polymer.test.build.common; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
(...skipping 13 matching lines...) Expand all Loading... |
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 */ | 26 */ |
27 class TestHelper implements PackageProvider { | 27 class TestHelper implements PackageProvider { |
28 /** | 28 /** |
29 * Maps from an asset string identifier of the form 'package|path' to the | 29 * Maps from an asset string identifier of the form 'package|path' to the |
30 * file contents. | 30 * file contents. |
31 */ | 31 */ |
32 final Map<String, String> files; | 32 final Map<String, String> files; |
33 final Iterable<String> packages; | 33 final Iterable<String> packages; |
| 34 final List<String> messages; |
| 35 int messagesSeen = 0; |
| 36 bool errorSeen = false; |
34 | 37 |
35 Barback barback; | 38 Barback barback; |
36 var errorSubscription; | 39 var errorSubscription; |
37 var resultSubscription; | 40 var resultSubscription; |
| 41 var logSubscription; |
38 | 42 |
39 Future<Asset> getAsset(AssetId id) => | 43 Future<Asset> getAsset(AssetId id) => |
40 new Future.value(new Asset.fromString(id, files[idToString(id)])); | 44 new Future.value(new Asset.fromString(id, files[idToString(id)])); |
41 TestHelper(List<List<Transformer>> transformers, Map<String, String> files) | 45 |
| 46 TestHelper(List<List<Transformer>> transformers, Map<String, String> files, |
| 47 this.messages) |
42 : files = files, | 48 : files = files, |
43 packages = files.keys.map((s) => idFromString(s).package) { | 49 packages = files.keys.map((s) => idFromString(s).package) { |
44 barback = new Barback(this); | 50 barback = new Barback(this); |
45 for (var p in packages) { | 51 for (var p in packages) { |
46 barback.updateTransformers(p, transformers); | 52 barback.updateTransformers(p, transformers); |
47 } | 53 } |
| 54 |
48 errorSubscription = barback.errors.listen((e) { | 55 errorSubscription = barback.errors.listen((e) { |
49 var trace = null; | 56 var trace = null; |
50 if (e is Error) trace = e.stackTrace; | 57 if (e is Error) trace = e.stackTrace; |
51 if (trace != null) { | 58 if (trace != null) { |
52 print(Trace.format(trace)); | 59 print(Trace.format(trace)); |
53 } | 60 } |
54 fail('error running barback: $e'); | 61 fail('error running barback: $e'); |
55 }); | 62 }); |
| 63 |
56 resultSubscription = barback.results.listen((result) { | 64 resultSubscription = barback.results.listen((result) { |
57 expect(result.succeeded, isTrue, reason: "${result.errors}"); | 65 expect(result.succeeded, !errorSeen, reason: "${result.errors}"); |
| 66 }); |
| 67 |
| 68 logSubscription = barback.log.listen((entry) { |
| 69 if (entry.level == LogLevel.ERROR) errorSeen = true; |
| 70 // We only check messages when an expectation is provided. |
| 71 if (messages == null) return; |
| 72 |
| 73 var msg = '${entry.level.name.toLowerCase()}: ${entry.message}'; |
| 74 var span = entry.span; |
| 75 var spanInfo = span == null ? '' : |
| 76 ' (${span.sourceUrl} ${span.start.line} ${span.start.column})'; |
| 77 expect(messagesSeen, lessThan(messages.length), |
| 78 reason: 'more messages than expected'); |
| 79 expect('$msg$spanInfo', messages[messagesSeen++]); |
58 }); | 80 }); |
59 } | 81 } |
60 | 82 |
61 void tearDown() { | 83 void tearDown() { |
62 errorSubscription.cancel(); | 84 errorSubscription.cancel(); |
63 resultSubscription.cancel(); | 85 resultSubscription.cancel(); |
| 86 logSubscription.cancel(); |
64 } | 87 } |
65 | 88 |
66 /** | 89 /** |
67 * Tells barback which files have changed, and thus anything that depends on | 90 * Tells barback which files have changed, and thus anything that depends on |
68 * it on should be computed. By default mark all the input files. | 91 * it on should be computed. By default mark all the input files. |
69 */ | 92 */ |
70 void run([Iterable<String> paths]) { | 93 void run([Iterable<String> paths]) { |
71 if (paths == null) paths = files.keys; | 94 if (paths == null) paths = files.keys; |
72 barback.updateSources(paths.map(idFromString)); | 95 barback.updateSources(paths.map(idFromString)); |
73 } | 96 } |
74 | 97 |
75 Future<String> operator [](String assetString){ | 98 Future<String> operator [](String assetString){ |
76 return barback.getAssetById(idFromString(assetString)) | 99 return barback.getAssetById(idFromString(assetString)) |
77 .then((asset) => asset.readAsString()); | 100 .then((asset) => asset.readAsString()); |
78 } | 101 } |
79 | 102 |
80 Future check(String assetIdString, String content) { | 103 Future check(String assetIdString, String content) { |
81 return this[assetIdString].then((value) { | 104 return this[assetIdString].then((value) { |
82 value = _removeTrailingWhitespace(value); | 105 value = _removeTrailingWhitespace(value); |
83 content = _removeTrailingWhitespace(content); | 106 content = _removeTrailingWhitespace(content); |
84 expect(value, content, reason: 'Final output of $assetIdString differs.'); | 107 expect(value, content, reason: 'Final output of $assetIdString differs.'); |
85 }); | 108 }); |
86 } | 109 } |
87 | 110 |
88 Future checkAll(Map<String, String> files) { | 111 Future checkAll(Map<String, String> files) { |
89 var futures = []; | 112 var futures = []; |
90 files.forEach((k, v) { | 113 files.forEach((k, v) { |
91 futures.add(check(k, v)); | 114 futures.add(check(k, v)); |
92 }); | 115 }); |
93 return Future.wait(futures); | 116 return Future.wait(futures).then((_) { |
| 117 // We only check messages when an expectation is provided. |
| 118 if (messages == null) return; |
| 119 expect(messages.length, messagesSeen, |
| 120 reason: 'less messages than expected'); |
| 121 }); |
94 } | 122 } |
95 } | 123 } |
96 | 124 |
97 testPhases(String testName, List<List<Transformer>> phases, | 125 testPhases(String testName, List<List<Transformer>> phases, |
98 Map<String, String> inputFiles, Map<String, String> expectedFiles) { | 126 Map<String, String> inputFiles, Map<String, String> expectedFiles, |
| 127 [List<String> expectedMessages]) { |
99 test(testName, () { | 128 test(testName, () { |
100 var helper = new TestHelper(phases, inputFiles)..run(); | 129 var helper = new TestHelper(phases, inputFiles, expectedMessages)..run(); |
101 return helper.checkAll(expectedFiles).then((_) => helper.tearDown()); | 130 return helper.checkAll(expectedFiles).then((_) => helper.tearDown()); |
102 }); | 131 }); |
103 } | 132 } |
104 | 133 |
105 // TODO(jmesserly): this is .debug to workaround issue 14720. | 134 // TODO(jmesserly): this is .debug to workaround issue 14720. |
106 const SHADOW_DOM_TAG = | 135 const SHADOW_DOM_TAG = |
107 '<script src="packages/shadow_dom/shadow_dom.debug.js"></script>\n'; | 136 '<script src="packages/shadow_dom/shadow_dom.debug.js"></script>\n'; |
108 | 137 |
109 const INTEROP_TAG = '<script src="packages/browser/interop.js"></script>\n'; | 138 const INTEROP_TAG = '<script src="packages/browser/interop.js"></script>\n'; |
110 const DART_JS_TAG = '<script src="packages/browser/dart.js"></script>'; | 139 const DART_JS_TAG = '<script src="packages/browser/dart.js"></script>'; |
111 | 140 |
112 const CUSTOM_ELEMENT_TAG = | 141 const CUSTOM_ELEMENT_TAG = |
113 '<script src="packages/custom_element/custom-elements.debug.js">' | 142 '<script src="packages/custom_element/custom-elements.debug.js">' |
114 '</script>\n'; | 143 '</script>\n'; |
OLD | NEW |