Skip to content

Commit

Permalink
If has_entry() has only a single matching key, report the matching va…
Browse files Browse the repository at this point in the history
…lue.
  • Loading branch information
brunns authored and offbyone committed Mar 6, 2021
1 parent bbd147d commit aa498af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/hamcrest/library/collection/isdict_containing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ def describe_to(self, description: Description) -> None:
).append_text(": ").append_description_of(self.value_matcher).append_text("]")

def describe_mismatch(self, item: Mapping[K, V], mismatch_description: Description) -> None:
key_matches: MutableMapping[K, V] = {}
if hasmethod(item, "items"):
for key, value in item.items():
if self.key_matcher.matches(key):
key_matches[key] = value
key_matches = self._matching_keys(item)
if len(key_matches) == 1:
key, value = key_matches.popitem()
mismatch_description.append_text("value for ").append_description_of(key).append_text(
Expand All @@ -48,7 +44,21 @@ def describe_mismatch(self, item: Mapping[K, V], mismatch_description: Descripti
super().describe_mismatch(item, mismatch_description)

def describe_match(self, item: Mapping[K, V], match_description: Description) -> None:
super().describe_match(item, match_description)
key_matches = self._matching_keys(item)
if len(key_matches) == 1:
key, value = key_matches.popitem()
match_description.append_text("value for ").append_description_of(key).append_text(" ")
self.value_matcher.describe_mismatch(value, match_description)
else:
super().describe_match(item, match_description)

def _matching_keys(self, item):
key_matches: MutableMapping[K, V] = {}
if hasmethod(item, "items"):
for key, value in item.items():
if self.key_matcher.matches(key):
key_matches[key] = value
return key_matches


def has_entry(
Expand Down
10 changes: 7 additions & 3 deletions tests/hamcrest_unit_test/collection/isdict_containing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from hamcrest import starts_with
from hamcrest.core.core.isequal import equal_to
from hamcrest.library.collection.isdict_containing import has_entry
from hamcrest_unit_test.matcher_test import MatcherTest, assert_mismatch_description
from hamcrest_unit_test.matcher_test import (
MatcherTest,
assert_match_description,
assert_mismatch_description,
)

from .quasidict import QuasiDictionary

Expand Down Expand Up @@ -50,8 +54,8 @@ def test_describe_single_matching_key_mismatching_value(self):
"was <{'ab': 2, 'ac': 3}>", has_entry(starts_with("a"), 1), {"ab": 2, "ac": 3}
)

# def test_describe_match(self):
# assert_match_description("value for 'a' was <1>", has_entry("a", 1), {"a": 1})
def test_describe_match(self):
assert_match_description("value for 'a' was <1>", has_entry("a", 1), {"a": 1, "b": 2})


if __name__ == "__main__":
Expand Down

0 comments on commit aa498af

Please sign in to comment.