git.m455.casa

fa

clone url: git://git.m455.casa/fa


esperbuild/espersrc/fennel-0.7.0/test/cli.fnl

1 (local l (require :test.luaunit))
2
3 ;; These are the slowest tests, so for now we just have a basic sanity check
4 ;; to ensure that it compiles and can evaluate math.
5
6 (local *testall* (os.getenv :FNL_TESTALL)) ; set by `make testall`
7
8 (fn file-exists? [filename]
9 (let [f (io.open filename)]
10 (when f (f:close) true)))
11
12 (λ peval [code ...]
13 (local cmd [(string.format "./fennel --eval %q" code) ...])
14 (let [proc (io.popen (table.concat cmd " "))
15 output (: (proc:read :*a) :gsub "\n$" "")]
16 (values (proc:close) output))) ; proc:close gives exit status
17
18 (fn test-cli []
19 ;; skip this if we haven't compiled the CLI
20 (when (file-exists? "./fennel")
21 (l.assertEquals [(peval "(+ 1 2 3)")] [true "6"])))
22
23 (fn test-lua-flag []
24 ;; skip this when cli is not compiled or not running tests with `make testall`
25 (when (and *testall* (file-exists? :./fennel))
26 (let [;; running io.popen for all 20 combinations of lua versions is slow,
27 ;; so we'll just pick the next one in the list after host-lua
28 host-lua (match _VERSION
29 "Lua 5.1" (if _G.jit :luajit :lua5.1)
30 (.. :lua (_VERSION:sub 5)))
31 lua-exec ((fn pick-lua [lua-vs i lua-v]
32 (if (= host-lua lua-v)
33 (. lua-vs (+ 1 (% i (# lua-vs)))) ; circular next
34 (pick-lua lua-vs (next lua-vs i))))
35 [:lua5.1 :lua5.2 :lua5.3 :lua5.4 :luajit])
36 run #(pick-values 2 (peval $ (: "--lua %q" :format lua-exec)))]
37 (l.assertEquals [(run "(match (_VERSION:sub 5)
38 :5.1 (if _G.jit :luajit :lua5.1)
39 v-num (.. :lua v-num))")]
40 [true lua-exec]
41 (.. "should execute code in Lua runtime: " lua-exec))
42 (l.assertEquals
43 [(run "(print :test) (os.exit 1 true)")]
44 ;; pcall in Lua 5.1 doesn't give status with (proc:close)
45 {1 (if (= _VERSION "Lua 5.1") true nil) 2 "test"}
46 (.. "errors should cause failing exit status with --lua " lua-exec)))))
47 {: test-cli : test-lua-flag}