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 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 | 320 |
| 321 Future<bool> isPrimary(AssetId asset) { | 321 Future<bool> isPrimary(AssetId asset) { |
| 322 return new Future.value(asset.extension == ".$from"); | 322 return new Future.value(asset.extension == ".$from"); |
| 323 } | 323 } |
| 324 | 324 |
| 325 Future apply(Transform transform) { | 325 Future apply(Transform transform) { |
| 326 numRuns++; | 326 numRuns++; |
| 327 if (!_started.isCompleted) _started.complete(); | 327 if (!_started.isCompleted) _started.complete(); |
| 328 _runningTransforms++; | 328 _runningTransforms++; |
| 329 return transform.primaryInput.then((input) { | 329 return transform.primaryInput.then((input) { |
| 330 for (var extension in to.split(" ")) { | 330 return Future.wait(to.split(" ").map((extension) { |
| 331 var id = transform.primaryId.changeExtension(".$extension"); | 331 var id = transform.primaryId.changeExtension(".$extension"); |
| 332 var content = input.readAsString() + ".$extension"; | 332 return input.readAsString().then((content) { |
| 333 transform.addOutput(id, new MockAsset(id, content)); | 333 transform.addOutput(id, new MockAsset(id, "$content.$extension")); |
| 334 | 334 }); |
| 335 } | 335 })).then((_) { |
| 336 | 336 if (_wait != null) return _wait.future; |
| 337 if (_wait != null) return _wait.future; | 337 }); |
| 338 }).whenComplete(() { | 338 }).whenComplete(() { |
| 339 _runningTransforms--; | 339 _runningTransforms--; |
| 340 }); | 340 }); |
| 341 } | 341 } |
| 342 | 342 |
| 343 String toString() => "$from->$to"; | 343 String toString() => "$from->$to"; |
| 344 } | 344 } |
| 345 | 345 |
| 346 /// A [Transformer] that takes an input asset that contains a comma-separated | 346 /// A [Transformer] that takes an input asset that contains a comma-separated |
| 347 /// list of paths and outputs a file for each path. | 347 /// list of paths and outputs a file for each path. |
| 348 class OneToManyTransformer extends Transformer { | 348 class OneToManyTransformer extends Transformer { |
| 349 final String extension; | 349 final String extension; |
| 350 | 350 |
| 351 /// The number of times the transformer has been applied. | 351 /// The number of times the transformer has been applied. |
| 352 int numRuns = 0; | 352 int numRuns = 0; |
| 353 | 353 |
| 354 /// Creates a transformer that consumes assets with [extension]. | 354 /// Creates a transformer that consumes assets with [extension]. |
| 355 /// | 355 /// |
| 356 /// That file contains a comma-separated list of paths and it will output | 356 /// That file contains a comma-separated list of paths and it will output |
| 357 /// files at each of those paths. | 357 /// files at each of those paths. |
| 358 OneToManyTransformer(this.extension); | 358 OneToManyTransformer(this.extension); |
| 359 | 359 |
| 360 Future<bool> isPrimary(AssetId asset) { | 360 Future<bool> isPrimary(AssetId asset) { |
| 361 return new Future.value(asset.extension == ".$extension"); | 361 return new Future.value(asset.extension == ".$extension"); |
| 362 } | 362 } |
| 363 | 363 |
| 364 Future apply(Transform transform) { | 364 Future apply(Transform transform) { |
| 365 numRuns++; | 365 numRuns++; |
| 366 return transform.primaryInput.then((input) { | 366 return transform.primaryInput.then((input) { |
| 367 for (var line in input.readAsString().split(",")) { | 367 return input.readAsString().then((lines) { |
| 368 var id = new AssetId(transform.primaryId.package, line); | 368 for (var line in lines.split(",")) { |
| 369 transform.addOutput(id, new MockAsset(id, "spread $extension")); | 369 var id = new AssetId(transform.primaryId.package, line); |
| 370 } | 370 transform.addOutput(id, new MockAsset(id, "spread $extension")); |
| 371 } | |
| 372 }); | |
| 371 }); | 373 }); |
| 372 } | 374 } |
| 373 | 375 |
| 374 String toString() => "1->many $extension"; | 376 String toString() => "1->many $extension"; |
| 375 } | 377 } |
| 376 | 378 |
| 377 /// A transformer that uses the contents of a file to define the other inputs. | 379 /// A transformer that uses the contents of a file to define the other inputs. |
| 378 /// | 380 /// |
| 379 /// Outputs a file with the same name as the primary but with an "out" | 381 /// Outputs a file with the same name as the primary but with an "out" |
| 380 /// extension containing the concatenated contents of all non-primary inputs. | 382 /// extension containing the concatenated contents of all non-primary inputs. |
| 381 class ManyToOneTransformer extends Transformer { | 383 class ManyToOneTransformer extends Transformer { |
| 382 final String extension; | 384 final String extension; |
| 383 | 385 |
| 384 /// The number of times the transformer has been applied. | 386 /// The number of times the transformer has been applied. |
| 385 int numRuns = 0; | 387 int numRuns = 0; |
| 386 | 388 |
| 387 /// Creates a transformer that consumes assets with [extension]. | 389 /// Creates a transformer that consumes assets with [extension]. |
| 388 /// | 390 /// |
| 389 /// That file contains a comma-separated list of paths and it will input | 391 /// That file contains a comma-separated list of paths and it will input |
| 390 /// files at each of those paths. | 392 /// files at each of those paths. |
| 391 ManyToOneTransformer(this.extension); | 393 ManyToOneTransformer(this.extension); |
| 392 | 394 |
| 393 Future<bool> isPrimary(AssetId asset) { | 395 Future<bool> isPrimary(AssetId asset) { |
| 394 return new Future.value(asset.extension == ".$extension"); | 396 return new Future.value(asset.extension == ".$extension"); |
| 395 } | 397 } |
| 396 | 398 |
| 397 Future apply(Transform transform) { | 399 Future apply(Transform transform) { |
| 398 numRuns++; | 400 numRuns++; |
| 399 return transform.primaryInput.then((primary) { | 401 return transform.primaryInput.then((primary) { |
| 400 // Get all of the included inputs. | 402 return primary.readAsString().then((contents) { |
| 401 var inputs = primary.readAsString().split(",").map((path) { | 403 // Get all of the included inputs. |
| 402 var id = new AssetId(transform.primaryId.package, path); | 404 var inputs = contents.split(",").map((path) { |
| 403 return transform.getInput(id); | 405 var id = new AssetId(transform.primaryId.package, path); |
| 404 }); | 406 return transform.getInput(id); |
| 407 }); | |
| 405 | 408 |
| 406 // Concatenate them to one output. | 409 return Future.wait(inputs); |
| 407 return Future.wait(inputs).then((inputs) { | 410 }).then((inputs) { |
| 408 var id = transform.primaryId.changeExtension(".out"); | 411 // Concatenate them to one output. |
| 409 var contents = inputs.map((input) => input.readAsString()).join(); | 412 var output = ""; |
| 410 transform.addOutput(id, new MockAsset(id, contents)); | 413 return Future.forEach(inputs, (input) { |
| 414 return input.readAsString().then((contents) { | |
| 415 output += contents; | |
|
nweiz
2013/07/03 18:21:07
Doing this imperatively seems less clean than the
Bob Nystrom
2013/07/03 19:45:23
I know, but Future.forEach() doesn't actually retu
nweiz
2013/07/03 20:11:16
[Future.wait] had better guarantee ordering; we re
| |
| 416 }); | |
| 417 }).then((_) { | |
| 418 var id = transform.primaryId.changeExtension(".out"); | |
| 419 transform.addOutput(id, new MockAsset(id, output)); | |
| 420 }); | |
| 411 }); | 421 }); |
| 412 }); | 422 }); |
| 413 } | 423 } |
| 414 | 424 |
| 415 String toString() => "many->1 $extension"; | 425 String toString() => "many->1 $extension"; |
| 416 } | 426 } |
| 417 | 427 |
| 418 /// A transformer that throws an exception when run, after generating the | 428 /// A transformer that throws an exception when run, after generating the |
| 419 /// given outputs. | 429 /// given outputs. |
| 420 class BadTransformer extends Transformer { | 430 class BadTransformer extends Transformer { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 441 } | 451 } |
| 442 } | 452 } |
| 443 | 453 |
| 444 /// An implementation of [Asset] that never hits the file system. | 454 /// An implementation of [Asset] that never hits the file system. |
| 445 class MockAsset implements Asset { | 455 class MockAsset implements Asset { |
| 446 final AssetId _id; | 456 final AssetId _id; |
| 447 String _contents; | 457 String _contents; |
| 448 | 458 |
| 449 MockAsset(this._id, this._contents); | 459 MockAsset(this._id, this._contents); |
| 450 | 460 |
| 451 String readAsString() => _contents; | 461 Future<String> readAsString() => new Future.value(_contents); |
| 452 Stream<List<int>> read() => throw new UnimplementedError(); | 462 Stream<List<int>> read() => throw new UnimplementedError(); |
| 453 | 463 |
| 454 serialize() => throw new UnimplementedError(); | 464 serialize() => throw new UnimplementedError(); |
| 455 | 465 |
| 456 String toString() => "MockAsset $_id $_contents"; | 466 String toString() => "MockAsset $_id $_contents"; |
| 457 } | 467 } |
| OLD | NEW |