| 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:convert' show Encoding; | 8 import 'dart:convert' show Encoding; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 /// | 77 /// |
| 78 /// [assets] may either be an [Iterable] or a [Map]. If it's an [Iterable], | 78 /// [assets] may either be an [Iterable] or a [Map]. If it's an [Iterable], |
| 79 /// each element may either be an [AssetId] or a string that can be parsed to | 79 /// each element may either be an [AssetId] or a string that can be parsed to |
| 80 /// one. If it's a [Map], each key should be a string that can be parsed to an | 80 /// one. If it's a [Map], each key should be a string that can be parsed to an |
| 81 /// [AssetId] and the value should be a string defining the contents of that | 81 /// [AssetId] and the value should be a string defining the contents of that |
| 82 /// asset. | 82 /// asset. |
| 83 /// | 83 /// |
| 84 /// [transformers] is a map from package names to the transformers for each | 84 /// [transformers] is a map from package names to the transformers for each |
| 85 /// package. | 85 /// package. |
| 86 void initGraph([assets, | 86 void initGraph([assets, |
| 87 Map<String, Iterable<Iterable<Transformer>>> transformers]) { | 87 Map<String, Iterable<Iterable<Transformer>>> transformers]) => |
| 88 initStaticGraph(assets, transformers: transformers); |
| 89 |
| 90 void initStaticGraph(assets, {Iterable<String> staticPackages, |
| 91 Map<String, Iterable<Iterable<Transformer>>> transformers}) { |
| 88 if (assets == null) assets = []; | 92 if (assets == null) assets = []; |
| 93 if (staticPackages == null) staticPackages = []; |
| 89 if (transformers == null) transformers = {}; | 94 if (transformers == null) transformers = {}; |
| 90 | 95 |
| 91 _provider = new MockProvider(assets, additionalPackages: transformers.keys); | 96 _provider = new MockProvider(assets, |
| 97 staticPackages: staticPackages, |
| 98 additionalPackages: transformers.keys); |
| 92 _barback = new Barback(_provider); | 99 _barback = new Barback(_provider); |
| 93 // Add a dummy listener to the log so it doesn't print to stdout. | 100 // Add a dummy listener to the log so it doesn't print to stdout. |
| 94 _barback.log.listen((_) {}); | 101 _barback.log.listen((_) {}); |
| 95 _nextBuildResult = 0; | 102 _nextBuildResult = 0; |
| 96 _nextLog = 0; | 103 _nextLog = 0; |
| 97 | 104 |
| 98 schedule(() => transformers.forEach(_barback.updateTransformers)); | 105 schedule(() => transformers.forEach(_barback.updateTransformers)); |
| 99 | 106 |
| 100 // There should be one successful build after adding all the transformers but | 107 // There should be one successful build after adding all the transformers but |
| 101 // before adding any sources. | 108 // before adding any sources. |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 "completed with result: $result"), | 469 "completed with result: $result"), |
| 463 trace); | 470 trace); |
| 464 }).catchError((error) { | 471 }).catchError((error) { |
| 465 currentSchedule.signalError(error); | 472 currentSchedule.signalError(error); |
| 466 }); | 473 }); |
| 467 | 474 |
| 468 return delay.then((_) => cancelable.cancel()); | 475 return delay.then((_) => cancelable.cancel()); |
| 469 } | 476 } |
| 470 | 477 |
| 471 /// An [AssetProvider] that provides the given set of assets. | 478 /// An [AssetProvider] that provides the given set of assets. |
| 472 class MockProvider implements PackageProvider { | 479 class MockProvider implements StaticPackageProvider { |
| 473 Iterable<String> get packages => _assets.keys; | 480 Iterable<String> get packages => |
| 481 _assets.keys.toSet().difference(staticPackages); |
| 482 |
| 483 final Set<String> staticPackages; |
| 474 | 484 |
| 475 final Map<String, AssetSet> _assets; | 485 final Map<String, AssetSet> _assets; |
| 476 | 486 |
| 477 /// The set of assets for which [MockLoadException]s should be emitted if | 487 /// The set of assets for which [MockLoadException]s should be emitted if |
| 478 /// they're loaded. | 488 /// they're loaded. |
| 479 final _errors = new Set<AssetId>(); | 489 final _errors = new Set<AssetId>(); |
| 480 | 490 |
| 481 /// The set of assets for which synchronous [MockLoadException]s should be | 491 /// The set of assets for which synchronous [MockLoadException]s should be |
| 482 /// emitted if they're loaded. | 492 /// emitted if they're loaded. |
| 483 final _syncErrors = new Set<AssetId>(); | 493 final _syncErrors = new Set<AssetId>(); |
| 484 | 494 |
| 485 /// The completer that [getAsset()] is waiting on to complete when paused. | 495 /// The completer that [getAsset()] is waiting on to complete when paused. |
| 486 /// | 496 /// |
| 487 /// If `null` it will return the asset immediately. | 497 /// If `null` it will return the asset immediately. |
| 488 Completer _pauseCompleter; | 498 Completer _pauseCompleter; |
| 489 | 499 |
| 490 /// Tells the provider to wait during [getAsset] until [complete()] | 500 /// Tells the provider to wait during [getAsset] until [complete()] |
| 491 /// is called. | 501 /// is called. |
| 492 /// | 502 /// |
| 493 /// Lets you test the asynchronous behavior of loading. | 503 /// Lets you test the asynchronous behavior of loading. |
| 494 void _pause() { | 504 void _pause() { |
| 495 _pauseCompleter = new Completer(); | 505 _pauseCompleter = new Completer(); |
| 496 } | 506 } |
| 497 | 507 |
| 498 void _resume() { | 508 void _resume() { |
| 499 _pauseCompleter.complete(); | 509 _pauseCompleter.complete(); |
| 500 _pauseCompleter = null; | 510 _pauseCompleter = null; |
| 501 } | 511 } |
| 502 | 512 |
| 503 MockProvider(assets, {Iterable<String> additionalPackages}) | 513 MockProvider(assets, {Iterable<String> staticPackages, |
| 504 : _assets = _normalizeAssets(assets, additionalPackages); | 514 Iterable<String> additionalPackages}) |
| 515 : staticPackages = staticPackages == null ? new Set() : |
| 516 staticPackages.toSet(), |
| 517 _assets = _normalizeAssets(assets, additionalPackages); |
| 505 | 518 |
| 506 static Map<String, AssetSet> _normalizeAssets(assets, | 519 static Map<String, AssetSet> _normalizeAssets(assets, |
| 507 Iterable<String> additionalPackages) { | 520 Iterable<String> additionalPackages) { |
| 508 var assetList; | 521 var assetList; |
| 509 if (assets is Map) { | 522 if (assets is Map) { |
| 510 assetList = assets.keys.map((asset) { | 523 assetList = assets.keys.map((asset) { |
| 511 var id = new AssetId.parse(asset); | 524 var id = new AssetId.parse(asset); |
| 512 return new _MockAsset(id, assets[asset]); | 525 return new _MockAsset(id, assets[asset]); |
| 513 }); | 526 }); |
| 514 } else if (assets is Iterable) { | 527 } else if (assets is Iterable) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 540 var id = new AssetId.parse(name); | 553 var id = new AssetId.parse(name); |
| 541 _errors.remove(id); | 554 _errors.remove(id); |
| 542 _syncErrors.remove(id); | 555 _syncErrors.remove(id); |
| 543 (_assets[id.package][id] as _MockAsset).contents = contents; | 556 (_assets[id.package][id] as _MockAsset).contents = contents; |
| 544 } | 557 } |
| 545 | 558 |
| 546 void _setAssetError(String name, bool async) { | 559 void _setAssetError(String name, bool async) { |
| 547 (async ? _errors : _syncErrors).add(new AssetId.parse(name)); | 560 (async ? _errors : _syncErrors).add(new AssetId.parse(name)); |
| 548 } | 561 } |
| 549 | 562 |
| 550 List<AssetId> listAssets(String package, {String within}) { | 563 List<AssetId> getAllAssets(String package) => |
| 551 if (within != null) { | 564 _assets[package].map((asset) => asset.id); |
| 552 throw new UnimplementedError("Doesn't handle 'within' yet."); | |
| 553 } | |
| 554 | |
| 555 return _assets[package].map((asset) => asset.id); | |
| 556 } | |
| 557 | 565 |
| 558 Future<Asset> getAsset(AssetId id) { | 566 Future<Asset> getAsset(AssetId id) { |
| 559 // Eagerly load the asset so we can test an asset's value changing between | 567 // Eagerly load the asset so we can test an asset's value changing between |
| 560 // when a load starts and when it finishes. | 568 // when a load starts and when it finishes. |
| 561 var assets = _assets[id.package]; | 569 var assets = _assets[id.package]; |
| 562 var asset; | 570 var asset; |
| 563 if (assets != null) asset = assets[id]; | 571 if (assets != null) asset = assets[id]; |
| 564 | 572 |
| 565 if (_syncErrors.contains(id)) throw new MockLoadException(id); | 573 if (_syncErrors.contains(id)) throw new MockLoadException(id); |
| 566 var hasError = _errors.contains(id); | 574 var hasError = _errors.contains(id); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 596 | 604 |
| 597 _MockAsset(this.id, this.contents); | 605 _MockAsset(this.id, this.contents); |
| 598 | 606 |
| 599 Future<String> readAsString({Encoding encoding}) => | 607 Future<String> readAsString({Encoding encoding}) => |
| 600 new Future.value(contents); | 608 new Future.value(contents); |
| 601 | 609 |
| 602 Stream<List<int>> read() => throw new UnimplementedError(); | 610 Stream<List<int>> read() => throw new UnimplementedError(); |
| 603 | 611 |
| 604 String toString() => "MockAsset $id $contents"; | 612 String toString() => "MockAsset $id $contents"; |
| 605 } | 613 } |
| OLD | NEW |