This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_level_05.py
173 lines (123 loc) · 4.03 KB
/
tests_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
import unittest
import hedy
import sys
import io
from contextlib import contextmanager
import textwrap
@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 TestsLevel5(unittest.TestCase):
# print should still work
def test_print_with_var(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet' naam""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
naam = 'Hedy'
print('ik heet'+naam)""")
self.assertEqual(expected, result)
def test_print_with_comma(self):
code = textwrap.dedent("""\
naam is Hedy
print 'ik heet,' naam""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
naam = 'Hedy'
print('ik heet,'+naam)""")
self.assertEqual(expected, result)
def test_print_Spanish(self):
code = textwrap.dedent("""\
print 'Cuál es tu color favorito?'""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
print('Cuál es tu color favorito?')""")
self.assertEqual(expected, result)
def test_transpile_ask_Spanish(self):
code = textwrap.dedent("""\
color is ask Cuál es tu color favorito?""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
color = input('Cuál es tu color favorito?')""")
self.assertEqual(expected, result)
def test_transpile_other(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("abc felienne 123", 5)
self.assertEqual(str(context.exception), 'Invalid')
# todo: a few more things repeated from 4 here?
# now add repeat
def test_repeat_basic_print(self):
code = textwrap.dedent("""\
repeat 5 times print 'me wants a cookie!'""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
for i in range(int('5')):
print('me wants a cookie!')""")
self.assertEqual(expected, result)
expected_output = textwrap.dedent("""\
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!""")
self.assertEqual(expected_output, run_code(result))
def test_repeat_with_variable_print(self):
code = textwrap.dedent("""\
n is 5
repeat n times print 'me wants a cookie!'""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
n = '5'
for i in range(int(n)):
print('me wants a cookie!')""")
self.assertEqual(expected, result)
expected_output = textwrap.dedent("""\
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!""")
self.assertEqual(expected_output, run_code(result))
def test_repeat_nested_in_if(self):
code = textwrap.dedent("""\
kleur is ask Wat is je lievelingskleur?
if kleur is groen repeat 3 times print 'mooi!'""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
kleur = input('Wat is je lievelingskleur?')
if kleur == 'groen':
for i in range(int('3')):
print('mooi!')""")
self.assertEqual(expected, result)
def test_repeat_over_9_times(self):
code = textwrap.dedent("""\
repeat 10 times print 'me wants a cookie!'""")
result = hedy.transpile(code, 5)
expected = textwrap.dedent("""\
for i in range(int('10')):
print('me wants a cookie!')""")
self.assertEqual(expected, result)
expected_output = textwrap.dedent("""\
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!
me wants a cookie!""")
self.assertEqual(expected_output, run_code(result))