From b70d6cfe9cd546f82885cdd1a3b3897791ccb0c7 Mon Sep 17 00:00:00 2001 From: Denis Alexeev Date: Thu, 11 Mar 2021 09:17:51 +0300 Subject: [PATCH 1/4] Add repr into StringDescription --- src/hamcrest/core/string_description.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hamcrest/core/string_description.py b/src/hamcrest/core/string_description.py index 96fbb260..10142d26 100644 --- a/src/hamcrest/core/string_description.py +++ b/src/hamcrest/core/string_description.py @@ -31,5 +31,9 @@ def __str__(self) -> str: """Returns the description.""" return self.out + def __repr__(self) -> str: + """Returns the description.""" + return self.out + def append(self, string: str) -> None: self.out += str(string) From 7c3eb70902093316c32268caf3a29866fa58a92e Mon Sep 17 00:00:00 2001 From: Denis Alexeev Date: Thu, 11 Mar 2021 10:56:33 +0300 Subject: [PATCH 2/4] Fixes according to reviewer's remarks --- changelog.d/170.feature.rst | 1 + src/hamcrest/core/string_description.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 changelog.d/170.feature.rst diff --git a/changelog.d/170.feature.rst b/changelog.d/170.feature.rst new file mode 100644 index 00000000..8c4acbb1 --- /dev/null +++ b/changelog.d/170.feature.rst @@ -0,0 +1 @@ +* Add pretty string representation for matchers objects \ No newline at end of file diff --git a/src/hamcrest/core/string_description.py b/src/hamcrest/core/string_description.py index 10142d26..85cf8e4e 100644 --- a/src/hamcrest/core/string_description.py +++ b/src/hamcrest/core/string_description.py @@ -1,4 +1,5 @@ from hamcrest.core.selfdescribing import SelfDescribing +from textwrap import shorten from .base_description import BaseDescription @@ -32,8 +33,8 @@ def __str__(self) -> str: return self.out def __repr__(self) -> str: - """Returns the description.""" - return self.out + """Returns the object string representation.""" + return "<{0}({1})>".format(self.__class__.__name__, shorten(self.out, 60, placeholder="...")) def append(self, string: str) -> None: self.out += str(string) From a4320a7b488940a833bdd917cf144cc7457933a1 Mon Sep 17 00:00:00 2001 From: Denis Alexeev Date: Thu, 11 Mar 2021 11:31:29 +0300 Subject: [PATCH 3/4] Fixes according to reviewer's remarks --- src/hamcrest/core/base_matcher.py | 7 +++++++ src/hamcrest/core/string_description.py | 5 ----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/hamcrest/core/base_matcher.py b/src/hamcrest/core/base_matcher.py index c1c01ea4..7edc994f 100644 --- a/src/hamcrest/core/base_matcher.py +++ b/src/hamcrest/core/base_matcher.py @@ -1,3 +1,4 @@ +from textwrap import shorten from typing import Optional, TypeVar from hamcrest.core.description import Description @@ -25,6 +26,12 @@ class BaseMatcher(Matcher[T]): def __str__(self) -> str: return tostring(self) + def __repr__(self) -> str: + """Returns matcher string representation.""" + return "<{0}({1})>".format( + self.__class__.__name__, shorten(tostring(self), 60, placeholder="...") + ) + def _matches(self, item: T) -> bool: raise NotImplementedError("_matches") diff --git a/src/hamcrest/core/string_description.py b/src/hamcrest/core/string_description.py index 85cf8e4e..96fbb260 100644 --- a/src/hamcrest/core/string_description.py +++ b/src/hamcrest/core/string_description.py @@ -1,5 +1,4 @@ from hamcrest.core.selfdescribing import SelfDescribing -from textwrap import shorten from .base_description import BaseDescription @@ -32,9 +31,5 @@ def __str__(self) -> str: """Returns the description.""" return self.out - def __repr__(self) -> str: - """Returns the object string representation.""" - return "<{0}({1})>".format(self.__class__.__name__, shorten(self.out, 60, placeholder="...")) - def append(self, string: str) -> None: self.out += str(string) From d20c7cf7d5c1d3e7eabac39aad4817b459e99b6c Mon Sep 17 00:00:00 2001 From: Denis Alexeev Date: Sat, 2 Oct 2021 00:22:12 +0300 Subject: [PATCH 4/4] add a couple of tests --- tests/hamcrest_unit_test/base_matcher_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/hamcrest_unit_test/base_matcher_test.py b/tests/hamcrest_unit_test/base_matcher_test.py index 1817388a..5ad2a610 100644 --- a/tests/hamcrest_unit_test/base_matcher_test.py +++ b/tests/hamcrest_unit_test/base_matcher_test.py @@ -37,6 +37,19 @@ def testMismatchDescriptionShouldDescribeItem(self): def testMatchDescriptionShouldDescribeItem(self): assert_match_description("was <99>", PassingBaseMatcher(), 99) + def testMatcherReprShouldDescribeMatcher(self): + assert repr(FailingBaseMatcher()) == "" + + def testMatcherReprShouldTruncateLongDescription(self): + class LongDescriptionMatcher(BaseMatcher): + def describe_to(self, description): + description.append_text("1234 " * 13) + + assert ( + repr(LongDescriptionMatcher()) + == "" + ) + if __name__ == "__main__": unittest.main()