dunlin/test/command.fnl

59 lines
1.6 KiB
Fennel

(local { : view } (require :fennel))
(local Command (require :command))
(var happened false)
(fn before [] (set happened false))
(assert
(match (Command.completion { :text "foo" })
(where { : widget : text : value }
(= text "foo")
(= value "foo"))
true))
(Command.define-command
"no-args-command"
[]
#(set happened true))
(Command.define-command
"multiply"
[[:a #[(Command.completion {:text $1})] #"3"]
[:b #[(Command.completion {:text $1})] #"2"]]
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b)))))
(before)
(let [commander (Command.commander)
(ok err)
(match-try (commander:activate)
{:active true} (commander:on-input-finished "not-a-command")
(where {:error e :active false} (e:match "can't find command")) true
(catch
x (values nil (view x))))]
(assert ok err))
(before)
(let [commander (Command.commander)
(ok err)
(match-try (commander:activate)
{:active true} (commander:on-input-finished "multiply")
{:active true :prompt p1} (commander:on-input-finished "2")
{:active true :prompt p2} (commander:on-input-finished "3")
(where {:active false} (= happened 6)) true
(catch
x (values nil (view x))))]
(assert ok err))
(before)
(let [commander (Command.commander)
(ok err)
(match
(commander:invoke-interactively
"multiply"
{:a #"7" :b #"9"})
(where {:active false} (= happened 63)) true
x (values nil (.. "wrong answer " (view x) " " (view happened)))
nil (values nil "???"))]
(assert ok err))