-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_plot.py
74 lines (54 loc) · 1.63 KB
/
test_plot.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
from hypothesis import given
from hypothesis.strategies import (
characters,
integers,
one_of,
recursive,
sampled_from,
text,
tuples,
)
from plot import _Parser, _Tokenizer, plot_re
def ends_with_repeat(x):
return x.endswith(("+", "*", "}"))
def ends_with_at(x):
return x.endswith(("^", "$", "\\A", "\\b", "\\B", "\\Z"))
def if_not_repeat_or_at(producer):
return lambda x: x if ends_with_repeat(x) or ends_with_at(x) else producer(x)
def repeat(inner):
def concat(start_stop):
(start, stop) = sorted(start_stop)
return inner.map(if_not_repeat_or_at(lambda x: f"{x}{{{start},{stop}}}"))
n = integers(min_value=1, max_value=2 ** 31)
return tuples(n, n).flatmap(concat)
def kleene(inner):
return inner.map(if_not_repeat_or_at(lambda x: x + "*"))
def kleene1(inner):
return inner.map(if_not_repeat_or_at(lambda x: x + "+"))
atom = one_of(
text(".", min_size=1),
text(characters(whitelist_categories={"Ll", "Lu"}), min_size=1),
sampled_from(["^", "$", "\\A", "\\b", "\\B", "\\Z"]),
)
regex = recursive(
atom,
lambda inner: one_of(
inner.map(lambda x: f"({x})"),
inner.map(lambda x: f"(?:{x})"),
inner.map(lambda x: f"(?!{x})"),
inner.map(lambda x: f"(?={x})"),
inner.flatmap(lambda x: inner.map(lambda y: f"{x}|{y}")),
repeat(inner),
kleene(inner),
kleene1(inner),
inner,
),
)
@given(regex)
def test_parse(pattern):
parser = _Parser(_Tokenizer(pattern, 0))
assert parser.parse()
@given(regex)
def test_plot(pattern):
# Should not raise an exception
plot_re(pattern)