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
/
tests_level_01.py
161 lines (123 loc) · 5.63 KB
/
tests_level_01.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
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 TestsLevel1(unittest.TestCase):
def test_transpile_other(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("abc felienne 123", 1)
self.assertEqual('Invalid', str(context.exception))
def test_transpile_incomplete(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("print", 1)
self.assertEqual('Incomplete', str(context.exception))
def test_transpile_incomplete_with_multiple_lines(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("print hallo allemaal\nprint", 1)
self.assertEqual('Incomplete', str(context.exception))
# def test_transpile_other_2(self):
# with self.assertRaises(Exception) as context:
# result = hedy.transpile("abc felienne 123", 1)
# self.assertEqual(str(context.exception), 'Invalid')
# self.assertEqual(str(context.exception.arguments),
# "{'invalid_command': 'abc', 'level': 1, 'guessed_command': 'ask'}")
def test_transpile_incomplete_not_a_keyword(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("groen", 1)
self.assertEqual('Invalid', str(context.exception))
def test_transpile_print(self):
result = hedy.transpile("print Hallo welkom bij Hedy!", 1)
self.assertEqual("print('Hallo welkom bij Hedy!')", result)
self.assertEqual('Hallo welkom bij Hedy!', run_code(result))
def test_transpile_ask_Spanish(self):
result = hedy.transpile("ask ask Cuál es tu color favorito?", 1)
self.assertEqual("answer = input('ask Cuál es tu color favorito?')", result)
def test_lines_may_end_in_spaces(self):
result = hedy.transpile("print Hallo welkom bij Hedy! ", 1)
self.assertEqual("print('Hallo welkom bij Hedy! ')", result)
self.assertEqual('Hallo welkom bij Hedy!', run_code(result))
def test_lines_may_not_start_with_spaces(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile(" print Hallo welkom bij Hedy! ", 1)
self.assertEqual('Invalid Space', str(context.exception))
def test_print_with_comma(self):
result = hedy.transpile("print iedereen zegt tegen hem: NERD, omdat hij de slimste van de klas is.", 1)
self.assertEqual("print('iedereen zegt tegen hem: NERD, omdat hij de slimste van de klas is.')", result)
def test_word_plus_period(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("word.", 1)
self.assertEqual('Invalid', str(context.exception))
def test_two_lines_start_with_spaces(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile(" print Hallo welkom bij Hedy!\n print Hallo welkom bij Hedy!", 1)
self.assertEqual('Invalid Space', str(context.exception))
def test_transpile_empty(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("", 1)
def test_transpile_ask(self):
result = hedy.transpile("ask wat is je lievelingskleur?", 1)
self.assertEqual("answer = input('wat is je lievelingskleur?')", result)
def test_transpile_print_multiple_lines(self):
result = hedy.transpile("print Hallo welkom bij Hedy\nprint Mooi hoor", 1)
self.assertEqual("print('Hallo welkom bij Hedy')\nprint('Mooi hoor')", result)
def test_transpile_three_lines(self):
input = textwrap.dedent("""\
print Hallo
ask Wat is je lievelingskleur
echo je lievelingskleur is""")
expected = textwrap.dedent("""\
print('Hallo')
answer = input('Wat is je lievelingskleur')
print('je lievelingskleur is'+answer)""")
result = hedy.transpile(input, 1)
self.assertEqual(expected, result)
def test_transpile_echo(self):
result = hedy.transpile("echo Jouw lievelingskleur is dus...", 1)
self.assertEqual("print('Jouw lievelingskleur is dus...'+answer)", result)
def test_transpile_echo_without_argument(self):
result = hedy.transpile("echo", 1)
self.assertEqual("print(answer)", result)
def test_use_quotes_in_print_allowed(self):
code = "print 'Welcome to OceanView!'"
result = hedy.transpile(code, 1)
expected = textwrap.dedent("""\
print('\\'Welcome to OceanView!\\'')""")
self.assertEqual(expected, result)
expected_output = run_code(result)
self.assertEqual("'Welcome to OceanView!'", expected_output)
def test_use_slashes_in_print_allowed(self):
code = "print 'Welcome to \O/ceanView!'"
result = hedy.transpile(code, 1)
expected = textwrap.dedent("""\
print('\\'Welcome to \O/ceanView!\\'')""")
self.assertEqual(expected, result)
expected_output = run_code(result)
self.assertEqual("'Welcome to \O/ceanView!'", expected_output)
def test_use_quotes_in_ask_allowed(self):
code = "ask 'Welcome to OceanView?'"
result = hedy.transpile(code, 1)
expected = textwrap.dedent("""\
answer = input('\\'Welcome to OceanView?\\'')""")
self.assertEqual(expected, result)
def test_use_quotes_in_echo_allowed(self):
code = "echo oma's aan de"
result = hedy.transpile(code, 1)
expected = textwrap.dedent("""\
print('oma\\'s aan de'+answer)""")
self.assertEqual(expected, result)