aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/cli.lux
blob: d6161a2b82e1ce37477fd1f5066fbbd5d860ae10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(;module:
  lux
  (lux [io]
       (control ["M" monad #+ do Monad]
                pipe
                ["p" parser])
       (data [product]
             [sum]
             ["E" error]
             [number]
             [text "text/" Eq<Text>]
             text/format
             (coll [list]))
       ["r" math/random]
       ["." cli])
  lux/test)

(context: "CLI"
  (<| (times +100)
      (do @
        [num-args (|> r;nat (:: @ map (n.% +10)))
         #let [(^open "Nat/") number;Codec<Text,Nat>
               gen-arg (:: @ map Nat/encode r;nat)]
         yes gen-arg
         #let [gen-ignore (|> (r;text +5) (r;filter (|>. (text/= yes) not)))]
         no gen-ignore
         pre-ignore (r;list +5 gen-ignore)
         post-ignore (r;list +5 gen-ignore)]
        ($_ seq
            (test "Can read any argument."
                  (|> (.;run (list yes) .;any)
                      (case> (#E;Error _)
                             false
                             
                             (#E;Success arg)
                             (text/= arg yes))))
            (test "Can test tokens."
                  (and (|> (.;run (list yes) (.;this yes))
                           (case> (#E;Error _) false (#E;Success _) true))
                       (|> (.;run (list no) (.;this yes))
                           (case> (#E;Error _) true (#E;Success _) false))))
            (test "Can use custom token parsers."
                  (|> (.;run (list yes) (.;parse Nat/decode))
                      (case> (#E;Error _)
                             false
                             
                             (#E;Success parsed)
                             (text/= (Nat/encode parsed)
                                     yes))))
            (test "Can query if there are any more inputs."
                  (and (|> (.;run (list) .;end)
                           (case> (#E;Success []) true _ false))
                       (|> (.;run (list yes) (p;not .;end))
                           (case> (#E;Success []) false _ true))))
            (test "Can parse CLI input anywhere."
                  (|> (.;run (list;concat (list pre-ignore (list yes) post-ignore))
                             (|> (.;somewhere (.;this yes))
                                 (p;before (p;some .;any))))
                      (case> (#E;Error _) false (#E;Success _) true)))
            ))))