Skip to content

Commit 4745039

Browse files
committed
Add assert_not_regex()
Closes: #10
1 parent b7f1988 commit 4745039

File tree

4 files changed

+99
-24
lines changed

4 files changed

+99
-24
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Drop support for Python 3.6.
66

7+
### API Additions
8+
9+
- Add `assert_not_regex()`.
10+
711
### Other Changes
812

913
- Modernize the type stubs.

asserts/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,30 @@ def assert_regex(text, regex, msg_fmt="{msg}"):
551551
fail(msg_fmt.format(msg=msg, text=text, pattern=compiled.pattern))
552552

553553

554+
def assert_not_regex(text, regex, msg_fmt="{msg}"):
555+
"""Fail if text does match the regular expression.
556+
557+
regex can be either a regular expression string or a compiled regular
558+
expression object.
559+
560+
>>> assert_regex("Hello World!", r"llo.*rld!$")
561+
>>> assert_regex("Hello World!", r"\\d")
562+
Traceback (most recent call last):
563+
...
564+
AssertionError: 'Hello World!' does not match '\\\\d'
565+
566+
The following msg_fmt arguments are supported:
567+
* msg - the default error message
568+
* text - text that is matched
569+
* pattern - regular expression pattern as string
570+
"""
571+
572+
compiled = re.compile(regex)
573+
if compiled.search(text):
574+
msg = "{!r} matches {!r}".format(text, compiled.pattern)
575+
fail(msg_fmt.format(msg=msg, text=text, pattern=compiled.pattern))
576+
577+
554578
def assert_is(first, second, msg_fmt="{msg}"):
555579
"""Fail if first and second do not refer to the same object.
556580

asserts/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def assert_greater_equal(
104104
def assert_regex(
105105
text: str, regex: str | Pattern[str], msg_fmt: str = ...
106106
) -> None: ...
107+
def assert_not_regex(
108+
text: str, regex: str | Pattern[str], msg_fmt: str = ...
109+
) -> None: ...
107110
def assert_is(first: object, second: object, msg_fmt: str = ...) -> None: ...
108111
def assert_is_not(
109112
first: object, second: object, msg_fmt: str = ...

test_asserts.py

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,47 @@
55
from collections import OrderedDict
66
from datetime import datetime, timedelta
77
from unittest import TestCase
8-
from warnings import warn, catch_warnings
8+
from warnings import catch_warnings, warn
99

1010
from asserts import (
11-
fail,
12-
assert_true,
13-
assert_false,
14-
assert_boolean_true,
15-
assert_boolean_false,
16-
assert_is_none,
17-
assert_is_not_none,
18-
assert_equal,
19-
assert_not_equal,
11+
Exists,
2012
assert_almost_equal,
21-
assert_not_almost_equal,
13+
assert_between,
14+
assert_boolean_false,
15+
assert_boolean_true,
16+
assert_count_equal,
17+
assert_datetime_about_now,
18+
assert_datetime_about_now_utc,
2219
assert_dict_equal,
2320
assert_dict_superset,
24-
assert_less,
25-
assert_less_equal,
21+
assert_equal,
22+
assert_false,
2623
assert_greater,
2724
assert_greater_equal,
28-
assert_between,
25+
assert_has_attr,
26+
assert_in,
2927
assert_is,
28+
assert_is_instance,
29+
assert_is_none,
3030
assert_is_not,
31-
assert_in,
31+
assert_is_not_none,
32+
assert_json_subset,
33+
assert_less,
34+
assert_less_equal,
35+
assert_not_almost_equal,
36+
assert_not_equal,
3237
assert_not_in,
33-
assert_count_equal,
34-
assert_regex,
35-
assert_is_instance,
3638
assert_not_is_instance,
37-
assert_has_attr,
38-
assert_datetime_about_now,
39-
assert_datetime_about_now_utc,
39+
assert_not_regex,
4040
assert_raises,
41-
assert_raises_regex,
4241
assert_raises_errno,
42+
assert_raises_regex,
43+
assert_regex,
4344
assert_succeeds,
45+
assert_true,
4446
assert_warns,
4547
assert_warns_regex,
46-
assert_json_subset,
47-
Exists,
48+
fail,
4849
)
4950

5051

@@ -617,6 +618,49 @@ def test_assert_regex__does_not_match_regex__custom_message(self):
617618
):
618619
assert_regex("Wrong text", regex, "{msg};{text!r};{pattern!r}")
619620

621+
# assert_not_regex()
622+
623+
def test_assert_not_regex__does_not_match_string(self):
624+
assert_not_regex("This is a test text", "no match")
625+
626+
def test_assert_not_regex__does_not_match_regex(self):
627+
regex = re.compile("no match")
628+
assert_not_regex("This is a test text", regex)
629+
630+
def test_assert_not_regex__matches_string__default_message(self):
631+
with _assert_raises_assertion(
632+
"'This is a test text' matches 'is.*test'"
633+
):
634+
assert_not_regex("This is a test text", "is.*test")
635+
636+
def test_assert_not_regex__matches_regex__default_message(self):
637+
regex = re.compile("is.*test")
638+
with _assert_raises_assertion(
639+
"'This is a test text' matches 'is.*test'"
640+
):
641+
assert_not_regex("This is a test text", regex)
642+
643+
def test_assert_not_regex__matches_string__custom_message(self):
644+
with _assert_raises_assertion(
645+
"'This is a test text' matches 'is.*test';"
646+
"'This is a test text';'is.*test'"
647+
):
648+
assert_not_regex(
649+
"This is a test text",
650+
"is.*test",
651+
"{msg};{text!r};{pattern!r}",
652+
)
653+
654+
def test_assert_not_regex__matches_regex__custom_message(self):
655+
regex = re.compile("is.*test")
656+
with _assert_raises_assertion(
657+
"'This is a test text' matches 'is.*test';'This is a test text';"
658+
"'is.*test'"
659+
):
660+
assert_not_regex(
661+
"This is a test text", regex, "{msg};{text!r};{pattern!r}"
662+
)
663+
620664
# assert_is()
621665

622666
def test_assert_is__same(self):

0 commit comments

Comments
 (0)