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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/unittest/test/matchers_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of matcher; 5 part of matcher;
6 6
7 /** 7 /**
8 * Returns a matcher which matches [Iterable]s in which all elements 8 * Returns a matcher which matches [Iterable]s in which all elements
9 * match the given [matcher]. 9 * match the given [matcher].
10 */ 10 */
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (item is! Iterable) { 177 if (item is! Iterable) {
178 return mismatchDescription. 178 return mismatchDescription.
179 addDescriptionOf(item). 179 addDescriptionOf(item).
180 add(' not an Iterable'); 180 add(' not an Iterable');
181 } else { 181 } else {
182 return super.describeMismatch(item, mismatchDescription, matchState, 182 return super.describeMismatch(item, mismatchDescription, matchState,
183 verbose); 183 verbose);
184 } 184 }
185 } 185 }
186 } 186 }
187
188 /**
189 * A pairwise matcher for iterable. You can pass an arbitrary [comparator]
190 * function that takes an expected and actual argument which will be applied
191 * to each pair in order. [description] should be a meaningful name for
192 * the comparator.
193 */
194 Matcher pairwiseCompare(Iterable expected, Function comparator,
195 String description) =>
196 new _PairwiseCompare(expected, comparator, description);
197
198 class _PairwiseCompare extends _IterableMatcher {
199 Iterable _expected;
Jennifer Messerly 2013/05/08 00:16:25 final?
200 Function _comparator;
201 String _description;
202
203 _PairwiseCompare(this._expected, this._comparator, this._description);
204
205 bool matches(item, MatchState matchState) {
206 if (item is! Iterable) return false;
207 if (item.length != _expected.length) return false;
208 for (var i = 0; i < item.length; i++) {
209 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
210 var a = item.elementAt(i);
211 if (!_comparator(e, a)) {
212 matchState.state = {
213 'index': i,
214 'expected': e,
215 'actual' : a,
216 'state': matchState.state
217 };
218 return false;
219 }
220 }
221 return true;
222 }
223
224 Description describe(Description description) =>
225 description.add('pairwise $_description ').addDescriptionOf(_expected);
226
227 Description describeMismatch(item, Description mismatchDescription,
228 MatchState matchState, bool verbose) {
229 if (item is !Iterable) {
230 return mismatchDescription.add('not an Iterable');
231 } else if (item.length != _expected.length) {
232 return mismatchDescription.
233 add('length was ${item.length} instead of ${_expected.length}');
234 } else {
235 return mismatchDescription.
236 addDescriptionOf(matchState.state["actual"]).
237 add(' not $_description ').
238 addDescriptionOf(matchState.state["expected"]).
239 add(' at position ${matchState.state["index"]}');
240 }
241 }
242 }
243
OLDNEW
« 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