dunlin

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

command.fnl (1626B)


      1 (local { : view } (require :fennel))
      2 
      3 (local Command (require :command))
      4 
      5 (var happened false)
      6 (fn before [] (set happened false))
      7 
      8 (assert
      9  (match (Command.completion { :text "foo" })
     10    (where { : widget : text : value }
     11      (= text "foo")
     12      (= value "foo"))
     13    true))
     14 
     15 (Command.define-command
     16  "no-args-command"
     17  []
     18  #(set happened true))
     19 
     20 (Command.define-command
     21  "multiply"
     22  [[:a #[(Command.completion {:text $1})] #"3"]
     23   [:b #[(Command.completion {:text $1})] #"2"]]
     24  (fn [{: a : b }] (set happened (* (tonumber a) (tonumber b)))))
     25 
     26 (before)
     27 (let [commander (Command.commander)
     28       (ok err)
     29       (match-try (commander:activate)
     30         {:active true} (commander:on-input-finished "not-a-command")
     31         (where {:error e :active false} (e:match "can't find command")) true
     32         (catch
     33          x (values nil (view x))))]
     34   (assert ok err))
     35 
     36 (before)
     37 (let [commander (Command.commander)
     38       (ok err)
     39       (match-try  (commander:activate)
     40         {:active true} (commander:on-input-finished "multiply")
     41         {:active true :prompt p1} (commander:on-input-finished "2")
     42         {:active true :prompt p2} (commander:on-input-finished "3")
     43         (where {:active false} (= happened 6)) true
     44         (catch
     45          x (values nil (view x))))]
     46   (assert ok err))
     47 
     48 (before)
     49 (let [commander (Command.commander)
     50       (ok err)
     51       (match
     52           (commander:invoke-interactively
     53            "multiply"
     54            {:a #"7" :b #"9"})
     55         (where {:active false} (= happened 63)) true
     56         x (values nil (.. "wrong answer " (view x) " " (view happened)))
     57         nil (values nil "???"))]
     58   (assert ok err))