forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_level_17.py
580 lines (503 loc) · 17.8 KB
/
tests_level_17.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import unittest
import hedy
import sys
import io
import textwrap
from contextlib import contextmanager
@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 TestsLevel17(unittest.TestCase):
maxDiff = None
level = 17
def test_print(self):
result = hedy.transpile("print('ik heet')", self.level)
expected = "print('ik heet')"
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_with_var(self):
result = hedy.transpile("naam is Hedy\nprint('ik heet' naam)", self.level)
expected = "naam = 'Hedy'\nprint('ik heet'+str(naam))"
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_with_calc_no_spaces(self):
result = hedy.transpile("print('5 keer 5 is ' 5*5)", self.level)
expected = "print('5 keer 5 is '+str(int(5) * int(5)))"
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_transpile_ask(self):
result = hedy.transpile("antwoord is input('wat is je lievelingskleur?')", self.level)
expected = "antwoord = input('wat is je lievelingskleur?')"
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_calculation_times_directly(self):
code = textwrap.dedent("""\
nummer is 5
nummertwee is 6
print(nummer * nummertwee)""")
result = hedy.transpile(code, self.level)
expected = textwrap.dedent("""\
nummer = '5'
nummertwee = '6'
print(str(int(nummer) * int(nummertwee)))""")
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
self.assertEqual("30", run_code(result))
def test_if_with_indent(self):
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy:
print('koekoek')""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if str(naam) == str('Hedy'):
print('koekoek')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
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('Hoeveel is 10 plus 10?')
if str(antwoord) == str('20'):
print('Goedzo!')
print('Het antwoord was inderdaad '+str(antwoord))
else:
print('Foutje')
print('Het antwoord moest zijn '+str(antwoord))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_print_random(self):
code = textwrap.dedent("""\
keuzes is ['steen', 'schaar', 'papier']
computerkeuze is keuzes[random]
print('computer koos ' computerkeuze)""")
expected = textwrap.dedent("""\
keuzes = ['steen', 'schaar', 'papier']
computerkeuze=random.choice(keuzes)
print('computer koos '+str(computerkeuze))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_for_loop(self):
code = textwrap.dedent("""\
a is 2
a is 3
for a in range(2,4):
a is a + 2
b is b + 2""")
expected = textwrap.dedent("""\
a = '2'
a = '3'
for a in range(int(2), int(4)+1):
a = int(a) + int(2)
b = int(b) + int(2)""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_if__else(self):
code = textwrap.dedent("""\
a is 5
if a is 1:
x is 2
else:
x is 222""")
expected = textwrap.dedent("""\
a = '5'
if str(a) == str('1'):
x = '2'
else:
x = '222'""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_forloop(self):
code = textwrap.dedent("""\
for i in range(1, 10):
print(i)
print('wie niet weg is is gezien')""")
expected = textwrap.dedent("""\
for i in range(int(1), int(10)+1):
print(str(i))
print('wie niet weg is is gezien')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_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("""\
for i in range(int(1), int(3)+1):
for j in range(int(1), int(4)+1):
print('rondje: '+str(i)+' tel: '+str(j))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_if_nesting(self):
code = textwrap.dedent("""\
kleur is blauw
kleurtwee is geel
if kleur is blauw:
if kleurtwee is geel:
print('Samen is dit groen!')""")
expected = textwrap.dedent("""\
kleur = 'blauw'
kleurtwee = 'geel'
if str(kleur) == str('blauw'):
if str(kleurtwee) == str('geel'):
print('Samen is dit groen!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_newprint(self):
code = textwrap.dedent("""\
leeftijd is input('Hoe oud ben jij?')
print('Dus jij hebt zo veel verjaardagen gehad:')
for i in range(0,leeftijd):
print(i)""")
expected = textwrap.dedent("""\
leeftijd = input('Hoe oud ben jij?')
print('Dus jij hebt zo veel verjaardagen gehad:')
for i in range(int(0), int(leeftijd)+1):
print(str(i))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_list(self):
code = textwrap.dedent("""\
fruit is ['appel', 'banaan', 'kers']
print(fruit)""")
expected = textwrap.dedent("""\
fruit = ['appel', 'banaan', 'kers']
print(str(fruit))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_random(self):
code = textwrap.dedent("""\
fruit is ['banaan', 'appel', 'kers']
randomfruit is fruit[random]
print(randomfruit)""")
expected = textwrap.dedent("""\
fruit = ['banaan', 'appel', 'kers']
randomfruit=random.choice(fruit)
print(str(randomfruit))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_specific_access(self):
code = textwrap.dedent("""\
fruit is ['banaan', 'appel', 'kers']
eerstefruit is fruit[1]
print(eerstefruit)""")
expected = textwrap.dedent("""\
fruit = ['banaan', 'appel', 'kers']
eerstefruit=fruit[1-1]
print(str(eerstefruit))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
# note that print(str(highscore)) will not print as it will compare 'score[i]' as str to a variable
def test_everything_combined(self):
code = textwrap.dedent("""\
score is ['100', '300', '500']
highscore is score[random]
print('De highscore is: ' highscore)
for i in range(1,3):
scorenu is score[i]
print('Score is nu ' scorenu)
if highscore is score[i]:
print(highscore)""")
expected = textwrap.dedent("""\
score = ['100', '300', '500']
highscore=random.choice(score)
print('De highscore is: '+str(highscore))
for i in range(int(1), int(3)+1):
scorenu=score[i-1]
print('Score is nu '+str(scorenu))
if str(highscore) == str('score[i]'):
print(str(highscore))""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_if_under_else_in_for(self):
code = textwrap.dedent("""\
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
else:
print('Dat is goed!')
if antwoord is 25:
i is 10""")
expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
antwoord = input('Wat is 5*5')
if str(antwoord) == str('24'):
print('Dat is fout!')
else:
print('Dat is goed!')
if str(antwoord) == str('25'):
i = '10'""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_bool_true(self):
code = textwrap.dedent("""\
ja is True
if ja is True:
print('ja')""")
expected = textwrap.dedent("""\
ja = True
if ja == True:
print('ja')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_bool_false(self):
code = textwrap.dedent("""\
ja is False
if ja is False:
print('ja')""")
expected = textwrap.dedent("""\
ja = False
if ja == False:
print('ja')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_bool_true2(self):
code = textwrap.dedent("""\
ja is true
if ja is True:
print('ja')""")
expected = textwrap.dedent("""\
ja = True
if ja == True:
print('ja')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_bool_false2(self):
code = textwrap.dedent("""\
ja is false
if ja is False:
print('ja')""")
expected = textwrap.dedent("""\
ja = False
if ja == False:
print('ja')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_bool_total(self):
code = textwrap.dedent("""\
jebenternog is False
benjeernog is input('ben je er nog? ja of nee?')
if benjeernog is ja:
jebenternog is True
if jebenternog is True:
print('Hallo!')
if jebenternog is False:
print('Doei!')""")
expected = textwrap.dedent("""\
jebenternog = False
benjeernog = input('ben je er nog? ja of nee?')
if str(benjeernog) == str('ja'):
jebenternog = True
if jebenternog == True:
print('Hallo!')
if jebenternog == False:
print('Doei!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_and(self):
code = textwrap.dedent("""\
if 5 is 5 and 4 is 4:
print('hallo')""")
expected = textwrap.dedent("""\
if str('5') == str('5') and str('4') == str('4'):
print('hallo')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_or(self):
code = textwrap.dedent("""\
if 5 is 5 or 4 is 4:
print('hallo')""")
expected = textwrap.dedent("""\
if str('5') == str('5') or str('4') == str('4'):
print('hallo')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_comment(self):
code = textwrap.dedent("""\
if 5 is 5 or 4 is 4:
print('hallo')
#comment""")
expected = textwrap.dedent("""\
if str('5') == str('5') or str('4') == str('4'):
print('hallo')
# ['comment']""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_commentbegin(self):
code = textwrap.dedent("""\
# comment word
if 5 is 5 or 4 is 4:
print('hallo')
""")
expected = textwrap.dedent("""\
# [' comment word']
if str('5') == str('5') or str('4') == str('4'):
print('hallo')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_commentresult(self):
code = textwrap.dedent("""\
# comment word
if 5 is 5 or 4 is 4:
print('hallo')
""")
expected = textwrap.dedent("""\
# [' comment word']
if str('5') == str('5') or str('4') == str('4'):
print('hallo')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_smaller(self):
code = textwrap.dedent("""\
leeftijd is input('Hoe oud ben jij?')
if leeftijd < 12:
print('Dan ben je jonger dan ik!')""")
expected = textwrap.dedent("""\
leeftijd = input('Hoe oud ben jij?')
if str(leeftijd) < str('12'):
print('Dan ben je jonger dan ik!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
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 str(leeftijd) > str('12'):
print('Dan ben je ouder dan ik!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
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 str(leeftijd) < str('12'):
print('Dan ben je jonger dan ik!')
elif str(leeftijd) > str('12'):
print('Dan ben je ouder dan ik!')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_whileloop(self):
code = textwrap.dedent("""\
goedantwoord is False
while goedantwoord is False:
antwoord is input('Wat is 5 keer 5?')
if antwoord is 25:
goedantwoord is True""")
expected = textwrap.dedent("""\
goedantwoord = False
while goedantwoord == False:
antwoord = input('Wat is 5 keer 5?')
if str(antwoord) == str('25'):
goedantwoord = True""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_whileloop2(self):
code = textwrap.dedent("""\
tel is 1
# we gaan door totdat tel 3 is!
while tel < 3:
print('Dit is de ' tel 'e keer')
tel is tel + 1
print('We zijn klaar')""")
expected = textwrap.dedent("""\
tel = '1'
# [' we gaan door totdat tel 3 is!']
while str(tel) < str('3'):
print('Dit is de '+str(tel)+'e keer')
tel = int(tel) + int(1)
print('We zijn klaar')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
def test_whileloop3(self):
code = textwrap.dedent("""\
goedantwoord is False
# we gaan door totdat een goed antwoord is gegeven!
while goedantwoord is False:
antwoord is input('Wat is 5 keer 5?')
if antwoord is 25:
goedantwoord is True
print('Er is een goed antwoord gegeven')""")
expected = textwrap.dedent("""\
goedantwoord = False
# [' we gaan door totdat een goed antwoord is gegeven!']
while goedantwoord == False:
antwoord = input('Wat is 5 keer 5?')
if str(antwoord) == str('25'):
goedantwoord = True
print('Er is een goed antwoord gegeven')""")
result = hedy.transpile(code, self.level)
self.assertEqual(expected, result.code)
self.assertEqual(False, result.has_turtle)
# programs with issues to see if we catch them properly
# (so this should fail, for now)
# at one point we want a real "Indent" error and a better error message
# for this!
# def test_level_7_no_indentation(self):
# #test that we get a parse error here
# code = textwrap.dedent("""\
# antwoord is ask Hoeveel is 10 keer tien?
# if antwoord is 100
# print 'goed zo'
# else
# print 'bah slecht'""")
#
# with self.assertRaises(Exception) as context:
# result = hedy.transpile(code, 10)
# self.assertEqual(str(context.exception), 'Parse')