diff --git a/changelog.d/217.feature.rst b/changelog.d/217.feature.rst new file mode 100644 index 00000000..ddd27854 --- /dev/null +++ b/changelog.d/217.feature.rst @@ -0,0 +1 @@ +Made explicitly constructed ``equal_to`` matchers describe the equality comparison, including inside composite matchers. diff --git a/src/hamcrest/core/core/isequal.py b/src/hamcrest/core/core/isequal.py index 18160919..baf37394 100644 --- a/src/hamcrest/core/core/isequal.py +++ b/src/hamcrest/core/core/isequal.py @@ -10,13 +10,19 @@ class IsEqual(BaseMatcher[Any]): - def __init__(self, equals: Any) -> None: + # Matchers pickled before equality was described explicitly stored only the object. + _describe_equality = False + + def __init__(self, equals: Any, *, describe_equality: bool = True) -> None: self.object = equals + self._describe_equality = describe_equality def _matches(self, item: Any) -> bool: return item == self.object def describe_to(self, description: Description) -> None: + if self._describe_equality: + description.append_text("a value equal to ") nested_matcher = isinstance(self.object, Matcher) if nested_matcher: description.append_text("<") diff --git a/src/hamcrest/core/helpers/wrap_matcher.py b/src/hamcrest/core/helpers/wrap_matcher.py index 99d3fc09..2338e927 100644 --- a/src/hamcrest/core/helpers/wrap_matcher.py +++ b/src/hamcrest/core/helpers/wrap_matcher.py @@ -1,7 +1,7 @@ from typing import Type, TypeVar, Union from hamcrest.core.base_matcher import Matcher -from hamcrest.core.core.isequal import equal_to +from hamcrest.core.core.isequal import IsEqual __author__ = "Jon Reid" __copyright__ = "Copyright 2011 hamcrest.org" @@ -15,12 +15,13 @@ def wrap_matcher(x: Union[Matcher[T], T]) -> Matcher[T]: :returns: the argument as-is if it is already a matcher, otherwise wrapped in an :py:func:`~hamcrest.core.core.isequal.equal_to` matcher. + Implicitly wrapped values retain their literal description. """ if isinstance(x, Matcher): return x else: - return equal_to(x) + return IsEqual(x, describe_equality=False) def is_matchable_type(expected_type: Type) -> bool: diff --git a/tests/hamcrest_unit_test/assert_that_test.py b/tests/hamcrest_unit_test/assert_that_test.py index 59fa36c9..130d78ae 100644 --- a/tests/hamcrest_unit_test/assert_that_test.py +++ b/tests/hamcrest_unit_test/assert_that_test.py @@ -23,7 +23,7 @@ def testAssertionErrorShouldDescribeExpectedAndActual(self): expected = "EXPECTED" actual = "ACTUAL" - expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" + expectedMessage = "\nExpected: a value equal to 'EXPECTED'\n but: was 'ACTUAL'\n" with self.assertRaises(AssertionError) as e: assert_that(actual, equal_to(expected)) @@ -34,7 +34,7 @@ def testAssertionErrorShouldIncludeOptionalReason(self): expected = "EXPECTED" actual = "ACTUAL" - expectedMessage = "REASON\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" + expectedMessage = "REASON\nExpected: a value equal to 'EXPECTED'\n but: was 'ACTUAL'\n" with self.assertRaises(AssertionError) as e: assert_that(actual, equal_to(expected), "REASON") diff --git a/tests/hamcrest_unit_test/core/allof_test.py b/tests/hamcrest_unit_test/core/allof_test.py index 2efd0793..8658a5aa 100644 --- a/tests/hamcrest_unit_test/core/allof_test.py +++ b/tests/hamcrest_unit_test/core/allof_test.py @@ -62,7 +62,7 @@ def testNoMatchIfArgumentFailsToSatisfyAllOfManyOtherMatchers(self): def testHasAReadableDescription(self): self.assert_description( - "('good' and 'bad' and 'ugly')", + "(a value equal to 'good' and a value equal to 'bad' and a value equal to 'ugly')", all_of(equal_to("good"), equal_to("bad"), equal_to("ugly")), ) @@ -71,17 +71,17 @@ def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): def testMismatchDescriptionDescribesFirstFailingMatch(self): self.assert_mismatch_description( - "'good' was 'bad'", all_of(equal_to("bad"), equal_to("good")), "bad" + "a value equal to 'good' was 'bad'", all_of(equal_to("bad"), equal_to("good")), "bad" ) def testDescribeMismatch(self): self.assert_describe_mismatch( - "'good' was 'bad'", all_of(equal_to("bad"), equal_to("good")), "bad" + "a value equal to 'good' was 'bad'", all_of(equal_to("bad"), equal_to("good")), "bad" ) def testMismatchDescriptionOptionallyDescribesMultipleFailingMatches(self): self.assert_mismatch_description( - "'bad' was 'indifferent' and 'good' was 'indifferent'", + "a value equal to 'bad' was 'indifferent' and a value equal to 'good' was 'indifferent'", AllOf( equal_to("bad"), equal_to("indifferent"), diff --git a/tests/hamcrest_unit_test/core/anyof_test.py b/tests/hamcrest_unit_test/core/anyof_test.py index a6141f71..4b991786 100644 --- a/tests/hamcrest_unit_test/core/anyof_test.py +++ b/tests/hamcrest_unit_test/core/anyof_test.py @@ -51,7 +51,7 @@ def testNoMatchIfArgumentFailsToSatisfyAnyOfManyOtherMatchers(self): def testHasAReadableDescription(self): self.assert_description( - "('good' or 'bad' or 'ugly')", + "(a value equal to 'good' or a value equal to 'bad' or a value equal to 'ugly')", any_of(equal_to("good"), equal_to("bad"), equal_to("ugly")), ) diff --git a/tests/hamcrest_unit_test/core/is_test.py b/tests/hamcrest_unit_test/core/is_test.py index 05b115d4..46bbdc94 100644 --- a/tests/hamcrest_unit_test/core/is_test.py +++ b/tests/hamcrest_unit_test/core/is_test.py @@ -19,7 +19,7 @@ def testDelegatesMatchingToNestedMatcher(self): self.assert_does_not_match("should not match", is_(equal_to(False)), True) def testDescriptionShouldPassThrough(self): - self.assert_description("", is_(equal_to(True))) + self.assert_description("a value equal to ", is_(equal_to(True))) def testProvidesConvenientShortcutForIsEqualTo(self): self.assert_matches("should match", is_("A"), "A") diff --git a/tests/hamcrest_unit_test/core/isequal_test.py b/tests/hamcrest_unit_test/core/isequal_test.py index 6e6fe1ac..928fa8c3 100644 --- a/tests/hamcrest_unit_test/core/isequal_test.py +++ b/tests/hamcrest_unit_test/core/isequal_test.py @@ -6,7 +6,8 @@ import unittest -from hamcrest.core.core.isequal import equal_to +from hamcrest import any_of, equal_to, greater_than +from hamcrest.core.core.isequal import IsEqual from hamcrest_unit_test.matcher_test import MatcherTest __author__ = "Jon Reid" @@ -44,11 +45,25 @@ class Argument: def __str__(self): return argument_description - self.assert_description("", equal_to(Argument())) + self.assert_description("a value equal to ", equal_to(Argument())) + + def testDescribesEqualityInsideACompoundMatcher(self): + self.assert_description( + "(a value equal to <3> or a value greater than <4>)", + any_of(equal_to(3), greater_than(4)), + ) + self.assert_description("(<3> or a value greater than <4>)", any_of(3, greater_than(4))) + + def testDescribesAnInstanceRestoredFromItsOriginalState(self): + matcher = IsEqual.__new__(IsEqual) + matcher.object = 3 + self.assert_description("<3>", matcher) def testReturnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake(self): inner_matcher = equal_to("NestedMatcher") - self.assert_description("<'NestedMatcher'>", equal_to(inner_matcher)) + self.assert_description( + "a value equal to ", equal_to(inner_matcher) + ) def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): self.assert_no_mismatch_description(equal_to("hi"), "hi")