Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Side by Side Diff: pkg/barback/test/utils.dart

Issue 238503003: Make [Barback.getAllAssets] work when called synchronously. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/barback/test/package_graph/get_all_assets_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 /// [AssetId] and the value should be a string defining the contents of that 71 /// [AssetId] and the value should be a string defining the contents of that
72 /// asset. 72 /// asset.
73 /// 73 ///
74 /// [transformers] is a map from package names to the transformers for each 74 /// [transformers] is a map from package names to the transformers for each
75 /// package. 75 /// package.
76 void initGraph([assets, 76 void initGraph([assets,
77 Map<String, Iterable<Iterable<Transformer>>> transformers]) { 77 Map<String, Iterable<Iterable<Transformer>>> transformers]) {
78 if (assets == null) assets = []; 78 if (assets == null) assets = [];
79 if (transformers == null) transformers = {}; 79 if (transformers == null) transformers = {};
80 80
81 var assetList; 81 _provider = new MockProvider(assets, additionalPackages: transformers.keys);
82 if (assets is Map) {
83 assetList = assets.keys.map((asset) {
84 var id = new AssetId.parse(asset);
85 return new _MockAsset(id, assets[asset]);
86 });
87 } else if (assets is Iterable) {
88 assetList = assets.map((asset) {
89 var id = new AssetId.parse(asset);
90 var contents = pathos.basenameWithoutExtension(id.path);
91 return new _MockAsset(id, contents);
92 });
93 }
94
95 var assetMap = mapMapValues(groupBy(assetList, (asset) => asset.id.package),
96 (package, assets) => new AssetSet.from(assets));
97
98 // Make sure that packages that have transformers but no assets are considered
99 // by MockProvider to exist.
100 for (var package in transformers.keys) {
101 assetMap.putIfAbsent(package, () => new AssetSet());
102 }
103
104 _provider = new MockProvider(assetMap);
105 _barback = new Barback(_provider); 82 _barback = new Barback(_provider);
106 // Add a dummy listener to the log so it doesn't print to stdout. 83 // Add a dummy listener to the log so it doesn't print to stdout.
107 _barback.log.listen((_) {}); 84 _barback.log.listen((_) {});
108 _nextBuildResult = 0; 85 _nextBuildResult = 0;
109 _nextLog = 0; 86 _nextLog = 0;
110 87
111 schedule(() => transformers.forEach(_barback.updateTransformers)); 88 schedule(() => transformers.forEach(_barback.updateTransformers));
112 89
113 // There should be one successful build after adding all the transformers but 90 // There should be one successful build after adding all the transformers but
114 // before adding any sources. 91 // before adding any sources.
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 currentSchedule.signalError(error); 454 currentSchedule.signalError(error);
478 }); 455 });
479 456
480 return delay.then((_) => cancelable.cancel()); 457 return delay.then((_) => cancelable.cancel());
481 } 458 }
482 459
483 /// An [AssetProvider] that provides the given set of assets. 460 /// An [AssetProvider] that provides the given set of assets.
484 class MockProvider implements PackageProvider { 461 class MockProvider implements PackageProvider {
485 Iterable<String> get packages => _assets.keys; 462 Iterable<String> get packages => _assets.keys;
486 463
487 Map<String, AssetSet> _assets; 464 final Map<String, AssetSet> _assets;
488 465
489 /// The set of assets for which [MockLoadException]s should be emitted if 466 /// The set of assets for which [MockLoadException]s should be emitted if
490 /// they're loaded. 467 /// they're loaded.
491 final _errors = new Set<AssetId>(); 468 final _errors = new Set<AssetId>();
492 469
493 /// The completer that [getAsset()] is waiting on to complete when paused. 470 /// The completer that [getAsset()] is waiting on to complete when paused.
494 /// 471 ///
495 /// If `null` it will return the asset immediately. 472 /// If `null` it will return the asset immediately.
496 Completer _pauseCompleter; 473 Completer _pauseCompleter;
497 474
498 /// Tells the provider to wait during [getAsset] until [complete()] 475 /// Tells the provider to wait during [getAsset] until [complete()]
499 /// is called. 476 /// is called.
500 /// 477 ///
501 /// Lets you test the asynchronous behavior of loading. 478 /// Lets you test the asynchronous behavior of loading.
502 void _pause() { 479 void _pause() {
503 _pauseCompleter = new Completer(); 480 _pauseCompleter = new Completer();
504 } 481 }
505 482
506 void _resume() { 483 void _resume() {
507 _pauseCompleter.complete(); 484 _pauseCompleter.complete();
508 _pauseCompleter = null; 485 _pauseCompleter = null;
509 } 486 }
510 487
511 MockProvider(this._assets) { 488 MockProvider(assets, {Iterable<String> additionalPackages})
489 : _assets = _normalizeAssets(assets, additionalPackages);
490
491 static Map<String, AssetSet> _normalizeAssets(assets,
492 Iterable<String> additionalPackages) {
493 var assetList;
494 if (assets is Map) {
495 assetList = assets.keys.map((asset) {
496 var id = new AssetId.parse(asset);
497 return new _MockAsset(id, assets[asset]);
498 });
499 } else if (assets is Iterable) {
500 assetList = assets.map((asset) {
501 var id = new AssetId.parse(asset);
502 var contents = pathos.basenameWithoutExtension(id.path);
503 return new _MockAsset(id, contents);
504 });
505 }
506
507 var assetMap = mapMapValues(groupBy(assetList, (asset) => asset.id.package),
508 (package, assets) => new AssetSet.from(assets));
509
510 // Make sure that packages that have transformers but no assets are
511 // considered by MockProvider to exist.
512 if (additionalPackages != null) {
513 for (var package in additionalPackages) {
514 assetMap.putIfAbsent(package, () => new AssetSet());
515 }
516 }
517
512 // If there are no assets or transformers, add a dummy package. This better 518 // If there are no assets or transformers, add a dummy package. This better
513 // simulates the real world, where there'll always be at least the 519 // simulates the real world, where there'll always be at least the
514 // entrypoint package. 520 // entrypoint package.
515 if (_assets.isEmpty) { 521 return assetMap.isEmpty ? {"app": new AssetSet()} : assetMap;
516 _assets = {"app": new AssetSet()};
517 }
518 } 522 }
519 523
520 void _modifyAsset(String name, String contents) { 524 void _modifyAsset(String name, String contents) {
521 var id = new AssetId.parse(name); 525 var id = new AssetId.parse(name);
522 _errors.remove(id); 526 _errors.remove(id);
523 (_assets[id.package][id] as _MockAsset).contents = contents; 527 (_assets[id.package][id] as _MockAsset).contents = contents;
524 } 528 }
525 529
526 void _setAssetError(String name) { 530 void _setAssetError(String name) {
527 _errors.add(new AssetId.parse(name)); 531 _errors.add(new AssetId.parse(name));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 579
576 _MockAsset(this.id, this.contents); 580 _MockAsset(this.id, this.contents);
577 581
578 Future<String> readAsString({Encoding encoding}) => 582 Future<String> readAsString({Encoding encoding}) =>
579 new Future.value(contents); 583 new Future.value(contents);
580 584
581 Stream<List<int>> read() => throw new UnimplementedError(); 585 Stream<List<int>> read() => throw new UnimplementedError();
582 586
583 String toString() => "MockAsset $id $contents"; 587 String toString() => "MockAsset $id $contents";
584 } 588 }
OLDNEW
« no previous file with comments | « pkg/barback/test/package_graph/get_all_assets_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698