Chromium Code Reviews| 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"]}'); |
| + } |
| + } |
| +} |
| + |