clone url: git://git.m455.casa/fa
esperbuild/espersrc/fennel-0.7.0/test/macro.fnl
1 | (local l (require :test.luaunit)) |
2 | (local fennel (require :fennel)) |
3 |
|
4 | (fn test-arrows [] |
5 | (let [cases {"(-> (+ 85 21) (+ 1) (- 99))" 8 |
6 | "(-> 1234 (string.reverse) (string.upper))" "4321" |
7 | "(-> 1234 string.reverse string.upper)" "4321" |
8 | "(->> (+ 85 21) (+ 1) (- 99))" (- 8) |
9 | "(-?> [:a :b] (table.concat \" \"))" "a b" |
10 | "(-?> {:a {:b {:c :z}}} (. :a) (. :b) (. :c))" "z" |
11 | "(-?> {:a {:b {:c :z}}} (. :a) (. :missing) (. :c))" nil |
12 | "(-?>> \" \" (table.concat [:a :b]))" "a b" |
13 | "(-?>> :w (. {:w :x}) (. {:x :missing}) (. {:y :z}))" nil |
14 | "(-?>> :w (. {:w :x}) (. {:x :y}) (. {:y :z}))" "z"}] |
15 | (each [code expected (pairs cases)] |
16 | (l.assertEquals (fennel.eval code) expected code)))) |
17 |
|
18 | (fn test-eval-compiler [] |
19 | (let [reverse "(eval-compiler |
20 | (tset _SPECIALS \"reverse-it\" (fn [ast scope parent opts] |
21 | (tset ast 1 \"do\") |
22 | (for [i 2 (math.ceil (/ (length ast) 2))] |
23 | (let [a (. ast i) b (. ast (- (length ast) (- i 2)))] |
24 | (tset ast (- (length ast) (- i 2)) a) |
25 | (tset ast i b))) |
26 | (_SPECIALS.do ast scope parent opts)))) |
27 | (reverse-it 1 2 3 4 5 6)" |
28 | nest-quote "(eval-compiler (set tbl.nest ``nest)) (tostring tbl.nest)" |
29 | env (setmetatable {:tbl {}} {:__index _G})] |
30 | (l.assertEquals (fennel.eval reverse) 1) |
31 | (l.assertEquals (fennel.eval nest-quote {:compiler-env env :env env}) |
32 | "(quote nest)") |
33 | (fennel.eval "(eval-compiler (set _SPECIALS.reverse-it nil))"))) |
34 |
|
35 | (fn test-import-macros [] |
36 | (let [multigensym "(import-macros m :test.macros) (m.multigensym)" |
37 | inc "(import-macros m :test.macros) (var x 1) (m.inc! x 2) (m.inc! x) x" |
38 | inc2 "(import-macros test :test.macros {:inc INC} :test.macros) |
39 | (INC (test.inc 5))" |
40 | rename "(import-macros {:defn1 defn : ->1} :test.macros) |
41 | (defn join [sep ...] (table.concat [...] sep)) |
42 | (join :: :num (->1 5 (* 2) (+ 8)))" |
43 | unsandboxed "(import-macros {: unsandboxed} :test.macros) |
44 | (unsandboxed)"] |
45 | (l.assertEquals (fennel.eval multigensym) 519) |
46 | (l.assertEquals (fennel.eval inc) 4) |
47 | (l.assertEquals (fennel.eval inc2) 7) |
48 | (l.assertEquals (fennel.eval rename) "num:18") |
49 | (l.assertEquals (fennel.eval unsandboxed {:compiler-env _G}) |
50 | "[\"no\" \"sandbox\"]") )) |
51 |
|
52 | (fn test-require-macros [] |
53 | (let [arrow "(require-macros \"test.macros\") (->1 9 (+ 2) (* 11))" |
54 | defn1 "(require-macros \"test.macros\") |
55 | (defn1 hui [x y] (global z (+ x y))) (hui 8 4) z"] |
56 | (l.assertEquals (fennel.eval arrow) 121) |
57 | (l.assertEquals (fennel.eval defn1) 12))) |
58 |
|
59 | (fn test-inline-macros [] |
60 | (let [cases {"(macro five [] 5) (five)" 5 |
61 | "(macro greet [] :Hi!) (greet)" "Hi!" |
62 | "(macro seq? [expr] (sequence? expr)) (seq? [65])" [65] |
63 | "(macros {:m (fn [y] `(let [xa# 1] (+ xa# ,y)))}) (m 4)" 5 |
64 | "(macros {:plus (fn [x y] `(+ ,x ,y))}) (plus 9 9)" 18 |
65 | "(macros {:when2 (fn [c val] `(when ,c ,val))}) |
66 | (when2 true :when2)" "when2" |
67 | "(macros {:when3 (fn [c val] `(do (when ,c ,val)))}) |
68 | (when3 true :when3)" "when3" |
69 | "(macros {:x (fn [] `(fn [...] (+ 1 1)))}) ((x))" 2 |
70 | "(macros {:yes (fn [] true) :no (fn [] false)}) [(yes) (no)]" |
71 | [true false]} |
72 | g-using "(macros {:m (fn [x] (set _G.sided x))}) (m 952) _G.sided"] |
73 | (each [code expected (pairs cases)] |
74 | (l.assertEquals (fennel.eval code) expected code)) |
75 | (l.assertEquals (fennel.eval g-using {:compiler-env _G}) 952))) |
76 |
|
77 | (fn test-macrodebug [] |
78 | (let [eval-normalize #(-> (pick-values 1 (fennel.eval $1 $2)) |
79 | (: :gsub "table: 0x[0-9a-f]+" "#<TABLE>") |
80 | (: :gsub "\n%s*" "")) |
81 | code "(macrodebug (when (= 1 1) (let [x :X] {: x})) true)" |
82 | expected-fennelview "(if (= 1 1) (do (let [x \"X\"] {:x x})))" |
83 | expected-no-fennelview "(if (= 1 1) (do (let #<TABLE> #<TABLE>)))"] |
84 | (l.assertEquals (eval-normalize code) expected-fennelview) |
85 | (let [fennelview package.loaded.fennelview |
86 | fennel-path fennel.path |
87 | package-path package.path] |
88 | (set (package.loaded.fennelview fennel.path package.path) |
89 | (values nil "" "")) |
90 | (l.assertEquals (eval-normalize code) expected-no-fennelview) |
91 | (set (package.loaded.fennelview fennel.path package.path) |
92 | (values fennelview fennel-path package-path))))) |
93 |
|
94 | {: test-arrows |
95 | : test-import-macros |
96 | : test-require-macros |
97 | : test-eval-compiler |
98 | : test-inline-macros |
99 | : test-macrodebug} |