Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 is means the map has the key, and for [Iterable]s |
|
gram
2013/02/07 17:35:23
While you're at it can you correct the comment? "i
butlermatt
2013/02/07 17:50:15
Done.
| |
| 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 Loading... | |
| 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 } |
| OLD | NEW |