Debian Codegraph @

test/repl.fnl

raw
1 (local t (require :test.faith))
2 (local fennel (require :fennel))
3 (local specials (require :fennel.specials))
4
5 ;; allow inputs to be structured as a form but converted to a string
6 (macro v [form] (view form))
7
8 (fn wrap-repl [options]
9 (var repl-complete nil)
10 (fn send []
11 (let [output []
12 opts (collect [k x (pairs (or options {})) :into {:useMetadata true}]
13 (values k x))]
14 (fn opts.readChunk []
15 (while (= "" (. output 1))
16 (table.remove output 1))
17 (let [chunk (coroutine.yield (table.concat output "\n"))]
18 (while (. output 1) (table.remove output))
19 (and chunk (.. chunk "\n"))))
20 (fn opts.onValues [x]
21 (when (not= :function (type (. x 1)))
22 (table.insert output (table.concat x "\t"))))
23 (fn opts.onError [_e-type e _lua-src]
24 (table.insert output (.. "error: " e)))
25 (fn opts.registerCompleter [x]
26 (set repl-complete x))
27 (fn opts.pp [x] x)
28 (set opts.env {: table : math : string : require
29 : pcall : ipairs :bit _G.bit})
30 (set opts.env._G opts.env)
31 (set opts.error-pinpoint ["«" "»"])
32 (fennel.repl opts)))
33 (let [repl-send (coroutine.wrap send)]
34 (repl-send)
35 (values repl-send repl-complete)))
36
37 (fn assert-equal-unordered [a b ...]
38 (t.= (table.sort a) (table.sort b) (table.concat [...] " ")))
39
40 (fn test-sym-completion []
41 (let [(send comp) (wrap-repl {:env (collect [k x (pairs _G)] (values k x))})]
42 ;; if not deduped, causes a duplication error completing foo
43 (send (v (global foo :DUPE)))
44 (send (v (local [foo foo-ba* moe-larry] [1 2 {:*curly* "Why soitenly"}])))
45 (send (v (local [!x-y !x_y] [1 2])))
46 (assert-equal-unordered ["foo" "foo-ba*"] (comp "foo")
47 "local completion works & accounts for mangling")
48 (assert-equal-unordered ["moe-larry.*curly*"] (comp "moe-larry")
49 "completion traverses tables without mangling"
50 "keys when input is \"tbl-var.\"")
51 (t.= "1\t2" (send (v (values !x-y !x_y)))
52 "mangled locals do not collide")
53 (assert-equal-unordered ["!x_y" "!x-y"] (comp "!x")
54 "completions on mangled locals do not collide")
55 (send (v (local dynamic-index
56 (setmetatable {:a 1 :b 2} {:__index #($2:upper)}))))
57 (assert-equal-unordered [:dynamic-index.a :dynamic-index.b]
58 (comp "dynamic-index.")
59 "completion doesn't error on table with a fn"
60 "on mt.__index")
61 (send (v (global global-is-nil nil)))
62 (send (v (tset _G :global-is-not-nil-unscoped :NOT-NIL)))
63 (assert-equal-unordered [:global-is-nil
64 :global-not-nil-unscoped]
65 (comp :global-is-n)
66 "completion includes repl-scoped nil globals"
67 "and unscoped non-nil globals")
68 (send (v (local val-is-nil nil)))
69 (send (v (lua "local val-is-nil-unscoped = nil")))
70 (t.= [:val-is-nil] (comp :val-is-ni)
71 "completion includes repl-scoped locals with nil values")
72 (send (v (global shadowed-is-nil nil)))
73 (send (v (local shadowed-nil nil)))
74 (t.= [:shadowed-is-nil] (comp :shadowed-is-n)
75 "completion includes repl-scoped shadowed variables only once")
76 (t.is (pcall send ",complete ]")
77 "shouldn't kill the repl on a parse error")))
78
79 (fn test-macro-completion []
80 (let [(send comp) (wrap-repl {:scope (fennel.scope)})]
81 (send (v (local mac {:incremented 9 :unsanitary 2})))
82 (send (v (import-macros mac :test.macros)))
83 (let [[c1 c2 c3] (doto (comp "mac.i") table.sort)]
84 ;; local should be shadowed!
85 (t.not= "mac.incremented" c1)
86 (t.not= "mac.incremented" c2)
87 (t.= nil c3))))
88
89 (fn test-method-completion []
90 (let [(send comp) (wrap-repl)]
91 (send (v (local ttt {:abc 12 :fff (fn [] :val) :inner {:foo #:f :fa #:f}})))
92 (t.= ["ttt:fff"] (comp "ttt:f") "method completion works on fns")
93 (assert-equal-unordered ["ttt:foo" "ttt:fa"] (comp "ttt.inner.f")
94 "method completion nests")
95 (t.= [] (comp "ttt:ab") "no method completion on numbers")))
96
97 (fn test-command-completion []
98 (let [(send comp) (wrap-repl)]
99 (t.= [",doc"] (comp ",do"))
100 (t.= ",doc" (send ",complete ,do"))
101 (t.is (< 5 (length (comp ",")))
102 "readline completion of bare `,` should list all commands")
103 (t.= ",complete" (send ",complete ,complete ,complete"))))
104
105 (fn test-help []
106 (let [send (wrap-repl)
107 help (send ",help")]
108 (t.match "Show this message" help)
109 (t.match "enter code to be evaluated" help)))
110
111 (fn test-exit []
112 (let [send (wrap-repl)
113 _ (send ",exit")
114 (ok? msg) (pcall send ":more")]
115 (t.is (not ok?))
116 (t.= "cannot resume dead coroutine" msg)))
117
118 (fn test-chunks []
119 (let [input ["(+ 99 " "101" ")\n" " " "\n\n"
120 "(.. :he \n" ":llo" ")"]
121 output []
122 opts {:readChunk #(table.remove input 1)
123 :onValues #(table.insert output (. $ 1))
124 :env (setmetatable {} {:__index _G})}]
125 (fennel.repl opts)
126 (t.= "200\n\"hello\"" (table.concat output "\n"))
127 (while (next output) (table.remove output))
128 (table.insert input "\"hello ")
129 (table.insert input "world!\"")
130 (fennel.repl opts)
131 (t.= "\"hello world!\"" (table.concat output "\n"))))
132
133 (fn test-reload []
134 (set package.loaded.dummy nil)
135 (let [modules {:dummy {:dummy :first-load}}]
136 (fn dummy-loader [module-name]
137 (if (= :dummy module-name)
138 #modules.dummy))
139 (table.insert (or package.searchers package.loaders) dummy-loader)
140 (let [dummy (require :dummy)
141 dummy-first-contents dummy.dummy
142 send (wrap-repl)]
143 (t.= :first-load dummy-first-contents)
144 (set modules.dummy {:dummy :reloaded})
145 (send ",reload dummy")
146 (table.remove (or package.searchers package.loaders))
147 (t.= :reloaded dummy.dummy)
148 (t.match "module 'lmao' not found" (send ",reload lmao")))))
149
150 (fn test-reload-macros []
151 (let [send (wrap-repl)]
152 (tset fennel.macro-loaded :test/macros {:inc #(error :lol)})
153 (t.is (not (pcall fennel.eval "(import-macros m :test/macros) (m.inc 1)")))
154 (send ",reload test/macros")
155 (t.is (pcall fennel.eval
156 "(import-macros m :test/macros) (m.inc 1)"))
157 (tset fennel.macro-loaded :test/macros nil)))
158
159 (fn test-reset []
160 (let [send (wrap-repl)
161 _ (send (v (local abc 123)))
162 abc (send "abc")
163 _ (send ",reset")
164 abc2 (send "abc")]
165 (t.= "123" abc)
166 (t.= "" abc2)))
167
168 (fn test-find []
169 (let [send (wrap-repl)
170 _ (send (v (local f (require :fennel))))
171 result (send ",find f.view")
172 err (send ",find f.viewwwww")]
173 (t.is (or (string.match result "fennel.lua:[0-9]+$")
174 ;; running tests from script
175 (string.match result "fennel:[0-9]+$")
176 ;; running tests from compiled binary
177 (string.match result "src.launcher"))
178 (.. "Expected to find f.view in fennel but got " result))
179 (t.= "error: Unknown value" err)))
180
181 (fn test-compile []
182 (let [send (wrap-repl {:useMetadata false :keywords {"new" true}})
183 result (send ",compile (fn new [] (+ 43 9))")
184 f "local function _new()\n return (43 + 9)\nend\nreturn _new"
185 err (send ",compile (fn ]")]
186 (t.= f result)
187 (t.= "error: Couldn't parse input." err)))
188
189 (fn set-boo [env]
190 "Set boo to exclaimation points."
191 (tset env :boo "!!!"))
192
193 (fn test-plugins []
194 (let [logged []
195 plugin1 {:repl-command-log #(table.insert logged (select 2 ($2)))
196 :versions [(fennel.version:gsub "-dev" "")]}
197 plugin2 {:repl-command-log #(error "p1 should handle this!")
198 :repl-command-set-boo set-boo
199 :versions [(fennel.version:gsub "-dev" "")]}
200 send (wrap-repl {:plugins [plugin1 plugin2] :allowedGlobals false})]
201 (send ",log :log-me")
202 (t.= ["log-me"] logged)
203 (send ",set-boo")
204 (t.= "!!!" (send "boo"))
205 (t.match "Set boo to" (send ",help"))))
206
207 (fn test-options []
208 ;; ensure options.useBitLib propagates to repl
209 (let [send (wrap-repl {:useBitLib true
210 :onError (fn [e] (values :ERROR e))})
211 bxor-result (send (v (bxor 0 0)))]
212 (if _G.jit
213 (t.= "0" bxor-result)
214 (t.match "error:.*attempt to index.*global 'bit'" bxor-result
215 "--use-bit-lib should make bitops fail in non-luajit"))))
216
217 (fn test-apropos []
218 (let [send (wrap-repl)]
219 (let [res (send ",apropos table%.")]
220 (each [_ k (ipairs ["table.concat" "table.insert" "table.remove"
221 "table.sort"])]
222 (t.match k res)))
223 (let [res (send ",apropos not-found")]
224 (t.= "" res "apropos returns no results for unknown pattern")
225 (t.= [] (doto (icollect [item (res:gmatch "[^%s]+")] item)
226 (table.sort))
227 "apropos returns no results for unknown pattern"))
228 (let [res (send ",apropos-doc function")]
229 (t.match "partial" res "apropos returns matching doc patterns")
230 (t.match "pick%-args" res "apropos returns matching doc patterns"))
231 (let [res (send ",apropos-doc \"there's no way this could match\"")]
232 (t.= "" res "apropos returns no results for unknown doc pattern"))))
233
234 (fn test-byteoffset []
235 (let [send (wrap-repl)
236 _ (send (v (macro b [x]
237 (view (doto (getmetatable x) (tset :__fennelview nil))))))
238 _ (send (v (macro f [x] (assert-compile false :lol-no x))))
239 out (send "(b [1])")
240 out2 (send "(b [1])")]
241 (t.= out out2 "lines and byte offsets should be stable")
242 (t.match ":bytestart%s+5" out)
243 (t.match ":byteend%s+7" out)
244 (t.match " %(f «%[123%]»%)" (send " (f [123])"))))
245
246 (fn test-code []
247 (let [(send comp) (wrap-repl)]
248 (send (v (local {: foo} (require :test.mod.foo7))))
249 ;; repro case for https://todo.sr.ht/~technomancy/fennel/85
250 (t.= :foo (send (v (foo))))
251 (t.= [:for :foo] (comp "fo"))))
252
253 (fn test-error-handling []
254 (let [send (wrap-repl)]
255 ;; we get the source in the error message
256 (t.match "%(let «" (send "(let a)"))
257 ;; repeated errors still get it
258 (t.match "%(let «" (send "(let b)"))
259 ;; repl commands don't mess it up
260 (send ",complete l")
261 (t.match "%(let «" (send "(let c)"))
262 ;; parser errors should be properly displayed, albeit without ^ at position
263 (t.match "invalid character: @" (send "(print @)"))
264 ;; don't ignore trailing delimiters
265 (t.match "unexpected closing delimiter %)" (send "565)"))))
266
267 (fn test-locals-saving []
268 (let [send (wrap-repl)]
269 (send (v (local x-y 5)))
270 (send (v (let [x-y 55] nil)))
271 (send (v (fn abc [] :def)))
272 (t.= "5" (send (v x-y)))
273 (t.= "def" (send (v (abc)))))
274 (let [send (wrap-repl {:correlate true})]
275 (send (v (local x 1)))
276 (t.= "1" (send "x")))
277 ;; now let's try with an env
278 (let [send (wrap-repl {:env {: debug}})]
279 (send (v (local xyz 55)))
280 (t.= "55" (send "xyz")))
281 ;; global doesn't accidentally trigger locals-saving
282 (let [send (wrap-repl)
283 mt (getmetatable _G)
284 {: __newindex} mt]
285 (set mt.__newindex nil)
286 (send "(global bar :original)")
287 (send "(set _G.bar :new)")
288 (set mt.__newindex __newindex)
289 (t.= :new (send "bar"))))
290
291 (fn test-docstrings []
292 (let [send (wrap-repl)]
293 (tset fennel.macro-loaded :test.macros nil)
294 (t.= (.. "(if cond1 body1 ... condN bodyN)\n"
295 " Conditional form.\n"
296 " Takes any number of condition/body pairs and evaluates the "
297 "first body where\n"
298 " the condition evaluates to truthy. Similar to "
299 "cond in other lisps.")
300 (send ",doc if")
301 "docstrings for specials")
302 (t.= "(each [vals... iterator] ...)"
303 (: (send ",doc each") :match "^([^\n]+)"))
304 (t.= (.. "(doto val ...)\n Evaluate val and splice it into the first "
305 "argument of subsequent forms.")
306 (send ",doc doto")
307 "docstrings for built-in macros")
308 (t.= "(table.concat #<unknown-arguments>)\n #<undocumented>"
309 (send ",doc table.concat")
310 "docstrings for built-in Lua functions")
311 (t.= "foo.bar not found" (send ",doc foo.bar"))
312 (t.= "(bork) not found" (send ",doc (bork)"))
313 (send (v (fn ew [] "so \"gross\" \\\"I\\\" can't" 1)))
314 (t.= "(ew)\n so \"gross\" \\\"I\\\" can't"
315 (send ",doc ew")
316 "docstrings should be auto-escaped")
317 (send (v (fn foo [a] :C 1)))
318 (t.= "(foo a)\n C"
319 (send ",doc foo")
320 "for named functions, doc shows name, args invocation, docstring")
321 (send (v (fn foo! [[] {} {:x []} [{}]] 1)))
322 (t.= "(foo! [] {} {:x []} [{}])\n #<undocumented>"
323 (send ",doc foo!")
324 "empty tables in arglist printed as defined")
325 (send (v (fn foo! [-kebab- {:x x}] 1)))
326 (t.= "(foo! -kebab- {:x x})\n #<undocumented>"
327 (send ",doc foo!")
328 "fn-name and args pretty-printing")
329 (send (v (fn foo! [-kebab- [a b {: x} [x y]]] 1)))
330 (t.= "(foo! -kebab- [a b {:x x} [x y]])\n #<undocumented>"
331 (send ",doc foo!")
332 "fn-name and args deep pretty-printing 1")
333 (send (v (fn foo! [-kebab- [a b {"a b c" a-b-c} [x y]]] 1)))
334 (t.= "(foo! -kebab- [a b {\"a b c\" a-b-c} [x y]])\n #<undocumented>"
335 (send ",doc foo!")
336 "fn-name and args deep pretty-printing 2")
337 (send (v (fn foo! [-kebab- [a b {"a \"b\" c" a-b-c} [x y]]] 1)))
338 (t.= "(foo! -kebab- [a b {\"a \\\"b\\\" c\" a-b-c} [x y]])\n #<undocumented>"
339 (send " ,doc foo!")
340 "fn-name and args deep pretty-printing 3")
341 (send (v (fn foo! [-kebab- [a b {"a \"b \\\"c\\\" d\" e" a-b-c-d-e}
342 [x y]]] 1)))
343 (t.= (.. "(foo! -kebab- [a b "
344 "{\"a \\\"b \\\\\\\"c\\\\\\\" d\\\" e\" a-b-c-d-e} [x y]])"
345 "\n #<undocumented>")
346 (send ",doc foo!")
347 "fn-name and args deep pretty-printing 4")
348 (send (v (fn ml [] "a
349 multiline
350 docstring" :result)))
351 (t.= "(ml)\n a\n multiline\n docstring"
352 (send ",doc ml")
353 "multiline docstrings work correctly")
354 (t.= "(generate depth ?choice)\n Generate a random piece of data."
355 (send "(local fennel (require :fennel))
356 (local {: generate}
357 (fennel.dofile \"test/generate.fnl\"
358 {:useMetadata true}))
359 ,doc generate")
360 "docstrings from required module.")
361 (send (v (macro abc [x y z] "this is a macro." :123)))
362 (t.= "(abc x y z)\n this is a macro."
363 (send ",doc abc")
364 "docstrings for user-defined macros")
365 (send (v (macro ten [] "[ten]" 10)))
366 (t.= "(ten)\n [ten]"
367 (send ",doc ten")
368 "macro docstrings with brackets")
369 (send (v (λ foo [] :D 1)))
370 (t.= "(foo)\n D"
371 (send ",doc foo")
372 ",doc fnname for named lambdas appear like named functions")
373 (send (v (fn foo [...] {:fnl/arglist [a b c] :fnl/docstring "D"} 1)))
374 (t.= "(foo a b c)\n D"
375 (send " ,doc foo")
376 ",doc arglist should be taken from function metadata table")
377 (send (v (fn foo [...] {:fnl/arglist [a b c]} 1)))
378 (t.= "(foo a b c)\n #<undocumented>"
379 (send ",doc foo")
380 ",doc arglist should be taken from function metadata table")
381 (send (v (fn foo [...] {:fnl/docstring "D"} 1)))
382 (t.= "(foo ...)\n D"
383 (send ",doc foo")
384 ",doc arglist should be taken from function metadata table")
385 (send (v (fn foo [...]
386 {:fnl/arglist [([a]) ([a b])]
387 :fnl/docstring "clojure-like multiarity arglist"} 1)))
388 (t.= "(foo ([a]) ([a b]))\n clojure-like multiarity arglist"
389 (send ",doc foo")
390 ",doc arglist should support lists")
391 (send (v (macro boo [] '(fn foo [...] {:fnl/dostring "D"} 1))))
392 (t.= "(fn foo [...] {:fnl/dostring \"D\"} 1)"
393 (send "(macrodebug (boo) true)")
394 "metadata table should be left as is if it contains invalid keys")
395 (send (v (macro boo [] '(fn foo [...] {:fnl/arlist [a b c]} 1))))
396 (t.= "(fn foo [...] {:fnl/arlist [a b c]} 1)"
397 (send "(macrodebug (boo) true)")
398 "metadata table should be left as is if it contains invalid keys")
399 (send (v (fn foo! [...] {:fnl/arglist [[] {} {:x []} [{}]]} 1)))
400 (t.= "(foo! [] {} {:x []} [{}])\n #<undocumented>"
401 (send ",doc foo!")
402 "empty tables in arglist printed as defined in metadata table")
403 (send (v (macro boo [] '(fn foo [...] [] 1))))
404 (t.= "(fn foo [...] {} 1)"
405 (send "(macrodebug (boo) true)")
406 "non-metadata tables are not removed")
407 (send (v (macro boo [] (let [mt [1 2 3]] '(fn foo [...] ,mt 1)))))
408 (t.= "(fn foo [...] [1 2 3] 1)"
409 (send "(macrodebug (boo) true)")
410 "non-static non-metadata tables are not removed")
411 (send (v (import-macros m :test.macros)))
412 (t.= "(m.inc n)\n Increments n by 1"
413 (send ",doc m.inc")
414 ",doc works on macro tables")
415 (t.= (send ",doc while") (send ",doc while")
416 ",doc <callable> does not mutate target's :fnl/arglist metadata")
417 (send "(local tbl {})")
418 (send "(: (. (require :fennel) :metadata) :set tbl :fnl/docstring \"A TABLE\")")
419 (t.= "tbl\n A TABLE" (send ",doc tbl")
420 ",doc works on tables")))
421
422 (fn test-no-undocumented []
423 (let [send (wrap-repl)
424 undocumented-ok? {:lua true "#" true
425 :set-forcibly! true
426 :reverse-it true}
427 {: _SPECIALS} (specials.make-compiler-env)]
428 (each [name (pairs _SPECIALS)]
429 (when (not (. undocumented-ok? name))
430 (let [docstring (send (: ",doc %s" :format name))]
431 (t.= :string (type docstring))
432 (t.not-match "^error" docstring)
433 (t.not-match "undocumented" docstring
434 (.. "Missing docstring for " name)))))))
435
436 (fn test-custom-metadata []
437 (let [send (wrap-repl)]
438 (send (v (local {: view : metadata} (require :fennel))))
439 (macro s [...] `(send (v (do ,...))))
440 (t.= "\"some-data\""
441 (s (fn foo [] {:foo :some-data} nil)
442 (view (metadata:get foo :foo)))
443 "expected ordinary string metadata to work")
444 (t.= "[\"seq\"]"
445 (s (fn bar [] {:bar [:seq]} nil)
446 (view (metadata:get bar :bar)))
447 "expected sequential table metadata to work")
448 (t.= "{:table \"table\"}"
449 (s (fn baz [] {:baz {:table :table}} nil)
450 (view (metadata:get baz :baz)))
451 "expected associative table metadata to work")
452 (t.= "{:compound [\"seq\" {:table \"table\"}]}"
453 (s (fn qux [] {:qux {:compound [:seq {:table :table}]}} nil)
454 (view (metadata:get qux :qux)))
455 "expected compound metadata to work")
456 (t.= "[\"some-data\" \"docs\"]"
457 (s (fn quux [] "docs" {:foo :some-data} nil)
458 (view [(metadata:get quux :foo)
459 (metadata:get quux :fnl/docstring)]))
460 "expected combined docstring and ordinary string metadata to work")
461 (t.= "[\"x\" \"y\" \"z\"]"
462 (s (λ a-lambda [x ...] {:fnl/arglist [x y z]} nil)
463 (view (metadata:get a-lambda :fnl/arglist)))
464 "expected lambda metadata literal to work")
465 (t.= "[[\"x\" \"y\" \"z\"] \"docs\"]"
466 (s (λ b-lambda [] "docs" {:fnl/arglist [x y z]} nil)
467 (view [(metadata:get b-lambda :fnl/arglist)
468 (metadata:get b-lambda :fnl/docstring)]))
469 "expected combined docstring and ordinary string metadata to work")
470 (t.= "{:fnl/arglist [\"x\"]}"
471 (s (fn whole [x] nil)
472 (view (metadata:get whole)))
473 "expected whole metadata table when no key is asked")))
474
475 (fn test-custom-metadata-failing []
476 (let [send (wrap-repl)]
477 (send (v (local {: view : metadata} (require :fennel))))
478 (t.match "expected literal value in metadata table, got: \"foo\" %(fn "
479 (send (v (fn foo [] {:foo (fn [] nil)} nil)))
480 "lists are not allowed as metadata fields")
481 (t.match "expected literal value in metadata table, got: \"foo\" %[%(fn "
482 (send (v (fn foo [] {:foo [(fn [] nil)]} nil)))
483 "nested lists are not allowed as metadata fields")
484 (t.match "expected literal value in metadata table, got: \"foo\" {:foo "
485 (send (v (fn foo [] {:foo {:foo [(fn [] nil)]}} nil)))
486 "nested lists as values are not allowed as metadata fields")
487 (t.match "expected literal value in metadata table, got: \"foo\" {%[%(fn "
488 (send (v (fn foo [] {:foo {[(fn [] nil)] :foo}} nil)))
489 "nested lists as values are not allowed as metadata fields")))
490
491 (fn test-default-overrides []
492 (set fennel.repl.view-opts {:max-sparse-gap 5})
493 (let [send (wrap-repl {:view-opts {}})]
494 ;; need to set pp back to the repl default for this test to work
495 (send (v (set ___repl___.pp (. (require :fennel) :view))))
496 (t.= "[\"a\" nil nil \"b\"]" (send (v [:a nil nil :b]))
497 "REPL merges explicit view-opts table without clobbering non-conflicting defaults"))
498 (let [send (wrap-repl {:view-opts {:max-sparse-gap 2}})]
499 ;; need to set pp back to the repl default for this test to work
500 (send (v (set ___repl___.pp (. (require :fennel) :view))))
501 (t.= "{1 \"a\" 4 \"b\"}" (send (v [:a nil nil :b]))
502 "Explicit options to :view-opts keys still override custom defaults")
503 (t.= "[\"a\" nil \"b\"]" (send (v [:a nil :b]))
504 "Explicit options to :view-opts keys still override built-in defaults")))
505
506
507 (fn test-long-string []
508 (let [send (wrap-repl)
509 long (fcollect [_ 1 8000 :into [":"]] "-")
510 back (send (table.concat long))]
511 (t.= 8000 (length back))))
512
513 (fn test-save-values []
514 (let [send (wrap-repl)]
515 (send ":lol")
516 (send ":hehe")
517 (send ":lmao")
518 (t.= "lmaohehelol" (send "(table.concat [*1 *2 *3])"))))
519
520 (fn test-return []
521 (let [opts {:readChunk #",return (.. :return :value)"
522 :onValues #nil
523 :env {}}]
524 (t.= :returnvalue (fennel.repl opts))))
525
526 (fn test-decorating-repl []
527 ;; overriding REPL methods from within the REPL via decoration.
528 (let [send (wrap-repl)]
529 (send (v (let [readChunk ___repl___.readChunk]
530 (fn ___repl___.readChunk [parser-state]
531 (set ___repl___.readChunk readChunk)
532 (string.format "(- %s)" (readChunk parser-state))))))
533 (t.= "-6" (send (v (+ 1 2 3)))
534 "expected the result to be negated by the new readChunk")
535 (send (v (let [onValues ___repl___.onValues]
536 (fn ___repl___.onValues [vals]
537 (onValues (icollect [_ v (ipairs vals)]
538 (string.format "res: %s" v)))))))
539 (t.= "res: 10" (send (v (+ 1 2 3 4)))
540 "expected result to include \"res: \" preffix")
541 (send (v (fn ___repl___.onError [errtype err lua-source] nil)))
542 (t.= "" (send (v (error :foo))) "expected error to be ignored")))
543
544 ;; Skip REPL tests in non-JIT Lua 5.1 only to avoid engine coroutine
545 ;; limitation. Normally we want all tests to run on all versions, but in
546 ;; this case the feature will work fine; we just can't use this method of
547 ;; testing it on PUC 5.1, so skip it.
548 (if (and (or (not= _VERSION "Lua 5.1") (= (type _G.jit) "table"))
549 (= "/" (package.config:sub 1 1)))
550 {: test-sym-completion
551 : test-macro-completion
552 : test-method-completion
553 : test-command-completion
554 : test-help
555 : test-exit
556 : test-reload
557 : test-reload-macros
558 : test-chunks
559 : test-reset
560 : test-find
561 : test-compile
562 : test-plugins
563 : test-options
564 : test-apropos
565 : test-byteoffset
566 : test-error-handling
567 : test-code
568 : test-locals-saving
569 : test-docstrings
570 : test-no-undocumented
571 : test-custom-metadata
572 : test-custom-metadata-failing
573 : test-long-string
574 : test-save-values
575 : test-return
576 : test-decorating-repl
577 : test-default-overrides
578 ;; remove any left over custom repl settings
579 :teardown #(each [repl-opt (pairs fennel.repl)]
580 (tset fennel.repl repl-opt nil))}
581 {})