OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library polymer.test.transfom.common; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:barback/barback.dart'; |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 String idToString(AssetId id) => '${id.package}|${id.path}'; |
| 14 AssetId idFromString(String s) { |
| 15 int index = s.indexOf('|'); |
| 16 return new AssetId(s.substring(0, index), s.substring(index + 1)); |
| 17 } |
| 18 |
| 19 /** |
| 20 * A helper package provider that has files stored in memory, also wraps |
| 21 * [Barback] to simply our tests. |
| 22 */ |
| 23 class TestHelper implements PackageProvider { |
| 24 /** |
| 25 * Maps from an asset string identifier of the form 'package|path' to the |
| 26 * file contents. |
| 27 */ |
| 28 final Map<String, String> files; |
| 29 final Iterable<String> packages; |
| 30 |
| 31 Barback barback; |
| 32 var errorSubscription; |
| 33 var resultSubscription; |
| 34 |
| 35 Future<Asset> getAsset(AssetId id) => |
| 36 new Future.value(new Asset.fromString(id, files[idToString(id)])); |
| 37 TestHelper(List<List<Transformer>> transformers, Map<String, String> files) |
| 38 : files = files, |
| 39 packages = files.keys.map((s) => idFromString(s).package) { |
| 40 barback = new Barback(this); |
| 41 for (var p in packages) { |
| 42 barback.updateTransformers(p, transformers); |
| 43 } |
| 44 errorSubscription = barback.errors.listen((e) { |
| 45 var trace = getAttachedStackTrace(e); |
| 46 if (trace != null) { |
| 47 print(Trace.format(trace)); |
| 48 } |
| 49 fail('error running barback: $e'); |
| 50 }); |
| 51 resultSubscription = barback.results.listen((result) { |
| 52 expect(result.succeeded, isTrue, reason: "${result.errors}"); |
| 53 }); |
| 54 } |
| 55 |
| 56 void tearDown() { |
| 57 errorSubscription.cancel(); |
| 58 resultSubscription.cancel(); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Tells barback which files have changed, and thus anything that depends on |
| 63 * it on should be computed. By default mark all the input files. |
| 64 */ |
| 65 void run([Iterable<String> paths]) { |
| 66 if (paths == null) paths = files.keys; |
| 67 barback.updateSources(paths.map(idFromString)); |
| 68 } |
| 69 |
| 70 Future<String> operator [](String assetString){ |
| 71 return barback.getAssetById(idFromString(assetString)) |
| 72 .then((asset) => asset.readAsString()); |
| 73 } |
| 74 |
| 75 Future check(String assetIdString, String content) { |
| 76 return this[assetIdString].then((value) { |
| 77 expect(value, content, reason: 'Final output of $assetIdString differs.'); |
| 78 }); |
| 79 } |
| 80 |
| 81 Future checkAll(Map<String, String> files) { |
| 82 var futures = []; |
| 83 files.forEach((k, v) { |
| 84 futures.add(check(k, v)); |
| 85 }); |
| 86 return Future.wait(futures); |
| 87 } |
| 88 } |
| 89 |
| 90 testPhases(String testName, List<List<Transformer>> phases, |
| 91 Map<String, String> inputFiles, Map<String, String> expectedFiles) { |
| 92 test(testName, () { |
| 93 var helper = new TestHelper(phases, inputFiles)..run(); |
| 94 return helper.checkAll(expectedFiles).then((_) => helper.tearDown()); |
| 95 }); |
| 96 } |
| 97 |
OLD | NEW |