2022-12-21 13:36:25 +00:00
|
|
|
(local { : view } (require :fennel))
|
|
|
|
|
|
|
|
(local Command (require :command))
|
|
|
|
|
|
|
|
(var happened false)
|
2022-12-27 12:25:50 +00:00
|
|
|
(fn before [] (set happened false))
|
2022-12-21 13:36:25 +00:00
|
|
|
|
2022-12-29 17:40:36 +00:00
|
|
|
(Command.define-command
|
|
|
|
"no-args-command"
|
|
|
|
[]
|
|
|
|
#(set happened true))
|
2022-12-21 13:36:25 +00:00
|
|
|
|
|
|
|
(Command.define-command
|
|
|
|
"multiply"
|
2022-12-30 17:44:53 +00:00
|
|
|
[[:a #{$1 $1} #"3"]
|
|
|
|
[:b #{$1 $1} #"2"]]
|
2022-12-29 17:40:36 +00:00
|
|
|
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b)))))
|
2022-12-21 13:36:25 +00:00
|
|
|
|
|
|
|
(before)
|
2022-12-27 12:25:50 +00:00
|
|
|
(let [commander (Command.commander)
|
|
|
|
(ok err)
|
|
|
|
(match-try (commander:activate)
|
2022-12-30 15:58:09 +00:00
|
|
|
{:active true} (commander:on-activate "not-a-command")
|
2022-12-21 13:36:25 +00:00
|
|
|
(where {:error e :active false} (e:match "can't find command")) true
|
|
|
|
(catch
|
|
|
|
x (values nil (view x))))]
|
|
|
|
(assert ok err))
|
|
|
|
|
|
|
|
(before)
|
2022-12-27 12:25:50 +00:00
|
|
|
(let [commander (Command.commander)
|
|
|
|
(ok err)
|
|
|
|
(match-try (commander:activate)
|
2022-12-30 15:58:09 +00:00
|
|
|
{:active true} (commander:on-activate "multiply")
|
|
|
|
{:active true :prompt p1} (commander:on-activate "2")
|
|
|
|
{:active true :prompt p2} (commander:on-activate "3")
|
2022-12-21 13:36:25 +00:00
|
|
|
(where {:active false} (= happened 6)) true
|
|
|
|
(catch
|
|
|
|
x (values nil (view x))))]
|
|
|
|
(assert ok err))
|
2022-12-21 18:04:20 +00:00
|
|
|
|
|
|
|
(before)
|
2022-12-27 12:25:50 +00:00
|
|
|
(let [commander (Command.commander)
|
|
|
|
(ok err)
|
2022-12-21 18:04:20 +00:00
|
|
|
(match
|
2022-12-27 12:25:50 +00:00
|
|
|
(commander:invoke-interactively
|
2022-12-21 18:04:20 +00:00
|
|
|
"multiply"
|
2022-12-27 12:25:50 +00:00
|
|
|
{:a #"7" :b #"9"})
|
2022-12-21 18:04:20 +00:00
|
|
|
(where {:active false} (= happened 63)) true
|
|
|
|
x (values nil (.. "wrong answer " (view x) " " (view happened)))
|
|
|
|
nil (values nil "???"))]
|
|
|
|
(assert ok err))
|