OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 barback.test.transformer.mock_aggregate; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:barback/barback.dart'; | |
10 import 'package:barback/src/utils.dart'; | |
11 import 'package:scheduled_test/scheduled_test.dart'; | |
12 | |
13 /// The abstract base class for aggregate transformers used to test barback. | |
14 /// | |
15 /// This adds the ability to pause and resume different components of the | |
16 /// transformers, and to tell whether they're running, when they start running, | |
17 /// and how many times they've run. | |
18 /// | |
19 /// Transformers extending this should override [doClassifyPrimary] and | |
20 /// [doApply] rather than [classifyPrimary] and [apply], and they should use | |
21 /// [getInput] and [getPrimaryInputs] rather than [transform.getInput] and | |
22 /// [transform.primaryInputs]. | |
23 abstract class MockAggregateTransformer extends AggregateTransformer { | |
24 /// The number of times the transformer has been applied. | |
25 /// | |
26 /// This is scheduled. The Future will complete at the point in the schedule | |
27 /// that this is called. | |
28 Future<int> get numRuns => schedule(() => _numRuns); | |
29 var _numRuns = 0; | |
30 | |
31 /// The number of currently running transforms. | |
32 int _runningTransforms = 0; | |
33 | |
34 /// A completer for pausing the transformer before it finishes running | |
35 /// [apply]. | |
36 Completer _apply; | |
37 | |
38 /// Completers for pausing the transformer before it finishes running | |
39 /// [classifyPrimary]. | |
40 final _classifyPrimary = new Map<AssetId, Completer>(); | |
41 | |
42 /// Completers for pausing the transformer before it finishes getting inputs | |
43 /// the [Transform]. | |
44 final _getInput = new Map<AssetId, Completer>(); | |
45 | |
46 /// Completer for pausing the transformer before it accesses | |
47 /// [getPrimaryInputs]. | |
48 Completer _primaryInputs; | |
49 | |
50 /// A completer that completes once this transformer begins running. | |
51 /// | |
52 /// Once this transformer finishes running, this is reset to a new completer, | |
53 /// so it can be used multiple times. | |
54 var _started = new Completer(); | |
55 | |
56 /// `true` if any transforms are currently running. | |
57 /// | |
58 /// This is scheduled. The Future will complete at the point in the schedule | |
59 /// that this is called. | |
60 Future<bool> get isRunning => schedule(() => _runningTransforms > 0); | |
61 | |
62 /// All elements of this set will be automatically consumed during [apply]. | |
63 final consumePrimaries = new Set<String>(); | |
64 | |
65 /// Pauses the schedule until this transformer begins running. | |
66 void waitUntilStarted() { | |
67 schedule(() => _started.future, "wait until $this starts"); | |
68 } | |
69 | |
70 /// Causes the transformer to pause after running [apply] but before the | |
71 /// returned Future completes. | |
72 /// | |
73 /// This can be resumed by calling [resumeApply]. This operation is scheduled. | |
74 void pauseApply() { | |
75 schedule(() { | |
76 _apply = new Completer(); | |
77 }, "pause apply for $this"); | |
78 } | |
79 | |
80 /// Resumes the transformer's [apply] call after [pauseApply] was called. | |
81 /// | |
82 /// This operation is scheduled. | |
83 void resumeApply() { | |
84 schedule(() { | |
85 _apply.complete(); | |
86 _apply = null; | |
87 }, "resume apply for $this"); | |
88 } | |
89 | |
90 /// Causes the transformer to pause after running [classifyPrimary] on the | |
91 /// asset with the given [name], but before the returned Future completes. | |
92 /// | |
93 /// This can be resumed by calling [resumeClassifyPrimary]. This operation is | |
94 /// scheduled. | |
95 void pauseClassifyPrimary(String name) { | |
96 schedule(() { | |
97 _classifyPrimary[new AssetId.parse(name)] = new Completer(); | |
98 }, "pause classifyPrimary($name) for $this"); | |
99 } | |
100 | |
101 /// Resumes the transformer's [classifyPrimary] call on the asset with the | |
102 /// given [name] after [pauseClassifyPrimary] was called. | |
103 /// | |
104 /// This operation is scheduled. | |
105 void resumeClassifyPrimary(String name) { | |
106 schedule(() { | |
107 _classifyPrimary.remove(new AssetId.parse(name)).complete(); | |
108 }, "resume classifyPrimary($name) for $this"); | |
109 } | |
110 | |
111 /// Causes the transformer to pause while loading the secondary input with | |
112 /// the given [name]. | |
113 /// | |
114 /// This can be resumed by calling [resumeGetInput]. This operation is | |
115 /// scheduled. | |
116 void pauseGetInput(String name) { | |
117 schedule(() { | |
118 _getInput[new AssetId.parse(name)] = new Completer(); | |
119 }, "pause getInput($name) for $this"); | |
120 } | |
121 | |
122 /// Resumes the transformer's loading of the input with the given [name] after | |
123 /// [pauseGetInput] was called. | |
124 /// | |
125 /// This operation is scheduled. | |
126 void resumeGetInput(String name) { | |
127 schedule(() { | |
128 _getInput.remove(new AssetId.parse(name)).complete(); | |
129 }, "resume getInput($name) for $this"); | |
130 } | |
131 | |
132 /// Causes the transformer to pause before accessing [getPrimaryInputs]. | |
133 /// | |
134 /// This can be resumed by calling [resumePrimaryInputs]. This operation is | |
135 /// scheduled. | |
136 void pausePrimaryInputs() { | |
137 schedule(() { | |
138 _primaryInputs = new Completer(); | |
139 }, "pause primaryInputs for $this"); | |
140 } | |
141 | |
142 /// Resumes the transformer's invocation of [primaryInputs] after | |
143 /// [pausePrimaryInputs] was called. | |
144 /// | |
145 /// This operation is scheduled. | |
146 void resumePrimaryInputs() { | |
147 schedule(() { | |
148 _primaryInputs.complete(); | |
149 _primaryInputs = null; | |
150 }, "resume primaryInputs for $this"); | |
151 } | |
152 | |
153 /// Like [AggregateTransform.getInput], but respects [pauseGetInput]. | |
154 /// | |
155 /// This is intended for use by subclasses of [MockAggregateTransformer]. | |
156 Future<Asset> getInput(AggregateTransform transform, AssetId id) { | |
157 return newFuture(() { | |
158 if (_getInput.containsKey(id)) return _getInput[id].future; | |
159 }).then((_) => transform.getInput(id)); | |
160 } | |
161 | |
162 /// Like [AggregateTransform.primaryInputs], but respects | |
163 /// [pausePrimaryInputs]. | |
164 /// | |
165 /// This is intended for use by subclasses of [MockAggregateTransformer]. | |
166 Stream<Asset> getPrimaryInputs(AggregateTransform transform) { | |
167 return futureStream(newFuture(() { | |
168 if (_primaryInputs != null) return _primaryInputs.future; | |
169 }).then((_) => transform.primaryInputs)); | |
170 } | |
171 | |
172 Future<String> classifyPrimary(AssetId id) { | |
173 return newFuture(() => doClassifyPrimary(id)).then((result) { | |
174 return newFuture(() { | |
175 if (_classifyPrimary.containsKey(id)) { | |
176 return _classifyPrimary[id].future; | |
177 } | |
178 }).then((_) => result); | |
179 }); | |
180 } | |
181 | |
182 Future apply(AggregateTransform transform) { | |
183 _numRuns++; | |
184 if (_runningTransforms == 0) _started.complete(); | |
185 _runningTransforms++; | |
186 return newFuture(() => doApply(transform)).then((_) { | |
187 if (_apply != null) return _apply.future; | |
188 }).whenComplete(() { | |
189 for (var id in consumePrimaries) { | |
190 transform.consumePrimary(new AssetId.parse(id)); | |
191 } | |
192 _runningTransforms--; | |
193 if (_runningTransforms == 0) _started = new Completer(); | |
194 }); | |
195 } | |
196 | |
197 /// The wrapped version of [classifyPrimary] for subclasses to override. | |
198 /// | |
199 /// This may return a `Future<String>` or, if it's entirely synchronous, a | |
200 /// `String`. | |
201 doClassifyPrimary(AssetId id); | |
202 | |
203 /// The wrapped version of [doApply] for subclasses to override. | |
204 /// | |
205 /// If this does asynchronous work, it should return a [Future] that completes | |
206 /// once it's finished. | |
207 doApply(AggregateTransform transform); | |
208 } | |
OLD | NEW |