forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_level_12.py
342 lines (281 loc) · 9.65 KB
/
tests_level_12.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
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 TestsLevel12(unittest.TestCase):
maxDiff = None
level = 12
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_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_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_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)
#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')