PyHamcrest icon indicating copy to clipboard operation
PyHamcrest copied to clipboard

Better handling for exception messages with special characters

Open tallus opened this issue 2 years ago • 0 comments

raises(Error, "message with (parens)") no longer fails with an exact match

def raise_error():
    raise AssertionError("Message with  (parens)")

assert_that(calling(raise_error),  raises(AssertionError, "Message with (parens)")
```
will fail with `Correct assertion type raised, but the expected pattern ("Message with (parens)") not found. Exception message was: "Message with (parens)"` which is a somewhat confusing message.

This is because the parens in the pattern are treated as a regex group and need to be escaped.

This pull request removes this source of potential confusion. The above code will now not error as a regex mismatch will only happen if the pattern does not match as a regex AND it is not a literal match for the error.

tallus avatar Mar 03 '23 21:03 tallus