| 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:convert' show Encoding; | 9 import 'dart:convert' show Encoding; |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 var _configured = false; | 28 var _configured = false; |
| 29 | 29 |
| 30 MockProvider _provider; | 30 MockProvider _provider; |
| 31 Barback _barback; | 31 Barback _barback; |
| 32 | 32 |
| 33 /// Calls to [buildShouldSucceed] and [buildShouldFail] set expectations on | 33 /// Calls to [buildShouldSucceed] and [buildShouldFail] set expectations on |
| 34 /// successive [BuildResult]s from [_barback]. This keeps track of how many | 34 /// successive [BuildResult]s from [_barback]. This keeps track of how many |
| 35 /// calls have already been made so later calls know which result to look for. | 35 /// calls have already been made so later calls know which result to look for. |
| 36 int _nextBuildResult; | 36 int _nextBuildResult; |
| 37 | 37 |
| 38 /// Calls to [buildShouldLog] set expectations on successive log entries from |
| 39 /// [_barback]. This keeps track of how many calls have already been made so |
| 40 /// later calls know which result to look for. |
| 41 int _nextLog; |
| 42 |
| 38 void initConfig() { | 43 void initConfig() { |
| 39 if (_configured) return; | 44 if (_configured) return; |
| 40 _configured = true; | 45 _configured = true; |
| 41 useCompactVMConfiguration(); | 46 useCompactVMConfiguration(); |
| 42 filterStacks = true; | 47 filterStacks = true; |
| 43 } | 48 } |
| 44 | 49 |
| 45 /// Creates a new [PackageProvider] and [PackageGraph] with the given [assets] | 50 /// Creates a new [PackageProvider] and [PackageGraph] with the given [assets] |
| 46 /// and [transformers]. | 51 /// and [transformers]. |
| 47 /// | 52 /// |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 86 |
| 82 // Make sure that packages that have transformers but no assets are considered | 87 // Make sure that packages that have transformers but no assets are considered |
| 83 // by MockProvider to exist. | 88 // by MockProvider to exist. |
| 84 for (var package in transformers.keys) { | 89 for (var package in transformers.keys) { |
| 85 assetMap.putIfAbsent(package, () => new AssetSet()); | 90 assetMap.putIfAbsent(package, () => new AssetSet()); |
| 86 } | 91 } |
| 87 | 92 |
| 88 _provider = new MockProvider(assetMap); | 93 _provider = new MockProvider(assetMap); |
| 89 _barback = new Barback(_provider); | 94 _barback = new Barback(_provider); |
| 90 _nextBuildResult = 0; | 95 _nextBuildResult = 0; |
| 96 _nextLog = 0; |
| 91 | 97 |
| 92 transformers.forEach(_barback.updateTransformers); | 98 transformers.forEach(_barback.updateTransformers); |
| 93 | 99 |
| 94 // There should be one successful build after adding all the transformers but | 100 // There should be one successful build after adding all the transformers but |
| 95 // before adding any sources. | 101 // before adding any sources. |
| 96 if (!transformers.isEmpty) buildShouldSucceed(); | 102 if (!transformers.isEmpty) buildShouldSucceed(); |
| 97 } | 103 } |
| 98 | 104 |
| 99 /// Updates [assets] in the current [PackageProvider]. | 105 /// Updates [assets] in the current [PackageProvider]. |
| 100 /// | 106 /// |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 void buildShouldFail(List matchers) { | 215 void buildShouldFail(List matchers) { |
| 210 expect(_getNextBuildResult("build should fail").then((result) { | 216 expect(_getNextBuildResult("build should fail").then((result) { |
| 211 expect(result.succeeded, isFalse); | 217 expect(result.succeeded, isFalse); |
| 212 expect(result.errors.length, equals(matchers.length)); | 218 expect(result.errors.length, equals(matchers.length)); |
| 213 for (var matcher in matchers) { | 219 for (var matcher in matchers) { |
| 214 expect(result.errors, contains(matcher)); | 220 expect(result.errors, contains(matcher)); |
| 215 } | 221 } |
| 216 }), completes); | 222 }), completes); |
| 217 } | 223 } |
| 218 | 224 |
| 225 /// Expects that the nexted logged [LogEntry] matches [matcher] which may be |
| 226 /// either a [Matcher] or a string to match a literal string. |
| 227 void buildShouldLog(LogLevel level, matcher) { |
| 228 expect(_getNextLog("build should log").then((log) { |
| 229 expect(log.level, equals(level)); |
| 230 expect(log.message, matcher); |
| 231 }), completes); |
| 232 } |
| 233 |
| 219 Future<BuildResult> _getNextBuildResult(String description) { | 234 Future<BuildResult> _getNextBuildResult(String description) { |
| 220 var result = currentSchedule.wrapFuture( | 235 var result = currentSchedule.wrapFuture( |
| 221 _barback.results.elementAt(_nextBuildResult++)); | 236 _barback.results.elementAt(_nextBuildResult++)); |
| 222 return schedule(() => result, description); | 237 return schedule(() => result, description); |
| 223 } | 238 } |
| 224 | 239 |
| 240 Future<LogEntry> _getNextLog(String description) { |
| 241 var result = currentSchedule.wrapFuture( |
| 242 _barback.log.elementAt(_nextLog++)); |
| 243 return schedule(() => result, description); |
| 244 } |
| 245 |
| 225 /// Schedules an expectation that the graph will deliver an asset matching | 246 /// Schedules an expectation that the graph will deliver an asset matching |
| 226 /// [name] and [contents]. | 247 /// [name] and [contents]. |
| 227 /// | 248 /// |
| 228 /// If [contents] is omitted, defaults to the asset's filename without an | 249 /// If [contents] is omitted, defaults to the asset's filename without an |
| 229 /// extension (which is the same default that [initGraph] uses). | 250 /// extension (which is the same default that [initGraph] uses). |
| 230 void expectAsset(String name, [String contents]) { | 251 void expectAsset(String name, [String contents]) { |
| 231 var id = new AssetId.parse(name); | 252 var id = new AssetId.parse(name); |
| 232 | 253 |
| 233 if (contents == null) { | 254 if (contents == null) { |
| 234 contents = pathos.basenameWithoutExtension(id.path); | 255 contents = pathos.basenameWithoutExtension(id.path); |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 | 542 |
| 522 _MockAsset(this.id, this.contents); | 543 _MockAsset(this.id, this.contents); |
| 523 | 544 |
| 524 Future<String> readAsString({Encoding encoding}) => | 545 Future<String> readAsString({Encoding encoding}) => |
| 525 new Future.value(contents); | 546 new Future.value(contents); |
| 526 | 547 |
| 527 Stream<List<int>> read() => throw new UnimplementedError(); | 548 Stream<List<int>> read() => throw new UnimplementedError(); |
| 528 | 549 |
| 529 String toString() => "MockAsset $id $contents"; | 550 String toString() => "MockAsset $id $contents"; |
| 530 } | 551 } |
| OLD | NEW |