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

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

Issue 250813003: verifyNoMoreInteractions() and verifyZeroInteractions() (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments 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
index 0b991257f7fc64c78fd99b8bd84b28af02a36d49..d2b91dd9dc2f2e5d2017cfd4798e6ed54dbf502c 100644
--- a/pkg/analysis_server/test/typed_mock.dart
+++ b/pkg/analysis_server/test/typed_mock.dart
@@ -34,6 +34,33 @@ Verifier verify(_ignored) {
}
}
+
+/// Verifies that the given mock doesn't have any unverified interaction.
+void verifyNoMoreInteractions(TypedMock mock) {
+ var notVerified = mock._computeNotVerifiedInvocations();
+ // OK
+ if (notVerified.isEmpty) {
+ return;
+ }
+ // fail
+ var invocationsString = _getInvocationsString(notVerified);
+ throw new VerifyError('Unexpected interactions:\n$invocationsString');
+}
+
+
+/// Verifies that no interactions happened on the given mock.
+void verifyZeroInteractions(TypedMock mock) {
+ var invocations = mock._invocations;
+ // OK
+ if (invocations.isEmpty) {
+ return;
+ }
+ // fail
+ var invocationsString = _getInvocationsString(invocations);
+ throw new VerifyError('Unexpected interactions:\n$invocationsString');
+}
+
+
/// [VerifyError] is thrown when one of the [verify] checks fails.
class VerifyError {
final String message;
@@ -41,6 +68,22 @@ class VerifyError {
String toString() => 'VerifyError: $message';
}
+
+String _getInvocationsString(Iterable<Invocation> invocations) {
+ var buffer = new StringBuffer();
+ invocations.forEach((invocation) {
+ var member = invocation.memberName;
+ buffer.write(member);
+ buffer.write(' ');
+ buffer.write(invocation.positionalArguments);
+ buffer.write(' ');
+ buffer.write(invocation.namedArguments);
+ buffer.writeln();
+ });
+ return buffer.toString();
+}
+
+
class _InvocationMatcher {
final Symbol _member;
final TypedMock _mock;
@@ -165,14 +208,23 @@ class Verifier {
Verifier._(this._mock, this._matcher);
+ /// Marks matching interactions as verified and never fails.
+ void any() {
+ // mark as verified, but don't check the actual count
+ _count();
+ }
+
+ /// Verifies that there was no matching interactions.
void never() {
times(0);
}
+ /// Verifies that there was excatly one martching interaction.
void once() {
times(1);
}
+ /// Verifies that there was the specified number of matching interactions.
void times(int expected) {
var times = _count();
if (times != expected) {
@@ -182,6 +234,8 @@ class Verifier {
}
}
+ /// Verifies that there was at least the specified number of matching
+ /// interactions.
void atLeast(int expected) {
var times = _count();
if (times < expected) {
@@ -191,6 +245,18 @@ class Verifier {
}
}
+ /// Verifies that there was at least one matching interaction.
+ void atLeastOnce() {
+ var times = _count();
+ if (times == 0) {
+ var member = _matcher._member;
+ throw new VerifyError('At least one expected, but only zero'
+ ' invocations of $member recorded.');
+ }
+ }
+
+ /// Verifies that there was at most the specified number of matching
+ /// interactions.
void atMost(int expected) {
var times = _count();
if (times > expected) {
@@ -209,6 +275,7 @@ class Verifier {
if (!_matcher.match(invocation)) {
return;
}
+ _mock._verifiedInvocations.add(invocation);
times++;
});
return times;
@@ -220,6 +287,7 @@ class TypedMock {
final Map<Symbol, List<_InvocationMatcher>> _matchersMap = {};
final List<Invocation> _invocations = [];
+ final Set<Invocation> _verifiedInvocations = new Set<Invocation>();
noSuchMethod(Invocation invocation) {
_invocations.add(invocation);
@@ -248,6 +316,11 @@ class TypedMock {
_lastMatcher = matcher;
}
+ Iterable<Invocation> _computeNotVerifiedInvocations() {
+ notVerified(e) => !_verifiedInvocations.contains(e);
+ return _invocations.where(notVerified);
+ }
+
void _removeLastInvocation() {
_invocations.removeLast();
}
« 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