Debian Codegraph @

test/misc.fnl

raw
1 (local t (require :test.faith))
2 (local fennel (require :fennel))
3 (local view (require :fennel.view))
4
5 (fn test-leak []
6 (t.is (not (pcall fennel.eval "(->1 1 (+ 4))" {:allowedGlobals false}))
7 "Expected require-macros not leak into next evaluation."))
8
9 (fn test-runtime-quote []
10 (t.is (not (pcall fennel.eval "`(hey)" {:allowedGlobals false}))
11 "Expected quoting lists to fail at runtime.")
12 (t.is (not (pcall fennel.eval "`[hey]" {:allowedGlobals false}))
13 "Expected quoting syms to fail at runtime."))
14
15 (fn test-global-mangling []
16 (t.is (pcall fennel.eval "(.. hello-world :w)" {:env {:hello-world "hi"}})
17 "Expected global mangling to work."))
18
19 (fn test-include []
20 (tset package.preload :test.mod.quux nil)
21 (let [stderr io.stderr
22 ;; disable warnings because these are supposed to fall back
23 _ (set io.stderr nil)
24 expected "foo:FOO-1bar:BAR-2-BAZ-3"
25 (ok out) (pcall fennel.dofile "test/mod/foo.fnl")
26 (ok2 out2) (pcall fennel.dofile "test/mod/foo2.fnl"
27 {:requireAsInclude true})
28 (ok3 out3) (pcall fennel.dofile "test/mod/foo3.fnl"
29 {:requireAsInclude true})
30 (ok4 out4) (pcall fennel.dofile "test/mod/foo4.fnl")
31 (ok5 out5) (pcall fennel.dofile "test/mod/foo5.fnl"
32 {:requireAsInclude true}
33 :test)
34 (ok6 out6) (pcall fennel.dofile "test/mod/foo6.fnl"
35 {:requireAsInclude true}
36 :test)
37 (ok6-2 out6-2) (pcall fennel.dofile "test/mod/foo6-2.fnl"
38 {:requireAsInclude true}
39 :test)]
40 (t.is ok (: "Expected foo to run but it failed with error %s" :format (tostring out)))
41 (t.is ok2 (: "Expected foo2 to run but it failed with error %s" :format (tostring out2)))
42 (t.is ok3 (: "Expected foo3 to run but it failed with error %s" :format (tostring out3)))
43 (t.is ok4 (: "Expected foo4 to run but it failed with error %s" :format (tostring out4)))
44 (t.is ok5 (: "Expected foo5 to run but it failed with error %s" :format (tostring out5)))
45 (t.is ok6 (: "Expected foo6 to run but it failed with error %s" :format (tostring out6)))
46 (t.is ok6-2 (: "Expected foo6 to run but it failed with error %s" :format (tostring out6-2)))
47 (t.= expected (and (= :table (type out)) out.result)
48 (.. "Expected include to have result: " expected))
49 (t.= [:FOO 1] out.quux
50 "Expected include to expose upvalues into included modules")
51 (t.= (view out) (view out2)
52 "Expected requireAsInclude to behave the same as include")
53 (t.= (view out) (view out3)
54 "Expected requireAsInclude to behave the same as include when given an expression")
55 (t.= (view out) (view out4)
56 "Expected include to work when given an expression")
57 (t.= (view out) (view out5)
58 "Expected relative requireAsInclude to work when given a ...")
59 (t.= (view out) (view out6)
60 "Expected relative requireAsInclude to work with nested modules")
61 (t.= (view out) (view out6-2)
62 "Expected relative requireAsInclude to work with nested modules")
63 (t.= nil _G.quux "Expected include to actually be local")
64 (let [spliceOk (pcall fennel.dofile "test/mod/splice.fnl")]
65 (t.is spliceOk "Expected splice to run")
66 (t.= nil _G.q "Expected include to actually be local"))
67 (set io.stderr stderr))
68 (let [stderr io.stderr
69 stderr-fail false
70 _ (set io.stderr {:write #(do (set-forcibly! stderr-fail $2) nil)})
71 code "(local (bar-ok bar) (pcall #(require :test.mod.bar)))
72 (local baz (require :test.mod.baz))
73 (local (quux-ok quux) (pcall #(require :test.mod.quuuuuuux)))
74 [(when bar-ok bar) baz (when quux-ok quux)]"
75 opts {:requireAsInclude true :skipInclude [:test.mod.bar :test.mod.quuuuuuux]}
76 out (fennel.compile-string code opts)
77 value (fennel.eval code opts)]
78 (t.match "baz = require%(\"test.mod.baz\"%)" out)
79 (t.match "bar = pcall" out)
80 (t.match "quux = pcall" out)
81 (t.not-match "baz = nil" out)
82 (t.= stderr-fail false)
83 (t.= value [[:BAR 2 :BAZ 3] [:BAZ 3] nil])
84 (set io.stderr stderr)))
85
86 (fn test-env-iteration []
87 (let [tbl []
88 g {:hello-world "hi"
89 :pairs (fn [t] (local mt (getmetatable t))
90 (if (and mt mt.__pairs)
91 (mt.__pairs t)
92 (pairs t)))
93 :tbl tbl}
94 e []]
95 (set g._G g)
96 (fennel.eval "(each [k (pairs _G)] (tset tbl k true))" {:env g})
97 (t.is (. tbl "hello-world")
98 "Expected wrapped _G to support env iteration.")
99 (var k [])
100 (fennel.eval "(global x-x 42)" {:env e})
101 (fennel.eval "x-x" {:env e})
102 (each [mangled (pairs e)]
103 (set k mangled))
104 (t.= (. e k) 42
105 "Expected mangled globals to be kept across eval invocations.")))
106
107 (fn test-empty-values []
108 (t.is (fennel.eval
109 "(let [a (values)
110 b (values (values))
111 (c d) (values)
112 e (if (values) (values))
113 f (while (values) (values))
114 [g] [(values)]
115 {: h} {:h (values)}]
116 (not (or a b c d e f g h)))")
117 "empty (values) should resolve to nil")
118 (t.= (fennel.eval "(select :# (values))") 0)
119 (t.= (fennel.eval "(select :# (#(values)))") 0)
120 (let [broken-code (fennel.compile "(local [x] (values)) (local {: y} (values))")]
121 (t.is broken-code "code should compile")
122 (t.error "attempt to call a string" broken-code "should fail at runtime")))
123
124 (fn test-short-circuit []
125 (let [method-code "(var shorted? false)
126 (fn set-shorted! [] (set shorted? true) {:f! (fn [])})
127 (and false (: (set-shorted!) :f!))
128 shorted?"
129 comparator-code "(and false (< 1 (error :nein!) 3))"]
130 (t.is (not (fennel.eval method-code)))
131 (t.is (not (fennel.eval comparator-code)))))
132
133 (fn test-precedence []
134 (let [bomb (setmetatable {} {:__add #(= $2 false)})]
135 (t.is (fennel.eval "(+ x (<= 1 5 3))" {:env {:x bomb : _G}})
136 "n-ary comparators should ignore operators precedence")))
137
138 (fn test-table []
139 (let [code "{:transparent 0 :sky 0 :sun 1 :stem 2 :cloud 3 :star 3 :moon 3
140 :cloud-2 4 :gray 4 :rain 5 :butterfly-body 6 :bee-body-1 6 :white 3
141 :butterfly-eye 7 :bee-body-2 7 :dying-plant 7 8 8 9 9}"
142 tbl (fennel.eval code)]
143 (t.= (. tbl 8) 8)))
144
145 (fn test-multisyms []
146 (t.is (pcall fennel.eval "(let [x {:0 #$1 :& #$1}] (x:0) (x:&) (x.0) (x.&))" {:allowedGlobals false})
147 "Expected to be able to use multisyms with digits and & in their second part"))
148
149 (fn test-strings []
150 ;; need bs var in order to test effectively while testing on escape issues
151 ;; that may be broken in the self-hosetd fennel verfsion
152 (let [bs (string.char 92)] ; backslash
153 (macro compile-string [...]
154 `(-> (fennel.compile-string ,...)
155 (: :gsub "^return " "")
156 (: :gsub "^\"([^\"]+)\"$" "%1")))
157 (t.= "\\r\\n" (compile-string "\"\r\n\"")
158 "expected compiling newlines to preserve backslash")
159 (t.= (.. bs "127") (compile-string (.. "\"" bs "127\""))
160 (.. "expected " bs "<digit> to output the byte for 3-digit escapes"))
161 (t.= (.. bs bs "12") (compile-string (.. "\"" bs bs "12\""))
162 (.. "expected even # of " bs "'s not to escape what follows"))))
163
164 (fn test-lambda-as-fn []
165 (each [_ name (ipairs [:lambda :λ])]
166 (let [f (-> (string.format "(%s [x y] (or x y))" name)
167 (fennel.eval {:lambdaAsFn true}))]
168 (t.= :x (f :x :y))
169 (t.= :x (f :x))
170 (t.= :y (f nil :y))
171 (t.= nil (f)))))
172
173 {: test-empty-values
174 : test-env-iteration
175 : test-global-mangling
176 : test-include
177 : test-leak
178 : test-table
179 : test-runtime-quote
180 : test-short-circuit
181 : test-precedence
182 : test-multisyms
183 : test-strings
184 : test-lambda-as-fn}