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

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

Issue 251733002: Restore thenReturnList(). (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 | « pkg/analysis_server/pubspec.yaml ('k') | 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
index 48673fda0ddf137e00814722cfb14d0e6917d79e..74b19abf8979351b6132ac40a8b8efcbdb2af1dd 100644
--- a/pkg/analysis_server/test/typed_mock.dart
+++ b/pkg/analysis_server/test/typed_mock.dart
@@ -1,10 +1,7 @@
library typed_mock;
-import 'package:collection/collection.dart';
-InvocationMatcher _lastMatcher;
-
-ListEquality LIST_EQUALITY = new ListEquality();
+_InvocationMatcher _lastMatcher;
/// Enables stubbing methods.
@@ -12,7 +9,7 @@ ListEquality LIST_EQUALITY = new ListEquality();
/// method is called.
Behavior when(_ignored) {
try {
- var behavior = new Behavior(_lastMatcher);
+ var behavior = new Behavior();
_lastMatcher._behavior = behavior;
return behavior;
} finally {
@@ -22,12 +19,12 @@ Behavior when(_ignored) {
}
-class InvocationMatcher {
+class _InvocationMatcher {
List<ArgumentMatcher> _matchers = [];
Behavior _behavior;
- InvocationMatcher(Invocation invocation) {
+ _InvocationMatcher(Invocation invocation) {
invocation.positionalArguments.forEach((argument) {
ArgumentMatcher matcher;
if (argument is ArgumentMatcher) {
@@ -39,10 +36,7 @@ class InvocationMatcher {
});
}
- bool operator ==(other) => LIST_EQUALITY.equals(other._matchers,
- _matchers);
-
- bool _match(Invocation invocation) {
+ bool match(Invocation invocation) {
var arguments = invocation.positionalArguments;
if (arguments.length != _matchers.length) {
return false;
@@ -59,19 +53,19 @@ class InvocationMatcher {
}
class Behavior {
- final InvocationMatcher _matcher;
-
bool _thenFunctionEnabled = false;
Function _thenFunction;
bool _returnAlwaysEnabled = false;
var _returnAlways;
+ bool _returnListEnabled = false;
+ List _returnList;
+ int _returnListIndex;
+
bool _throwExceptionEnabled = false;
var _throwException;
- Behavior(InvocationMatcher this._matcher);
-
Behavior thenInvoke(Function function) {
_reset();
_thenFunctionEnabled = true;
@@ -86,6 +80,14 @@ class Behavior {
return this;
}
+ Behavior thenReturnList(List list) {
+ _reset();
+ _returnListEnabled = true;
+ _returnList = list;
+ _returnListIndex = 0;
+ return this;
+ }
+
Behavior thenThrow(exception) {
_reset();
_throwExceptionEnabled = true;
@@ -96,10 +98,11 @@ class Behavior {
_reset() {
_thenFunctionEnabled = false;
_returnAlwaysEnabled = false;
+ _returnListEnabled = false;
_throwExceptionEnabled = false;
}
- dynamic getReturnValue(Invocation invocation) {
+ dynamic _getReturnValue(Invocation invocation) {
// function
if (_thenFunctionEnabled) {
return Function.apply(_thenFunction, invocation.positionalArguments,
@@ -109,6 +112,14 @@ class Behavior {
if (_returnAlwaysEnabled) {
return _returnAlways;
}
+ // list
+ if (_returnListEnabled) {
+ if (_returnListIndex >= _returnList.length) {
+ throw new StateError('List of ${_returnList.length} elements'
+ ' $_returnList has been exhausted.');
+ }
+ return _returnList[_returnListIndex++];
+ }
// exception
if (_throwExceptionEnabled) {
throw _throwException;
@@ -120,7 +131,7 @@ class Behavior {
class TypedMock {
- final Map<Symbol, List<InvocationMatcher>> _invocationMatchersMap = {};
+ final Map<Symbol, List<_InvocationMatcher>> _invocationMatchersMap = {};
noSuchMethod(Invocation invocation) {
var member = invocation.memberName;
@@ -132,14 +143,13 @@ class TypedMock {
}
// check if there is a matcher
for (var matcher in matchers) {
- if (matcher._match(invocation)) {
+ if (matcher.match(invocation)) {
_lastMatcher = matcher;
- return matcher._behavior.getReturnValue(invocation);
+ return matcher._behavior._getReturnValue(invocation);
}
}
// add a new matcher
- InvocationMatcher matcher = new InvocationMatcher(invocation);
- matchers.remove(matcher);
+ _InvocationMatcher matcher = new _InvocationMatcher(invocation);
matchers.add(matcher);
_lastMatcher = matcher;
}
« no previous file with comments | « pkg/analysis_server/pubspec.yaml ('k') | pkg/analysis_server/test/typed_mock_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698