OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library matcher.map_matchers; |
| 6 |
| 7 import 'interfaces.dart'; |
| 8 import 'util.dart'; |
| 9 |
| 10 /// Returns a matcher which matches maps containing the given [value]. |
| 11 Matcher containsValue(value) => new _ContainsValue(value); |
| 12 |
| 13 class _ContainsValue extends Matcher { |
| 14 final _value; |
| 15 |
| 16 const _ContainsValue(this._value); |
| 17 |
| 18 bool matches(item, Map matchState) => item.containsValue(_value); |
| 19 Description describe(Description description) => |
| 20 description.add('contains value ').addDescriptionOf(_value); |
| 21 } |
| 22 |
| 23 /// Returns a matcher which matches maps containing the key-value pair |
| 24 /// with [key] => [value]. |
| 25 Matcher containsPair(key, value) => |
| 26 new _ContainsMapping(key, wrapMatcher(value)); |
| 27 |
| 28 class _ContainsMapping extends Matcher { |
| 29 final _key; |
| 30 final Matcher _valueMatcher; |
| 31 |
| 32 const _ContainsMapping(this._key, Matcher this._valueMatcher); |
| 33 |
| 34 bool matches(item, Map matchState) => |
| 35 item.containsKey(_key) && _valueMatcher.matches(item[_key], matchState); |
| 36 |
| 37 Description describe(Description description) { |
| 38 return description |
| 39 .add('contains pair ') |
| 40 .addDescriptionOf(_key) |
| 41 .add(' => ') |
| 42 .addDescriptionOf(_valueMatcher); |
| 43 } |
| 44 |
| 45 Description describeMismatch( |
| 46 item, Description mismatchDescription, Map matchState, bool verbose) { |
| 47 if (!item.containsKey(_key)) { |
| 48 return mismatchDescription |
| 49 .add(" doesn't contain key ") |
| 50 .addDescriptionOf(_key); |
| 51 } else { |
| 52 mismatchDescription |
| 53 .add(' contains key ') |
| 54 .addDescriptionOf(_key) |
| 55 .add(' but with value '); |
| 56 _valueMatcher.describeMismatch( |
| 57 item[_key], mismatchDescription, matchState, verbose); |
| 58 return mismatchDescription; |
| 59 } |
| 60 } |
| 61 } |
OLD | NEW |