forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_level_04.py
477 lines (383 loc) · 13.1 KB
/
test_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
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
import hedy
import textwrap
from test_level_01 import HedyTester
class TestsLevel4(HedyTester):
level = 4
# tests should be ordered as follows:
# * commands in the order of hedy.py for level 3: ['print', 'ask', 'is', 'turn', 'forward'],
# * combined tests
# * markup tests
# * negative tests (inc. negative & multilevel)
# test name conventions are like this:
# * single keyword positive tests are just keyword or keyword_special_case
# * multi keyword positive tests are keyword1_keywords_2
# * negative tests should be
# * situation_gives_exception
# print tests
def test_print(self):
code = textwrap.dedent("""\
print 'hallo wereld!'""")
expected = textwrap.dedent("""\
print(f'hallo wereld!')""")
self.single_level_tester(code=code, expected=expected)
def test_print_comma(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet ,'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
print(f'ik heet ,')""")
self.multi_level_tester(
code=code,
max_level=10,
expected=expected
)
def test_print_two_spaces(self):
code = "print 'hallo!'"
expected = textwrap.dedent("""\
print(f'hallo!')""")
self.multi_level_tester(
code=code,
max_level=4,
expected=expected
)
def test_print_with_slashes(self):
code = "print 'Welcome to \\'"
expected = textwrap.dedent("""\
print(f'Welcome to \\\\')""")
output = "Welcome to \\"
self.single_level_tester(code=code, expected=expected, output=output)
# ask
def test_assign_print(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(
max_level=10,
code=code,
expected=expected
)
def test_ask_Spanish(self):
code = textwrap.dedent("""\
color is ask 'Cuál es tu color favorito?'""")
expected = textwrap.dedent("""\
color = input(f'Cuál es tu color favorito?')""")
self.multi_level_tester(
max_level=10,
code=code,
expected=expected
)
def test_ask_without_quotes(self):
code = textwrap.dedent("""
ding is kleur
kleur is ask Wat is je lievelingskleur'
print 'Jouw favoriet is dus ' kleur""")
self.single_level_tester(code, exception=hedy.exceptions.UnquotedTextException)
def test_ask_with_list_var(self):
code = textwrap.dedent("""\
colors is orange, blue, green
favorite is ask 'Is your fav color ' colors at random""")
expected = textwrap.dedent("""\
colors = ['orange', 'blue', 'green']
favorite = input(f'Is your fav color {random.choice(colors)}')""")
self.multi_level_tester(
max_level=10,
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_ask_with_list_gives_type_error(self):
code = textwrap.dedent("""\
colors is orange, blue, green
favorite is ask 'Is your fav color' colors""")
self.multi_level_tester(
max_level=11,
code=code,
exception=hedy.exceptions.InvalidArgumentTypeException
)
def test_ask_with_string_var(self):
code = textwrap.dedent("""\
color is orange
favorite is ask 'Is your fav color ' color""")
expected = textwrap.dedent("""\
color = 'orange'
favorite = input(f'Is your fav color {color}')""")
self.multi_level_tester(
max_level=10,
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
def test_ask_with_integer_var(self):
code = textwrap.dedent("""\
number is 10
favorite is ask 'Is your fav number' number""")
expected = textwrap.dedent("""\
number = '10'
favorite = input(f'Is your fav number{number}')""")
self.multi_level_tester(
max_level=10,
code=code,
expected=expected,
extra_check_function=self.is_not_turtle()
)
# is - assign tests
def test_assign_underscore(self):
code = textwrap.dedent("""\
voor_naam is Hedy
print 'ik heet '""")
expected = textwrap.dedent("""\
voor_naam = 'Hedy'
print(f'ik heet ')""")
self.single_level_tester(code=code, expected=expected)
def test_assign_bengali(self):
hashed_var = hedy.hash_var("নাম")
code = textwrap.dedent("""\
নাম is হেডি""")
expected = textwrap.dedent(f"""\
{hashed_var} = 'হেডি'""")
self.single_level_tester(code=code, expected=expected)
def test_assign_Python_keyword(self):
hashed_var = hedy.hash_var("for")
code = textwrap.dedent("""\
for is Hedy
print 'ik heet ' for """)
expected = textwrap.dedent("""\
vd55669822f1a8cf72ec1911e462a54eb = 'Hedy'
print(f'ik heet {vd55669822f1a8cf72ec1911e462a54eb}')""")
self.single_level_tester(code=code, expected=expected)
#add/remove tests
#add/remove tests
def test_add_text_to_list(self):
code = textwrap.dedent("""\
dieren is koe, kiep
add muis to dieren
print dieren at random""")
expected = textwrap.dedent("""\
dieren = ['koe', 'kiep']
dieren.append('muis')
print(f'{random.choice(dieren)}')""")
self.multi_level_tester(
max_level=11,
code=code,
expected=expected,
extra_check_function = self.result_in(['koe', 'kiep', 'muis']),
)
def test_remove_text_from_list(self):
code = textwrap.dedent("""\
dieren is koe, kiep
remove kiep from dieren
print dieren at random""")
expected = textwrap.dedent("""\
dieren = ['koe', 'kiep']
try:
dieren.remove('kiep')
except:
pass
print(f'{random.choice(dieren)}')""")
self.multi_level_tester(
max_level=11,
code=code,
expected=expected,
extra_check_function=self.result_in(['koe']),
)
def test_add_to_list(self):
code = textwrap.dedent("""\
color is ask 'what is your favorite color? '
colors is green, red, blue
add color to colors
print colors at random""")
expected = textwrap.dedent("""\
color = input(f'what is your favorite color? ')
colors = ['green', 'red', 'blue']
colors.append(color)
print(f'{random.choice(colors)}')""")
self.multi_level_tester(
max_level=11,
code=code,
expected=expected,
expected_commands=['ask', 'is', 'add', 'print', 'random']
)
def test_remove_from_list(self):
code = textwrap.dedent("""\
colors is green, red, blue
color is ask 'what color to remove?'
remove color from colors
print colors at random""")
expected = textwrap.dedent("""\
colors = ['green', 'red', 'blue']
color = input(f'what color to remove?')
try:
colors.remove(color)
except:
pass
print(f'{random.choice(colors)}')""")
self.multi_level_tester(
max_level=11,
code=code,
expected=expected
)
# negative tests
def test_print_without_quotes(self):
with self.assertRaises(hedy.exceptions.UnquotedTextException) as context:
result = hedy.transpile("print felienne 123", self.level)
self.assertEqual('Unquoted Text', context.exception.error_code) # hier moet nog we een andere foutmelding komen!
#combined tests
def test_assign_print_bengali(self):
hashed_var = hedy.hash_var("নাম")
self.assertEqual('veb9b5c786e8cde0910df4197f630ee75', hashed_var)
code = textwrap.dedent("""\
নাম is হেডি
print 'আমার নাম is ' নাম """)
expected = textwrap.dedent("""\
veb9b5c786e8cde0910df4197f630ee75 = 'হেডি'
print(f'আমার নাম is {veb9b5c786e8cde0910df4197f630ee75}')""")
self.single_level_tester(code=code, expected=expected)
def test_assign_print_chinese(self):
hashed_var = hedy.hash_var("你世界")
self.assertEqual('v406b71a2caed270b782fe8a1f2d5741a', hashed_var)
code = textwrap.dedent("""\
你世界 is 你好世界
print 你世界""")
expected = textwrap.dedent("""\
v406b71a2caed270b782fe8a1f2d5741a = '你好世界'
print(f'{v406b71a2caed270b782fe8a1f2d5741a}')""")
self.single_level_tester(code=code, expected=expected)
def test_print_list_var_random(self):
code = textwrap.dedent("""\
dieren is Hond, Kat, Kangoeroe
print 'hallo ' dieren at random""")
expected = textwrap.dedent("""\
dieren = ['Hond', 'Kat', 'Kangoeroe']
print(f'hallo {random.choice(dieren)}')""")
list = ['hallo Hond', 'hallo Kat', 'hallo Kangoeroe']
self.single_level_tester(code=code, expected=expected, extra_check_function=self.result_in(list))
def test_ask_print(self):
code = textwrap.dedent("""
kleur is ask 'wat is je lievelingskleur?'
print 'jouw lievelingskleur is dus' kleur '!'""")
expected = textwrap.dedent("""\
kleur = input(f'wat is je lievelingskleur?')
print(f'jouw lievelingskleur is dus{kleur}!')""")
self.single_level_tester(code=code, expected=expected)
def test_ask_assign(self):
code = textwrap.dedent("""
ding is kleur
antwoord is ask 'Wat is je lievelings ' ding
print 'Jouw favoriet is dus ' antwoord""")
expected = textwrap.dedent("""\
ding = 'kleur'
antwoord = input(f'Wat is je lievelings {ding}')
print(f'Jouw favoriet is dus {antwoord}')""")
self.single_level_tester(code=code, expected=expected)
def test_forward_ask(self):
code = textwrap.dedent("""\
afstand is ask 'hoe ver dan?'
forward afstand""")
expected = textwrap.dedent("""\
afstand = input(f'hoe ver dan?')
t.forward(afstand)
time.sleep(0.1)""")
self.multi_level_tester(
max_level=self.max_turtle_level,
code=code,
expected=expected,
extra_check_function=self.is_turtle()
)
#negative tests
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,
max_level=10
)
# 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_issue_375(self):
code = textwrap.dedent("""\
is Foobar
print welcome""")
with self.assertRaises(hedy.exceptions.ParseException) as context:
result = hedy.transpile(code, self.level)
#todo these could be tested in an extra_function too?
self.assertEqual('Parse', context.exception.error_code)
self.assertEqual(1, context.exception.error_location[0])
self.assertEqual(1, context.exception.error_location[1])
def test_missing_opening_quote(self):
code = textwrap.dedent("""\
print hallo wereld'""")
self.single_level_tester(code, exception=hedy.exceptions.UnquotedTextException)
def test_missing_all_quotes(self):
code = textwrap.dedent("""\
print hallo wereld""")
self.multi_level_tester(
code=code,
max_level=17,
exception=hedy.exceptions.UndefinedVarException,
)
def test_no_keyword_detected(self):
code = textwrap.dedent("""\
'competitie die gaan we winnen'""")
self.multi_level_tester(
code=code,
exception=hedy.exceptions.MissingCommandException,
max_level=11 # todo we should update the grammar of level 12 so this also works
)
def test_print_Spanish(self):
code = textwrap.dedent("""\
print 'Cuál es tu color favorito?'""")
expected = textwrap.dedent("""\
print(f'Cuál es tu color favorito?')""")
self.multi_level_tester(
code=code,
max_level=11,
expected=expected
)
def test_repair(self):
code = "print ,'Hello'"
with self.assertRaises(hedy.exceptions.ParseException) as context:
result = hedy.transpile(code, self.level)
self.assertEqual("print 'Hello'", context.exception.fixed_code)
#assorti
def test_detect_accented_chars(self):
self.assertEqual(True, hedy.hash_needed('éyyy'))
self.assertEqual(True, hedy.hash_needed('héyyy'))
self.assertEqual(False, hedy.hash_needed('heyyy'))
def test_meta_column_missing_closing_quote(self):
code = textwrap.dedent("""\
print 'Hello'
print 'World""")
instance = hedy.IsValid()
instance.level = self.level
program_root = hedy.parse_input(code, self.level, 'en')
is_valid = instance.transform(program_root)
_, invalid_info = is_valid
invalid_info = invalid_info[0]
line = invalid_info.line
column = invalid_info.column
# Boryana Jan 22
# Proabably we want to change the column so that it points to
# the place where the quote is actually missing?
self.assertEqual(2, line)
self.assertEqual(7, column)
def test_meta_column_missing_opening_quote(self):
code = textwrap.dedent("""\
print 'Hello'
print World'""")
instance = hedy.IsValid()
instance.level = self.level
program_root = hedy.parse_input(code, self.level, 'en')
is_valid = instance.transform(program_root)
_, invalid_info = is_valid
invalid_info = invalid_info[0]
line = invalid_info.line
column = invalid_info.column
self.assertEqual(2, line)
self.assertEqual(7, column)