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

Unified Diff: pkg/unittest/lib/src/iterable_matchers.dart

Issue 15017013: Added a new matcher for pairwise comparison of iterables with a custom comparator. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 7 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/unittest/test/matchers_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/unittest/lib/src/iterable_matchers.dart
===================================================================
--- pkg/unittest/lib/src/iterable_matchers.dart (revision 22469)
+++ pkg/unittest/lib/src/iterable_matchers.dart (working copy)
@@ -184,3 +184,60 @@
}
}
}
+
+/**
+ * A pairwise matcher for iterable. You can pass an arbitrary [comparator]
+ * function that takes an expected and actual argument which will be applied
+ * to each pair in order. [description] should be a meaningful name for
+ * the comparator.
+ */
+Matcher pairwiseCompare(Iterable expected, Function comparator,
+ String description) =>
+ new _PairwiseCompare(expected, comparator, description);
+
+class _PairwiseCompare extends _IterableMatcher {
+ Iterable _expected;
Jennifer Messerly 2013/05/08 00:16:25 final?
+ Function _comparator;
+ String _description;
+
+ _PairwiseCompare(this._expected, this._comparator, this._description);
+
+ bool matches(item, MatchState matchState) {
+ if (item is! Iterable) return false;
+ if (item.length != _expected.length) return false;
+ for (var i = 0; i < item.length; i++) {
+ var e = _expected.elementAt(i);
Jennifer Messerly 2013/05/08 00:16:25 any concern about the O(N^2) if this is a genuine
Siggi Cherem (dart-lang) 2013/05/08 00:31:32 you could use the iterable api and visit them conc
+ var a = item.elementAt(i);
+ if (!_comparator(e, a)) {
+ matchState.state = {
+ 'index': i,
+ 'expected': e,
+ 'actual' : a,
+ 'state': matchState.state
+ };
+ return false;
+ }
+ }
+ return true;
+ }
+
+ Description describe(Description description) =>
+ description.add('pairwise $_description ').addDescriptionOf(_expected);
+
+ Description describeMismatch(item, Description mismatchDescription,
+ MatchState matchState, bool verbose) {
+ if (item is !Iterable) {
+ return mismatchDescription.add('not an Iterable');
+ } else if (item.length != _expected.length) {
+ return mismatchDescription.
+ add('length was ${item.length} instead of ${_expected.length}');
+ } else {
+ return mismatchDescription.
+ addDescriptionOf(matchState.state["actual"]).
+ add(' not $_description ').
+ addDescriptionOf(matchState.state["expected"]).
+ add(' at position ${matchState.state["index"]}');
+ }
+ }
+}
+
« no previous file with comments | « no previous file | pkg/unittest/test/matchers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698