5 from mal_types
import MalInvalidArgumentException
6 from mal_types
import MalList
, MalInt
, MalFunctionCompiled
, MalBoolean
7 from mal_types
import MalSymbol
10 class TestStep4(unittest
.TestCase
):
11 def test_step4_nil(self
):
12 self
.assertEqual("nil", step4_if_fn_do
.rep("nil"))
14 def test_step4_boolean(self
):
15 self
.assertEqual("true", step4_if_fn_do
.rep("true"))
16 self
.assertEqual("false", step4_if_fn_do
.rep("false"))
18 def test_print_function(self
):
19 self
.assertEqual("#<function>", str(MalFunctionCompiled(lambda x
: MalInt(0))))
21 def test_if_basic_true(self
):
27 [MalSymbol("if"), MalBoolean(True), MalInt(4321), MalInt(1234)]
33 def test_if_basic_false(self
):
39 [MalSymbol("if"), MalBoolean(False), MalInt(4321), MalInt(1234)]
45 def test_if_basic_false_no_fourth_arg(self
):
51 MalList([MalSymbol("if"), MalBoolean(False), MalInt(4321)]), env
56 def test_env_constructor_binds(self
):
57 env
= Env(outer
=None, binds
=[MalSymbol("a")], exprs
=[MalInt(3)])
58 self
.assertEqual(3, env
.get("a").native())
60 def test_env_constructor_binds_multiple(self
):
63 binds
=[MalSymbol("a"), MalSymbol("b")],
64 exprs
=[MalInt(44), MalInt(32)],
66 self
.assertEqual(44, env
.get("a").native())
67 self
.assertEqual(32, env
.get("b").native())
69 def test_step4_do(self
):
70 self
.assertEqual("44", step4_if_fn_do
.rep("(do 1 2 3 44)"))
71 self
.assertEqual("21", step4_if_fn_do
.rep("(do 21)"))
73 def test_step4_fn(self
):
74 self
.assertEqual("#<function>", step4_if_fn_do
.rep("(fn* (a) 0)"))
76 def test_step4_use_fn(self
):
77 self
.assertEqual("7", step4_if_fn_do
.rep("((fn* (a) a) 7)"))
79 def test_step4_use_fn_multiple(self
):
80 self
.assertEqual("8", step4_if_fn_do
.rep("((fn* (a b) a) 8 9)"))
82 def test_step4_use_fn_multiple_nested(self
):
83 self
.assertEqual("10", step4_if_fn_do
.rep("((fn* (a b) (+ a (+ b 1))) 4 5)"))
85 def test_step4_use_fn_func_param(self
):
87 "8", step4_if_fn_do
.rep("((fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)")
90 def test_step4_prn(self
):
91 self
.assertEqual("nil", step4_if_fn_do
.rep("(prn 4)"))
93 def test_step4_list(self
):
94 self
.assertEqual("(1 2 (3 4) 5)", step4_if_fn_do
.rep("(list 1 2 (list 3 4) 5)"))
96 def test_step4_listP(self
):
97 self
.assertEqual("true", step4_if_fn_do
.rep("(list? (list 1 2))"))
98 self
.assertEqual("false", step4_if_fn_do
.rep("(list? 4)"))
100 def test_step4_empty(self
):
101 self
.assertEqual("true", step4_if_fn_do
.rep("(empty? (list))"))
103 def test_step4_count(self
):
104 self
.assertEqual("0", step4_if_fn_do
.rep("(count (list))"))
105 self
.assertEqual("2", step4_if_fn_do
.rep("(count (list 1 2))"))
106 self
.assertEqual("0", step4_if_fn_do
.rep("(count nil)"))
108 def test_step4_equal(self
):
109 self
.assertEqual("true", step4_if_fn_do
.rep("(= 0 0)"))
110 self
.assertEqual("true", step4_if_fn_do
.rep("(= (list 1) (list 1))"))
111 self
.assertEqual("false", step4_if_fn_do
.rep("(= (list 1) (list 1 2))"))
114 step4_if_fn_do
.rep("(= (list (list 1) (list 2)) (list (list 1) (list 2)))"),
116 self
.assertEqual("true", step4_if_fn_do
.rep("(= nil nil)"))
118 def test_step4_less(self
):
119 self
.assertEqual("true", step4_if_fn_do
.rep("(< 1 2)"))
120 self
.assertEqual("false", step4_if_fn_do
.rep("(< 2 1)"))
121 self
.assertEqual("false", step4_if_fn_do
.rep("(< 1 1)"))
123 step4_if_fn_do
.rep("(< 1 nil)")
124 self
.fail("Expected exception")
125 except MalInvalidArgumentException
:
128 step4_if_fn_do
.rep("(< nil 1)")
129 self
.fail("Expected exception")
130 except MalInvalidArgumentException
:
133 def test_step4_less_equal(self
):
134 self
.assertEqual("true", step4_if_fn_do
.rep("(<= 1 2)"))
135 self
.assertEqual("false", step4_if_fn_do
.rep("(<= 2 1)"))
136 self
.assertEqual("true", step4_if_fn_do
.rep("(<= 1 1)"))
138 step4_if_fn_do
.rep("(<= 1 nil)")
139 self
.fail("Expected exception")
140 except MalInvalidArgumentException
:
143 step4_if_fn_do
.rep("(<= nil 1)")
144 self
.fail("Expected exception")
145 except MalInvalidArgumentException
:
148 def test_step4_more(self
):
149 self
.assertEqual("false", step4_if_fn_do
.rep("(> 1 2)"))
150 self
.assertEqual("true", step4_if_fn_do
.rep("(> 2 1)"))
151 self
.assertEqual("false", step4_if_fn_do
.rep("(> 1 1)"))
153 step4_if_fn_do
.rep("(> 1 nil)")
154 self
.fail("Expected exception")
155 except MalInvalidArgumentException
:
158 step4_if_fn_do
.rep("(> nil 1)")
159 self
.fail("Expected exception")
160 except MalInvalidArgumentException
:
163 def test_step4_more_equal(self
):
164 self
.assertEqual("false", step4_if_fn_do
.rep("(>= 1 2)"))
165 self
.assertEqual("true", step4_if_fn_do
.rep("(>= 2 1)"))
166 self
.assertEqual("true", step4_if_fn_do
.rep("(>= 1 1)"))
168 step4_if_fn_do
.rep("(>= 1 nil)")
169 self
.fail("Expected exception")
170 except MalInvalidArgumentException
:
173 step4_if_fn_do
.rep("(>= nil 1)")
174 self
.fail("Expected exception")
175 except MalInvalidArgumentException
:
178 def test_step4_closures(self
):
180 "12", step4_if_fn_do
.rep("(( (fn* (a) (fn* (b) (+ a b))) 5) 7)")
184 step4_if_fn_do
.rep("(def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))"),
188 step4_if_fn_do
.rep("(def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))"),
190 self
.assertEqual("#<function>", step4_if_fn_do
.rep("(def! plus5 (gen-plus5))"))
191 self
.assertEqual("12", step4_if_fn_do
.rep("(plus5 7)"))
193 def test_step4_variadic_a(self
):
195 "3", step4_if_fn_do
.rep("( (fn* (& more) (count more)) 1 2 3)")
198 def test_step4_variadic_b(self
):
199 self
.assertEqual("0", step4_if_fn_do
.rep("((fn* (& more) (count more)))"))
201 def test_step4_quoted_string(self
):
202 self
.assertEqual('"\\""', step4_if_fn_do
.rep('"\\""'))
204 def test_step4_str(self
):
205 self
.assertEqual('"(1 a 2 3)"', step4_if_fn_do
.rep('(str (list 1 "a" 2 3))'))
207 def test_step4_equal_vector_list(self
):
208 self
.assertEqual("true", step4_if_fn_do
.rep("(=[] (list))"))
211 if __name__
== "__main__":