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 library matcher.core_matchers; | 5 library matcher.core_matchers; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'description.dart'; | 9 import 'description.dart'; |
10 import 'expect.dart'; | 10 import 'expect.dart'; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 Matcher equals(expected, [int limit=100]) => | 89 Matcher equals(expected, [int limit=100]) => |
90 expected is String | 90 expected is String |
91 ? new _StringEqualsMatcher(expected) | 91 ? new _StringEqualsMatcher(expected) |
92 : new _DeepMatcher(expected, limit); | 92 : new _DeepMatcher(expected, limit); |
93 | 93 |
94 class _DeepMatcher extends Matcher { | 94 class _DeepMatcher extends Matcher { |
95 final _expected; | 95 final _expected; |
96 final int _limit; | 96 final int _limit; |
97 var count; | 97 var count; |
98 | 98 |
99 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; | 99 _DeepMatcher(this._expected, [int limit = 1000]) : this._limit = limit; |
100 | 100 |
101 // Returns a pair (reason, location) | 101 // Returns a pair (reason, location) |
102 List _compareIterables(expected, actual, matcher, depth, location) { | 102 List _compareIterables(expected, actual, matcher, depth, location) { |
103 if (actual is! Iterable) return ['is not Iterable', location]; | 103 if (actual is! Iterable) return ['is not Iterable', location]; |
104 | 104 |
105 var expectedIterator = expected.iterator; | 105 var expectedIterator = expected.iterator; |
106 var actualIterator = actual.iterator; | 106 var actualIterator = actual.iterator; |
107 for (var index = 0; ; index++) { | 107 for (var index = 0; ; index++) { |
108 // Advance in lockstep. | 108 // Advance in lockstep. |
109 var expectedNext = expectedIterator.moveNext(); | 109 var expectedNext = expectedIterator.moveNext(); |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 * For now the only solution for all platforms seems to be separate classes | 506 * For now the only solution for all platforms seems to be separate classes |
507 * for each exception type. | 507 * for each exception type. |
508 */ | 508 */ |
509 | 509 |
510 abstract class TypeMatcher extends Matcher { | 510 abstract class TypeMatcher extends Matcher { |
511 final String _name; | 511 final String _name; |
512 const TypeMatcher(this._name); | 512 const TypeMatcher(this._name); |
513 Description describe(Description description) => description.add(_name); | 513 Description describe(Description description) => description.add(_name); |
514 } | 514 } |
515 | 515 |
516 | |
517 | |
518 /// A matcher for Map types. | 516 /// A matcher for Map types. |
519 const isMap = const _IsMap(); | 517 const Matcher isMap = const _IsMap(); |
520 | 518 |
521 class _IsMap extends TypeMatcher { | 519 class _IsMap extends TypeMatcher { |
522 const _IsMap() : super("Map"); | 520 const _IsMap() : super("Map"); |
523 bool matches(item, Map matchState) => item is Map; | 521 bool matches(item, Map matchState) => item is Map; |
524 } | 522 } |
525 | 523 |
526 /// A matcher for List types. | 524 /// A matcher for List types. |
527 const isList = const _IsList(); | 525 const Matcher isList = const _IsList(); |
528 | 526 |
529 class _IsList extends TypeMatcher { | 527 class _IsList extends TypeMatcher { |
530 const _IsList() : super("List"); | 528 const _IsList() : super("List"); |
531 bool matches(item, Map matchState) => item is List; | 529 bool matches(item, Map matchState) => item is List; |
532 } | 530 } |
533 | 531 |
534 /// Returns a matcher that matches if an object has a length property | 532 /// Returns a matcher that matches if an object has a length property |
535 /// that matches [matcher]. | 533 /// that matches [matcher]. |
536 Matcher hasLength(matcher) => new _HasLength(wrapMatcher(matcher)); | 534 Matcher hasLength(matcher) => new _HasLength(wrapMatcher(matcher)); |
537 | 535 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 return mismatchDescription.add('is not a string, map or iterable'); | 606 return mismatchDescription.add('is not a string, map or iterable'); |
609 } | 607 } |
610 } | 608 } |
611 } | 609 } |
612 | 610 |
613 /// Returns a matcher that matches if the match argument is in | 611 /// Returns a matcher that matches if the match argument is in |
614 /// the expected value. This is the converse of [contains]. | 612 /// the expected value. This is the converse of [contains]. |
615 Matcher isIn(expected) => new _In(expected); | 613 Matcher isIn(expected) => new _In(expected); |
616 | 614 |
617 class _In extends Matcher { | 615 class _In extends Matcher { |
618 | |
619 final _expected; | 616 final _expected; |
620 | 617 |
621 const _In(this._expected); | 618 const _In(this._expected); |
622 | 619 |
623 bool matches(item, Map matchState) { | 620 bool matches(item, Map matchState) { |
624 if (_expected is String) { | 621 if (_expected is String) { |
625 return _expected.indexOf(item) >= 0; | 622 return _expected.indexOf(item) >= 0; |
626 } else if (_expected is Iterable) { | 623 } else if (_expected is Iterable) { |
627 return _expected.any((e) => e == item); | 624 return _expected.any((e) => e == item); |
628 } else if (_expected is Map) { | 625 } else if (_expected is Map) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 addDescriptionOf(matchState['feature']); | 699 addDescriptionOf(matchState['feature']); |
703 var innerDescription = new StringDescription(); | 700 var innerDescription = new StringDescription(); |
704 _matcher.describeMismatch(matchState['feature'], innerDescription, | 701 _matcher.describeMismatch(matchState['feature'], innerDescription, |
705 matchState['state'], verbose); | 702 matchState['state'], verbose); |
706 if (innerDescription.length > 0) { | 703 if (innerDescription.length > 0) { |
707 mismatchDescription.add(' which ').add(innerDescription.toString()); | 704 mismatchDescription.add(' which ').add(innerDescription.toString()); |
708 } | 705 } |
709 return mismatchDescription; | 706 return mismatchDescription; |
710 } | 707 } |
711 } | 708 } |
OLD | NEW |