Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/217.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made explicitly constructed ``equal_to`` matchers describe the equality comparison, including inside composite matchers.
8 changes: 7 additions & 1 deletion src/hamcrest/core/core/isequal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<")
Expand Down
5 changes: 3 additions & 2 deletions src/hamcrest/core/helpers/wrap_matcher.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/hamcrest_unit_test/assert_that_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions tests/hamcrest_unit_test/core/allof_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)

Expand All @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion tests/hamcrest_unit_test/core/anyof_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)

Expand Down
2 changes: 1 addition & 1 deletion tests/hamcrest_unit_test/core/is_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<True>", is_(equal_to(True)))
self.assert_description("a value equal to <True>", is_(equal_to(True)))

def testProvidesConvenientShortcutForIsEqualTo(self):
self.assert_matches("should match", is_("A"), "A")
Expand Down
21 changes: 18 additions & 3 deletions tests/hamcrest_unit_test/core/isequal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -44,11 +45,25 @@ class Argument:
def __str__(self):
return argument_description

self.assert_description("<ARGUMENT DESCRIPTION>", equal_to(Argument()))
self.assert_description("a value equal to <ARGUMENT DESCRIPTION>", 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 <a value equal to 'NestedMatcher'>", equal_to(inner_matcher)
)

def testSuccessfulMatchDoesNotGenerateMismatchDescription(self):
self.assert_no_mismatch_description(equal_to("hi"), "hi")
Expand Down
Loading