forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_level_04.py
377 lines (287 loc) · 10.6 KB
/
tests_level_04.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import unittest
import hedy
import sys
import io
from contextlib import contextmanager
import textwrap
import inspect
@contextmanager
def captured_output():
new_out, new_err = io.StringIO(), io.StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
def run_code(parse_result):
code = "import random\n" + parse_result.code
with captured_output() as (out, err):
exec(code)
return out.getvalue().strip()
class TestsLevel4(unittest.TestCase):
level = 4
def test_name(self):
return inspect.stack()[1][3]
# invalid, ask and print should still work as in level 4
def test_transpile_other(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("abc felienne 123", self.level)
self.assertEqual(str(context.exception), 'Invalid')
def test_print_with_var(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet' naam""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet{naam}')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_with_comma(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet,' naam""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet,{naam}')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_transpile_turtle_basic(self):
result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
expected = textwrap.dedent("""\
t.forward(50)
t.right(90)
t.forward(100)""")
self.assertEqual(expected, result.code)
self.assertEqual(True, result.has_turtle)
def test_turtle_with_ask_has_turtle(self):
code = textwrap.dedent("""\
afstand is ask 'hoe ver dan?'
forward afstand""")
result = hedy.transpile_inner(code, self.level)
self.assertEqual(True, result.has_turtle)
def test_transpile_turtle_with_ask(self):
code = textwrap.dedent("""\
afstand is ask 'hoe ver dan?'
forward afstand""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
afstand = input('hoe ver dan?')
t.forward(afstand)""")
self.assertEqual(expected, result.code)
self.assertEqual(True, result.has_turtle)
def test_transpile_ask_with_print(self):
code = textwrap.dedent("""\
kleur is ask 'wat is je lievelingskleur?'
print 'jouw lievelingskleur is dus' kleur '!'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
kleur = input('wat is je lievelingskleur?')
print(f'jouw lievelingskleur is dus{kleur}!')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_transpile_ask_Spanish(self):
code = textwrap.dedent("""\
color is ask 'Cuál es tu color favorito?'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
color = input('Cuál es tu color favorito?')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_save_list_access_to_var(self):
code = textwrap.dedent("""\
dieren is Hond, Kat, Kangoeroe
dier is dieren at random
print dier""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
dieren = ['Hond', 'Kat', 'Kangoeroe']
dier=random.choice(dieren)
print(f'{dier}')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
self.assertIn(run_code(result), ['Hond', 'Kat', 'Kangoeroe'])
def test_print_Spanish(self):
code = textwrap.dedent("""\
print 'Cuál es tu color favorito?'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
print(f'Cuál es tu color favorito?')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
# now adds if
def test_print_if_else(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet' naam
if naam is Hedy print 'leuk' else print 'minder leuk'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet{naam}')
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_if_else_with_line_break(self):
# line breaks should be allowed in if-elses until level 7 when we start with indentation
max_level = 6
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet' naam
if naam is Hedy print 'leuk'
else print 'minder leuk'""")
for level in range(self.level, max_level + 1):
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet{naam}')
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_if_else_with_line_break_and_space(self):
# line breaks should be allowed in if-elses until level 7 when we start with indentation
max_level = 6
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet' naam
if naam is Hedy print 'leuk'
else print 'minder leuk'""")
for level in range(self.level, max_level + 1):
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet{naam}')
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_if_else_with_ask(self):
code = textwrap.dedent("""\
kleur is ask 'Wat is je lievelingskleur?'
if kleur is groen print 'mooi!' else print 'niet zo mooi'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
kleur = input('Wat is je lievelingskleur?')
if kleur == 'groen':
print(f'mooi!')
else:
print(f'niet zo mooi')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
# steen schaar papier
def test_print_if_else_with_and_var(self):
code = textwrap.dedent("""\
jouwkeuze is steen
computerkeuze is schaar
if computerkeuze is schaar and jouwkeuze is steen print 'jij wint'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
jouwkeuze = 'steen'
computerkeuze = 'schaar'
if computerkeuze == 'schaar' and jouwkeuze == 'steen':
print(f'jij wint')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
self.assertEqual(run_code(result), 'jij wint')
def test_turtle_with_if_has_no_turtle(self):
code = textwrap.dedent("""\
jouwkeuze is schaar
computerkeuze is schaar
if computerkeuze is jouwkeuze print 'gelijkspel!'""")
result = hedy.transpile_inner(code, self.level)
self.assertEqual(False, result.has_turtle)
def test_print_if_with_var(self):
code = textwrap.dedent("""\
jouwkeuze is schaar
computerkeuze is schaar
if computerkeuze is jouwkeuze print 'gelijkspel!'""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
jouwkeuze = 'schaar'
computerkeuze = 'schaar'
if computerkeuze == jouwkeuze:
print(f'gelijkspel!')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
self.assertEqual(run_code(result), 'gelijkspel!')
def test_if_in_array(self):
code = textwrap.dedent("""\
items is red, green
selected is red
if selected in items print 'found!'""")
expected = textwrap.dedent("""\
items = ['red', 'green']
selected = 'red'
if selected in items:
print(f'found!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
self.assertEqual('found!', run_code(result))
def test_pront_should_suggest_print(self):
code = "pront 'Hedy is leuk!'"
with self.assertRaises(Exception) as context:
result = hedy.transpile(code, self.level)
self.assertEqual('Invalid', str(context.exception))
self.assertEqual('print', str(context.exception.arguments['guessed_command']))
def test_parser_errors_should_be_caught_and_beautified(self):
code = textwrap.dedent("""\
option is ask 'Rock Paper or Scissors?'
print 'Player 2 ' option
if option is Scissors
print 'Its a tie!'""")
with self.assertRaises(Exception) as context:
result = hedy.transpile(code, self.level)
self.assertEqual('Parse', str(context.exception))
def test_single_quote_in_assign_should_not_break(self):
code = """message is 'Hello welcome to Hedy.'"""
expected = "message = '\\'Hello welcome to Hedy.\\''"
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_ifelse_should_go_before_assign(self):
maxlevel = 5
code = textwrap.dedent("""\
kleur is geel
if kleur is groen antwoord is ok else antwoord is stom
print antwoord""")
for level in range(self.level, maxlevel+1):
result = hedy.transpile(code, level)
expected = textwrap.dedent("""\
kleur = 'geel'
if kleur == 'groen':
antwoord = 'ok'
else:
antwoord = 'stom'
print(f'{antwoord}')""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
print(f'{self.test_name()} level {level}')
# while this looks STRANGE it is in essence a string issue like the one above
# at least we solve it with the same fix of escaping quotes
def test_bad_input_should_be_caught(self):
code = textwrap.dedent("""\
naam is ask 'hoe heet jij?'
ifnaam is Hedy print 'leuk' else print 'minder leuk!'""")
def test_transpile_other(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile(code, self.level)
self.assertEqual(str(context.exception), 'Invalid')
# def test_list_find_issue(self):
# #'list' object has no attribute 'find'
# # FH dd sept 2021 for later fixing!
# code = textwrap.dedent("""\
# নাম is ask আপনার নাম কি?
# if নাম is হেডি print 'ভালো!' else print 'মন্দ'\"""")
#
#