-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtest_parse_inputs.py
143 lines (111 loc) · 3.17 KB
/
test_parse_inputs.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from approvaltests import Options, verify_all
from approvaltests.inline.parse import Parse
def test_single_strings():
"""
Sam -> SAM
Llewellyn -> LLEWELLYN
"""
parse = Parse.doc_string()
verify_all(
"",
parse.get_inputs(),
lambda s: f"{s} -> {s.upper()}",
options=Options().inline(),
)
parse.verify_all(lambda s: s.upper())
def test_with_types_transformers_and_both():
"""
1 -> 0b1
9 -> 0b1001
"""
parse = Parse.doc_string()
verify_all(
"",
parse.get_inputs(),
lambda s: f"{s} -> {bin(int(s))}",
options=Options().inline(),
)
parse.transform(lambda a: int(a)).verify_all(lambda i: bin(i))
# parse.transform(int).transform(str).transform(int).verify_all(lambda i: bin(i), options= Options().with_reporter(ReporterThatAutomaticallyApproves())
def test_with_2_types_transformers_and_both():
"""
1, 5.0 -> 5.0
4, 0.5 -> 2.0
"""
parse = Parse.doc_string(auto_approve=True)
parse.transform2(int, float).verify_all(lambda i, f: i * f)
parse.transform2(str, str).transform2(int, float).verify_all(lambda i, f: i * f)
def test_example_step_1():
# begin-snippet: parse_input_step_2
"""
Kody -> 0
"""
# end-snippet
# begin-snippet: parse_input_step_1
def count_vowels(s: str) -> int:
return 0
def test_count_vowels():
"""
Kody
"""
parse = Parse.doc_string(auto_approve=True)
parse.verify_all(count_vowels)
# end-snippet
test_count_vowels()
def test_example_step_2():
"""
Kody -> 1
Teresa -> 3
Green -> 2
"""
# begin-snippet: parse_input_step_3
def count_vowels(s: str) -> int:
return sum(1 for c in s if c in "aeo")
def test_count_vowels():
"""
Kody -> 1
Teresa -> 3
Green -> 2
"""
parse = Parse.doc_string(auto_approve=True)
parse.verify_all(count_vowels)
# end-snippet
test_count_vowels()
# begin-snippet: parse_input_transformation
def test_with_transformation():
"""
1 -> 0b1
9 -> 0b1001
"""
parse = Parse.doc_string(auto_approve=True)
parse.transform(int).verify_all(bin)
# end-snippet
# begin-snippet: parse_input_two_parameters
def test_with_two_parameters():
"""
a, 3 -> aaa
!, 7 -> !!!!!!!
"""
parse = Parse.doc_string(auto_approve=True)
parse.transform2(str, int).verify_all(lambda s, i: s * i)
# end-snippet
# long-term intention: get the test to fail, randomSauce is incorrect
def test_with_3_parameters():
"""
a, 3, 1 -> aaaa
"""
def to_int(s: str, t: type = str):
assert type(s) is t
return int(s)
counter = 0
def to(tin: type, tout: type):
def wrapped(input):
nonlocal counter
counter += 1
assert type(input) is tin
return tout(input)
return wrapped
parse = Parse.doc_string(auto_approve=True)
parse.transform3( to(str, str), to(str, int), to(str, int)).verify_all(lambda a, b, c: to(str, str)(a) * (to(int, int)(b) + to(int, int)(c)))
assert counter == 6
# assert on all the paramaters