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.transformer.mock; | 5 library barback.test.transformer.mock; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
| 10 import 'package:barback/src/utils.dart'; | 10 import 'package:barback/src/utils.dart'; |
| 11 import 'package:scheduled_test/scheduled_test.dart'; | |
| 11 | 12 |
| 12 /// The abstract base class for transformers used to test barback. | 13 /// The abstract base class for transformers used to test barback. |
| 13 /// | 14 /// |
| 14 /// This adds the ability to pause and resume different components of the | 15 /// This adds the ability to pause and resume different components of the |
| 15 /// transformers, and to tell whether they're running, when they start running, | 16 /// transformers, and to tell whether they're running, when they start running, |
| 16 /// and how many times they've run. | 17 /// and how many times they've run. |
| 17 /// | 18 /// |
| 18 /// Transformers extending this should override [doIsPrimary] and [doApply] | 19 /// Transformers extending this should override [doIsPrimary] and [doApply] |
| 19 /// rather than [isPrimary] and [apply], and they should use [getInput] and | 20 /// rather than [isPrimary] and [apply], and they should use [getInput] and |
| 20 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. | 21 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. |
| 21 abstract class MockTransformer extends Transformer { | 22 abstract class MockTransformer extends Transformer { |
| 22 /// The number of times the transformer has been applied. | 23 /// The number of times the transformer has been applied. |
| 23 int get numRuns => _numRuns; | 24 /// |
| 25 /// This is scheduled. The Future will complete at the point in the schedule | |
| 26 /// that this is called. | |
|
Bob Nystrom
2013/08/08 21:35:35
Scheduled getters feel a bit magical to me. What m
nweiz
2013/08/09 19:41:51
Every place this was used, it was wrapped in [sche
| |
| 27 Future<int> get numRuns => schedule(() => _numRuns); | |
| 24 var _numRuns = 0; | 28 var _numRuns = 0; |
| 25 | 29 |
| 26 /// The number of currently running transforms. | 30 /// The number of currently running transforms. |
| 27 int _runningTransforms = 0; | 31 int _runningTransforms = 0; |
| 28 | 32 |
| 29 // A completer for pausing the transformer before it finishes running [apply]. | 33 // A completer for pausing the transformer before it finishes running [apply]. |
| 30 Completer _apply; | 34 Completer _apply; |
| 31 | 35 |
| 32 // Completers for pausing the transformer before it finishes running | 36 // Completers for pausing the transformer before it finishes running |
| 33 // [isPrimary]. | 37 // [isPrimary]. |
| 34 final _isPrimary = new Map<AssetId, Completer>(); | 38 final _isPrimary = new Map<AssetId, Completer>(); |
| 35 | 39 |
| 36 // Completers for pausing the transformer before it finishes getting inputs | 40 // Completers for pausing the transformer before it finishes getting inputs |
| 37 // the [Transform]. | 41 // the [Transform]. |
| 38 final _getInput = new Map<AssetId, Completer>(); | 42 final _getInput = new Map<AssetId, Completer>(); |
| 39 | 43 |
| 40 /// A future that completes when this transformer begins running. | 44 /// A completer that completes once this transformer begins running. |
| 41 /// | 45 /// |
| 42 /// Once this transformer finishes running, this is reset to a new completer, | 46 /// Once this transformer finishes running, this is reset to a new completer, |
| 43 /// so it can be used multiple times. | 47 /// so it can be used multiple times. |
| 44 Future get started => _started.future; | |
| 45 var _started = new Completer(); | 48 var _started = new Completer(); |
| 46 | 49 |
| 47 /// `true` if any transforms are currently running. | 50 /// `true` if any transforms are currently running. |
| 48 bool get isRunning => _runningTransforms > 0; | 51 /// |
| 52 /// This is scheduled. The Future will complete at the point in the schedule | |
| 53 /// that this is called. | |
| 54 Future<bool> get isRunning => schedule(() => _runningTransforms > 0); | |
| 55 | |
| 56 /// Pauses the schedule until this transformer begins running. | |
| 57 void waitUntilStarted() { | |
| 58 schedule(() => _started.future, "wait until $this starts"); | |
| 59 } | |
| 49 | 60 |
| 50 /// Causes the transformer to pause after running [apply] but before the | 61 /// Causes the transformer to pause after running [apply] but before the |
| 51 /// returned Future completes. | 62 /// returned Future completes. |
| 52 /// | 63 /// |
| 53 /// This can be resumed by calling [resumeApply]. | 64 /// This can be resumed by calling [resumeApply]. This operation is scheduled. |
| 54 void pauseApply() { | 65 void pauseApply() { |
| 55 _apply = new Completer(); | 66 schedule(() { |
| 67 _apply = new Completer(); | |
| 68 }, "pause apply for $this"); | |
| 56 } | 69 } |
| 57 | 70 |
| 58 /// Resumes the transformer's [apply] call after [pauseApply] was called. | 71 /// Resumes the transformer's [apply] call after [pauseApply] was called. |
| 72 /// | |
| 73 /// This operation is scheduled. | |
| 59 void resumeApply() { | 74 void resumeApply() { |
| 60 _apply.complete(); | 75 schedule(() { |
| 61 _apply = null; | 76 _apply.complete(); |
| 77 _apply = null; | |
| 78 }, "resume apply for $this"); | |
| 62 } | 79 } |
| 63 | 80 |
| 64 /// Causes the transformer to pause after running [isPrimary] on the asset | 81 /// Causes the transformer to pause after running [isPrimary] on the asset |
| 65 /// with the given [name], but before the returned Future completes. | 82 /// with the given [name], but before the returned Future completes. |
| 66 /// | 83 /// |
| 67 /// This can be resumed by calling [resumeIsPrimary]. | 84 /// This can be resumed by calling [resumeIsPrimary]. This operation is |
| 85 /// scheduled. | |
| 68 void pauseIsPrimary(String name) { | 86 void pauseIsPrimary(String name) { |
| 69 _isPrimary[new AssetId.parse(name)] = new Completer(); | 87 schedule(() { |
| 88 _isPrimary[new AssetId.parse(name)] = new Completer(); | |
| 89 }, "pause isPrimary($name) for $this"); | |
| 70 } | 90 } |
| 71 | 91 |
| 72 /// Resumes the transformer's [isPrimary] call on the asset with the given | 92 /// Resumes the transformer's [isPrimary] call on the asset with the given |
| 73 /// [name] after [pauseIsPrimary] was called. | 93 /// [name] after [pauseIsPrimary] was called. |
| 94 /// | |
| 95 /// This operation is scheduled. | |
| 74 void resumeIsPrimary(String name) { | 96 void resumeIsPrimary(String name) { |
| 75 _isPrimary.remove(new AssetId.parse(name)).complete(); | 97 schedule(() { |
| 98 _isPrimary.remove(new AssetId.parse(name)).complete(); | |
| 99 }, "resume isPrimary($name) for $this"); | |
| 76 } | 100 } |
| 77 | 101 |
| 78 /// Causes the transformer to pause while loading the input with the given | 102 /// Causes the transformer to pause while loading the input with the given |
| 79 /// [name]. This can be the primary input or a secondary input. | 103 /// [name]. This can be the primary input or a secondary input. |
| 80 /// | 104 /// |
| 81 /// This can be resumed by calling [resumeGetInput]. | 105 /// This can be resumed by calling [resumeGetInput]. This operation is |
| 106 /// scheduled. | |
| 82 void pauseGetInput(String name) { | 107 void pauseGetInput(String name) { |
| 83 _getInput[new AssetId.parse(name)] = new Completer(); | 108 schedule(() { |
| 109 _getInput[new AssetId.parse(name)] = new Completer(); | |
| 110 }, "pause getInput($name) for $this"); | |
| 84 } | 111 } |
| 85 | 112 |
| 86 /// Resumes the transformer's loading of the input with the given [name] after | 113 /// Resumes the transformer's loading of the input with the given [name] after |
| 87 /// [pauseGetInput] was called. | 114 /// [pauseGetInput] was called. |
| 115 /// | |
| 116 /// This operation is scheduled. | |
| 88 void resumeGetInput(String name) { | 117 void resumeGetInput(String name) { |
| 89 _getInput.remove(new AssetId.parse(name)).complete(); | 118 schedule(() { |
| 119 _getInput.remove(new AssetId.parse(name)).complete(); | |
| 120 }, "resume getInput($name) for $this"); | |
| 90 } | 121 } |
| 91 | 122 |
| 92 /// Like [Transform.getInput], but respects [pauseGetInput]. | 123 /// Like [Transform.getInput], but respects [pauseGetInput]. |
| 93 /// | 124 /// |
| 94 /// This is intended for use by subclasses of [MockTransformer]. | 125 /// This is intended for use by subclasses of [MockTransformer]. |
| 95 Future<Asset> getInput(Transform transform, AssetId id) { | 126 Future<Asset> getInput(Transform transform, AssetId id) { |
| 96 return newFuture(() { | 127 return newFuture(() { |
| 97 if (_getInput.containsKey(id)) return _getInput[id].future; | 128 if (_getInput.containsKey(id)) return _getInput[id].future; |
| 98 }).then((_) => transform.getInput(id)); | 129 }).then((_) => transform.getInput(id)); |
| 99 } | 130 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 125 if (_runningTransforms == 0) _started = new Completer(); | 156 if (_runningTransforms == 0) _started = new Completer(); |
| 126 }); | 157 }); |
| 127 } | 158 } |
| 128 | 159 |
| 129 /// The wrapped version of [isPrimary] for subclasses to override. | 160 /// The wrapped version of [isPrimary] for subclasses to override. |
| 130 Future<bool> doIsPrimary(Asset asset); | 161 Future<bool> doIsPrimary(Asset asset); |
| 131 | 162 |
| 132 /// The wrapped version of [doApply] for subclasses to override. | 163 /// The wrapped version of [doApply] for subclasses to override. |
| 133 Future doApply(Transform transform); | 164 Future doApply(Transform transform); |
| 134 } | 165 } |
| OLD | NEW |