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

Unified Diff: pkg/analysis_server/test/typed_mock.dart

Issue 252463002: Typed mocks library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/typed_mock_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+ }
+}
« no previous file with comments | « no previous file | pkg/analysis_server/test/typed_mock_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698