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

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: Fixed typo per comment. 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 iterables
9 * (including collections).
9 */ 10 */
10 const Matcher isEmpty = const _Empty(); 11 const Matcher isEmpty = const _Empty();
11 12
12 class _Empty extends BaseMatcher { 13 class _Empty extends BaseMatcher {
13 const _Empty(); 14 const _Empty();
14 bool matches(item, MatchState matchState) { 15 bool matches(item, MatchState matchState) {
15 if (item is Map || item is Collection) { 16 if (item is Map || item is Iterable) {
16 return item.isEmpty; 17 return item.isEmpty;
17 } else if (item is String) { 18 } else if (item is String) {
18 return item.length == 0; 19 return item.length == 0;
19 } else { 20 } else {
20 return false; 21 return false;
21 } 22 }
22 } 23 }
23 Description describe(Description description) => 24 Description describe(Description description) =>
24 description.add('empty'); 25 description.add('empty');
25 } 26 }
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 } 566 }
566 } catch (e) { 567 } catch (e) {
567 return mismatchDescription.add(' has no length property'); 568 return mismatchDescription.add(' has no length property');
568 } 569 }
569 } 570 }
570 } 571 }
571 572
572 /** 573 /**
573 * Returns a matcher that matches if the match argument contains 574 * Returns a matcher that matches if the match argument contains
574 * the expected value. For [String]s this means substring matching; 575 * the expected value. For [String]s this means substring matching;
575 * for [Map]s is means the map has the key, and for [Collection]s it 576 * for [Map]s it means the map has the key, and for [Iterable]s
576 * means the collection has a matching element. In the case of collections, 577 * (including [Collection]s) it means the iterable has a matching
577 * [expected] can itself be a matcher. 578 * element. In the case of iterables, [expected] can itself be a
579 * matcher.
578 */ 580 */
579 Matcher contains(expected) => new _Contains(expected); 581 Matcher contains(expected) => new _Contains(expected);
580 582
581 class _Contains extends BaseMatcher { 583 class _Contains extends BaseMatcher {
582 584
583 final _expected; 585 final _expected;
584 586
585 const _Contains(this._expected); 587 const _Contains(this._expected);
586 588
587 bool matches(item, MatchState matchState) { 589 bool matches(item, MatchState matchState) {
588 if (item is String) { 590 if (item is String) {
589 return item.indexOf(_expected) >= 0; 591 return item.indexOf(_expected) >= 0;
590 } else if (item is Collection) { 592 } else if (item is Iterable) {
591 if (_expected is Matcher) { 593 if (_expected is Matcher) {
592 return item.any((e) => _expected.matches(e, matchState)); 594 return item.any((e) => _expected.matches(e, matchState));
593 } else { 595 } else {
594 return item.any((e) => e == _expected); 596 return item.contains(_expected);
595 } 597 }
596 } else if (item is Map) { 598 } else if (item is Map) {
597 return item.containsKey(_expected); 599 return item.containsKey(_expected);
598 } 600 }
599 return false; 601 return false;
600 } 602 }
601 603
602 Description describe(Description description) => 604 Description describe(Description description) =>
603 description.add('contains ').addDescriptionOf(_expected); 605 description.add('contains ').addDescriptionOf(_expected);
604 } 606 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); 697 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher);
696 698
697 Description describeMismatch(item, Description mismatchDescription, 699 Description describeMismatch(item, Description mismatchDescription,
698 MatchState matchState, bool verbose) { 700 MatchState matchState, bool verbose) {
699 mismatchDescription.add(_featureName).add(' '); 701 mismatchDescription.add(_featureName).add(' ');
700 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, 702 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription,
701 matchState.state['innerState'], verbose); 703 matchState.state['innerState'], verbose);
702 return mismatchDescription; 704 return mismatchDescription;
703 } 705 }
704 } 706 }
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