Debian Codegraph @

test/macro.fnl

raw
1 (local fennel (require :fennel))
2 (local unpack (or _G.unpack table.unpack))
3 (local t (require :test.faith))
4
5 (local env (setmetatable {} {:__index _G}))
6 (set env._G env)
7
8 (macro view [x] (view x))
9 (macro macro-wrap [helper ...]
10 (let [expr `(do (macro ,helper [,_VARARG] (,helper ,_VARARG))
11 ,...)]
12 `(fennel.eval ,(view expr))))
13
14 (macro == [form expected ?msg ?opts]
15 `(let [(ok# val#) (pcall fennel.eval ,(view form) ,?opts)]
16 (t.is ok# val#)
17 (t.= ,expected val# ,?msg)))
18
19 (fn test-lambda []
20 (lambda arglist-lambda [x]
21 "docstring"
22 {:fnl/arglist [y]}
23 (do :something))
24 (t.= [:y] (. fennel.metadata arglist-lambda :fnl/arglist))
25 (let [l2 (lambda [x]
26 "docstring"
27 {:fnl/arglist [z]}
28 (do :something))]
29 (t.= [:z] (. fennel.metadata l2 :fnl/arglist)))
30 (fn call-lambda [] (arglist-lambda) nil)
31 (let [(ok msg) (pcall call-lambda)] (t.match "test/macro.fnl:20" msg)))
32
33 (fn test-arrows []
34 (== (-> (+ 85 21) (+ 1) (- 99)) 8)
35 (== (-> 1234 (string.reverse) (string.upper)) "4321")
36 (== (-> 1234 string.reverse string.upper) "4321")
37 (== (->> (+ 85 21) (+ 1) (- 99)) -8)
38 (== (-?> {:a {:b {:c :z}}} (. :a) (. :b) (. :c)) "z")
39 (== (-?> {:a {:b {:c :z}}} (. :a) (. :missing) (. :c)) nil)
40 (== (-?>> :w (. {:w :x}) (. {:x :missing}) (. {:y :z})) nil)
41 (== (-?>> :w (. {:w :x}) (. {:x :y}) (. {:y :z})) "z")
42 (== (-?> [:a :b] (table.concat :=)) "a=b")
43 (== (let [(result count) (-?> :abc (: :gsub "a" "b"))] count) 1)
44 (== (-?>> := (table.concat [:a :b])) "a=b"))
45
46 (fn test-doto []
47 (== (doto [1 3 2] (table.sort #(> $1 $2)) table.sort) [1 2 3])
48 (== (do (macro twice [x] `(do ,x ,x))
49 (let [t []] (twice (doto t (table.insert 5)))))
50 [5 5])
51 (== (do (var x 1) (let [y (doto x (set 2))]
52 [x y]))
53 [2 2]))
54
55 (fn test-?. []
56 (== (?. {:a 1}) {:a 1})
57 (== (?. {:a 1} :a) 1)
58 (== (?. {:a 1} :b) nil)
59 (== (?. [-1 -2]) [-1 -2])
60 (== (?. [-1 -2] 1) -1)
61 (== (?. [-1 -2] 3) nil)
62 (== (?. {:a {:b {:c 3}}} :a :b :c) 3)
63 (== (?. {:a {:b {:c 3}}} :d :b :c) nil)
64 (== (?. nil 1 2 3) nil)
65 (== (?. [-1 [-2 [-3] [-4]]] 2 3 1) -4)
66 (== (pcall #(?. [1] 1 2)) false)
67 (== (pcall #(?. {:a true} :a :b)) false)
68 (== (?. {:a [{} {:b {:c 4}}]} :a 2 :b :c) 4)
69 (== (?. {:a [[{:b {:c 5}}]]} :a 1 :b :c) nil)
70 (== (?. {:a [[{:b {:c 5}}]]} :a 1 1 :b :c) 5)
71 (== (do (local t {:a [[{:b {:c 5}}]]}) (?. t :a 1 :b :c)) nil)
72 (== (do (local t {:a [[{:b {:c 5}}]]}) (?. t :a 1 1 :b :c)) 5)
73 (== (?. {:a [[{:b {:c false}}]]} :a 1 1 :b :c) false))
74
75 (fn test-eval-compiler []
76 (== (do (eval-compiler
77 (tset _SPECIALS :reverse-it
78 (fn [ast scope parent opts]
79 (tset ast 1 :do)
80 (for [i 2 (math.ceil (/ (length ast) 2))]
81 (let [a (. ast i) b (. ast (- (length ast) (- i 2)))]
82 (tset ast (- (length ast) (- i 2)) a)
83 (tset ast i b)))
84 (_SPECIALS.do ast scope parent opts))))
85 (reverse-it 1 2 3 4 5 6)) 1)
86 (== (eval-compiler 99) 99)
87 (== (eval-compiler true) true)
88 (let [env (setmetatable {:tbl {}} {:__index _G})]
89 (== (do (eval-compiler (set tbl.nest ``nest)) (tostring tbl.nest))
90 "(quote nest)" ""
91 {:compiler-env env :env env})))
92
93 (fn test-import-macros []
94 (== (do (import-macros m :test.macros) (m.multigensym)) 519)
95 (== (do (import-macros m :test.macros) (var x 1) (m.inc! x 2) (m.inc! x) x) 4)
96 (== (do (import-macros test :test.macros {:inc INC} :test.macros)
97 (INC (test.inc 5))) 7)
98 (== (do (import-macros {:defn1 defn : ->1} :test.macros)
99 (defn join [sep ...] (table.concat [...] sep))
100 (join :: :num (->1 5 (* 2) (+ 8))))
101 "num:18" nil {: env})
102 (== (do (import-macros {: unsandboxed} :test.macros) (unsandboxed))
103 "[\"no\" \"sandbox\"]" "should disable sandbox" {:compiler-env _G})
104 (let [not-unqualified "(import-macros hi :test.macros) (print (inc 1))"]
105 (t.is (not (pcall fennel.eval not-unqualified))))
106 (== 2 (do (import-macros {: gensym-shadow} :test.macros) (gensym-shadow))))
107
108 (fn test-macro-path []
109 (== (do (import-macros m :test.other-macros) (m.m)) "testing macro path")
110 (== (do (import-macros m :test.mod.macroed) (m.reverse3 [1 2 3]))
111 [3 2 1]))
112
113 (fn test-relative-macros []
114 (== (require :test.relative) 3))
115
116 (fn test-relative-chained-mac-mod-mac []
117 (== (require :test.relative-chained-mac-mod-mac) [:a :b :c]))
118
119 (fn test-relative-filename []
120 ;; manual pcall instead of == macro for smaller failure message
121 (let [(ok? val) (pcall require :test.relative-filename)]
122 (t.is ok? val)
123 (t.= val 2)))
124
125 (fn test-require-macros []
126 (== (do (require-macros :test.macros) (->1 9 (+ 2) (* 11))) 121)
127 (== (do (require-macros :test.macros)
128 (defn1 hui [x y] (global z (+ x y)))
129 (hui 8 4) z)
130 12 nil {: env}))
131
132 (fn test-inline-macros []
133 (== (do (macro five [] 5) (five)) 5)
134 (== (do (macros {:m (fn [y] `(let [xa# 1] (+ xa# ,y)))}) (m 4)) 5)
135 (== (do (macros {:x (fn [] `(fn [...] (+ 1 1)))}) ((x))) 2)
136 (== (do (macros {:yes (fn [] true) :no (fn [] false)}) [(yes) (no)]) [true false])
137 (== (do (macro seq? [expr] (sequence? expr)) (seq? [65])) [65])
138 (== (do (macros {:when3 (fn [c val] `(do (when ,c ,val)))})
139 (when3 true :when3)) "when3")
140 (== (do (macro greet [] :Hi!) (greet)) "Hi!")
141 (== (do (macros {:when2 (fn [c val] `(when ,c ,val))})
142 (when2 true :when2)) "when2")
143 (== (do (macros {:plus (fn [x y] `(+ ,x ,y))}) (plus 9 9)) 18)
144 (== (do (macros {:m (fn [x] (set _G.sided x))}) (m 952) _G.sided) 952
145 "should disable sandbox" {:compiler-env env : env})
146 (== (do (macro n [] 1) (local x (n)) (macro n [] 2) (values x (n)))
147 (values 1 2) "macro-macro shadowing should be allowed")
148 (== (do (macros (let [noop #nil] {: noop})) (noop))
149 nil "(macros) should accept an expr that returns a table"))
150
151 (fn test-macrodebug []
152 (let [eval-normalize #(-> (pick-values 1 (fennel.eval $1 $2))
153 (: :gsub "table: 0x[0-9a-f]+" "#<TABLE>")
154 (: :gsub "\n%s*" " "))
155 code "(macrodebug (when (= 1 1) (let [x :X] {: x})) true)"
156 expected "(if (= 1 1) (do (let [x \"X\"] {:x x})))"
157 cyclic "(macrodebug (case [1 2] (where (or [x y] [y nil x]) (= 3 (+ x y))) x) true)"]
158 (t.= (eval-normalize code) expected)
159 (t.= (fennel.eval (pick-values 1 (eval-normalize cyclic)))
160 1
161 "cyclic/recursive AST's should serialize to valid syntax")))
162
163 ;; many of these are copied wholesale from test-match, pending implementation,
164 ;; if match is implemented via case then it should be reasonable to remove much
165 ;; of test-match except for those tests that unify bindings
166 ;;
167 ;; See NEW-CASE-TESTS for new case specific tests
168 (fn test-case []
169 (== (case false
170 false false
171 _ true)
172 false)
173 (== (case [:a {:b 8}]
174 [a b :c] :no
175 [:a {:b b}] b)
176 8)
177 (== (case (values 1 2 3 4 :ok)
178 (where (a b c d e) (= 1 a)) e
179 _ :not-ok) "ok")
180 (== (case [:a :b :c]
181 [a b c] (.. b :eee))
182 "beee")
183
184 ;; regression test, ensure that we don't try to bind `&` even though both patterns technically have it.
185 (== (case [:a :b]
186 (where (or [:a & rest] [:b & rest])) rest)
187 [:b])
188
189 ;; regression test, ensure that we don't try to bind `&as`
190 (== (case [:a :b]
191 (where (or [:a &as rest] [:b &as rest])) rest)
192 [:a :b])
193
194 ;; can't expand blind multi sym
195 (let [(_ msg1) (pcall fennel.eval "(let [x {:y :z}] (case :z x.y 1 _ 0))")]
196 (t.match ".*unexpected multi symbol x.y.*" msg1))
197 ;; but can unify with a multi sym
198 (== (let [x {:y :z}]
199 (case :z
200 (where (= x.y)) 1
201 _ 0))
202 1)
203
204 (== (case (io.open "/does/not/exist")
205 (nil msg) :err
206 f f)
207 "err")
208
209 (== (let [k :k]
210 (case [5 :k]
211 :b :no
212 ;; k will rebind to the same value, but it has no effect on result
213 [n k] n))
214 5)
215
216 (== (let [k :k]
217 (case [5 :k]
218 :b :no
219 ;; should check unified value and match
220 (where [n (= k)]) n))
221 5)
222
223 (== (case true
224 (where (or false true)) :true
225 _ :nil)
226 "true")
227
228 (== (case [1 2 1]
229 [x y x] :yes)
230 "yes")
231
232 (== (case false
233 (where (or false true)) :false
234 _ :nil)
235 "false")
236
237 (== (case [1 2 3]
238 ;; case will check el 1 and el 3 are the same
239 [x y x] :no
240 [x y z] :yes)
241 "yes")
242
243 (== (case [1 2 3]
244 [3 2 1] :no
245 [2 9 1] :NO
246 _ :default)
247 "default")
248
249 (== (case [1 2 [[3]]]
250 [x y [[x]]] :no
251 [x y z] :yes)
252 "yes")
253
254 (== (case [9 5]
255 [a b c] :three
256 [a b] (+ a b))
257 14)
258
259 (== (let [s :hey]
260 (case s
261 :wat :no
262 :hey :yes))
263 "yes")
264
265 (== (case nil
266 (where (or nil false true))
267 :ok
268 _ :not-ok)
269 "ok")
270
271 (== (case [1]
272 [a & b] (length b))
273 0)
274
275 (== (case (+ 1 6)
276 7 8)
277 8)
278
279 ;; legacy not supported
280 (let [(_ msg1) (pcall fennel.eval "(case [1 2] ([a 2] ? (> a 10)) :error-please)")]
281 (t.match ".*legacy guard clause not supported in case.*" msg1))
282
283 ;; new syntax -- general case
284 (== (case [1 2 3 4]
285 (where (or [1 2 4] [4 5 6])) :nope1
286 (where [a 2 3 4] (> a 0)) :success1
287 (where [a 2 3 4] (> a 10)) :nope3
288 (where [a 2 3 4] (= a 1)) :success2) "success1")
289
290 (== (case {:a 1 :b 2}
291 {:c 3} :no
292 {:a n} n)
293 1)
294
295 (== (case [1 2]
296 [a & [b c]] (+ a b c)
297 _ :subrest)
298 "subrest")
299
300 (== (case nil
301 (where (1 2 3 4) true) :nope1
302 (where {:a 1 :b 2} true) :nope2
303 (where [a b c d] (= 100 (* a b c d))) :nope3
304 _ :success)
305 "success")
306
307 (== (case (values nil :nonnil)
308 (true _) :no
309 (nil b) b)
310 "nonnil")
311
312 (== (case [1 2 3]
313 [a b &as t] (+ a b (. t 3)))
314 6)
315
316 (== (case {:a 1 :b 2}
317 {: a &as t} (+ a t.b))
318 3)
319
320 (== (case [1 2 3]
321 [a & b] (+ a (. b 1) (. b 2)))
322 6)
323
324 (== (case [{:sieze :him} 5]
325 (where [f 4] f.sieze (= f.sieze :him)) 4
326 (where [f 5] f.sieze (= f.sieze :him)) 5)
327 5)
328
329 (== (case true
330 (where (or nil false true)) :ok
331 _ :not-ok)
332 "ok")
333
334 (== (case [1 2]
335 [_ _] :wildcard)
336 "wildcard")
337
338 (== (case (+ 1 6)
339 7 8
340 8 1
341 9 2)
342 8)
343
344 (== (case nil
345 false false
346 _ true)
347 true)
348
349 (== (case [1]
350 [a & b] (# b))
351 0)
352
353 (== (case {:sieze :him}
354 (where tbl tbl.sieze tbl.no) :no
355 (where tbl tbl.sieze (= tbl.sieze :him)) :siezed2)
356 "siezed2")
357
358 (== (case {:sieze :him}
359 (where tbl (. tbl :no)) :no
360 (where tbl (. tbl :sieze)) :siezed)
361 "siezed")
362
363 (== (case [1 2 [[1]]]
364 [x y [z]] (. z 1))
365 1)
366
367 ;; Booleans are ORed as patterns
368 (== (case false
369 (where (or nil false true)) :ok
370 _ :not-ok)
371 "ok")
372
373 (== (case [1 2 3 4]
374 (1 2 3 4) :nope1
375 {:a 1 :b 2} :nope2
376 (where [a b c d] (= 100 (* a b c d))) :nope3
377 _ :success)
378 "success")
379
380 (== (case [:a :b :c]
381 [1 t d] :no
382 [a b :d] :NO
383 [a b :c] b)
384 "b")
385
386 ;; nil matching
387 (== (case nil
388 1 :nope1
389 1.2 :nope2
390 :2 :nope3
391 "3 4" :nope4
392 [1] :nope5
393 [1 2] :nope6
394 (1) :nope7
395 (1 2) :nope8
396 {:a 1} :nope9
397 [[1 2] [3 4]] :nope10
398 nil :success
399 _ :nope11)
400 "success")
401
402 (== (case [1 2 3 4]
403 (where (or [1 2 4] [4 5 6])) :nope1
404 (where [a 2 3 4] (> a 10)) :nope2
405 (where [a 2 3 4] (= a 1)) :success)
406 "success")
407
408 (== (case (values 1 [1 2])
409 (x [x x]) :no
410 (x [x y]) :yes)
411 "yes")
412
413 (== (case [1 2 3 4]
414 1 :nope1
415 [1 2 4] :nope2
416 (where [1 2 4]) :nope3
417 (where (or [1 2 4] [4 5 6])) :nope4
418 (where [a 1 2] (> a 0)) :nope5
419 (where [a b c] (> a 2) (> b 0) (> c 0)) :nope6
420 (where (or [a 1] [a -2 -3] [a 2 3 4]) (> a 0)) :success
421 _ :nope7)
422 "success")
423
424 (== (case [:a [:b :c]]
425 [a b :c] :no
426 [:a [:b c]] c)
427 "c")
428
429 (== (do
430 (var x 1)
431 (fn i []
432 (set x (+ x 1))
433 x)
434 (case (i)
435 4 :N
436 3 :n
437 2 :y))
438 "y")
439
440 ;; should not unify x
441 (== (let [x 95]
442 (case [52 85 95]
443 [x y z] :nope
444 [a b x] :yes))
445 "nope")
446
447 ;; unify x
448 (== (let [x 95]
449 (case [52 85 95]
450 (where [(= x) y z]) :nope
451 (where [a b (= x)]) :yes))
452 "yes")
453
454 (== (case (values 5 9)
455 9 :no
456 (a b) (+ a b))
457 14)
458
459 (== (let [x 3
460 res (case x
461 1 :ONE
462 2 :TWO
463 _ :???)]
464 res)
465 "???")
466
467 (== (case [9 5]
468 [a b ?c] :three
469 [a b] (+ a b))
470 "three")
471
472 (== (case nil
473 _ :yes
474 nil :no)
475 "yes")
476
477 (== (let [_ :bar]
478 (case :foo
479 _ :should-match
480 :foo :no))
481 "should-match")
482
483 ;; NEW-CASE-TESTS
484
485 (== (let [x 1]
486 (case [:hello]
487 [x] x))
488 :hello)
489
490 (== (let [x 1]
491 (case [:hello]
492 ;; 1 != :hello
493 (where [(= x)]) x
494 _ :no-match))
495 :no-match)
496
497 (== (let [x 1]
498 (case [1]
499 ;; 1 == 1
500 (where [(= x)]) x
501 _ :no-match))
502 1)
503
504 (== (let [pass :hunter2
505 user-input #:hunter2]
506 (case (user-input)
507 (where (= pass)) :login
508 _ :try-again!))
509 :login)
510
511 (== (let [limit 10]
512 (case [5 6]
513 (where [a b] (<= limit (+ a b))) :over-the-limit
514 [a b] (+ a b limit)))
515 :over-the-limit)
516
517 (== (let [x 99]
518 (case [10 20]
519 ;; x is not unified, so it's a new binding
520 [x y] (+ x y)))
521 (+ 10 20))
522
523 (== (let [x 99]
524 (case [10 20]
525 ;; x was not rebound, so the existing symbol is used
526 [a b] (+ a x)))
527 (+ 10 99))
528
529 (== (let [x 99]
530 (case [99 20]
531 ;; [99 20] = [99 b]
532 (where [(= x) b]) (+ x b)))
533 (+ 99 20))
534
535 (== (let [x 99]
536 (case [99 20]
537 ;; [99 20] = [99 x]
538 ;; note that x is bound in the pattern, so the body uses that value
539 (where [(= x) x]) (+ x x)))
540 (+ 20 20))
541
542 (== (let [x 99]
543 (case [20 99]
544 ;; [20 99] = [x 20]
545 ;; x is bound in the pattern, so the body uses that value
546 (where (or [(= x) x]
547 [x (= x)])) (+ x x)))
548 (+ 20 20)))
549
550 (fn test-case-try []
551 ;; ensure we do not unify in a success path
552 ;; these can be sense checked by running match-try with fresh bindings at
553 ;; each step
554 (== (case-try 10
555 a [(+ a a) (+ 1 1)]
556 ;; should rebind, not unify up
557 (where [a b] (<= a 20)) true
558 (catch
559 a :ay ;; should not get here
560 _ :nothing))
561 true)
562
563 ;; ensure we do not unify in a catch
564 ;; these can be sense checked by running match-try with fresh bindings at
565 ;; each step
566 (== (case-try 20
567 a [(+ a a) (+ 1 1)]
568 ;; fail this match
569 (where [a b] (<= a 20)) true
570 (catch
571 [a _] :we-can-work-it-out
572 _ :nothing))
573 :we-can-work-it-out))
574
575 (fn test-match []
576 (== (match false false false _ true) false)
577 (== (match [:a {:b 8}] [a b :c] :no [:a {:b b}] b) 8)
578 (== (match (values 1 2 3 4 :ok)
579 (where (a b c d e) (= 1 a)) e
580 _ :not-ok) "ok")
581 (== (match [:a :b :c] [a b c] (.. b :eee)) "beee")
582 (== (let [x {:y :z}] (match :z x.y 1 _ 0)) 1)
583 (== (match (io.open "/does/not/exist") (nil msg) :err f f) "err")
584 (== (let [k :k] (match [5 :k] :b :no [n k] n)) 5)
585 (== (match true
586 (where (or false true)) :true
587 _ :nil) "true")
588 (== (match [1 2 1] [x y x] :yes) "yes")
589 (== (match false
590 (where (or false true)) :false
591 _ :nil) "false")
592 (== (match [1 2 3] [x y x] :no [x y z] :yes) "yes")
593 (== (match [1 2 3] [3 2 1] :no [2 9 1] :NO _ :default) "default")
594 (== (match [1 2 [[3]]] [x y [[x]]] :no [x y z] :yes) "yes")
595 (== (match [9 5] [a b c] :three [a b] (+ a b)) 14)
596 (== (let [s :hey] (match s :wat :no :hey :yes)) "yes")
597 (== (match nil (where (or nil false true)) :ok _ :not-ok) "ok")
598 (== (match {:sieze :him}
599 (tbl ? tbl.sieze tbl.no) :no
600 (tbl ? tbl.sieze (= tbl.sieze :him)) :siezed2) "siezed2")
601 (== (match [1] [a & b] (length b)) 0)
602 (== (match (+ 1 6) 7 8) 8)
603 ;; new syntax -- general case
604 (== (match [1 2 3 4]
605 (where (or [1 2 4] [4 5 6])) :nope1
606 (where [a 2 3 4] (> a 0)) :success1
607 ([a 2 3 4] ? (> a 10)) :nope3
608 ([a 2 3 4] ? (= a 1)) :success2) "success1")
609 (== (match {:a 1 :b 2} {:c 3} :no {:a n} n) 1)
610 (== (match [1 2] [a & [b c]] (+ a b c) _ :subrest) "subrest")
611 (== (match nil
612 (where (1 2 3 4) true) :nope1
613 (where {:a 1 :b 2} true) :nope2
614 (where [a b c d] (= 100 (* a b c d))) :nope3
615 ([a b c d] ? (= 100 (* a b c d))) :nope4
616 _ :success) "success")
617 (== (match (values nil :nonnil) (true _) :no (nil b) b) "nonnil")
618 (== (match [1 2 3] [a b &as t] (+ a b (. t 3))) 6)
619 (== (match {:a 1 :b 2} {: a &as t} (+ a t.b)) 3)
620 (== (match [1 2 3] [a & b] (+ a (. b 1) (. b 2))) 6)
621 (== (match [{:sieze :him} 5]
622 (where [f 4] f.sieze (= f.sieze :him)) 4
623 (where [f 5] f.sieze (= f.sieze :him)) 5) 5)
624 (== (match true (where (or nil false true)) :ok _ :not-ok) "ok")
625 (== (match [1 2] [_ _] :wildcard) "wildcard")
626 (== (match (+ 1 6) 7 8 8 1 9 2) 8)
627 (== (match nil false false _ true) true)
628 (== (match [1] [a & b] (# b)) 0)
629 (== (match [{:sieze :him} 5]
630 ([f 4] ? f.sieze (= f.sieze :him)) 4
631 ([f 5] ? f.sieze (= f.sieze :him)) 5) 5)
632 (== (match {:sieze :him}
633 (where tbl tbl.sieze tbl.no) :no
634 (where tbl tbl.sieze (= tbl.sieze :him)) :siezed2) "siezed2")
635 (== (match {:sieze :him}
636 (tbl ? (. tbl :no)) :no
637 (tbl ? (. tbl :sieze)) :siezed) "siezed")
638 (== (match {:sieze :him}
639 (where tbl (. tbl :no)) :no
640 (where tbl (. tbl :sieze)) :siezed) "siezed")
641 (== (match [1 2 [[1]]] [x y [z]] (. z 1)) 1)
642 ;; Booleans are ORed as patterns
643 (== (match false (where (or nil false true)) :ok _ :not-ok) "ok")
644 (== (match [1 2 3 4]
645 (1 2 3 4) :nope1
646 {:a 1 :b 2} :nope2
647 (where [a b c d] (= 100 (* a b c d))) :nope3
648 ([a b c d] ? (= 100 (* a b c d))) :nope4
649 _ :success) "success")
650 (== (match [:a :b :c] [1 t d] :no [a b :d] :NO [a b :c] b) "b")
651 ;; nil matching
652 (== (match nil
653 1 :nope1
654 1.2 :nope2
655 :2 :nope3
656 "3 4" :nope4
657 [1] :nope5
658 [1 2] :nope6
659 (1) :nope7
660 (1 2) :nope8
661 {:a 1} :nope9
662 [[1 2] [3 4]] :nope10
663 nil :success
664 _ :nope11) "success")
665 (== (match [1 2 3 4]
666 (where (or [1 2 4] [4 5 6])) :nope1
667 (where [a 2 3 4] (> a 10)) :nope2
668 ([a 2 3 4] ? (> a 10)) :nope3
669 ([a 2 3 4] ? (= a 1)) :success) "success")
670 (== (match (values 1 [1 2]) (x [x x]) :no (x [x y]) :yes) "yes")
671 (== (match [1 2 3 4]
672 1 :nope1
673 [1 2 4] :nope2
674 (where [1 2 4]) :nope3
675 (where (or [1 2 4] [4 5 6])) :nope4
676 (where [a 1 2] (> a 0)) :nope5
677 (where [a b c] (> a 2) (> b 0) (> c 0)) :nope6
678 (where (or [a 1] [a -2 -3] [a 2 3 4]) (> a 0)) :success
679 _ :nope7) "success")
680 (== (match [:a [:b :c]] [a b :c] :no [:a [:b c]] c) "c")
681 (== (do (var x 1) (fn i [] (set x (+ x 1)) x) (match (i) 4 :N 3 :n 2 :y)) "y")
682 (== (let [x 95] (match [52 85 95] [x y z] :nope [a b x] :yes)) "yes")
683 (== (match (values 5 9) 9 :no (a b) (+ a b)) 14)
684 (== (let [x 3 res (match x 1 :ONE 2 :TWO _ :???)] res) "???")
685 (== (match [9 5] [a b ?c] :three [a b] (+ a b)) "three")
686 (== (match nil _ :yes nil :no) "yes")
687 (== (let [_ :bar] (match :foo _ :should-match :foo :no)) "should-match")
688 ;; match (or) without bindings
689 (== (let [x 10 y 9] (match 9 (where (or x 1 2 3)) :a (where (or x y)) :b)) :b)
690 (== (let [x 3] (match x (where (or 1 2 3) true false) :a (where (or 1 2 3) true) :b)) :b)
691 (== (match 4 (where (or 1 3)) :odd (where (or 2 4)) :even) :even)
692 (== (match [3] (where (or [1] [3])) :odd (where (or [2] [4])) :even) :odd)
693 (== (match (values 1 2) (where (or (1 1) (2 2))) :bad (where (or (2 1) (1 2))) :good) :good)
694 ;; match (or) with bindings
695 (== (match [1 2] (where (or [x y] [y x]) (< y x)) x) 2)
696 (== (let [x 10] (match [10 5] (where (or [y x] [x y])) y)) 5)
697 (== (let [x 5] (match [10 5] (where (or [y x] [x y])) y)) 10)
698 (== (match [:a] (where (or [x] x)) (x:upper)) :A)
699 (== (match :a (where (or [x] x)) (x:upper)) :A)
700 (== (match nil (where (or [x] x)) (x:upper)) nil)
701 (== (match (values 1 2) (where (or (y y x) (x y) (y x))) [x y]) [1 2])
702 (== (do (var x 5) (match 5 (where (or x x)) (set x 6)) x) 6))
703
704 (fn test-match-try []
705 (== (match-try [1 2 1]
706 [1 a b] [b a]
707 [1 & rest] rest)
708 [2]
709 "matching all the way thru")
710 (== (match-try [1 2 3]
711 [1 a b] [b a]
712 [1 & rest] rest)
713 [3 2]
714 "stopping on the second clause")
715 (== [(match-try (values nil "whatever")
716 [1 a b] [b a]
717 [1 & rest] rest)]
718 [nil :whatever]
719 "nil, msg failure representation stops immediately")
720 (== (select 2 (match-try "hey"
721 x (values nil "error")
722 y nil))
723 "error"
724 "all values are in fact propagated even on a mid-chain mismatch")
725 (== (select :# (match-try "hey"
726 x (values nil "error" nil nil)
727 y nil))
728 4
729 "trailing nils are preserved")
730 (== (match-try {:a "abc" :x "xyz"}
731 {: b} :son-of-a-b!
732 (catch
733 {: a : x} (.. a x)))
734 "abcxyz"
735 "catch clause works")
736 (== (match-try {:a "abc" :x "xyz"}
737 {: a} {:abc "whatever"}
738 {: b} :son-of-a-b!
739 (catch
740 [hey] :idunno
741 {: abc} (.. abc "yo")))
742 "whateveryo"
743 "multiple catch clauses works")
744 (let [(_ msg1) (pcall fennel.eval "(match-try abc def)")
745 (_ msg2) (pcall fennel.eval "(match-try abc {} :def _ :wat (catch 55))")
746 (_ msg3) (pcall fennel.eval "(match-try abc {} :def _ :wat [catch 55 :body])")]
747 (t.match ".*expected every pattern to have a body.*" msg1)
748 (t.match ".*expected every catch pattern to have a body.*" msg2)
749 (t.match ".*expected every pattern to have a body.*" msg3)))
750
751 (fn test-lua-module []
752 (== (do (macro abc [] (let [l (require :test.luamod)] (l.abc))) (abc)) :abc)
753 (== (do (import-macros {: reverse} :test.mod.reverse) (reverse (29 2 +))) 31)
754 (let [badcode "(macro bad [] (let [l (require :test.luabad)] (l.bad))) (bad)"]
755 (t.is (not (pcall fennel.eval badcode {:compiler-env :strict})))))
756
757 (fn test-disabled-sandbox-searcher []
758 (let [opts {:env :_COMPILER :compiler-env _G}
759 code "{:path (fn [] (os.getenv \"PATH\"))}"
760 searcher #(match $
761 :dummy (fn [] (fennel.eval code opts)))]
762 (table.insert fennel.macro-searchers 1 searcher)
763 (t.is (pcall fennel.eval "(import-macros {: path} :dummy) (path)"))
764 (table.remove fennel.macro-searchers 1)
765 (t.is (pcall fennel.eval "(import-macros i :test.indirect-macro)"
766 {:compiler-env _G}))))
767
768 (fn test-read-only []
769 (== (do (macro firstline [filename]
770 (with-open [f (assert (io.open filename))]
771 (f:read)))
772 (firstline "test/macro.fnl"))
773 "(local fennel (require :fennel))")
774 (== (do (macro writer [filename]
775 (case (pcall io.open filename :w)
776 (true f) (f:write "uh oh")
777 _ "yeah no"))
778 (writer "out.txt"))
779 "yeah no")
780 (== (do (macro firstline [filename]
781 (case (pcall io.open filename)
782 (true f) (f:read)
783 _ "nah"))
784 (firstline "/etc/passwd"))
785 "nah"))
786
787 (fn test-expand []
788 (== (do (macro expand-string [f]
789 (list (sym :table.concat)
790 (icollect [_ x (ipairs (macroexpand f))] (tostring x))))
791 (expand-string (when true #x)))
792 "iftrue(do (hashfn x))"))
793
794 (fn test-literal []
795 (== (do (macro splice [t] (doto t (tset :hello :world)))
796 (splice {:greetings "comrade"}))
797 {:hello "world" :greetings "comrade"}))
798
799 (fn test-assert-repl []
800 (let [inputs ["x\n" "(inc x)\n" "(length hello)\n" ",return 22\n"]
801 outputs []
802 _ (do (set fennel.repl.readChunk #(table.remove inputs 1))
803 (set fennel.repl.onValues (fn [[x]] (table.insert outputs x))))
804 form (view (let [hello :world]
805 (fn inc [x] (+ x 1))
806 (fn g [x]
807 (assert-repl (< x 2000) "AAAAAH" "WAHHHH"))
808 (fn f [x] (g (* x 2)))
809 (f 28)
810 (f 1010)))]
811 (t.= [true 22]
812 [(pcall fennel.eval form)])
813 (t.= [] inputs)
814 (t.= ["AAAAAH" "2020" "2021" "5" "22"]
815 [(string.gsub (. outputs 1) "%s*stack traceback:.*" "")
816 (unpack outputs 2)])
817 (t.= [(assert-repl :a-string :b-string :c-string)] [:a-string :b-string :c-string])
818 ;; Set REPL to return immediately for next assertions
819 (set fennel.repl.readChunk #",return nil")
820 (set fennel.repl.onError #nil)
821 (set fennel.repl.onValues #nil)
822 (let [form (view (assert-repl false "oh no"))
823 multi-args-form (view (assert-repl (select 1 :a :b nil nil :c)))
824 (ok? msg) (pcall fennel.eval form)]
825 (t.= false ok? "assertion should fail from repl when returning nil")
826 (t.= [true :a :b nil nil :c] [(pcall fennel.eval multi-args-form)]
827 "assert-repl should pass along all runtime ret vals upon success"))))
828
829 (fn test-assert-as-repl []
830 (set fennel.repl.readChunk #",return :nerevar")
831 (set fennel.repl.onValues #nil)
832 (let [form (view (assert nil "you nwah"))
833 (ok? val) (pcall fennel.eval form {:assertAsRepl true})]
834 (t.is ok? "should be able to recover from nil assertion.")
835 (t.= "nerevar" val)))
836
837 (fn check-lambda [ok? msg-pattern l ...]
838 (fn escape-pattern [p]
839 (p:gsub "([][().%+-*?$^])" "%%%1"))
840 (let [(res msg) (pcall l ...)]
841 (t.is (= ok? res) msg-pattern)
842 (when (not res)
843 (t.match (.. ".*" (escape-pattern msg-pattern) ".*") msg))))
844
845 (fn test-lambda-checks []
846 (lambda l [plain-arg {:table table-destr :optional ?key} [vec-dest ?optional-idx]]
847 [plain-arg {:table table-destr :optional ?key} [vec-dest ?optional-idx]])
848
849 (check-lambda true "expected true" l 10 {:table {} :optional :foo} [42 123])
850 (check-lambda true "expected true" l 10 {:table {}} [42])
851
852 (check-lambda false "Missing argument plain-arg on" l nil {:table {} :optional :foo} [42 123])
853 (check-lambda false "attempt to index" l nil nil nil)
854 (check-lambda false "attempt to index" l)
855
856 (check-lambda false "Missing argument table-destr on" l 10 {:optional :foo} [42 123])
857 (check-lambda false "Missing argument vec-dest on" l 10 {:table {} :optional :foo} [nil 123])
858
859 (check-lambda false "attempt to index" l 10 nil [42 123])
860 (check-lambda false "attempt to index" l 10 {:table {} :optional :foo} nil))
861
862 (fn test-lambda-nested-checks []
863 (lambda l [[[vec2]] [[[vec3] vec23 ?optvec2]] {:a {:b table2}} {:a {:b {:c table3} :d ?opt-table32}} [{:a [sudden [{:b nested}]]}]]
864 [[[vec2]] [[[vec3] vec23 ?optvec2]] {:a {:b table2}} {:a {:b {:c table3} :d ?opt-table32}} [{:a [sudden [{:b nested}]]}]])
865
866 (check-lambda false "Missing argument vec2 on" l [[nil]] [[[2] 22]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [5 [{:b 6}]]}])
867 (check-lambda false "Missing argument vec3 on" l [[1]] [[[nil] 22]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [5 [{:b 6}]]}])
868 (check-lambda false "Missing argument vec23 on" l [[1]] [[[2] nil]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [5 [{:b 6}]]}])
869 (check-lambda false "Missing argument table2 on" l [[1]] [[[2] 22]] {:a {:b nil}} {:a {:b {:c 4}}} [{:a [5 [{:b 6}]]}])
870 (check-lambda false "Missing argument table3 on" l [[1]] [[[2] 22]] {:a {:b 3}} {:a {:b {:c nil}}} [{:a [5 [{:b 6}]]}])
871 (check-lambda false "Missing argument sudden on" l [[1]] [[[2] 22]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [nil [{:b 6}]]}])
872 (check-lambda false "Missing argument nested on" l [[1]] [[[2] 22]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [5 [{:b nil}]]}])
873
874 (check-lambda true "expected true" l [[1]] [[[2] 22]] {:a {:b 3}} {:a {:b {:c 4}}} [{:a [5 [{:b 6}]]}])
875 (check-lambda true "expected true" l [[1]] [[[2] 22 23]] {:a {:b 3}} {:a {:b {:c 4} :d 44}} [{:a [5 [{:b 6}]]}]))
876
877 (fn test-env-lua-helpers []
878 (t.= :e (macro-wrap unpack (unpack [:a :b nil nil :e] 5))
879 "unpack is in compiler-env")
880 (t.= {1 :a 3 :c 5 :e :n 5} (macro-wrap pack (pack :a nil :c nil :e))
881 "pack is in compiler-env"))
882
883 (fn test-extra-compiler-env []
884 (== (do (macro m []
885 (if _G.lol
886 :LOL
887 :not-funny))
888 (m)) :LOL
889 "did not propagate extra-compiler-env" {:extra-compiler-env {:lol true}}))
890
891 (fn test-sym []
892 (macro use-sym [arg]
893 `(do ,(sym arg)))
894
895 (t.= "(do something)"
896 (macrodebug (use-sym "something") true)
897 "constructing a symbol from string should not fail")
898
899 (let [(ok? err) (pcall fennel.eval "(do
900 (macro use-sym [arg] `(do ,(sym arg)))
901 (use-sym something))")]
902 (t.is (not ok?))
903 (t.match ".*sym expects a string as the first argument.*" err)))
904
905 (fn test-macro-names []
906 (t.error "multisym" #(fennel.eval "(macro case.str [m] m)")))
907
908 {:teardown #(each [k (pairs fennel.repl)]
909 (tset fennel.repl k nil))
910 : test-arrows
911 : test-doto
912 : test-?.
913 : test-import-macros
914 : test-require-macros
915 : test-relative-macros
916 : test-relative-chained-mac-mod-mac
917 : test-relative-filename
918 : test-eval-compiler
919 : test-inline-macros
920 : test-macrodebug
921 : test-macro-path
922 : test-match
923 : test-case
924 : test-lua-module
925 : test-disabled-sandbox-searcher
926 : test-read-only
927 : test-assert-repl
928 : test-assert-as-repl
929 : test-expand
930 : test-match-try
931 : test-case-try
932 : test-lambda
933 : test-lambda-checks
934 : test-lambda-nested-checks
935 : test-literal
936 : test-env-lua-helpers
937 : test-extra-compiler-env
938 : test-macro-names
939 : test-sym}