|
1
|
|
|
import os |
|
2
|
|
|
import unittest |
|
3
|
|
|
from tempfile import NamedTemporaryFile |
|
4
|
|
|
|
|
5
|
|
|
from main import entry_point, USAGE |
|
6
|
|
|
from test_utils import mock_stdout |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TopLevelWithoutArgsTest(unittest.TestCase): |
|
10
|
|
|
def test_no_args_return_value(self): |
|
11
|
|
|
return_value = entry_point([]) |
|
12
|
|
|
self.assertNotEqual(return_value, 0) |
|
13
|
|
|
|
|
14
|
|
|
def test_no_args_prints_usage(self): |
|
15
|
|
|
with mock_stdout() as stdout: |
|
16
|
|
|
entry_point([]) |
|
17
|
|
|
|
|
18
|
|
|
self.assertEqual(stdout.getvalue(), USAGE + '\n') |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class TopLevelSnippetTest(unittest.TestCase): |
|
22
|
|
|
def test_snippet(self): |
|
23
|
|
|
with mock_stdout() as stdout: |
|
24
|
|
|
entry_point(['trifle', '-i', '(+ 1 2)']) |
|
25
|
|
|
|
|
26
|
|
|
self.assertEqual(stdout.getvalue(), '3\n') |
|
27
|
|
|
|
|
28
|
|
|
def test_snippet_error(self): |
|
29
|
|
|
"""If given a snippet that throws an error, we should have a non-zero |
|
30
|
|
|
return code. |
|
31
|
|
|
|
|
32
|
|
|
""" |
|
33
|
|
|
return_value = entry_point(['trifle', '-i', '(+ 1 i-dont-exist)']) |
|
34
|
|
|
self.assertNotEqual(return_value, 0) |
|
35
|
|
|
|
|
36
|
|
|
def test_snippet_error_as_value(self): |
|
37
|
|
|
"""If given a snippet that returns an error, we should have a return |
|
38
|
|
|
code of zero. |
|
39
|
|
|
|
|
40
|
|
|
""" |
|
41
|
|
|
return_value = entry_point([ |
|
42
|
|
|
'trifle', '-i', |
|
43
|
|
|
'(try (/ 1 0) :catch error e e)' |
|
44
|
|
|
]) |
|
45
|
|
|
self.assertEqual(return_value, 0) |
|
46
|
|
|
|
|
47
|
|
|
def test_snippet_evaluate_empty_list_catch(self): |
|
48
|
|
|
# Regression test. |
|
49
|
|
|
return_value = entry_point([ |
|
50
|
|
|
'trifle', '-i', |
|
51
|
|
|
'(try () :catch error e 1)' |
|
52
|
|
|
]) |
|
53
|
|
|
self.assertEqual(return_value, 0) |
|
54
|
|
|
|
|
55
|
|
|
def test_snippet_lex_error(self): |
|
56
|
|
|
"""If given a snippet that throws an error during lexing, we should |
|
57
|
|
|
have a non-zero return code. |
|
58
|
|
|
|
|
59
|
|
|
""" |
|
60
|
|
|
return_value = entry_point(['trifle', '-i', '1/0']) |
|
61
|
|
|
self.assertNotEqual(return_value, 0) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class TopLevelFileTest(unittest.TestCase): |
|
65
|
|
|
def test_eval_file(self): |
|
66
|
|
|
with NamedTemporaryFile() as f: |
|
67
|
|
|
f.write('(set! f (open "foo.txt" :write)) (close! f)') |
|
68
|
|
|
f.flush() |
|
69
|
|
|
|
|
70
|
|
|
entry_point(['trifle', f.name]) |
|
71
|
|
|
|
|
72
|
|
|
self.assertTrue(os.path.exists("foo.txt")) |
|
73
|
|
|
|
|
74
|
|
|
os.remove("foo.txt") |
|
75
|
|
|
|
|
76
|
|
|
def test_eval_file_error(self): |
|
77
|
|
|
with NamedTemporaryFile() as f: |
|
78
|
|
|
f.write('(div 1 0)') |
|
79
|
|
|
f.flush() |
|
80
|
|
|
|
|
81
|
|
|
return_value = entry_point(['trifle', f.name]) |
|
82
|
|
|
|
|
83
|
|
|
self.assertNotEqual(return_value, 0) |
|
84
|
|
|
|
|
85
|
|
|
def test_eval_file_lex_error(self): |
|
86
|
|
|
with NamedTemporaryFile() as f: |
|
87
|
|
|
f.write('1/0') |
|
88
|
|
|
f.flush() |
|
89
|
|
|
|
|
90
|
|
|
return_value = entry_point(['trifle', f.name]) |
|
91
|
|
|
|
|
92
|
|
|
self.assertNotEqual(return_value, 0) |
|
93
|
|
|
|