-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
67 lines (57 loc) · 2.13 KB
/
testing.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
import re
import sys
import hypothesis.strategies as st
from hypothesis import given, settings, HealthCheck
from regex_parser import DIGITS, Token, TT_ALT, TT_CONCAT, TT_STAR, TT_PLUS, \
CharNode, UnaryOpNode, BinOpNode
from automata import regex_to_DFA
from generate import positive, negative
def gen_regex(max_leaves=10):
return st.recursive(
st.sampled_from(DIGITS).map(CharNode),
lambda G:
st.tuples(G, G).map(lambda t: BinOpNode(t[0], Token(TT_ALT), t[1]))
| st.tuples(G, G).map(lambda t: BinOpNode(t[0], Token(TT_CONCAT), t[1]))
| G.map(lambda r: UnaryOpNode(r, Token(TT_STAR)))
| G.map(lambda r: UnaryOpNode(r, Token(TT_PLUS))),
max_leaves=max_leaves
).map(str)
@settings(deadline=None)
@given(r=gen_regex())
def test_test(r):
print(r)
dfa = regex_to_DFA(r)
@settings(suppress_health_check=HealthCheck.all())
@given(s=positive(dfa, max_size=30))
def test_positive(s):
t = ''.join(s)
if print_it: print(' ', t)
# Check if t recognized by r.
assert dfa.accepts(t), \
'Generated string \'{}\' does not match regexp {}'.format(t, str(r))
assert re.match(r"^"+str(r)+"$", t), \
"Generated string \"{}\" does not match regexp {}".format(t, str(r))
@settings(suppress_health_check=HealthCheck.all())
@given(s=negative(dfa, max_size=30))
def test_negative(s):
t = ''.join(s)
if print_it: print(' ', t)
# Check if t not recognized by r.
#re.match does not work here (tries to match first instance)
assert not dfa.accepts(t), \
'Generated string \'{}\' does matches regexp {}'.format(t, str(r))
#assert not re.match(r"^("+str(r)+")$", t), \
# "Generated string \"{}\" does matches regexp {}".format(t, str(r))
test_positive()
test_negative()
@given(r=gen_regex())
def test_regex(r):
print(r)
if __name__ == '__main__':
n = len(sys.argv)
print_it = False
if n == 2 and sys.argv[1]=='print':
print_it = True
#test_regex()
test_test()
print('Passed all tests!')