-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_randre.py
96 lines (66 loc) · 2.14 KB
/
test_randre.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
85
86
87
88
89
90
91
92
93
94
95
96
import re
import pytest
from randre import randre
def validate_pattern(pattern, runs=1000):
# ugh I would love this to be deterministically testable
# but this is better than nothing
compiled = re.compile(pattern)
for i in range(runs):
generated = randre(pattern)
match = compiled.fullmatch(generated)
assert match is not None, '%r does not match %r' % (generated, pattern)
def test_randre_literal():
assert randre('aaa') == 'aaa'
assert randre('') == ''
assert randre('x') == 'x'
def test_randre_category():
validate_pattern(r'\d')
validate_pattern(r'\D')
validate_pattern(r'\s')
validate_pattern(r'\S')
validate_pattern(r'\w')
validate_pattern(r'\W')
def test_randre_any():
validate_pattern('.')
def test_randre_in():
validate_pattern('[abcde]')
validate_pattern('[^abcde]')
def test_randre_repeat():
validate_pattern('a?')
validate_pattern('a*')
validate_pattern('a+')
validate_pattern('a{10}')
validate_pattern('a{1,10}')
assert randre(r'a{0}') == ''
assert randre(r'a{1}') == 'a'
assert randre(r'a{5}') == 'aaaaa'
def test_randre_range():
validate_pattern('[a-z]')
validate_pattern('[^a-z]')
validate_pattern(r'[\x00-\xff]')
with pytest.raises(RuntimeError):
validate_pattern('r[^\x00-\xff]')
def test_randre_subpattern():
validate_pattern('(abcd)')
validate_pattern('a([bc])+')
def test_randre_branch():
validate_pattern('(aaa|bbb)')
validate_pattern('(a{0,5}|b{0,5})')
def test_randre_groupref():
assert randre(r'(abc) \1') == 'abc abc'
validate_pattern(r'([a-z]+)\1')
validate_pattern(r'(?P<hello>[a-z]+)(?P=hello)')
def test_randre_groupref_exists():
assert randre(r'(aaa)(?(1)yes|no)') == 'aaayes'
assert randre(r'(?(1)yes|no)') == 'no'
assert randre(r'(?(1)yes)') == ''
def test_randre_not_literal():
validate_pattern('[^a]')
validate_pattern('[^ ]')
def test_lookahead():
assert randre('a(?=xxx)b') == 'ab'
assert randre('a(?!xxx)b') == 'ab'
def test_at():
assert randre('^a') == 'a'
assert randre('a$') == 'a'
assert randre('^a$') == 'a'