diff --git a/src/hamcrest/core/core/raises.py b/src/hamcrest/core/core/raises.py index c62bc0c..3677c13 100644 --- a/src/hamcrest/core/core/raises.py +++ b/src/hamcrest/core/core/raises.py @@ -41,7 +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: + 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 8532da4..dbf4240 100644 --- a/tests/hamcrest_unit_test/core/raises_test.py +++ b/tests/hamcrest_unit_test/core/raises_test.py @@ -1,7 +1,7 @@ import sys import unittest -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 @@ -130,6 +130,19 @@ def test_gives_correct_message_when_wrapped_with_is_not(): ) +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: