Chromium Code Reviews| 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 barback.test.utils; | 5 library barback.test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | |
| 8 | 9 |
| 9 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| 10 import 'package:barback/src/asset_graph.dart'; | 11 import 'package:barback/src/asset_graph.dart'; |
| 11 import 'package:pathos/path.dart' as pathos; | 12 import 'package:pathos/path.dart' as pathos; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | 13 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 | 14 |
| 14 // TODO(rnystrom): Get rid of this or find a better path for it. | 15 // TODO(rnystrom): Get rid of this or find a better path for it. |
| 15 import '../../../sdk/lib/_internal/pub/test/command_line_config.dart'; | 16 import '../../../sdk/lib/_internal/pub/test/command_line_config.dart'; |
| 16 | 17 |
| 17 var _configured = false; | 18 var _configured = false; |
| 18 | 19 |
| 20 MockProvider _provider; | |
| 21 AssetGraph _graph; | |
| 22 | |
| 23 /// Calls to [buildShouldSucceed] and [buildShouldFail] set expectations on | |
| 24 /// successive [BuildResult]s from [_graph]. This keeps track of how many calls | |
| 25 /// have already been made so later calls know which result to look for. | |
| 26 int _nextBuildResult; | |
|
nweiz
2013/06/27 23:12:20
Apparently we have a StreamIterator class, which s
Bob Nystrom
2013/07/02 21:41:28
I tried that, but it doesn't allow queueing up mul
nweiz
2013/07/03 00:40:35
Gross. File a bug?
Bob Nystrom
2013/07/03 17:12:16
It's the documented behavior. :-/
nweiz
2013/07/03 18:17:15
Then file a feature request? It's good to have the
| |
| 27 | |
| 19 void initConfig() { | 28 void initConfig() { |
| 20 if (_configured) return; | 29 if (_configured) return; |
| 21 _configured = true; | 30 _configured = true; |
| 22 unittestConfiguration = new CommandLineConfiguration(); | 31 unittestConfiguration = new CommandLineConfiguration(); |
| 23 } | 32 } |
| 24 | 33 |
| 25 /// Expects that [graph] will return an asset matching [name] and [contents]. | 34 /// Creates a new [AssetProvider] and [AssetGraph] with the given [assets] and |
| 26 void expectAsset(AssetGraph graph, String name, [String contents]) { | 35 /// [transformers]. |
| 36 /// | |
| 37 /// This graph is used internally by most of the other functions in this | |
| 38 /// library so you must call it in the test before calling any of the other | |
| 39 /// functions. | |
| 40 /// | |
| 41 /// [assets] may either be an [Iterable] or a [Map]. If it's an [Iterable], | |
| 42 /// each element may either be an [AssetId] or a string that can be parsed to | |
| 43 /// one. If it's a [Map], each key should be a string that can be parsed to an | |
| 44 /// [AssetId] and the value should be a string defining the contents of that | |
| 45 /// asset. | |
| 46 void initGraph([assets, Iterable<Iterable<Transformer>> transformers]) { | |
| 47 if (assets == null) assets = []; | |
| 48 if (transformers == null) transformers = []; | |
| 49 | |
| 50 _provider = new MockProvider(assets); | |
| 51 _graph = new AssetGraph(_provider, transformers); | |
| 52 _nextBuildResult = 0; | |
| 53 } | |
| 54 | |
| 55 /// Updates [assets] in the current [AssetProvider]. | |
| 56 /// | |
| 57 /// Each item in the list may either be an [AssetId] or a string that can be | |
| 58 /// parsed as one. Note that this method is not automatically scheduled. | |
| 59 void updateSources(Iterable assets) { | |
| 60 // Allow strings as asset IDs. | |
| 61 assets = assets.map((asset) { | |
| 62 if (asset is String) return new AssetId.parse(asset); | |
| 63 return asset; | |
| 64 }); | |
| 65 | |
| 66 _graph.updateSources(assets); | |
| 67 } | |
| 68 | |
| 69 /// Removes [assets] from the current [AssetProvider]. | |
| 70 /// | |
| 71 /// Each item in the list may either be an [AssetId] or a string that can be | |
| 72 /// parsed as one. Note that this method is not automatically scheduled. | |
| 73 void removeSources(Iterable assets) { | |
| 74 // Allow strings as asset IDs. | |
| 75 assets = assets.map((asset) { | |
| 76 if (asset is String) return new AssetId.parse(asset); | |
| 77 return asset; | |
| 78 }); | |
| 79 | |
| 80 _graph.removeSources(assets); | |
| 81 } | |
| 82 | |
| 83 /// Schedules a change to the contents of an asset identified by [name] to | |
| 84 /// [contents]. | |
| 85 /// | |
| 86 /// Does not update it in the graph. | |
| 87 void modifyAsset(String name, String contents) { | |
| 88 schedule(() { | |
| 89 _provider._modifyAsset(name, contents); | |
| 90 }, "modify asset $name"); | |
| 91 } | |
| 92 | |
| 93 /// Schedules a pause of the internally created [AssetProvider]. | |
| 94 /// | |
| 95 /// All asset requests that the [AssetGraph] makes to the provider after this | |
| 96 /// will not complete until [resumeProvider] is called. | |
| 97 void pauseProvider() { | |
| 98 schedule(() =>_provider._pause(), "pause provider"); | |
| 99 } | |
| 100 | |
| 101 /// Schedules an unpause of the provider after a call to [pauseProvider] and | |
| 102 /// allows all pending asset loads to finish. | |
| 103 void resumeProvider() { | |
| 104 schedule(() => _provider._resume(), "resume provider"); | |
| 105 } | |
| 106 | |
| 107 /// Expects that the next [BuildResult] is a build success. | |
| 108 void buildShouldSucceed([void callback()]) { | |
| 109 _graph.results.elementAt(_nextBuildResult++).then(wrapAsync((result) { | |
|
nweiz
2013/06/27 23:12:20
I strongly prefer expect(..., completes) to [wrapA
Bob Nystrom
2013/07/02 21:41:28
Done.
| |
| 110 expect(result.succeeded, isTrue); | |
| 111 if (callback != null) callback(); | |
| 112 })); | |
| 113 } | |
| 114 | |
| 115 /// Expects that the next [BuildResult] emitted is a failure. | |
| 116 /// | |
| 117 /// Invokes [callback] with the error (not the result) so that it can provide | |
| 118 /// more precise expectations. | |
| 119 void buildShouldFail(void callback(error)) { | |
| 120 _graph.results.elementAt(_nextBuildResult++).then(wrapAsync((result) { | |
| 121 expect(result.succeeded, isFalse); | |
| 122 callback(result.error); | |
| 123 })); | |
| 124 } | |
| 125 | |
| 126 /// Pauses the schedule until the currently running build completes. | |
| 127 void waitForBuild() { | |
|
nweiz
2013/06/27 23:12:20
Isn't it possible that a build emits an error befo
Bob Nystrom
2013/07/02 21:41:28
Yup. Modified this to check that the result is act
| |
| 128 schedule(() { | |
| 129 return _graph.results.first; | |
|
nweiz
2013/06/27 23:12:20
Style nit: =>
Bob Nystrom
2013/07/02 21:41:28
Invalidated by above change.
| |
| 130 }); | |
| 131 } | |
| 132 | |
| 133 /// Schedules an expectation that the graph will deliver an asset matching | |
| 134 /// [name] and [contents]. | |
| 135 /// | |
| 136 /// If [contents] is omitted, defaults to the asset's filename without an | |
| 137 /// extension (which is the same default that [initGraph] uses). | |
| 138 void expectAsset(String name, [String contents]) { | |
| 27 var id = new AssetId.parse(name); | 139 var id = new AssetId.parse(name); |
| 28 | 140 |
| 29 if (contents == null) { | 141 if (contents == null) { |
| 30 contents = pathos.basenameWithoutExtension(id.path); | 142 contents = pathos.basenameWithoutExtension(id.path); |
| 31 } | 143 } |
| 32 | 144 |
| 33 schedule(() { | 145 schedule(() { |
| 34 return graph.getAssetById(id).then((asset) { | 146 return _graph.getAssetById(id).then((asset) { |
| 35 // TODO(rnystrom): Make an actual Matcher class for this. | 147 // TODO(rnystrom): Make an actual Matcher class for this. |
| 36 expect(asset, new isInstanceOf<MockAsset>()); | 148 expect(asset, new isInstanceOf<MockAsset>()); |
| 37 expect(asset._id.package, equals(id.package)); | 149 expect(asset._id.package, equals(id.package)); |
| 38 expect(asset._id.path, equals(id.path)); | 150 expect(asset._id.path, equals(id.path)); |
| 39 expect(asset._contents, equals(contents)); | 151 expect(asset._contents, equals(contents)); |
| 40 }); | 152 }); |
| 41 }, "get asset $name"); | 153 }, "get asset $name"); |
| 42 } | 154 } |
| 43 | 155 |
| 44 /// Expects that [graph] will not find an asset matching [name]. | 156 /// Schedules an expectation that the graph will not find an asset matching |
| 45 void expectNoAsset(AssetGraph graph, String name) { | 157 /// [name]. |
| 158 void expectNoAsset(String name) { | |
| 46 var id = new AssetId.parse(name); | 159 var id = new AssetId.parse(name); |
| 47 | 160 |
| 48 // Make sure the future gets the error. | 161 // Make sure the future gets the error. |
| 49 schedule(() { | 162 schedule(() { |
| 50 return graph.getAssetById(id).then((asset) { | 163 return _graph.getAssetById(id).then((asset) { |
| 51 fail("Should have thrown error but got $asset."); | 164 fail("Should have thrown error but got $asset."); |
| 52 }).catchError((error) { | 165 }).catchError((error) { |
| 53 expect(error, new isInstanceOf<AssetNotFoundException>()); | 166 expect(error, new isInstanceOf<AssetNotFoundException>()); |
| 54 expect(error.id, equals(id)); | 167 expect(error.id, equals(id)); |
| 55 }); | 168 }); |
| 56 }, "get asset $name"); | 169 }, "get asset $name"); |
| 57 } | 170 } |
| 58 | 171 |
| 59 /// Expects that [graph] will have an output file collision error on an asset | 172 /// Expects that the next [BuildResult] is an output file collision error on an |
| 60 /// matching [name]. | 173 /// asset matching [name]. |
| 61 Future expectCollision(AssetGraph graph, String name) { | 174 Future expectCollision(String name) { |
| 62 var id = new AssetId.parse(name); | 175 var id = new AssetId.parse(name); |
| 63 return schedule(() { | 176 _graph.results.first.then(wrapAsync((result) { |
| 64 return graph.results.first.then((result) { | 177 expect(result.error, new isInstanceOf<AssetCollisionException>()); |
| 65 expect(result.error, new isInstanceOf<AssetCollisionException>()); | 178 expect(result.error.id, equals(id)); |
| 66 expect(result.error.id, equals(id)); | 179 })); |
| 67 }); | |
| 68 }, "get collision on $name"); | |
| 69 } | 180 } |
| 70 | 181 |
| 71 /// Expects that [graph] will have an error on an asset matching [name] for | 182 /// Schedules an expectation that [graph] will have an error on an asset |
| 72 /// missing [input]. | 183 /// matching [name] for missing [input]. |
| 73 Future expectMissingInput(AssetGraph graph, String name, String input) { | 184 Future expectMissingInput(AssetGraph graph, String name, String input) { |
| 74 var missing = new AssetId.parse(input); | 185 var missing = new AssetId.parse(input); |
| 75 | 186 |
| 76 // Make sure the future gets the error. | 187 // Make sure the future gets the error. |
| 77 schedule(() { | 188 schedule(() { |
| 78 return graph.getAssetById(new AssetId.parse(name)).then((asset) { | 189 return graph.getAssetById(new AssetId.parse(name)).then((asset) { |
| 79 fail("Should have thrown error but got $asset."); | 190 fail("Should have thrown error but got $asset."); |
| 80 }).catchError((error) { | 191 }).catchError((error) { |
| 81 expect(error, new isInstanceOf<MissingInputException>()); | 192 expect(error, new isInstanceOf<MissingInputException>()); |
| 82 expect(error.id, equals(missing)); | 193 expect(error.id, equals(missing)); |
| 83 }); | 194 }); |
| 84 }, "get missing input on $name"); | 195 }, "get missing input on $name"); |
| 85 } | 196 } |
| 86 | 197 |
| 87 /// An [AssetProvider] that provides the given set of assets. | 198 /// An [AssetProvider] that provides the given set of assets. |
| 88 class MockProvider implements AssetProvider { | 199 class MockProvider implements AssetProvider { |
| 89 Iterable<String> get packages => _packages.keys; | 200 Iterable<String> get packages => _packages.keys; |
| 90 | 201 |
| 91 final _packages = new Map<String, List<MockAsset>>(); | 202 final _packages = new Map<String, List<MockAsset>>(); |
| 92 | 203 |
| 93 /// The completer that [getAsset()] is waiting on to complete. | 204 /// The completer that [getAsset()] is waiting on to complete when paused. |
| 94 /// | 205 /// |
| 95 /// If `null` it will return the asset immediately. | 206 /// If `null` it will return the asset immediately. |
| 96 Completer _wait; | 207 Completer _pauseCompleter; |
| 97 | 208 |
| 98 /// Tells the provider to wait during [getAsset] until [complete()] | 209 /// Tells the provider to wait during [getAsset] until [complete()] |
| 99 /// is called. | 210 /// is called. |
| 100 /// | 211 /// |
| 101 /// Lets you test the asynchronous behavior of loading. | 212 /// Lets you test the asynchronous behavior of loading. |
| 102 void wait() { | 213 void _pause() { |
| 103 _wait = new Completer(); | 214 _pauseCompleter = new Completer(); |
| 104 } | 215 } |
| 105 | 216 |
| 106 void complete() { | 217 void _resume() { |
| 107 _wait.complete(); | 218 _pauseCompleter.complete(); |
| 108 _wait = null; | 219 _pauseCompleter = null; |
| 109 } | 220 } |
| 110 | 221 |
| 111 MockProvider(assets) { | 222 MockProvider(assets) { |
| 112 if (assets is Map) { | 223 if (assets is Map) { |
| 113 assets.forEach((asset, contents) { | 224 assets.forEach((asset, contents) { |
| 114 var id = new AssetId.parse(asset); | 225 var id = new AssetId.parse(asset); |
| 115 var package = _packages.putIfAbsent(id.package, () => []); | 226 var package = _packages.putIfAbsent(id.package, () => []); |
| 116 package.add(new MockAsset(id, contents)); | 227 package.add(new MockAsset(id, contents)); |
| 117 }); | 228 }); |
| 118 } else if (assets is Iterable) { | 229 } else if (assets is Iterable) { |
| 119 for (var asset in assets) { | 230 for (var asset in assets) { |
| 120 var id = new AssetId.parse(asset); | 231 var id = new AssetId.parse(asset); |
| 121 var package = _packages.putIfAbsent(id.package, () => []); | 232 var package = _packages.putIfAbsent(id.package, () => []); |
| 122 var contents = pathos.basenameWithoutExtension(id.path); | 233 var contents = pathos.basenameWithoutExtension(id.path); |
| 123 package.add(new MockAsset(id, contents)); | 234 package.add(new MockAsset(id, contents)); |
| 124 } | 235 } |
| 125 } | 236 } |
| 126 } | 237 } |
| 127 | 238 |
| 128 void modifyAsset(String name, String contents) { | 239 void _modifyAsset(String name, String contents) { |
| 129 var id = new AssetId.parse(name); | 240 var id = new AssetId.parse(name); |
| 130 var asset = _packages[id.package].firstWhere((a) => a._id == id); | 241 var asset = _packages[id.package].firstWhere((a) => a._id == id); |
| 131 asset._contents = contents; | 242 asset._contents = contents; |
| 132 } | 243 } |
| 133 | 244 |
| 134 List<AssetId> listAssets(String package, {String within}) { | 245 List<AssetId> listAssets(String package, {String within}) { |
| 135 if (within != null) { | 246 if (within != null) { |
| 136 throw new UnimplementedError("Doesn't handle 'within' yet."); | 247 throw new UnimplementedError("Doesn't handle 'within' yet."); |
| 137 } | 248 } |
| 138 | 249 |
| 139 return _packages[package].map((asset) => asset.id); | 250 return _packages[package].map((asset) => asset.id); |
| 140 } | 251 } |
| 141 | 252 |
| 142 Future<Asset> getAsset(AssetId id) { | 253 Future<Asset> getAsset(AssetId id) { |
| 143 var future; | 254 var future; |
| 144 if (_wait != null) { | 255 if (_pauseCompleter != null) { |
| 145 future = _wait.future; | 256 future = _pauseCompleter.future; |
| 146 } else { | 257 } else { |
| 147 future = new Future.value(); | 258 future = new Future.value(); |
| 148 } | 259 } |
| 149 | 260 |
| 150 return future.then((_) { | 261 return future.then((_) { |
| 151 var package = _packages[id.package]; | 262 var package = _packages[id.package]; |
| 152 if (package == null) throw new AssetNotFoundException(id); | 263 if (package == null) throw new AssetNotFoundException(id); |
| 153 | 264 |
| 154 return package.firstWhere((asset) => asset._id == id, | 265 return package.firstWhere((asset) => asset._id == id, |
| 155 orElse: () => throw new AssetNotFoundException(id)); | 266 orElse: () => throw new AssetNotFoundException(id)); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 | 444 |
| 334 MockAsset(this._id, this._contents); | 445 MockAsset(this._id, this._contents); |
| 335 | 446 |
| 336 String readAsString() => _contents; | 447 String readAsString() => _contents; |
| 337 Stream<List<int>> read() => throw new UnimplementedError(); | 448 Stream<List<int>> read() => throw new UnimplementedError(); |
| 338 | 449 |
| 339 serialize() => throw new UnimplementedError(); | 450 serialize() => throw new UnimplementedError(); |
| 340 | 451 |
| 341 String toString() => "MockAsset $_id $_contents"; | 452 String toString() => "MockAsset $_id $_contents"; |
| 342 } | 453 } |
| OLD | NEW |