git.m455.casa

fa

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


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

1 (local l (require :test.luaunit))
2 (local fennel (require :fennel))
3
4 (fn wrap-repl []
5 (var repl-complete nil)
6 (fn send []
7 (var output [])
8 (fennel.repl {:readChunk (fn []
9 (let [chunk (coroutine.yield output)]
10 (set output [])
11 (and chunk (.. chunk "\n"))))
12 :onValues #(table.insert output (table.concat $ "\t"))
13 :registerCompleter #(set repl-complete $)
14 :pp #$}))
15 (local repl-send (coroutine.wrap send))
16 (repl-send)
17 (values repl-send repl-complete))
18
19 (fn assert-equal-unordered [a b msg]
20 (l.assertEquals (table.sort a) (table.sort b) msg))
21
22 (fn test-completion []
23 (let [(send comp) (wrap-repl)]
24 (send "(local [foo foo-ba* moe-larry] [1 2 {:*curly* \"Why soitenly\"}])")
25 (send "(local [!x-y !x_y] [1 2])")
26 (assert-equal-unordered (comp "foo") ["foo" "foo-ba*"]
27 "local completion works & accounts for mangling")
28 (assert-equal-unordered (comp "moe-larry") ["moe-larry.*curly*"]
29 (.. "completion traverses tables without mangling"
30 " keys when input is \"tbl-var.\""))
31 (assert-equal-unordered (send "(values !x-y !x_y)") [[1 2]]
32 "mangled locals do not collide")
33 (assert-equal-unordered (comp "!x") ["!x_y" "!x-y"]
34 "completions on mangled locals do not collide")))
35
36 (fn test-help []
37 (let [send (wrap-repl)
38 help (table.concat (send ",help"))]
39 (l.assertStrContains help "show this message")
40 (l.assertStrContains help "enter code to be evaluated")))
41
42 (fn test-exit []
43 (let [send (wrap-repl)
44 _ (send ",exit")
45 (ok? msg) (pcall send ":more")]
46 (l.assertFalse ok?)
47 (l.assertEquals msg "cannot resume dead coroutine")))
48
49 (var dummy-module nil)
50
51 (fn dummy-loader [module-name]
52 (if (= :dummy module-name)
53 #dummy-module))
54
55 (fn test-reload []
56 (set dummy-module {:dummy :first-load})
57 (table.insert (or package.searchers package.loaders) dummy-loader)
58 (let [dummy (require :dummy)
59 dummy-first-contents dummy.dummy
60 send (wrap-repl)]
61 (set dummy-module {:dummy :reloaded})
62 (send ",reload dummy")
63 (l.assertEquals :first-load dummy-first-contents)
64 (l.assertEquals :reloaded dummy.dummy)))
65
66 (fn test-reset []
67 (let [send (wrap-repl)
68 _ (send "(local abc 123)")
69 abc (table.concat (send "abc"))
70 _ (send ",reset")
71 abc2 (table.concat (send "abc"))]
72 (l.assertEquals abc "123")
73 (l.assertEquals abc2 "")))
74
75 ;; Skip REPL tests in non-JIT Lua 5.1 only to avoid engine coroutine
76 ;; limitation. Normally we want all tests to run on all versions, but in
77 ;; this case the feature will work fine; we just can't use this method of
78 ;; testing it on PUC 5.1, so skip it.
79 (if (or (not= _VERSION "Lua 5.1") (= (type _G.jit) "table"))
80 {: test-completion
81 : test-help
82 : test-exit
83 : test-reload
84 : test-reset}
85 {})