From 99d2665d8a427740fbd7587365331b537f5a255f Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 3 Mar 2023 20:46:31 +0000 Subject: [PATCH 1/7] Better handling for exception messages with special characters - raises(Error, "message with (parens)") no longer fails with an exact match --- src/hamcrest/core/core/raises.py | 3 ++- .../core/raises_with_parens_test.py | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/hamcrest_unit_test/core/raises_with_parens_test.py diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index 0fcd6fee..dc9fd4f4 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,7 +41,8 @@ def _call_function(self, function: Callable[..., Any]) -> bool: if isinstance(self.actual, self.expected): if self.pattern is not None: - if re.search(self.pattern, str(self.actual)) is None: + if (re.search(self.pattern, str(self.actual)) is None + and self.pattern is not str(self.actual)): return False if self.matcher is not None: if not self.matcher.matches(self.actual): diff --git a/tests/hamcrest_unit_test/core/raises_with_parens_test.py b/tests/hamcrest_unit_test/core/raises_with_parens_test.py new file mode 100644 index 00000000..22b47c67 --- /dev/null +++ b/tests/hamcrest_unit_test/core/raises_with_parens_test.py @@ -0,0 +1,24 @@ +import unittest + +from hamcrest import assert_that, calling, raises + + +def raise_error(msg): + raise AssertionError(msg) + + +class ParensTest(unittest.TestCase): + + def test_literal_parens(self): + + message = 'Message with (parens)' + assert_that( + calling(raise_error).with_args(message), + raises(AssertionError, message) + ) + + def test_parens_in_regex(self): + assert_that( + calling(raise_error).with_args('abab'), + raises(AssertionError, r'(ab)+') + ) From e0d0ce5758f65466662e71886e023edb819544f0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 21:10:14 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/hamcrest/core/core/raises.py | 5 +++-- .../core/raises_with_parens_test.py | 13 +++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index dc9fd4f4..da562e7a 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,8 +41,9 @@ def _call_function(self, function: Callable[..., Any]) -> bool: if isinstance(self.actual, self.expected): if self.pattern is not None: - if (re.search(self.pattern, str(self.actual)) is None - and self.pattern is not str(self.actual)): + if re.search( + self.pattern, str(self.actual) + ) is None and self.pattern is not str(self.actual): return False if self.matcher is not None: if not self.matcher.matches(self.actual): diff --git a/tests/hamcrest_unit_test/core/raises_with_parens_test.py b/tests/hamcrest_unit_test/core/raises_with_parens_test.py index 22b47c67..4c48f794 100644 --- a/tests/hamcrest_unit_test/core/raises_with_parens_test.py +++ b/tests/hamcrest_unit_test/core/raises_with_parens_test.py @@ -8,17 +8,10 @@ def raise_error(msg): class ParensTest(unittest.TestCase): - def test_literal_parens(self): - message = 'Message with (parens)' - assert_that( - calling(raise_error).with_args(message), - raises(AssertionError, message) - ) + message = "Message with (parens)" + assert_that(calling(raise_error).with_args(message), raises(AssertionError, message)) def test_parens_in_regex(self): - assert_that( - calling(raise_error).with_args('abab'), - raises(AssertionError, r'(ab)+') - ) + assert_that(calling(raise_error).with_args("abab"), raises(AssertionError, r"(ab)+")) From e53c445d898e612d05ed40e4cd0da4209aa5332a Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 3 Mar 2023 22:33:42 +0000 Subject: [PATCH 3/7] Use != instead of is not4 - consolidate unit tests --- src/hamcrest/core/core/raises.py | 5 ++--- tests/hamcrest_unit_test/core/raises_test.py | 22 ++++++++++++++++++- .../core/raises_with_parens_test.py | 17 -------------- 3 files changed, 23 insertions(+), 21 deletions(-) delete mode 100644 tests/hamcrest_unit_test/core/raises_with_parens_test.py diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index da562e7a..17132f36 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,9 +41,8 @@ def _call_function(self, function: Callable[..., Any]) -> bool: if isinstance(self.actual, self.expected): if self.pattern is not None: - if re.search( - self.pattern, str(self.actual) - ) is None and self.pattern is not str(self.actual): + if re.search(self.pattern, str(self.actual)) is None + and self.pattern != str(self.actual): return False if self.matcher is not None: if not self.matcher.matches(self.actual): diff --git a/tests/hamcrest_unit_test/core/raises_test.py b/tests/hamcrest_unit_test/core/raises_test.py index 5f99fbd4..91cac2a9 100644 --- a/tests/hamcrest_unit_test/core/raises_test.py +++ b/tests/hamcrest_unit_test/core/raises_test.py @@ -2,7 +2,7 @@ import unittest import pytest -from hamcrest import has_properties, not_ +from hamcrest import has_properties, not_, assert_that from hamcrest.core.core.raises import calling, raises from hamcrest_unit_test.matcher_test import MatcherTest, assert_mismatch_description @@ -166,6 +166,26 @@ def test_gives_correct_message_when_wrapped_with_is_not(expected_message): ) +def raise_error(msg): + raise AssertionError(msg) + + +class ParensTest(unittest.TestCase): + + def test_literal_parens(self): + + message = 'Message with (parens)' + assert_that( + calling(raise_error).with_args(message), + raises(AssertionError, message) + ) + + def test_parens_in_regex(self): + assert_that( + calling(raise_error).with_args('abab'), + raises(AssertionError, r'(ab)+') + ) + class CallingTest(unittest.TestCase): def testCallingDoesNotImmediatelyExecuteFunction(self): try: diff --git a/tests/hamcrest_unit_test/core/raises_with_parens_test.py b/tests/hamcrest_unit_test/core/raises_with_parens_test.py deleted file mode 100644 index 4c48f794..00000000 --- a/tests/hamcrest_unit_test/core/raises_with_parens_test.py +++ /dev/null @@ -1,17 +0,0 @@ -import unittest - -from hamcrest import assert_that, calling, raises - - -def raise_error(msg): - raise AssertionError(msg) - - -class ParensTest(unittest.TestCase): - def test_literal_parens(self): - - message = "Message with (parens)" - assert_that(calling(raise_error).with_args(message), raises(AssertionError, message)) - - def test_parens_in_regex(self): - assert_that(calling(raise_error).with_args("abab"), raises(AssertionError, r"(ab)+")) From aabca769615fdb54b01d05ce0b735741ab19efb2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:41:08 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/hamcrest_unit_test/core/raises_test.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/hamcrest_unit_test/core/raises_test.py b/tests/hamcrest_unit_test/core/raises_test.py index 91cac2a9..f0436ceb 100644 --- a/tests/hamcrest_unit_test/core/raises_test.py +++ b/tests/hamcrest_unit_test/core/raises_test.py @@ -171,20 +171,14 @@ def raise_error(msg): class ParensTest(unittest.TestCase): - def test_literal_parens(self): - message = 'Message with (parens)' - assert_that( - calling(raise_error).with_args(message), - raises(AssertionError, message) - ) + message = "Message with (parens)" + assert_that(calling(raise_error).with_args(message), raises(AssertionError, message)) def test_parens_in_regex(self): - assert_that( - calling(raise_error).with_args('abab'), - raises(AssertionError, r'(ab)+') - ) + assert_that(calling(raise_error).with_args("abab"), raises(AssertionError, r"(ab)+")) + class CallingTest(unittest.TestCase): def testCallingDoesNotImmediatelyExecuteFunction(self): From af803488cdd95596af7a4cbcaad213a7ff8b3f08 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 3 Mar 2023 22:46:12 +0000 Subject: [PATCH 5/7] fixes parens --- src/hamcrest/core/core/raises.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index 17132f36..858681f9 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,8 +41,8 @@ def _call_function(self, function: Callable[..., Any]) -> bool: if isinstance(self.actual, self.expected): if self.pattern is not None: - if re.search(self.pattern, str(self.actual)) is None - and self.pattern != str(self.actual): + if (re.search(self.pattern, str(self.actual)) is None + and self.pattern != str(self.actual)): return False if self.matcher is not None: if not self.matcher.matches(self.actual): From 194dcac289da4bed36e2bf6e27862fcc0ee5857a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:46:48 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/hamcrest/core/core/raises.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index 858681f9..15cccdde 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,8 +41,9 @@ def _call_function(self, function: Callable[..., Any]) -> bool: if isinstance(self.actual, self.expected): if self.pattern is not None: - if (re.search(self.pattern, str(self.actual)) is None - and self.pattern != str(self.actual)): + if re.search(self.pattern, str(self.actual)) is None and self.pattern != str( + self.actual + ): return False if self.matcher is not None: if not self.matcher.matches(self.actual): From 301150c48d4c72f6d7058b16b7058aa79a34d71d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:05:53 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/hamcrest_unit_test/core/raises_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/hamcrest_unit_test/core/raises_test.py b/tests/hamcrest_unit_test/core/raises_test.py index faa46bc2..dbf42404 100644 --- a/tests/hamcrest_unit_test/core/raises_test.py +++ b/tests/hamcrest_unit_test/core/raises_test.py @@ -1,7 +1,6 @@ import sys import unittest -import pytest from hamcrest import has_properties, not_, assert_that from hamcrest.core.core.raises import calling, raises from hamcrest_unit_test.matcher_test import MatcherTest, assert_mismatch_description @@ -137,7 +136,6 @@ def raise_error(msg): class ParensTest(unittest.TestCase): def test_literal_parens(self): - message = "Message with (parens)" assert_that(calling(raise_error).with_args(message), raises(AssertionError, message))