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

Side by Side Diff: test/transform/common.dart

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

Powered by Google App Engine
This is Rietveld 408576698