From aa498af0c2b850d5da157bad51fccb0b35d8accb Mon Sep 17 00:00:00 2001 From: Simon Brunning Date: Wed, 24 Feb 2021 14:23:27 +0000 Subject: [PATCH] If has_entry() has only a single matching key, report the matching value. --- .../library/collection/isdict_containing.py | 22 ++++++++++++++----- .../collection/isdict_containing_test.py | 10 ++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/hamcrest/library/collection/isdict_containing.py b/src/hamcrest/library/collection/isdict_containing.py index 092ca7b4..6ddce47f 100644 --- a/src/hamcrest/library/collection/isdict_containing.py +++ b/src/hamcrest/library/collection/isdict_containing.py @@ -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( @@ -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( diff --git a/tests/hamcrest_unit_test/collection/isdict_containing_test.py b/tests/hamcrest_unit_test/collection/isdict_containing_test.py index a9a92cb0..b0737876 100644 --- a/tests/hamcrest_unit_test/collection/isdict_containing_test.py +++ b/tests/hamcrest_unit_test/collection/isdict_containing_test.py @@ -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 @@ -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__":