Test that compiled regex patterns work with pytest.raises.
(self)
| 441 | exc_info.value.close() class="cm"># avoid a resource warning |
| 442 | |
| 443 | def test_raises_match_compiled_regex(self) -> None: |
| 444 | class="st">""class="st">"Test that compiled regex patterns work with pytest.raises."class="st">"" |
| 445 | class="cm"># Test with a compiled pattern that matches |
| 446 | pattern = re.compile(rclass="st">"with base \d+") |
| 447 | with pytest.raises(ValueError, match=pattern): |
| 448 | int(class="st">"asdf") |
| 449 | |
| 450 | class="cm"># Test with a compiled pattern that doesn't match |
| 451 | pattern_nomatch = re.compile(rclass="st">"with base 16") |
| 452 | expr = ( |
| 453 | class="st">"Regex pattern did not match.\n" |
| 454 | fclass="st">" Expected regex: {pattern_nomatch.pattern!r}\n" |
| 455 | fclass="st">" Actual message: \"invalid literal for int() with base 10: &class="cm">#x27;asdf'\class="st">"" |
| 456 | ) |
| 457 | with pytest.raises(AssertionError, match=class="st">"^" + re.escape(expr) + class="st">"$"): |
| 458 | with pytest.raises(ValueError, match=pattern_nomatch): |
| 459 | int(class="st">"asdf", base=10) |
| 460 | |
| 461 | class="cm"># Test compiled pattern with flags |
| 462 | pattern_with_flags = re.compile(rclass="st">"INVALID LITERAL", re.IGNORECASE) |
| 463 | with pytest.raises(ValueError, match=pattern_with_flags): |
| 464 | int(class="st">"asdf") |
| 465 | |
| 466 | def test_pipe_is_treated_as_regex_metacharacter(self) -> None: |
| 467 | class="st">""class="st">"| (pipe) must be recognized as a regex metacharacter."class="st">"" |