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