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