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