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 'description.dart'; | 7 import 'description.dart'; |
8 import 'interfaces.dart'; | 8 import 'interfaces.dart'; |
9 import 'util.dart'; | 9 import 'util.dart'; |
10 | 10 |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 class isInstanceOf<T> extends Matcher { | 382 class isInstanceOf<T> extends Matcher { |
383 final String _name; | 383 final String _name; |
384 const isInstanceOf([name = 'specified type']) : this._name = name; | 384 const isInstanceOf([name = 'specified type']) : this._name = name; |
385 bool matches(obj, Map matchState) => obj is T; | 385 bool matches(obj, Map matchState) => obj is T; |
386 // The description here is lame :-( | 386 // The description here is lame :-( |
387 Description describe(Description description) => | 387 Description describe(Description description) => |
388 description.add('an instance of ${_name}'); | 388 description.add('an instance of ${_name}'); |
389 } | 389 } |
390 | 390 |
391 /// A matcher that matches a function call against no exception. | 391 /// A matcher that matches a function call against no exception. |
| 392 /// |
392 /// The function will be called once. Any exceptions will be silently swallowed. | 393 /// The function will be called once. Any exceptions will be silently swallowed. |
393 /// The value passed to expect() should be a reference to the function. | 394 /// The value passed to expect() should be a reference to the function. |
394 /// Note that the function cannot take arguments; to handle this | 395 /// Note that the function cannot take arguments; to handle this |
395 /// a wrapper will have to be created. | 396 /// a wrapper will have to be created. |
396 const Matcher returnsNormally = const _ReturnsNormally(); | 397 const Matcher returnsNormally = const _ReturnsNormally(); |
397 | 398 |
398 class _ReturnsNormally extends Matcher { | 399 class _ReturnsNormally extends Matcher { |
399 const _ReturnsNormally(); | 400 const _ReturnsNormally(); |
400 | 401 |
401 bool matches(f, Map matchState) { | 402 bool matches(f, Map matchState) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 // property; we use the same trick as in matches(). | 496 // property; we use the same trick as in matches(). |
496 if (item.length * item.length >= 0) { | 497 if (item.length * item.length >= 0) { |
497 return mismatchDescription.add('has length of '). | 498 return mismatchDescription.add('has length of '). |
498 addDescriptionOf(item.length); | 499 addDescriptionOf(item.length); |
499 } | 500 } |
500 } catch (e) {} | 501 } catch (e) {} |
501 return mismatchDescription.add('has no length property'); | 502 return mismatchDescription.add('has no length property'); |
502 } | 503 } |
503 } | 504 } |
504 | 505 |
505 /// Returns a matcher that matches if the match argument contains | 506 /// Returns a matcher that matches if the match argument contains the expected |
506 /// the expected value. For [String]s this means substring matching; | 507 /// value. |
| 508 /// |
| 509 /// For [String]s this means substring matching; |
507 /// for [Map]s it means the map has the key, and for [Iterable]s | 510 /// for [Map]s it means the map has the key, and for [Iterable]s |
508 /// (including [Iterable]s) it means the iterable has a matching | 511 /// it means the iterable has a matching element. In the case of iterables, |
509 /// element. In the case of iterables, [expected] can itself be a | 512 /// [expected] can itself be a matcher. |
510 /// matcher. | |
511 Matcher contains(expected) => new _Contains(expected); | 513 Matcher contains(expected) => new _Contains(expected); |
512 | 514 |
513 class _Contains extends Matcher { | 515 class _Contains extends Matcher { |
514 final _expected; | 516 final _expected; |
515 | 517 |
516 const _Contains(this._expected); | 518 const _Contains(this._expected); |
517 | 519 |
518 bool matches(item, Map matchState) { | 520 bool matches(item, Map matchState) { |
519 if (item is String) { | 521 if (item is String) { |
520 return item.indexOf(_expected) >= 0; | 522 return item.indexOf(_expected) >= 0; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 return _expected.containsKey(item); | 564 return _expected.containsKey(item); |
563 } | 565 } |
564 return false; | 566 return false; |
565 } | 567 } |
566 | 568 |
567 Description describe(Description description) => | 569 Description describe(Description description) => |
568 description.add('is in ').addDescriptionOf(_expected); | 570 description.add('is in ').addDescriptionOf(_expected); |
569 } | 571 } |
570 | 572 |
571 /// Returns a matcher that uses an arbitrary function that returns | 573 /// Returns a matcher that uses an arbitrary function that returns |
572 /// true or false for the actual value. For example: | 574 /// true or false for the actual value. |
| 575 /// |
| 576 /// For example: |
573 /// | 577 /// |
574 /// expect(v, predicate((x) => ((x % 2) == 0), "is even")) | 578 /// expect(v, predicate((x) => ((x % 2) == 0), "is even")) |
575 Matcher predicate(bool f(value), [String description = 'satisfies function']) => | 579 Matcher predicate(bool f(value), [String description = 'satisfies function']) => |
576 new _Predicate(f, description); | 580 new _Predicate(f, description); |
577 | 581 |
578 typedef bool _PredicateFunction(value); | 582 typedef bool _PredicateFunction(value); |
579 | 583 |
580 class _Predicate extends Matcher { | 584 class _Predicate extends Matcher { |
581 final _PredicateFunction _matcher; | 585 final _PredicateFunction _matcher; |
582 final String _description; | 586 final String _description; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 addDescriptionOf(matchState['feature']); | 639 addDescriptionOf(matchState['feature']); |
636 var innerDescription = new StringDescription(); | 640 var innerDescription = new StringDescription(); |
637 _matcher.describeMismatch(matchState['feature'], innerDescription, | 641 _matcher.describeMismatch(matchState['feature'], innerDescription, |
638 matchState['state'], verbose); | 642 matchState['state'], verbose); |
639 if (innerDescription.length > 0) { | 643 if (innerDescription.length > 0) { |
640 mismatchDescription.add(' which ').add(innerDescription.toString()); | 644 mismatchDescription.add(' which ').add(innerDescription.toString()); |
641 } | 645 } |
642 return mismatchDescription; | 646 return mismatchDescription; |
643 } | 647 } |
644 } | 648 } |
OLD | NEW |