forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests_level_10.py
158 lines (136 loc) · 4.49 KB
/
tests_level_10.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
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(code):
code = "import random\n" + code
with captured_output() as (out, err):
exec(code)
return out.getvalue().strip()
class TestsLevel10(unittest.TestCase):
def test_print(self):
result = hedy.transpile("print 'ik heet'", 10)
self.assertEqual("print('ik heet')", result)
def test_print_with_var(self):
result = hedy.transpile("naam is Hedy\nprint 'ik heet' naam", 10)
self.assertEqual("naam = 'Hedy'\nprint('ik heet'+str(naam))", result)
def test_print_with_calc_no_spaces(self):
result = hedy.transpile("print '5 keer 5 is ' 5*5", 10)
self.assertEqual("print('5 keer 5 is '+str(int(5) * int(5)))", result)
def test_print_calculation_times_directly(self):
result = hedy.transpile("""nummer is 5
nummertwee is 6
print nummer * nummertwee""", 10)
self.assertEqual("""nummer = '5'
nummertwee = '6'
print(str(int(nummer) * int(nummertwee)))""", result)
self.assertEqual(run_code(result), "30")
def test_transpile_ask(self):
result = hedy.transpile("antwoord is ask wat is je lievelingskleur?", 10)
self.assertEqual(result, "antwoord = input('wat is je lievelingskleur?')")
def test_if_with_indent(self):
result = hedy.transpile("""naam is Hedy
if naam is Hedy:
print 'koekoek'""", 10)
self.assertEqual("""naam = 'Hedy'
if str(naam) == str('Hedy'):
print('koekoek')""", result)
def test_if_else(self):
result = hedy.transpile("""antwoord is ask 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""", 10)
self.assertEqual("""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)
def test_print_random(self):
result = hedy.transpile("""keuzes is steen, schaar, papier
computerkeuze is keuzes at random
print 'computer koos ' computerkeuze""", 10)
self.assertEqual("""keuzes = ['steen', 'schaar', 'papier']
computerkeuze=random.choice(keuzes)
print('computer koos '+str(computerkeuze))""", result)
def test_for_loop(self):
result = hedy.transpile("""
a is 2
a is 3
for a in range 2 to 4:
a is a + 2
b is b + 2""", 10)
self.assertEqual(result, """a = '2'
a = '3'
for a in range(int(2), int(4)+1):
a = int(a) + int(2)
b = int(b) + int(2)""")
def test_if__else(self):
result = hedy.transpile("""
a is 5
if a is 1:
x is 2
else:
x is 222""", 10)
self.assertEqual(result, """a = '5'
if str(a) == str('1'):
x = '2'
else:
x = '222'""")
def test_forloop(self):
result = hedy.transpile("""
for i in range 1 to 10:
print i
print 'wie niet weg is is gezien'""", 10)
self.assertEqual(result, """for i in range(int(1), int(10)+1):
print(str(i))
print('wie niet weg is is gezien')""")
def test_for_nesting(self):
result = hedy.transpile("""for i in range 1 to 3:
for j in range 1 to 4:
print 'rondje: ' i ' tel: ' j""", 10)
self.assertEqual(result,"""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))""")
def test_if_nesting(self):
result = hedy.transpile("""kleur is blauw
kleurtwee is geel
if kleur is blauw:
if kleurtwee is geel:
print 'Samen is dit groen!'""", 10)
self.assertEqual(result, """kleur = 'blauw'
kleurtwee = 'geel'
if str(kleur) == str('blauw'):
if str(kleurtwee) == str('geel'):
print('Samen is dit groen!')""")
#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')