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

Side by Side Diff: pkg/unittest/lib/src/core_matchers.dart

Issue 12224053: Updated contains and isEmpty matches to use iterables. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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 that matches empty strings, maps or collections. 8 * Returns a matcher that matches empty strings, maps or collections.
9 */ 9 */
10 const Matcher isEmpty = const _Empty(); 10 const Matcher isEmpty = const _Empty();
11 11
12 class _Empty extends BaseMatcher { 12 class _Empty extends BaseMatcher {
13 const _Empty(); 13 const _Empty();
14 bool matches(item, MatchState matchState) { 14 bool matches(item, MatchState matchState) {
15 if (item is Map || item is Collection) { 15 if (item is Map || item is Iterable) {
16 return item.isEmpty; 16 return item.isEmpty;
17 } else if (item is String) { 17 } else if (item is String) {
18 return item.length == 0; 18 return item.length == 0;
19 } else { 19 } else {
20 return false; 20 return false;
21 } 21 }
22 } 22 }
23 Description describe(Description description) => 23 Description describe(Description description) =>
24 description.add('empty'); 24 description.add('empty');
25 } 25 }
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 580
581 class _Contains extends BaseMatcher { 581 class _Contains extends BaseMatcher {
582 582
583 final _expected; 583 final _expected;
584 584
585 const _Contains(this._expected); 585 const _Contains(this._expected);
586 586
587 bool matches(item, MatchState matchState) { 587 bool matches(item, MatchState matchState) {
588 if (item is String) { 588 if (item is String) {
589 return item.indexOf(_expected) >= 0; 589 return item.indexOf(_expected) >= 0;
590 } else if (item is Collection) { 590 } else if (item is Iterable) {
591 if (_expected is Matcher) { 591 if (_expected is Matcher) {
592 return item.any((e) => _expected.matches(e, matchState)); 592 return item.any((e) => _expected.matches(e, matchState));
593 } else { 593 } else {
594 return item.any((e) => e == _expected); 594 return item.contains(_expected);
butlermatt 2013/02/07 15:30:28 I changed this to contains(), to allow Iterables t
595 } 595 }
596 } else if (item is Map) { 596 } else if (item is Map) {
597 return item.containsKey(_expected); 597 return item.containsKey(_expected);
598 } 598 }
599 return false; 599 return false;
600 } 600 }
601 601
602 Description describe(Description description) => 602 Description describe(Description description) =>
603 description.add('contains ').addDescriptionOf(_expected); 603 description.add('contains ').addDescriptionOf(_expected);
604 } 604 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); 695 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher);
696 696
697 Description describeMismatch(item, Description mismatchDescription, 697 Description describeMismatch(item, Description mismatchDescription,
698 MatchState matchState, bool verbose) { 698 MatchState matchState, bool verbose) {
699 mismatchDescription.add(_featureName).add(' '); 699 mismatchDescription.add(_featureName).add(' ');
700 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, 700 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription,
701 matchState.state['innerState'], verbose); 701 matchState.state['innerState'], verbose);
702 return mismatchDescription; 702 return mismatchDescription;
703 } 703 }
704 } 704 }
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