forked from doyensec/regexploit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_character.py
84 lines (56 loc) · 2.44 KB
/
test_character.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from sre_parse import parse as sre_parse
from char import Character
from sre import SreOpParser
def from_regex(pattern: str) -> Character:
(parsed_char,) = sre_parse(pattern)
char = SreOpParser.parse_op(*parsed_char)
assert isinstance(char, Character)
return char
def test_literal_and():
assert from_regex("[abc]") & from_regex("[bcd]") == from_regex("[bc]")
def test_literal_negated_and():
assert from_regex("[^abc]") & from_regex("[^bcd]") == from_regex("[^abcd]")
def test_literal_mixed_and():
assert from_regex("[abcz]") & from_regex("[^bcd]") == from_regex("[az]")
assert from_regex("[^bcd]") & from_regex("[abcz]") == from_regex("[az]")
def test_category_and():
assert from_regex(r"[\s\d]") & from_regex(r"[\d\w]") == from_regex(r"\d")
def test_category_negated_and():
assert from_regex(r"[^\s\d]") & from_regex(r"[^\d\w]") == from_regex(r"[^\s\d\w]")
def test_category_negated_and_simplifies_to_nothing():
assert (from_regex(r"[^\s\d]") & from_regex(r"[^\D]")) is None
def test_mixed_and():
assert from_regex(r"[abc123\s]") & from_regex(r"[^\d\s]") == from_regex("[abc]")
def test_mixed_and_none():
c = from_regex(r"[123]") & from_regex(r"[^\d\s]")
assert c is None
def test_and_any_none():
any = Character.ANY()
for other in (
from_regex(r"a"),
from_regex(r"\s"),
from_regex(r"[a\s\S\d]"),
from_regex(r"[A-z]"),
from_regex(r"[^A-z\d]"),
):
assert any & other == other
assert other & any == other
assert any & None == None
assert None & any == None
def test_class():
assert from_regex("[abc]").exact_character_class() == from_regex("[cba]")
def test_negate_simple():
assert from_regex("a").negate() == from_regex("[^a]")
assert from_regex(r"\w").negate() == from_regex(r"[^\w]")
assert from_regex("[^ab]").negate() == from_regex("[ab]")
assert from_regex(r"[^\s]").negate() == from_regex(r"\s")
def test_negate_mixed():
assert from_regex(r"[a\s\w]").negate() == from_regex(r"[^a\s\w]")
def test_or():
assert from_regex("a") | from_regex("a") == from_regex("a")
assert from_regex("a") | from_regex("b") == from_regex("[ab]")
assert from_regex(r"\w") | from_regex("b") == from_regex(r"\w").expand_categories()
assert (
from_regex(r"\w") | from_regex("9") == from_regex(r"[9\w]").expand_categories()
)
assert from_regex("[^a]") | from_regex("[^b]") == from_regex(".")