git.s-ol.nu alive / cedda1d
use testing module for lib/logic tests s-ol 3 months ago
3 changed file(s) with 102 addition(s) and 245 deletion(s). Raw diff Collapse all Expand all
2323 tick: =>
2424 { :check, :msg } = @unwrap_all!
2525
26 if not check
27 error Error 'assertion', msg or "assertion failed"
26 assert check, Error 'assertion', msg or "assertion failed"
2827
2928 eval: (scope, tail) =>
3029 L\trace "evaling #{@}"
00 import TestPilot from require 'spec.test_setup'
1 import T, Array, Constant from require 'alv'
21
32 describe "logic", ->
43 test = TestPilot '', '(import* testing logic)\n'
5 TRUE = T.bool\mk_const true
6 FALSE = T.bool\mk_const false
74
85 describe "==", ->
9 it "can compare any type", ->
10 with COPILOT\eval_once '(== 1 1)'
11 assert.is.true \is_const!
12 assert.is.equal TRUE, .result
13
14 with COPILOT\eval_once '(== 1 2)'
15 assert.is.true \is_const!
16 assert.is.equal FALSE, .result
17
18 with COPILOT\eval_once '(== 1 "hello")'
19 assert.is.true \is_const!
20 assert.is.equal FALSE, .result
21
22 with COPILOT\eval_once '(== "hello" "hello")'
23 assert.is.true \is_const!
24 assert.is.equal TRUE, .result
25
26 with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 3))'
27 assert.is.true \is_const!
28 assert.is.equal TRUE, .result
29
30 with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 1))'
31 assert.is.true \is_const!
32 assert.is.equal FALSE, .result
33
34 with COPILOT\eval_once '(== (array 1 2 3) (array 1 2))'
35 assert.is.true \is_const!
36 assert.is.equal FALSE, .result
37
38 with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 3 4))'
39 assert.is.true \is_const!
40 assert.is.equal FALSE, .result
41
42 with COPILOT\eval_once '(==
6 it "can compare any type", -> COPILOT\eval_once '
7 (expect= true (== 1 1))
8 (expect= false (== 1 2))
9 (expect= false (== 1 "hello"))
10 (expect= true (== "hello" "hello"))
11 (expect= true (== (array 1 2 3) (array 1 2 3)))
12 (expect= false (== (array 1 2 3) (array 1 2 1)))
13 (expect= false (== (array 1 2 3) (array 1 2)))
14 (expect= false (== (array 1 2 3) (array 1 2 3 4)))
15 (expect= true (==
4316 (struct "a" 1 "b" true "c" (array "test"))
44 (struct "a" 1 "b" true "c" (array "test")))'
45 assert.is.true \is_const!
46 assert.is.equal TRUE, .result
47
48 with COPILOT\eval_once '(==
17 (struct "a" 1 "b" true "c" (array "test"))))
18 (expect= false (==
4919 (struct "a" 1 "b" false "c" (array "test"))
50 (struct "a" 1 "b" true "c" (array "test")))'
51 assert.is.true \is_const!
52 assert.is.equal FALSE, .result
53
54 with COPILOT\eval_once '(==
20 (struct "a" 1 "b" true "c" (array "test"))))
21 (expect= false (==
5522 (struct "a" 1 "b" true "c" (array "test" "toast"))
56 (struct "a" 1 "b" true "c" (array "test")))'
57 assert.is.true \is_const!
58 assert.is.equal FALSE, .result
59
60 with COPILOT\eval_once '(==
61 (struct "a" 1 "b" true)
62 (struct "a" 1 "b" true "c" (array "test")))'
63 assert.is.true \is_const!
64 assert.is.equal FALSE, .result
65
66 with COPILOT\eval_once '(==
67 (struct "a" 1 "b" true)
68 (struct "a" 1))'
69 assert.is.true \is_const!
70 assert.is.equal FALSE, .result
71
72 with COPILOT\eval_once '(== print print)'
73 assert.is.true \is_const!
74 assert.is.equal TRUE, .result
75
76 with COPILOT\eval_once '(== print ==)'
77 assert.is.true \is_const!
78 assert.is.equal FALSE, .result
79
80 it "is aliased as eq", ->
81 with COPILOT\eval_once '(== eq ==)'
82 assert.is.true \is_const!
83 assert.is.equal TRUE, .result
23 (struct "a" 1 "b" true "c" (array "test"))))
24 (expect= false (==
25 (struct "a" 1 "b" true)
26 (struct "a" 1 "b" true "c" (array "test"))))
27 (expect= false (==
28 (struct "a" 1 "b" true)
29 (struct "a" 1)))
30 (expect= true (== print print))
31 (expect= false (== print ==))
32 '
33
34 it "is aliased as eq", -> COPILOT\eval_once '
35 (expect= true (== eq ==))
36 '
8437
8538 describe "!=", ->
86 it "can compare any type", ->
87 with COPILOT\eval_once '(!= 1 1)'
88 assert.is.true \is_const!
89 assert.is.equal FALSE, .result
90
91 with COPILOT\eval_once '(!= 1 2)'
92 assert.is.true \is_const!
93 assert.is.equal TRUE, .result
94
95 with COPILOT\eval_once '(!= 1 "hello")'
96 assert.is.true \is_const!
97 assert.is.equal TRUE, .result
98
99 with COPILOT\eval_once '(!= "hello" "hello")'
100 assert.is.true \is_const!
101 assert.is.equal FALSE, .result
102
103 with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 3))'
104 assert.is.true \is_const!
105 assert.is.equal FALSE, .result
106
107 with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 1))'
108 assert.is.true \is_const!
109 assert.is.equal TRUE, .result
110
111 with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2))'
112 assert.is.true \is_const!
113 assert.is.equal TRUE, .result
114
115 with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 3 4))'
116 assert.is.true \is_const!
117 assert.is.equal TRUE, .result
118
119 with COPILOT\eval_once '(!=
39 it "can compare any type", -> COPILOT\eval_once '
40 (expect= false (!= 1 1))
41 (expect= true (!= 1 2))
42 (expect= true (!= 1 "hello"))
43 (expect= false (!= "hello" "hello"))
44 (expect= false (!= (array 1 2 3) (array 1 2 3)))
45 (expect= true (!= (array 1 2 3) (array 1 2 1)))
46 (expect= true (!= (array 1 2 3) (array 1 2)))
47 (expect= true (!= (array 1 2 3) (array 1 2 3 4)))
48 (expect= false (!=
12049 (struct "a" 1 "b" true "c" (array "test"))
121 (struct "a" 1 "b" true "c" (array "test")))'
122 assert.is.true \is_const!
123 assert.is.equal FALSE, .result
124
125 with COPILOT\eval_once '(!=
50 (struct "a" 1 "b" true "c" (array "test"))))
51 (expect= true (!=
12652 (struct "a" 1 "b" false "c" (array "test"))
127 (struct "a" 1 "b" true "c" (array "test")))'
128 assert.is.true \is_const!
129 assert.is.equal TRUE, .result
130
131 with COPILOT\eval_once '(!=
53 (struct "a" 1 "b" true "c" (array "test"))))
54 (expect= true (!=
13255 (struct "a" 1 "b" true "c" (array "test" "toast"))
133 (struct "a" 1 "b" true "c" (array "test")))'
134 assert.is.true \is_const!
135 assert.is.equal TRUE, .result
136
137 with COPILOT\eval_once '(!=
138 (struct "a" 1 "b" true)
139 (struct "a" 1 "b" true "c" (array "test")))'
140 assert.is.true \is_const!
141 assert.is.equal TRUE, .result
142
143 with COPILOT\eval_once '(!=
144 (struct "a" 1 "b" true)
145 (struct "a" 1))'
146 assert.is.true \is_const!
147 assert.is.equal TRUE, .result
148
149 with COPILOT\eval_once '(!= print print)'
150 assert.is.true \is_const!
151 assert.is.equal FALSE, .result
152
153 with COPILOT\eval_once '(!= print ==)'
154 assert.is.true \is_const!
155 assert.is.equal TRUE, .result
156
157 it "is aliased as note-eq", ->
158 with COPILOT\eval_once '(== not-eq !=)'
159 assert.is.true \is_const!
160 assert.is.equal TRUE, .result
56 (struct "a" 1 "b" true "c" (array "test"))))
57 (expect= true (!=
58 (struct "a" 1 "b" true)
59 (struct "a" 1 "b" true "c" (array "test"))))
60 (expect= true (!=
61 (struct "a" 1 "b" true)
62 (struct "a" 1)))
63 (expect= false (!= print print))
64 (expect= true (!= print ==))
65 '
66
67 it "is aliased as not-eq", -> COPILOT\eval_once '
68 (expect= true (== not-eq !=))
69 '
16170
16271 describe "bool", ->
163 it "coerces numbers", ->
164 with COPILOT\eval_once '(bool 0)'
165 assert.is.true \is_const!
166 assert.is.equal FALSE, .result
167
168 with COPILOT\eval_once '(bool 1)'
169 assert.is.true \is_const!
170 assert.is.equal TRUE, .result
171
172 with COPILOT\eval_once '(bool -1)'
173 assert.is.true \is_const!
174 assert.is.equal TRUE, .result
175
176 with COPILOT\eval_once '(bool 1024)'
177 assert.is.true \is_const!
178 assert.is.equal TRUE, .result
179
180 it "accepts booleans", ->
181 with COPILOT\eval_once '(bool false)'
182 assert.is.true \is_const!
183 assert.is.equal FALSE, .result
184
185 with COPILOT\eval_once '(bool true)'
186 assert.is.true \is_const!
187 assert.is.equal TRUE, .result
72 it "coerces numbers", -> COPILOT\eval_once '
73 (expect= false (bool 0))
74 (expect= true (bool 1))
75 (expect= true (bool -1))
76 (expect= true (bool 1024))
77 '
78
79 it "accepts booleans", -> COPILOT\eval_once '
80 (expect= false (bool false))
81 (expect= true (bool true))
82 '
18883
18984 describe "not", ->
190 it "accepts booleans", ->
191 with COPILOT\eval_once '(not false)'
192 assert.is.true \is_const!
193 assert.is.equal TRUE, .result
194
195 with COPILOT\eval_once '(not true)'
196 assert.is.true \is_const!
197 assert.is.equal FALSE, .result
198
199 it "coerces numbers", ->
200 with COPILOT\eval_once '(not 0)'
201 assert.is.true \is_const!
202 assert.is.equal TRUE, .result
203
204 with COPILOT\eval_once '(not 1)'
205 assert.is.true \is_const!
206 assert.is.equal FALSE, .result
207
208 with COPILOT\eval_once '(not -1)'
209 assert.is.true \is_const!
210 assert.is.equal FALSE, .result
211
212 with COPILOT\eval_once '(not 1024)'
213 assert.is.true \is_const!
214 assert.is.equal FALSE, .result
215
85 it "accepts booleans", -> COPILOT\eval_once '
86 (expect= true (not false))
87 (expect= false (not true))
88 '
89
90 it "coerces numbers", -> COPILOT\eval_once '
91 (expect= true (not 0))
92 (expect= false (not 1))
93 (expect= false (not -1))
94 (expect= false (not 1024))
95 '
21696
21797 describe "or", ->
218 it "accepts any number of mixed arguments", ->
219 with COPILOT\eval_once '(or false 0)'
220 assert.is.true \is_const!
221 assert.is.equal FALSE, .result
222
223 with COPILOT\eval_once '(or 1 0)'
224 assert.is.true \is_const!
225 assert.is.equal TRUE, .result
226
227 with COPILOT\eval_once '(or 0 false 0 0 0 0)'
228 assert.is.true \is_const!
229 assert.is.equal FALSE, .result
230
231 with COPILOT\eval_once '(or 0 0 0 true 0 0)'
232 assert.is.true \is_const!
233 assert.is.equal TRUE, .result
234
235 with COPILOT\eval_once '(or 0 true 0 1 0 0)'
236 assert.is.true \is_const!
237 assert.is.equal TRUE, .result
98 it "accepts any number of mixed arguments", -> COPILOT\eval_once '
99 (expect= false (or false 0))
100 (expect= true (or 1 0))
101 (expect= false (or 0 false 0 0 0 0))
102 (expect= true (or 0 0 0 true 0 0))
103 (expect= true (or 0 true 0 1 0 0))
104 '
238105
239106 describe "and", ->
240 it "accepts any number of mixed arguments", ->
241 with COPILOT\eval_once '(and false 1)'
242 assert.is.true \is_const!
243 assert.is.equal FALSE, .result
244
245 with COPILOT\eval_once '(and 1 true)'
246 assert.is.true \is_const!
247 assert.is.equal TRUE, .result
248
249 with COPILOT\eval_once '(and false 0)'
250 assert.is.true \is_const!
251 assert.is.equal FALSE, .result
252
253 with COPILOT\eval_once '(and 1 1 true 0)'
254 assert.is.true \is_const!
255 assert.is.equal FALSE, .result
256
257 with COPILOT\eval_once '(and 1 1 true true 1)'
258 assert.is.true \is_const!
259 assert.is.equal TRUE, .result
260
261 with COPILOT\eval_once '(and 1 1 1)'
262 assert.is.true \is_const!
263 assert.is.equal TRUE, .result
107 it "accepts any number of mixed arguments", -> COPILOT\eval_once '
108 (expect= false (and false 1))
109 (expect= true (and 1 true))
110 (expect= false (and false 0))
111 (expect= false (and 1 1 true 0))
112 (expect= true (and 1 1 true true 1))
113 (expect= true (and 1 1 1))
114 '
264115
265116 describe "<", ->
266117 it "is aliased as asc?", -> COPILOT\eval_once '
33 import Logger from require 'alv.logger'
44 import Error from require 'alv.error'
55 import RTNode from require 'alv.rtnode'
6 busted = require "busted"
67
78 Logger\init 'error'
89 os.time = do
3334
3435 eval_once: (code) =>
3536 @active_module\spit @preamble .. code
36 @tick!
37 ok, err = pcall @tick, @
38 if not ok
39 if (type err) == 'table' and err.__class == Error and err.kind == 'assertion'
40 busted.fail err, 0
41
42 error err, 0
43
3744 @active_module.root
3845
3946 --- poll for changes and tick.