-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
356 lines (304 loc) · 12.3 KB
/
test.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
#! /usr/bin/python3
import unittest
import subprocess
def run_leema(f, cli_args=None):
args = ["target/debug/leema", "T/"+f+".lma"]
env = {"LEEMA_PATH": "lib"}
if cli_args is not None:
args += cli_args
print(args)
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
output = proc.stdout.read()
err = proc.stderr.read()
result = proc.wait(3)
output = output + proc.stdout.read()
err = err + proc.stderr.read()
proc.stdout.close()
proc.stderr.close()
return {'code': result, 'output': output, 'stderr': err}
def test_booland():
result = run_leema('booland')
assert 0 == result['code']
lines = result['output'].split(b"\n")
assert b"a is True" == lines[0]
assert b"b is False" == lines[1]
assert b"a and b is False" == lines[2]
class TestScripts(unittest.TestCase):
def test_block(self):
result = run_leema('block')
self.assertEqual(0, result['code'])
self.assertEqual(b"z is: 36\n", result['output'])
def test_print(self):
result = run_leema('print_quotes')
self.assertEqual(0, result['code'])
self.assertEqual(b"hello \"quotes\"\n", result['output'])
def test_pmatch_tuples(self):
result = run_leema('pmatch_tuples')
self.assertEqual(0, result['code'])
self.assertEqual(b"both\nfirst\nsecond\nneither\n", result['output'])
def test_fact_match(self):
result = run_leema('fact_match')
self.assertEqual(0, result['code'])
self.assertEqual(b"factorial(4) = 24\n", result['output'])
def test_fact_fmatch(self):
result = run_leema('fact_fmatch')
self.assertEqual(0, result['code'])
self.assertEqual(b"factorial(4) = 24\n", result['output'])
def test_factorial(self):
result = run_leema('factorial')
self.assertEqual(0, result['code'])
self.assertEqual(b"factorial(4) = 24\n", result['output'])
def test_fizzbuzz(self):
result = run_leema('fizzbuzz')
self.assertEqual(0, result['code'])
lines = result['output'].strip().splitlines()
self.assertEqual(b"1", lines[0])
self.assertEqual(b"2", lines[1])
self.assertEqual(b"fizz", lines[2])
self.assertEqual(b"4", lines[3])
self.assertEqual(b"buzz", lines[4])
self.assertEqual(b"fizz", lines[5])
self.assertEqual(b"7", lines[6])
self.assertEqual(b"14", lines[13])
self.assertEqual(b"fizzbuzz", lines[14])
self.assertEqual(b"16", lines[15])
self.assertEqual(b"buzz", lines[-6])
self.assertEqual(b"fizz", lines[-5])
self.assertEqual(b"97", lines[-4])
self.assertEqual(b"98", lines[-3])
self.assertEqual(b"fizz", lines[-2])
self.assertEqual(b"buzz", lines[-1])
def test_func_3params(self):
result = run_leema('func_3params')
self.assertEqual(0, result['code'])
self.assertEqual(b"sum(3, 8, 2) = 13\n", result['output'])
def test_func_inc(self):
"""Test that we can add a const to a function parameter"""
result = run_leema('func_inc')
self.assertEqual(0, result['code'])
self.assertEqual(b"inc(-4) = -3\n", result['output'])
def test_footag_match(self):
result = run_leema('footag_match')
self.assertEqual(0, result['code'])
self.assertEqual(b"h is #foo\nmatched #foo\n", result['output'])
def test_footag_nomatch(self):
result = run_leema('footag_nomatch')
self.assertEqual(0, result['code'])
self.assertEqual(b"h is #foot\nsome other thing? #foot\n",
result['output'])
def test_generic_func(self):
result = run_leema('test_generic')
self.assertEqual(0, result['code'])
self.assertEqual(b"x: (#burrito,taco,)\n" \
+ b"y: (True,3,)\n" \
+ b"z: (3,#burrito,)\n"
, result['output'])
def test_if_else_true(self):
result = run_leema('if_else_true')
self.assertEqual(0, result['code'])
self.assertEqual(b"hello\n", result['output'])
def test_if_else_false(self):
result = run_leema('if_else_false')
self.assertEqual(0, result['code'])
self.assertEqual(b"burritos\n", result['output'])
def test_if_nested(self):
result = run_leema('if_nested')
self.assertEqual(0, result['code'])
self.assertEqual(b"x = 1, y = 2\n", result['output'])
def test_str(self):
result = run_leema('test_str')
self.assertEqual(0, result['code'])
self.assertEqual(b"a: aBcDeFg\n" \
+ b"upper: ABCDEFG\n" \
+ b"lower: abcdefg\n"
, result['output'])
def test_typevar(self):
result = run_leema('typevar')
self.assertEqual(0, result['code'])
self.assertEqual(b"first: 4, second: b\n", result['output'])
def test_let_type(self):
result = run_leema('let_type')
self.assertEqual(0, result['code'])
self.assertEqual(b"5 + 3 = 8\n", result['output'])
def test_list_cons(self):
result = run_leema('list_cons')
self.assertEqual(0, result['code'])
self.assertEqual(
b"l1: [3,2,8,]\n" +
b"l2: [4,6,3,2,8,]\n",
result['output'])
def test_list_map(self):
result = run_leema('test_list_map')
self.assertEqual(0, result['code'])
self.assertEqual(b"output: [6,8,10,]\n", result['output'])
def test_list_match_all(self):
result = run_leema('list_match_all')
self.assertEqual(0, result['code'])
self.assertEqual(
b"l: [3,2,8,]\n" +
b"l is a list with 3 elements [3, 2, 8]\n",
result['output'])
def test_list_match_head(self):
result = run_leema('list_match_head')
self.assertEqual(0, result['code'])
self.assertEqual(
b"l: [3,2,8,4,]\n" +
b"l is a list starting with 3, 2 and ending with [8,4,]\n",
result['output'])
def test_fmatch_list_tail(self):
result = run_leema('fmatch_list_tail')
self.assertEqual(0, result['code'])
self.assertEqual(
b"l: [3,2,8,]\n" +
b"l is a list with head 3 and tail [2,8,]\n",
result['output'])
def test_fmatch_list_empty(self):
result = run_leema('fmatch_list_empty')
self.assertEqual(0, result['code'])
self.assertEqual(b"is_empty? False\n", result['output'])
def test_fmatch_scope_depth(self):
result = run_leema('fmatch_scope_depth')
self.assertEqual(0, result['code'])
self.assertEqual(
b"found a! what else is in [#b,#c,] ?\n" +
b"found b! what else is in [#c,] ?\n" +
b"found c.\n" +
b"done\n",
result['output'])
def test_failure(self):
self.skipTest("not reimplemented yet")
result = run_leema('failure')
self.assertEqual(249, result['code'])
self.assertEqual(
b"Failure(#xis4 'tacos are delicious')\n",
result['stderr'])
def test_failed_handled(self):
self.skipTest("not reimplemented yet")
result = run_leema('failed_handled')
self.assertEqual(0, result['code'])
self.assertEqual(
b"c failed. no propagate.\n" +
b"e: str interp: whoa - not linear!\n",
result['output'])
def test_failed_propagated(self):
self.skipTest("not reimplemented yet")
result = run_leema('failed_propagated')
self.assertEqual(249, result['code'])
self.assertEqual(
b"c failed. log and propagate\n",
result['output'])
self.assertEqual(
b"Failure(#xis4 'tacos are delicious')\n",
result['stderr'])
def test_anon_func(self):
result = run_leema('test_anon_func')
self.assertEqual(0, result['code'])
exp = b"triple i = [3,6,9,12,]\n"
self.assertEqual(exp, result['output'])
def test_closures(self):
result = run_leema('test_closures')
self.assertEqual(0, result['code'])
exp = b"multiplied i = [4,8,12,16,]\n"
self.assertEqual(exp, result['output'])
def test_destruct(self):
result = run_leema('destruct')
self.assertEqual(0, result['code'])
self.assertEqual(
b"date is: /destruct/Date(year:2010,month:9,day:8,)\n" +
b"year: 2010 / month: 9 / day: 8\n",
result['output'])
def test_chess960(self):
result = run_leema('chess960')
self.assertEqual(0, result['code'])
output = result['output'].strip().decode("utf-8")
self.assertEqual(8, len(output))
self.assertEqual(1, output.count("K"))
self.assertEqual(1, output.count("Q"))
self.assertEqual(2, output.count("B"))
self.assertEqual(2, output.count("N"))
self.assertEqual(2, output.count("R"))
pr1 = output.index("R")
pr2 = output.rindex("R")
pk = output.index("K")
self.assertTrue(pr1 < pk)
self.assertTrue(pk < pr2)
def test_map(self):
result = run_leema('test_map')
self.assertEqual(0, result['code'])
expected = b"map contains tacos? true\n" \
+ b"map length is 1\n" \
+ b"map[tacos] = </core/Option T:/core/Int>.Some(4,)\n"
self.assertEqual(expected, result['output'])
def test_rgb(self):
result = run_leema('rgb')
self.assertEqual(0, result['code'])
expected = b"color: /rgb/Rgb(red:10,green:20,blue:30,)\n" \
+ b"red: 10\n" \
+ b"blue: 30\n" \
+ b"hex green is: #00ff00\n" \
+ b"reddish is: /rgb/Rgb(red:120,green:20,blue:10,)\n"
self.assertEqual(expected, result['output'])
def test_const(self):
result = run_leema('test_const')
self.assertEqual(0, result['code'])
lines = result['output'].splitlines()
self.assertEqual(b"red is: #ff0000", lines[0])
def test_empty_struct(self):
result = run_leema('empty_struct')
self.assertEqual(0, result['code'])
expected = b"empty: /empty_struct/Empty\n"
self.assertEqual(expected, result['output'])
def test_tuple_named_fields(self):
result = run_leema('tuple_named_fields')
self.assertEqual(0, result['code'])
expected = b"""area is: 12\n"""
self.assertEqual(expected, result['output'])
def test_named_tuple(self):
result = run_leema('named_tuple')
self.assertEqual(0, result['code'])
expected = b"""greeting is: "/named_tuple/Greeting(hello,world,)"\n"""
self.assertEqual(expected, result['output'])
def test_color_enum(self):
result = run_leema('color_enum')
self.assertEqual(0, result['code'])
exp = b"red: Red\n" \
+ b"blue: Blue\n" \
+ b"yellow: Yellow\n"
self.assertEqual(exp, result['output'])
def test_option(self):
result = run_leema('test_option')
self.assertEqual(0, result['code'])
exp = b"option a? None\n" \
+ b"no option\n" \
+ b"option b? </core/Option T:/core/Int>.Some(4,)\n" \
+ b"option is 4\n"
self.assertEqual(exp, result['output'])
def test_json(self):
self.skipTest("not ready for testing yet")
result = run_leema('test_json')
self.assertEqual(0, result['code'])
exp = b'6\nfalse\n"hello"\n"#world"\n' \
+ b'["a","b"]\n' \
+ b'{"x":4}\n' \
+ b'{"id":4,"name":"Javier"}\n' \
+ b'coded \' 9 or true\n' \
+ b'test_json::User(id:8,name:Gerald,)\n'
self.assertEqual(exp, result['output'])
def test_cli(self):
result = run_leema('hi_to')
self.assertEqual(0, result['code'])
exp = b'hi world\n'
self.assertEqual(exp, result['output'])
result = run_leema('hi_to', ['you'])
self.assertEqual(0, result['code'])
exp = b'hi you\n'
self.assertEqual(exp, result['output'])
result = run_leema('hi_to', ['tacos and burritos'])
self.assertEqual(0, result['code'])
exp = b'hi tacos and burritos\n'
self.assertEqual(exp, result['output'])
def test_read_file(self):
result = run_leema('read_file')
self.assertEqual(0, result['code'])
expected = b"hello leema friend\n\n"
self.assertEqual(expected, result['output'])