Skip to content

Commit 8a07bee

Browse files
authored
Use dynamic methods for map matchers (#2415)
Allow using `containsKey` and `containsValue` on non-map types like built collection maps which do not implement the core `Map` but has the required methods and operators.
1 parent 40e1f91 commit 8a07bee

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

pkgs/matcher/lib/src/map_matchers.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'feature_matcher.dart';
65
import 'interfaces.dart';
76
import 'util.dart';
87

98
/// Returns a matcher which matches maps containing the given [value].
109
Matcher containsValue(Object? value) => _ContainsValue(value);
1110

12-
class _ContainsValue extends FeatureMatcher<Map> {
11+
class _ContainsValue extends Matcher {
1312
final Object? _value;
1413

1514
const _ContainsValue(this._value);
1615

1716
@override
18-
bool typedMatches(Map item, Map matchState) => item.containsValue(_value);
17+
bool matches(Object? item, Map matchState) =>
18+
// ignore: avoid_dynamic_calls
19+
(item as dynamic).containsValue(_value);
1920
@override
2021
Description describe(Description description) =>
2122
description.add('contains value ').addDescriptionOf(_value);
@@ -26,15 +27,17 @@ class _ContainsValue extends FeatureMatcher<Map> {
2627
Matcher containsPair(Object? key, Object? valueOrMatcher) =>
2728
_ContainsMapping(key, wrapMatcher(valueOrMatcher));
2829

29-
class _ContainsMapping extends FeatureMatcher<Map> {
30+
class _ContainsMapping extends Matcher {
3031
final Object? _key;
3132
final Matcher _valueMatcher;
3233

3334
const _ContainsMapping(this._key, this._valueMatcher);
3435

3536
@override
36-
bool typedMatches(Map item, Map matchState) =>
37-
item.containsKey(_key) && _valueMatcher.matches(item[_key], matchState);
37+
bool matches(Object? item, Map matchState) =>
38+
// ignore: avoid_dynamic_calls
39+
(item as dynamic).containsKey(_key) &&
40+
_valueMatcher.matches((item as dynamic)[_key], matchState);
3841

3942
@override
4043
Description describe(Description description) {
@@ -46,9 +49,10 @@ class _ContainsMapping extends FeatureMatcher<Map> {
4649
}
4750

4851
@override
49-
Description describeTypedMismatch(
50-
Map item, Description mismatchDescription, Map matchState, bool verbose) {
51-
if (!item.containsKey(_key)) {
52+
Description describeMismatch(Object? item, Description mismatchDescription,
53+
Map matchState, bool verbose) {
54+
// ignore: avoid_dynamic_calls
55+
if (!((item as dynamic).containsKey(_key) as bool)) {
5256
return mismatchDescription
5357
.add(" doesn't contain key ")
5458
.addDescriptionOf(_key);
@@ -58,7 +62,7 @@ class _ContainsMapping extends FeatureMatcher<Map> {
5862
.addDescriptionOf(_key)
5963
.add(' but with value ');
6064
_valueMatcher.describeMismatch(
61-
item[_key], mismatchDescription, matchState, verbose);
65+
(item as dynamic)[_key], mismatchDescription, matchState, verbose);
6266
return mismatchDescription;
6367
}
6468
}

0 commit comments

Comments
 (0)