-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdd.py
60 lines (43 loc) · 1.57 KB
/
tdd.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
from hypothesis.testdecorators import given
from hypothesis.statefultesting import requires, step, StatefulTest
from hypothesis.verifier import Unfalsifiable
from amazeballs import cycle, fix_string, replace_if, replace_alternating
def for_all_not_in(xs, in_, whitelist):
wl = whitelist + ['']
return all([(x not in in_) or (x in wl) for x in xs])
def get_from_gen(gen, i):
for _ in range(i):
gen.next()
return gen.next()
@given(int, [int], [str])
def test_replace_if(x, y, z):
assert replace_if(x, y, z) == (z if x in y else x)
@given([int])
def test_cycle(x):
assert (not x) or \
(get_from_gen(cycle(x), 0) == get_from_gen(cycle(x), len(x)))
assert (not x) or \
(get_from_gen(cycle(x), 1) ==
get_from_gen(cycle(x), 2 + 2 * len(x)))
@given([str], [str], [str])
def test_replace_alternating(ss, ts, rs):
assert for_all_not_in(ts, replace_alternating(ss, ts, rs), rs)
@given(str, [str], [str])
def test_fix_string(s, ps, cs):
assert for_all_not_in(ps, fix_string(s, ps, cs), cs)
def test_stateful():
try:
example = ApplyMultiple().breaking_example()
print example
assert False
except Unfalsifiable, e:
assert True
class ApplyMultiple(StatefulTest):
def __init__(self):
self.target = "abc dere euege eueg A E O U I"
@step
@requires(str, str)
def fix_one(self, problem, correction):
self.target = fix_string(self.target, [problem], [correction])
assert problem not in self.target or problem == correction
self.target = " ".join(self.target)