forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_level_05.py
1228 lines (1027 loc) · 36.8 KB
/
test_level_05.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hedy
import textwrap
from tests.Tester import HedyTester
from parameterized import parameterized
class TestsLevel5(HedyTester):
level = 5
'''
Tests should be ordered as follows:
* commands in the order of hedy.py e.g. for level 1: ['print', 'ask', 'echo', 'turn', 'forward']
* combined tests
* markup tests
* negative tests
Naming 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
'''
#
# if tests
#
def test_if_equality_linebreak_print(self):
# line breaks after if-condition are allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy
print 'leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_trailing_space_linebreak_print(self):
code = textwrap.dedent("""\
naam is James
if naam is trailing_space
print 'shaken'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'trailing_space':
print(f'shaken')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_unquoted_rhs_with_space_linebreak_print(self):
code = textwrap.dedent("""\
naam is James
if naam is James Bond
print 'shaken'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'James Bond':
print(f'shaken')""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_equality_unquoted_rhs_with_space_and_trailing_space_linebreak_print(self):
code = textwrap.dedent("""\
naam is James
if naam is trailing space
print 'shaken'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'trailing space':
print(f'shaken')""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_2_vars_equality_print(self):
code = textwrap.dedent("""\
jouwkeuze is schaar
computerkeuze is schaar
if computerkeuze is jouwkeuze print 'gelijkspel!'""")
expected = textwrap.dedent("""\
jouwkeuze = 'schaar'
computerkeuze = 'schaar'
if computerkeuze == jouwkeuze:
print(f'gelijkspel!')""")
self.single_level_tester(code=code, expected=expected, output='gelijkspel!')
def test_if_not_in_list_print(self):
code = textwrap.dedent("""\
items is red, green
selected is red
if selected not in items print 'not found!'
else print 'found'""")
expected = textwrap.dedent("""\
items = ['red', 'green']
selected = 'red'
if selected not in items:
print(f'not found!')
else:
print(f'found')""")
self.multi_level_tester(
max_level=7,
code=code,
expected=expected,
output='found'
)
def test_if_not_in_list_in_print(self):
code = textwrap.dedent("""\
items is red, green
selected is purple
if selected not in items print 'not found!'
else print 'found'""")
expected = textwrap.dedent("""\
items = ['red', 'green']
selected = 'purple'
if selected not in items:
print(f'not found!')
else:
print(f'found')""")
self.multi_level_tester(
max_level=7,
code=code,
expected=expected,
output='not found!'
)
def test_if_in_list_print(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!')""")
self.multi_level_tester(
max_level=7,
code=code,
expected=expected,
output='found!',
expected_commands=['is', 'is', 'if', 'in', 'print']
)
def test_if_in_undefined_list(self):
code = textwrap.dedent("""\
selected is red
if selected in items print 'found!'""")
self.multi_level_tester(code=code, exception=hedy.exceptions.UndefinedVarException, max_level=7)
def test_if_equality_unquoted_rhs_with_space_print_gives_error(self):
code = textwrap.dedent("""\
naam is James
if naam is James Bond print 'shaken'""")
self.multi_level_tester(code=code, exception=hedy.exceptions.UnquotedEqualityCheck, max_level=11)
def test_if_equality_unquoted_rhs_with_space_assign_gives_error(self):
code = textwrap.dedent("""\
naam is James
if naam is James Bond naam is 'Pietjansma'""")
self.multi_level_tester(code=code, exception=hedy.exceptions.UnquotedEqualityCheck, max_level=11)
@parameterized.expand(HedyTester.quotes)
def test_if_equality_quoted_rhs_with_space(self, q):
code = textwrap.dedent(f"""\
naam is James
if naam is {q}James Bond{q} print {q}shaken{q}""")
expected = textwrap.dedent(f"""\
naam = 'James'
if naam == 'James Bond':
print(f'shaken')""")
self.single_level_tester(
code=code,
expected_commands=['is', 'if', 'print'],
expected=expected)
@parameterized.expand(HedyTester.quotes)
def test_if_equality_quoted_rhs_with_spaces(self, q):
code = textwrap.dedent(f"""\
naam is James
if naam is {q}Bond James Bond{q} print 'shaken'""")
expected = textwrap.dedent(f"""\
naam = 'James'
if naam == 'Bond James Bond':
print(f'shaken')""")
self.single_level_tester(code=code, expected=expected)
def test_ask_if(self):
code = textwrap.dedent(f"""\
name is ask 'what is your name?'
if name is Hedy print 'nice' else print 'boo!'""")
expected = textwrap.dedent(f"""\
name = input(f'what is your name?')
if name == 'Hedy':
print(f'nice')
else:
print(f'boo!')""")
self.single_level_tester(
code=code,
expected_commands=['ask', 'if', 'else', 'print', 'print'],
expected=expected)
def test_if_equality_single_quoted_rhs_with_inner_double_quote(self):
code = textwrap.dedent(f"""\
answer is no
if answer is 'He said "no"' print 'no'""")
expected = textwrap.dedent(f"""\
answer = 'no'
if answer == 'He said "no"':
print(f'no')""")
self.single_level_tester(
code=code,
expected_commands=['is', 'if', 'print'],
expected=expected)
def test_if_equality_double_quoted_rhs_with_inner_single_quote(self):
code = textwrap.dedent(f"""\
answer is no
if answer is "He said 'no'" print 'no'""")
expected = textwrap.dedent(f"""\
answer = 'no'
if answer == 'He said \\'no\\'':
print(f'no')""")
self.single_level_tester(code=code, expected=expected)
def test_equality_promotes_int_to_string(self):
code = textwrap.dedent("""\
a is test
b is 15
if a is b c is 1""")
expected = textwrap.dedent("""\
a = 'test'
b = '15'
if a == b:
c = '1'""")
self.single_level_tester(code=code, expected=expected)
def test_equality_with_lists_gives_error(self):
code = textwrap.dedent("""\
n is 1, 2
m is 1, 2
if n is m print 'success!'""")
self.multi_level_tester(
max_level=7,
code=code,
exception=hedy.exceptions.InvalidArgumentTypeException
)
def test_if_in_list_with_string_var_gives_type_error(self):
code = textwrap.dedent("""\
items is red
if red in items print 'found!'""")
self.multi_level_tester(
max_level=7,
code=code,
exception=hedy.exceptions.InvalidArgumentTypeException
)
def test_if_in_list_with_input_gives_type_error(self):
code = textwrap.dedent("""\
items is ask 'What are the items?'
if red in items print 'found!'""")
self.multi_level_tester(
max_level=7,
code=code,
exception=hedy.exceptions.InvalidArgumentTypeException
)
#
# if else tests
#
def test_if_equality_print_else_print(self):
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy print 'leuk' else print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_ask_equality_print_else_print(self):
code = textwrap.dedent("""\
kleur is ask 'Wat is je lievelingskleur?'
if kleur is groen print 'mooi!' else print 'niet zo mooi'""")
expected = textwrap.dedent("""\
kleur = input(f'Wat is je lievelingskleur?')
if kleur == 'groen':
print(f'mooi!')
else:
print(f'niet zo mooi')""")
self.single_level_tester(code=code, expected=expected)
def test_if_else_followed_by_print(self):
code = textwrap.dedent("""\
kleur is geel
if kleur is groen antwoord is ok else antwoord is stom
print antwoord""")
expected = textwrap.dedent("""\
kleur = 'geel'
if kleur == 'groen':
antwoord = 'ok'
else:
antwoord = 'stom'
print(f'{antwoord}')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_trailing_space_linebreak_print_else(self):
# this code has a space at the end of line 2
code = textwrap.dedent("""\
naam is James
if naam is trailing space
print 'shaken' else print 'biertje!'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'trailing space':
print(f'shaken')
else:
print(f'biertje!')""")
self.multi_level_tester(max_level=7, code=code, expected=expected)
def test_if_equality_print_linebreak_else_print(self):
# line break before else is allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy print 'leuk'
else print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_linebreak_print_else_print(self):
# line break after if-condition is allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy
print 'leuk' else print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_unquoted_with_space_linebreak_print_else_print(self):
code = textwrap.dedent("""\
naam is James
if naam is James Bond
print 'shaken' else print 'biertje!'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'James Bond':
print(f'shaken')
else:
print(f'biertje!')""")
self.multi_level_tester(max_level=7, code=code, expected=expected)
def test_if_equality_print_else_linebreak_print(self):
# line break after else is allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy print 'leuk' else
print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_with_negative_number(self):
code = textwrap.dedent("""\
antwoord is -10
if antwoord is -10 print 'Nice' else print 'Oh no'""")
expected = textwrap.dedent("""\
antwoord = '-10'
if antwoord == '-10':
print(f'Nice')
else:
print(f'Oh no')""")
self.single_level_tester(code=code, expected=expected, output='Nice')
def test_if_equality_linebreak_print_linebreak_else_print(self):
# line breaks after if-condition and before else are allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy
print 'leuk'
else print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_linebreak_print_linebreak_else_linebreak_print(self):
# line breaks after if-condition, before else and after else are allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy
print 'leuk'
else
print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_linebreak_print_else_linebreak_print(self):
# line breaks after if-condition and after else are allowed
code = textwrap.dedent("""\
naam is Hedy
if naam is Hedy
print 'leuk' else
print 'minder leuk'""")
expected = textwrap.dedent("""\
naam = 'Hedy'
if naam == 'Hedy':
print(f'leuk')
else:
print(f'minder leuk')""")
self.single_level_tester(code=code, expected=expected)
def test_if_list_assignment_else_print(self):
code = textwrap.dedent("""\
people is mom, dad, Emma, Sophie
dishwasher is people at random
if dishwasher is Sophie
print 'too bad I have to do the dishes'
else
print 'luckily no dishes because' dishwasher 'is already washing up'""")
expected = textwrap.dedent("""\
people = ['mom', 'dad', 'Emma', 'Sophie']
dishwasher = random.choice(people)
if dishwasher == 'Sophie':
print(f'too bad I have to do the dishes')
else:
print(f'luckily no dishes because{dishwasher}is already washing up')""")
self.single_level_tester(code=code, expected=expected)
@parameterized.expand(HedyTester.quotes)
def test_if_equality_quoted_rhs_with_space_else(self, q):
code = textwrap.dedent(f"""\
naam is James
if naam is {q}James Bond{q} print {q}shaken{q} else print {q}biertje!{q}""")
expected = textwrap.dedent(f"""\
naam = 'James'
if naam == 'James Bond':
print(f'shaken')
else:
print(f'biertje!')""")
self.single_level_tester(code=code, expected=expected)
def test_if_equality_text_with_spaces_and_single_quotes_linebreak_print_else_print(self):
code = textwrap.dedent("""\
naam is James
if naam is James 'Bond'
print 'shaken' else print 'biertje!'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'James \\'Bond\\'':
print(f'shaken')
else:
print(f'biertje!')""")
self.multi_level_tester(max_level=7, code=code, expected=expected)
def test_if_equality_text_with_spaces_and_double_quotes_linebreak_print_else_print(self):
code = textwrap.dedent("""\
naam is James
if naam is James "Bond"
print 'shaken' else print 'biertje!'""")
expected = textwrap.dedent("""\
naam = 'James'
if naam == 'James "Bond"':
print(f'shaken')
else:
print(f'biertje!')""")
self.multi_level_tester(max_level=7, code=code, expected=expected)
def test_if_equality_print_else_print_bengali(self):
code = textwrap.dedent("""\
নাম is ask 'আপনার নাম কি?'
if নাম is হেডি print 'ভালো!' else print 'মন্দ'""")
expected = textwrap.dedent("""\
নাম = input(f'আপনার নাম কি?')
if নাম == 'হেডি':
print(f'ভালো!')
else:
print(f'মন্দ')""")
self.single_level_tester(code=code, expected=expected)
#
# combined tests
#
def test_consecutive_if_statements(self):
code = textwrap.dedent("""\
names is Hedy, Lamar
name is ask 'What is a name you like?'
if name is Hedy print 'nice!'
if name in names print 'nice!'""")
expected = textwrap.dedent("""\
names = ['Hedy', 'Lamar']
name = input(f'What is a name you like?')
if name == 'Hedy':
print(f'nice!')
else:
_ = 'x'
if name in names:
print(f'nice!')""")
self.single_level_tester(code=code, expected=expected, translate=False)
def test_onno_3372(self):
code = textwrap.dedent("""\
antw is ask 'wat kies jij'
if antw is schaar print 'gelijk spel!'
print 'test'""")
expected = textwrap.dedent("""\
antw = input(f'wat kies jij')
if antw == 'schaar':
print(f'gelijk spel!')
else:
_ = 'x'
print(f'test')""")
self.single_level_tester(code=code,
expected=expected,
translate=False)
def test_restaurant_example(self):
code = textwrap.dedent("""\
print 'Welkom bij McHedy'
eten is ask 'Wat wilt u eten?'
if eten is friet saus is ask 'Welke saus wilt u bij de friet?'
if eten is pizza topping is ask 'Welke topping wilt u op de pizza?'
print eten""")
expected = textwrap.dedent("""\
print(f'Welkom bij McHedy')
eten = input(f'Wat wilt u eten?')
if eten == 'friet':
saus = input(f'Welke saus wilt u bij de friet?')
else:
_ = 'x'
if eten == 'pizza':
topping = input(f'Welke topping wilt u op de pizza?')
else:
_ = 'x'
print(f'{eten}')""")
self.single_level_tester(code=code,
expected=expected,
translate=False)
def test_onno_3372_else(self):
code = textwrap.dedent("""\
antw is ask 'wat kies jij'
if antw is schaar print 'gelijk spel!' else print ''
print 'test'""")
expected = textwrap.dedent("""\
antw = input(f'wat kies jij')
if antw == 'schaar':
print(f'gelijk spel!')
else:
print(f'')
print(f'test')""")
self.single_level_tester(code=code,
expected=expected,
translate=False)
def test_consecutive_if_and_if_else_statements(self):
code = textwrap.dedent("""\
naam is ask 'hoe heet jij?'
if naam is Hedy print 'leuk'
if naam is Python print 'ook leuk'
else print 'minder leuk!'""")
expected = textwrap.dedent("""\
naam = input(f'hoe heet jij?')
if naam == 'Hedy':
print(f'leuk')
else:
_ = 'x'
if naam == 'Python':
print(f'ook leuk')
else:
print(f'minder leuk!')""")
self.single_level_tester(code=code,
expected=expected,
translate=False)
def test_consecutive_if_else_statements(self):
code = textwrap.dedent("""\
names is Hedy, Lamar
name is ask 'What is a name you like?'
if name is Hedy print 'nice!' else print 'meh'
if name in names print 'nice!' else print 'meh'""")
expected = textwrap.dedent("""\
names = ['Hedy', 'Lamar']
name = input(f'What is a name you like?')
if name == 'Hedy':
print(f'nice!')
else:
print(f'meh')
if name in names:
print(f'nice!')
else:
print(f'meh')""")
self.single_level_tester(code=code, expected=expected, translate=False)
def test_turn_if_forward(self):
code = textwrap.dedent("""\
angle is 90, 180, 270
direction is angle at random
turn direction
if direction is 180 forward 100""")
expected = HedyTester.dedent(
"""\
angle = ['90', '180', '270']
direction = random.choice(angle)""",
HedyTester.turn_transpiled('direction', self.level),
"if direction == '180':",
(HedyTester.forward_transpiled(100, self.level), ' '))
self.single_level_tester(code=code, expected=expected)
def test_turn_if_forward_else_forward(self):
code = textwrap.dedent("""\
angle is 90, 180, 270
direction is angle at random
if direction is 180 forward direction
else turn direction""")
expected = HedyTester.dedent(
"""\
angle = ['90', '180', '270']
direction = random.choice(angle)
if direction == '180':""",
(HedyTester.forward_transpiled('direction', self.level), ' '),
"else:",
(HedyTester.turn_transpiled('direction', self.level), ' '))
self.single_level_tester(code=code, expected=expected)
def test_list_access_index(self):
code = textwrap.dedent("""\
friends is Hedy, Lola, Frida
friend is friends at 2
print friend""")
expected = textwrap.dedent("""\
friends = ['Hedy', 'Lola', 'Frida']
friend = friends[2-1]
print(f'{friend}')""")
self.multi_level_tester(code=code, expected=expected, max_level=11)
#
# negative tests
#
def test_if_indent_gives_parse_error(self):
code = textwrap.dedent("""\
option is ask 'Rock Paper or Scissors?'
if option is Scissors
print 'Its a tie!'""")
self.multi_level_tester(
max_level=7,
code=code,
exception=hedy.exceptions.ParseException,
extra_check_function=lambda c: c.exception.error_location[0] == 3 and c.exception.error_location[1] == 1
)
def test_line_with_if_with_space_gives_invalid(self):
code = textwrap.dedent("""\
name is Hedy
if name is 3 print 'leuk' else print 'stom'""")
self.multi_level_tester(
code=code,
exception=hedy.exceptions.InvalidSpaceException,
max_level=7)
def test_pront_should_suggest_print(self):
code = "pront 'Hedy is leuk!'"
self.multi_level_tester(
code=code,
exception=hedy.exceptions.InvalidCommandException,
extra_check_function=lambda c: str(c.exception.arguments['guessed_command']) == 'print'
)
def test_print_no_quotes(self):
code = textwrap.dedent("""\
print 'Hoi ik ben Hedy de Waarzegger
print 'Ik kan voorspellen wie morgen de loterij wint!'
naam is ask 'Wie ben jij?'""")
self.single_level_tester(
code=code,
exception=hedy.exceptions.UnquotedTextException,
extra_check_function=lambda c: c.exception.error_location[0] == 1
)
def test_if_equality_print_backtick_text_gives_error(self):
code = "if 1 is 1 print `yay!` else print `nay`"
self.multi_level_tester(
max_level=6,
code=code,
exception=hedy.exceptions.UnquotedTextException
)
def test_if_fix_nl(self):
code = textwrap.dedent("""\
naam is 5
als naam is 5 print 'leuk'
print 'minder leuk!'""")
expected = HedyTester.dedent("""\
naam = '5'
if naam == '5':
print(f'leuk')
else:
_ = 'x'
print(f'minder leuk!')""")
self.multi_level_tester(
max_level=5,
code=code,
lang='nl',
expected=expected,
translate=False
)
#
# if pressed tests
#
def test_if_pressed_x_is_letter_key(self):
code = textwrap.dedent("""\
if x is pressed print 'it is a letter key'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'it is a letter key')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_double_if_pressed(self):
code = textwrap.dedent("""\
if x is pressed print 'first key'
if y is pressed print 'second key'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'first key')
break
if event.unicode == 'y':
print(f'second key')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_has_enter_after_pressed(self):
code = textwrap.dedent("""\
if x is pressed
print 'it is a letter key'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'it is a letter key')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_1_is_number_key(self):
code = textwrap.dedent("""\
if 1 is pressed print 'it is a number key'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == '1':
print(f'it is a number key')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_with_trailing_spaces_after_key(self):
code = textwrap.dedent("""\
if x is pressed print 'trailing spaces!'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'trailing spaces!')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
#
# if pressed else tests
#
def test_if_pressed_x_else(self):
code = textwrap.dedent("""\
if x is pressed print 'x is pressed!' else print 'x is not pressed!'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
print(f'x is not pressed!')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_has_enter_before_else(self):
code = textwrap.dedent("""\
if x is pressed print 'x is pressed!'
else print 'x is not pressed!'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
print(f'x is not pressed!')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_has_enter_before_both_prints_and_else(self):
code = textwrap.dedent("""\
if x is pressed
print 'x is pressed!'
else
print 'x is not pressed!'""")
expected = HedyTester.dedent("""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
print(f'x is not pressed!')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=7)
def test_if_pressed_has_enter_before_first_print_and_else(self):
code = textwrap.dedent("""\
if x is pressed