forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_level_18.py
264 lines (231 loc) · 7.32 KB
/
test_level_18.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import hedy
import textwrap
from test_level_01 import HedyTester
from parameterized import parameterized
class TestsLevel18(HedyTester):
level = 18
def test_print_brackets(self):
code = textwrap.dedent("""\
print('Hallo!')""")
expected = textwrap.dedent("""\
print(f'Hallo!')""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle(),
expected_commands=['print']
)
def test_print_var_brackets(self):
code = textwrap.dedent("""\
naam is 'Hedy'
print('ik heet', naam)""")
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet{naam}')""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
@parameterized.expand(['=', 'is'])
def test_input(self, assigment):
code = textwrap.dedent(f"""\
leeftijd {assigment} input('Hoe oud ben jij?')
print(leeftijd)""")
expected = textwrap.dedent("""\
leeftijd = input(f'Hoe oud ben jij?')
try:
leeftijd = int(leeftijd)
except ValueError:
try:
leeftijd = float(leeftijd)
except ValueError:
pass
print(f'{leeftijd}')""")
self.multi_level_tester(
max_level=20,
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_if_with_equals_sign(self):
code = textwrap.dedent("""\
naam is 'Hedy'
if naam == Hedy:
print('koekoek')""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if str(naam) == str('Hedy'):
print(f'koekoek')""")
self.single_level_tester(code=code, expected=expected)
# issue also in level 17, leaving for now.
# def test_bigger(self):
# code = textwrap.dedent("""\
# leeftijd is input('Hoe oud ben jij?')
# if leeftijd > 12:
# print('Dan ben je ouder dan ik!')""")
# expected = textwrap.dedent("""\
# leeftijd = input('Hoe oud ben jij?')
# if int(leeftijd) > int('12'):
# print(f'Dan ben je ouder dan ik!')""")
#
# self.multi_level_tester(
# code=code,
# expected=expected,
# extra_check_function=self.is_not_turtle(),
# test_name=self.name()
# )
# def test_big_and_small(self):
# code = textwrap.dedent("""\
# leeftijd is input('Hoe oud ben jij?')
# if leeftijd < 12:
# print('Dan ben je jonger dan ik!')
# elif leeftijd > 12:
# print('Dan ben je ouder dan ik!')""")
# expected = textwrap.dedent("""\
# leeftijd = input('Hoe oud ben jij?')
# if int(leeftijd) < int('12'):
# print(f'Dan ben je jonger dan ik!')
# elif int(leeftijd) > int('12'):
# print(f'Dan ben je ouder dan ik!')""")
#
# self.multi_level_tester(
# max_level=20,
# code=code,
# expected=expected,
# extra_check_function=self.is_not_turtle(),
# test_name=self.name()
# )
def test_if_else(self):
code = textwrap.dedent("""\
antwoord is input('Hoeveel is 10 plus 10?')
if antwoord is 20:
print('Goedzo!')
print('Het antwoord was inderdaad', antwoord)
else:
print('Foutje')
print('Het antwoord moest zijn', antwoord)""")
expected = textwrap.dedent("""\
antwoord = input(f'Hoeveel is 10 plus 10?')
try:
antwoord = int(antwoord)
except ValueError:
try:
antwoord = float(antwoord)
except ValueError:
pass
if str(antwoord) == str('20'):
print(f'Goedzo!')
print(f'Het antwoord was inderdaad{antwoord}')
else:
print(f'Foutje')
print(f'Het antwoord moest zijn{antwoord}')""")
self.multi_level_tester(
code=code,
expected=expected,
expected_commands=['input', 'if', 'print', 'print', 'print', 'print'],
extra_check_function=self.is_not_turtle()
)
def test_for_loop(self):
code = textwrap.dedent("""\
a is 2
b is 3
for a in range(2,4):
a is a + 2
b is b + 2""")
expected = textwrap.dedent("""\
a = 2
b = 3
step = 1 if int(2) < int(4) else -1
for a in range(int(2), int(4) + step, step):
a = a + 2
b = b + 2
time.sleep(0.1)""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_for_nesting(self):
code = textwrap.dedent("""\
for i in range(1, 3):
for j in range(1,4):
print('rondje: ', i, ' tel: ', j)""")
expected = textwrap.dedent("""\
step = 1 if int(1) < int(3) else -1
for i in range(int(1), int(3) + step, step):
step = 1 if int(1) < int(4) else -1
for j in range(int(1), int(4) + step, step):
print(f'rondje: {i} tel: {j}')
time.sleep(0.1)""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_input_with_list(self):
code = textwrap.dedent("""
color is ['green', 'blue']
choice is input('Is your favorite color one of: ' color)""")
expected = textwrap.dedent("""\
color = ['green', 'blue']
choice = input(f'Is your favorite color one of: {color}')
try:
choice = int(choice)
except ValueError:
try:
choice = float(choice)
except ValueError:
pass""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_input_without_text_inside(self):
code = "x = input()"
expected = textwrap.dedent("""\
x = input(f'')
try:
x = int(x)
except ValueError:
try:
x = float(x)
except ValueError:
pass""")
self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_print_without_text_inside(self):
self.multi_level_tester(
code="print()",
expected="print(f'')",
extra_check_function=self.is_not_turtle()
)
# negative tests
def test_while_undefined_var(self):
code = textwrap.dedent("""\
while antwoord != 25:
print('hoera')""")
self.single_level_tester(
code=code,
exception=hedy.exceptions.UndefinedVarException
)
def test_var_undefined_error_message(self):
code = textwrap.dedent("""\
naam is 'Hedy'
print('ik heet ', name)""")
self.multi_level_tester(
code=code,
exception=hedy.exceptions.UndefinedVarException
)
# deze extra check functie kan nu niet mee omdat die altijd op result werkt
# evt toch splitsen in 2 (pos en neg?)
# self.assertEqual('name', context.exception.arguments['name'])
def test_input_without_argument(self):
self.multi_level_tester(
code="name is input",
exception=hedy.exceptions.IncompleteCommandException
)