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 import 'dart:collection'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 if (transformers == null) transformers = {}; | 62 if (transformers == null) transformers = {}; |
| 63 | 63 |
| 64 _provider = new MockProvider(assets, transformers); | 64 _provider = new MockProvider(assets, transformers); |
| 65 _barback = new Barback(_provider); | 65 _barback = new Barback(_provider); |
| 66 _nextBuildResult = 0; | 66 _nextBuildResult = 0; |
| 67 } | 67 } |
| 68 | 68 |
| 69 /// Updates [assets] in the current [PackageProvider]. | 69 /// Updates [assets] in the current [PackageProvider]. |
| 70 /// | 70 /// |
| 71 /// Each item in the list may either be an [AssetId] or a string that can be | 71 /// Each item in the list may either be an [AssetId] or a string that can be |
| 72 /// parsed as one. | |
| 73 void updateSources(Iterable assets) { | |
| 74 assets = _parseAssets(assets); | |
| 75 schedule(() => _barback.updateSources(assets), | |
| 76 "updating ${assets.join(', ')}"); | |
| 77 } | |
| 78 | |
| 79 /// Updates [assets] in the current [PackageProvider]. | |
| 80 /// | |
| 81 /// 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. | 82 /// parsed as one. Note that this method is not automatically scheduled. |
|
Bob Nystrom
2013/08/08 21:35:35
Remove the "Note that..." sentence and collapse th
nweiz
2013/08/09 19:41:51
Done.
| |
| 73 void updateSources(Iterable assets) { | 83 /// |
| 74 // Allow strings as asset IDs. | 84 /// Unlike [updateSources], this is not automatically scheduled and will be run |
| 75 assets = assets.map((asset) { | 85 /// synchronously when called. |
| 76 if (asset is String) return new AssetId.parse(asset); | 86 void updateSourcesSync(Iterable assets) => |
| 77 return asset; | 87 _barback.updateSources(_parseAssets(assets)); |
| 78 }); | |
| 79 | 88 |
| 80 _barback.updateSources(assets); | 89 /// Removes [assets] from the current [PackageProvider]. |
| 90 /// | |
| 91 /// Each item in the list may either be an [AssetId] or a string that can be | |
| 92 /// parsed as one. | |
| 93 void removeSources(Iterable assets) { | |
| 94 assets = _parseAssets(assets); | |
| 95 schedule(() => _barback.removeSources(assets), | |
| 96 "removing ${assets.join(', ')}"); | |
| 81 } | 97 } |
| 82 | 98 |
| 83 /// Removes [assets] from the current [PackageProvider]. | 99 /// Removes [assets] from the current [PackageProvider]. |
| 84 /// | 100 /// |
| 85 /// Each item in the list may either be an [AssetId] or a string that can be | 101 /// Each item in the list may either be an [AssetId] or a string that can be |
| 86 /// parsed as one. Note that this method is not automatically scheduled. | 102 /// parsed as one. |
| 87 void removeSources(Iterable assets) { | 103 /// |
| 88 // Allow strings as asset IDs. | 104 /// Unlike [removeSources], this is not automatically scheduled and will be run |
|
Bob Nystrom
2013/08/08 21:35:35
Nit, but make this part of previous paragraph.
nweiz
2013/08/09 19:41:51
Done.
| |
| 89 assets = assets.map((asset) { | 105 /// synchronously when called. |
| 106 void removeSourcesSync(Iterable assets) => | |
| 107 _barback.removeSources(_parseAssets(assets)); | |
| 108 | |
| 109 /// Parse a list of strings or [AssetId]s into a list of [AssetId]s. | |
| 110 List<AssetId> _parseAssets(Iterable assets) { | |
| 111 return assets.map((asset) { | |
| 90 if (asset is String) return new AssetId.parse(asset); | 112 if (asset is String) return new AssetId.parse(asset); |
| 91 return asset; | 113 return asset; |
| 92 }); | 114 }).toList(); |
| 93 | |
| 94 _barback.removeSources(assets); | |
| 95 } | 115 } |
| 96 | 116 |
| 97 /// Schedules a change to the contents of an asset identified by [name] to | 117 /// Schedules a change to the contents of an asset identified by [name] to |
| 98 /// [contents]. | 118 /// [contents]. |
| 99 /// | 119 /// |
| 100 /// Does not update it in the graph. | 120 /// Does not update it in the graph. |
| 101 void modifyAsset(String name, String contents) { | 121 void modifyAsset(String name, String contents) { |
| 102 schedule(() { | 122 schedule(() { |
| 103 _provider._modifyAsset(name, contents); | 123 _provider._modifyAsset(name, contents); |
| 104 }, "modify asset $name"); | 124 }, "modify asset $name"); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 /// after this. | 156 /// after this. |
| 137 void buildShouldNotBeDone() { | 157 void buildShouldNotBeDone() { |
| 138 _futureShouldNotCompleteUntil( | 158 _futureShouldNotCompleteUntil( |
| 139 _barback.results.elementAt(_nextBuildResult), | 159 _barback.results.elementAt(_nextBuildResult), |
| 140 schedule(() => pumpEventQueue(), "build should not terminate"), | 160 schedule(() => pumpEventQueue(), "build should not terminate"), |
| 141 "build"); | 161 "build"); |
| 142 } | 162 } |
| 143 | 163 |
| 144 /// Expects that the next [BuildResult] is a build success. | 164 /// Expects that the next [BuildResult] is a build success. |
| 145 void buildShouldSucceed() { | 165 void buildShouldSucceed() { |
| 146 expect(_getNextBuildResult().then((result) { | 166 expect(_getNextBuildResult("build should succeed").then((result) { |
| 147 result.errors.forEach(currentSchedule.signalError); | 167 result.errors.forEach(currentSchedule.signalError); |
| 148 expect(result.succeeded, isTrue); | 168 expect(result.succeeded, isTrue); |
| 149 }), completes); | 169 }), completes); |
| 150 } | 170 } |
| 151 | 171 |
| 152 /// Expects that the next [BuildResult] emitted is a failure. | 172 /// Expects that the next [BuildResult] emitted is a failure. |
| 153 /// | 173 /// |
| 154 /// [matchers] is a list of matchers to match against the errors that caused the | 174 /// [matchers] is a list of matchers to match against the errors that caused the |
| 155 /// build to fail. Every matcher is expected to match an error, but the order of | 175 /// build to fail. Every matcher is expected to match an error, but the order of |
| 156 /// matchers is unimportant. | 176 /// matchers is unimportant. |
| 157 void buildShouldFail(List matchers) { | 177 void buildShouldFail(List matchers) { |
| 158 expect(_getNextBuildResult().then((result) { | 178 expect(_getNextBuildResult("build should fail").then((result) { |
| 159 expect(result.succeeded, isFalse); | 179 expect(result.succeeded, isFalse); |
| 160 expect(result.errors.length, equals(matchers.length)); | 180 expect(result.errors.length, equals(matchers.length)); |
| 161 for (var matcher in matchers) { | 181 for (var matcher in matchers) { |
| 162 expect(result.errors, contains(matcher)); | 182 expect(result.errors, contains(matcher)); |
| 163 } | 183 } |
| 164 }), completes); | 184 }), completes); |
| 165 } | 185 } |
| 166 | 186 |
| 167 Future<BuildResult> _getNextBuildResult() => | 187 Future<BuildResult> _getNextBuildResult(String description) { |
| 168 _barback.results.elementAt(_nextBuildResult++); | 188 var result = currentSchedule.wrapFuture( |
| 169 | 189 _barback.results.elementAt(_nextBuildResult++)); |
| 170 /// Pauses the schedule until the currently running build completes. | 190 return schedule(() => result, description); |
| 171 /// | |
| 172 /// Validates that the build completed successfully. | |
| 173 void waitForBuild() { | |
| 174 schedule(() { | |
| 175 return _barback.results.first.then((result) { | |
| 176 expect(result.succeeded, isTrue); | |
| 177 }); | |
| 178 }, "wait for build"); | |
| 179 } | 191 } |
| 180 | 192 |
| 181 /// Schedules an expectation that the graph will deliver an asset matching | 193 /// Schedules an expectation that the graph will deliver an asset matching |
| 182 /// [name] and [contents]. | 194 /// [name] and [contents]. |
| 183 /// | 195 /// |
| 184 /// If [contents] is omitted, defaults to the asset's filename without an | 196 /// If [contents] is omitted, defaults to the asset's filename without an |
| 185 /// extension (which is the same default that [initGraph] uses). | 197 /// extension (which is the same default that [initGraph] uses). |
| 186 void expectAsset(String name, [String contents]) { | 198 void expectAsset(String name, [String contents]) { |
| 187 var id = new AssetId.parse(name); | 199 var id = new AssetId.parse(name); |
| 188 | 200 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 | 438 |
| 427 _MockAsset(this.id, this.contents); | 439 _MockAsset(this.id, this.contents); |
| 428 | 440 |
| 429 Future<String> readAsString({Encoding encoding}) => | 441 Future<String> readAsString({Encoding encoding}) => |
| 430 new Future.value(contents); | 442 new Future.value(contents); |
| 431 | 443 |
| 432 Stream<List<int>> read() => throw new UnimplementedError(); | 444 Stream<List<int>> read() => throw new UnimplementedError(); |
| 433 | 445 |
| 434 String toString() => "MockAsset $id $contents"; | 446 String toString() => "MockAsset $id $contents"; |
| 435 } | 447 } |
| OLD | NEW |