Chromium Code Reviews| Index: pkg/analysis_server/test/typed_mock.dart |
| diff --git a/pkg/analysis_server/test/typed_mock.dart b/pkg/analysis_server/test/typed_mock.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bc265bf3261c34e55ce7841c0ca590fc89b8143d |
| --- /dev/null |
| +++ b/pkg/analysis_server/test/typed_mock.dart |
| @@ -0,0 +1,117 @@ |
| +library typed_mock; |
| + |
| +import 'dart:collection' show Queue; |
| + |
| +Behavior _lastBehavior; |
| + |
| + |
| +/// Enables stubbing methods. |
| +/// Use it when you want the mock to return particular value when particular |
| +/// method is called. |
| +Behavior when(_ignored) { |
| + try { |
| + return _lastBehavior; |
| + } finally { |
| + // clear to prevent memory leak |
| + _lastBehavior = null; |
| + } |
| +} |
| + |
| + |
| +class Behavior { |
| + final Symbol _member; |
| + |
| + bool _thenFunctionEnabled = false; |
| + Function _thenFunction; |
| + |
| + bool _returnAlwaysEnabled = false; |
| + var _returnAlways; |
| + |
| + bool _returnQueueEnabled = false; |
| + Queue _returnQueue; |
| + |
| + bool _throwExceptionEnabled = false; |
| + var _throwException; |
| + |
| + Behavior(this._member); |
| + |
| + _reset() { |
| + _thenFunctionEnabled = false; |
| + _returnAlwaysEnabled = false; |
| + _returnQueueEnabled = false; |
| + _throwExceptionEnabled = false; |
| + } |
| + |
| + Behavior then(Function function) { |
|
devoncarew
2014/04/24 16:31:50
Uses of this method end up looking a lot like a Fu
scheglov
2014/04/24 16:44:21
Does Future has an exclusive right on "then" name?
|
| + _reset(); |
| + _thenFunctionEnabled = true; |
| + _thenFunction = function; |
| + return this; |
| + } |
| + |
| + Behavior thenReturn(value) { |
| + _reset(); |
| + _returnAlwaysEnabled = true; |
| + _returnAlways = value; |
| + return this; |
| + } |
| + |
| + Behavior thenReturnList(Iterable values) { |
| + _reset(); |
| + _returnQueueEnabled = true; |
| + _returnQueue = new Queue(); |
| + _returnQueue.addAll(values); |
| + return this; |
| + } |
| + |
| + Behavior thenThrow(exception) { |
| + _reset(); |
| + _throwExceptionEnabled = true; |
| + _throwException = exception; |
| + return this; |
| + } |
| + |
| + dynamic getReturnValue(Invocation invocation) { |
| + // function |
| + if (_thenFunctionEnabled) { |
| + return Function.apply(_thenFunction, invocation.positionalArguments, |
| + invocation.namedArguments); |
| + } |
| + // always |
| + if (_returnAlwaysEnabled) { |
| + return _returnAlways; |
| + } |
| + // queue |
| + if (_returnQueueEnabled) { |
| + if (_returnQueue.isNotEmpty) { |
| + var result = _returnQueue.removeFirst(); |
| + if (_returnQueue.isEmpty) { |
| + _returnQueueEnabled = false; |
| + } |
| + return result; |
| + } |
| + } |
| + // exception |
| + if (_throwExceptionEnabled) { |
| + throw _throwException; |
| + } |
| + // no value |
| + return null; |
| + } |
| +} |
| + |
| + |
| +class TypedMock { |
| + final Map<Symbol, Behavior> _behaviors = {}; |
| + |
| + noSuchMethod(Invocation invocation) { |
| + var member = invocation.memberName; |
| + Behavior behavior = _behaviors[member]; |
| + if (behavior == null) { |
| + behavior = new Behavior(member); |
| + _behaviors[member] = behavior; |
| + } |
| + _lastBehavior = behavior; |
| + return behavior.getReturnValue(invocation); |
| + } |
| +} |