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 collections. |
9 */ | 9 */ |
10 const Matcher isEmpty = const _Empty(); | 10 const Matcher isEmpty = const _Empty(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 final _expected; | 91 final _expected; |
92 final int _limit; | 92 final int _limit; |
93 var count; | 93 var count; |
94 | 94 |
95 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; | 95 _DeepMatcher(this._expected, [limit = 1000]) : this._limit = limit; |
96 | 96 |
97 String _compareIterables(expected, actual, matcher, depth) { | 97 String _compareIterables(expected, actual, matcher, depth) { |
98 if (actual is !Iterable) { | 98 if (actual is !Iterable) { |
99 return 'is not Iterable'; | 99 return 'is not Iterable'; |
100 } | 100 } |
101 var expectedIterator = expected.iterator(); | 101 var expectedIterator = expected.iterator; |
102 var actualIterator = actual.iterator(); | 102 var actualIterator = actual.iterator; |
103 var position = 0; | 103 var position = 0; |
104 String reason = null; | 104 String reason = null; |
105 while (reason == null) { | 105 while (reason == null) { |
106 if (expectedIterator.hasNext) { | 106 if (expectedIterator.moveNext()) { |
107 if (actualIterator.hasNext) { | 107 if (actualIterator.moveNext()) { |
108 Description r = matcher(expectedIterator.next(), | 108 Description r = matcher(expectedIterator.current, |
109 actualIterator.next(), | 109 actualIterator.current, |
110 'mismatch at position ${position}', | 110 'mismatch at position ${position}', |
111 depth); | 111 depth); |
112 if (r != null) reason = r.toString(); | 112 if (r != null) reason = r.toString(); |
113 ++position; | 113 ++position; |
114 } else { | 114 } else { |
115 reason = 'shorter than expected'; | 115 reason = 'shorter than expected'; |
116 } | 116 } |
117 } else if (actualIterator.hasNext) { | 117 } else if (actualIterator.moveNext()) { |
118 reason = 'longer than expected'; | 118 reason = 'longer than expected'; |
119 } else { | 119 } else { |
120 return null; | 120 return null; |
121 } | 121 } |
122 } | 122 } |
123 return reason; | 123 return reason; |
124 } | 124 } |
125 | 125 |
126 Description _recursiveMatch(expected, actual, String location, int depth) { | 126 Description _recursiveMatch(expected, actual, String location, int depth) { |
127 Description reason = null; | 127 Description reason = null; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 class Throws extends BaseMatcher { | 277 class Throws extends BaseMatcher { |
278 final Matcher _matcher; | 278 final Matcher _matcher; |
279 | 279 |
280 const Throws([Matcher matcher]) : | 280 const Throws([Matcher matcher]) : |
281 this._matcher = matcher; | 281 this._matcher = matcher; |
282 | 282 |
283 bool matches(item, MatchState matchState) { | 283 bool matches(item, MatchState matchState) { |
284 if (item is Future) { | 284 if (item is Future) { |
285 // Queue up an asynchronous expectation that validates when the future | 285 // Queue up an asynchronous expectation that validates when the future |
286 // completes. | 286 // completes. |
287 item.onComplete(wrapAsync((future) { | 287 item.then((value) { |
288 if (future.hasValue) { | 288 expect(false, isTrue, reason: |
289 expect(false, isTrue, reason: | 289 "Expected future to fail, but succeeded with '$value'."); |
290 "Expected future to fail, but succeeded with '${future.value}'."); | 290 }); |
291 } else if (_matcher != null) { | 291 |
292 var reason; | 292 item.catchError((e) { |
293 if (future.stackTrace != null) { | 293 var reason; |
294 var stackTrace = future.stackTrace.toString(); | 294 if (e.stackTrace != null) { |
295 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; | 295 var stackTrace = e.stackTrace.toString(); |
296 reason = "Actual exception trace:\n$stackTrace"; | 296 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; |
297 } | 297 reason = "Actual exception trace:\n$stackTrace"; |
298 expect(future.exception, _matcher, reason: reason); | |
299 } | 298 } |
300 })); | 299 expect(e.error, _matcher, reason: reason); |
| 300 }); |
301 | 301 |
302 // It hasn't failed yet. | 302 // It hasn't failed yet. |
303 return true; | 303 return true; |
304 } | 304 } |
305 | 305 |
306 try { | 306 try { |
307 item(); | 307 item(); |
308 return false; | 308 return false; |
309 } catch (e, s) { | 309 } catch (e, s) { |
310 if (_matcher == null ||_matcher.matches(e, matchState)) { | 310 if (_matcher == null ||_matcher.matches(e, matchState)) { |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 | 566 |
567 final _expected; | 567 final _expected; |
568 | 568 |
569 const _Contains(this._expected); | 569 const _Contains(this._expected); |
570 | 570 |
571 bool matches(item, MatchState matchState) { | 571 bool matches(item, MatchState matchState) { |
572 if (item is String) { | 572 if (item is String) { |
573 return item.indexOf(_expected) >= 0; | 573 return item.indexOf(_expected) >= 0; |
574 } else if (item is Collection) { | 574 } else if (item is Collection) { |
575 if (_expected is Matcher) { | 575 if (_expected is Matcher) { |
576 return item.some((e) => _expected.matches(e, matchState)); | 576 return item.any((e) => _expected.matches(e, matchState)); |
577 } else { | 577 } else { |
578 return item.some((e) => e == _expected); | 578 return item.any((e) => e == _expected); |
579 } | 579 } |
580 } else if (item is Map) { | 580 } else if (item is Map) { |
581 return item.containsKey(_expected); | 581 return item.containsKey(_expected); |
582 } | 582 } |
583 return false; | 583 return false; |
584 } | 584 } |
585 | 585 |
586 Description describe(Description description) => | 586 Description describe(Description description) => |
587 description.add('contains ').addDescriptionOf(_expected); | 587 description.add('contains ').addDescriptionOf(_expected); |
588 } | 588 } |
589 | 589 |
590 /** | 590 /** |
591 * Returns a matcher that matches if the match argument is in | 591 * Returns a matcher that matches if the match argument is in |
592 * the expected value. This is the converse of [contains]. | 592 * the expected value. This is the converse of [contains]. |
593 */ | 593 */ |
594 Matcher isIn(expected) => new _In(expected); | 594 Matcher isIn(expected) => new _In(expected); |
595 | 595 |
596 class _In extends BaseMatcher { | 596 class _In extends BaseMatcher { |
597 | 597 |
598 final _expected; | 598 final _expected; |
599 | 599 |
600 const _In(this._expected); | 600 const _In(this._expected); |
601 | 601 |
602 bool matches(item, MatchState matchState) { | 602 bool matches(item, MatchState matchState) { |
603 if (_expected is String) { | 603 if (_expected is String) { |
604 return _expected.indexOf(item) >= 0; | 604 return _expected.indexOf(item) >= 0; |
605 } else if (_expected is Collection) { | 605 } else if (_expected is Collection) { |
606 return _expected.some((e) => e == item); | 606 return _expected.any((e) => e == item); |
607 } else if (_expected is Map) { | 607 } else if (_expected is Map) { |
608 return _expected.containsKey(item); | 608 return _expected.containsKey(item); |
609 } | 609 } |
610 return false; | 610 return false; |
611 } | 611 } |
612 | 612 |
613 Description describe(Description description) => | 613 Description describe(Description description) => |
614 description.add('is in ').addDescriptionOf(_expected); | 614 description.add('is in ').addDescriptionOf(_expected); |
615 } | 615 } |
616 | 616 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); | 677 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); |
678 | 678 |
679 Description describeMismatch(item, Description mismatchDescription, | 679 Description describeMismatch(item, Description mismatchDescription, |
680 MatchState matchState, bool verbose) { | 680 MatchState matchState, bool verbose) { |
681 mismatchDescription.add(_featureName).add(' '); | 681 mismatchDescription.add(_featureName).add(' '); |
682 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, | 682 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, |
683 matchState.state['innerState'], verbose); | 683 matchState.state['innerState'], verbose); |
684 return mismatchDescription; | 684 return mismatchDescription; |
685 } | 685 } |
686 } | 686 } |
OLD | NEW |