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