aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/program.lux
blob: dbe03460649df90cf3b34c1e853b4119d53fef3e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" io]
    ["[0]" try]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)]
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random]
    [number
     ["n" nat (.use "[1]#[0]" decimal)]]]
   [meta
    ["[0]" code
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["^" pattern]]]
   [test
    ["_" property (.only Test)]]]]
 ["[0]" \\parser]
 [\\library
  ["[0]" /]])

(def !expect
  (template (_ <pattern> <value>)
    [(when <value>
       <pattern>
       true
       
       _
       false)]))

(def \\parser
  Test
  (<| (_.covering \\parser._)
      (_.for [\\parser.Parser])
      (do [! random.monad]
        [expected (at ! each n#encoded random.nat)
         .let [random_dummy (random.only (|>> (text#= expected) not)
                                         (random.unicode 5))]
         dummy random_dummy
         short (random.unicode 1)
         long (random.unicode 2)
         pre_ignore (random.list 5 random_dummy)
         post_ignore (random.list 5 random_dummy)]
        (all _.and
             (_.coverage [\\parser.result \\parser.any]
               (|> (\\parser.result \\parser.any (list expected))
                   (!expect (^.multi {try.#Success actual}
                                     (text#= expected actual)))))
             (_.coverage [\\parser.parse]
               (|> (\\parser.result (\\parser.parse n#decoded) (list expected))
                   (!expect (^.multi {try.#Success actual}
                                     (text#= expected
                                             (n#encoded actual))))))
             (_.coverage [\\parser.this]
               (and (|> (\\parser.result (\\parser.this expected) (list expected))
                        (!expect {try.#Success _}))
                    (|> (\\parser.result (\\parser.this expected) (list dummy))
                        (!expect {try.#Failure _}))))
             (_.coverage [\\parser.somewhere]
               (|> (\\parser.result (|> (\\parser.somewhere (\\parser.this expected))
                                        (<>.before (<>.some \\parser.any)))
                                    (list.together (list pre_ignore (list expected) post_ignore)))
                   (!expect {try.#Success _})))
             (_.coverage [\\parser.end]
               (and (|> (\\parser.result \\parser.end (list))
                        (!expect {try.#Success _}))
                    (|> (\\parser.result (<>.not \\parser.end) (list expected))
                        (!expect {try.#Failure _}))))
             (_.coverage [\\parser.named]
               (|> (\\parser.result (\\parser.named dummy \\parser.any) (list dummy expected))
                   (!expect (^.multi {try.#Success actual}
                                     (text#= expected actual)))))
             (_.coverage [\\parser.parameter]
               (and (|> (\\parser.result (\\parser.parameter [short long] \\parser.any)
                                         (list short expected))
                        (!expect (^.multi {try.#Success actual}
                                          (text#= expected actual))))
                    (|> (\\parser.result (\\parser.parameter [short long] \\parser.any)
                                         (list long expected))
                        (!expect (^.multi {try.#Success actual}
                                          (text#= expected actual))))
                    (|> (\\parser.result (\\parser.parameter [short long] \\parser.any)
                                         (list dummy expected))
                        (!expect {try.#Failure _}))))
             ))))

(def .public test
  Test
  (<| (_.covering /._)
      (do random.monad
        [inputs (random.list 5 (random.upper_case 5))]
        (all _.and
             (_.coverage [/.Program /.program]
               (let [(open "list#[0]") (list.equivalence text.equivalence)]
                 (and (let [outcome ((is /.Program
                                         (/.program all_arguments
                                           (io.io all_arguments)))
                                     inputs)]
                        (same? (is Any inputs)
                               (io.run! outcome)))
                      (let [outcome ((is /.Program
                                         (/.program [arg/0 \\parser.any
                                                     arg/1 \\parser.any
                                                     arg/2 \\parser.any
                                                     arg/3 \\parser.any
                                                     arg/4 \\parser.any]
                                           (io.io (list arg/4 arg/3 arg/2 arg/1 arg/0))))
                                     inputs)]
                        (list#= (list.reversed inputs)
                                (as (List Text) (io.run! outcome))))
                      (let [outcome ((is /.Program
                                         (/.program [all_arguments (<>.many \\parser.any)]
                                           (io.io all_arguments)))
                                     inputs)]
                        (list#= inputs
                                (as (List Text) (io.run! outcome))))
                      (when (try ((is /.Program
                                      (/.program [arg/0 \\parser.any
                                                  arg/1 \\parser.any
                                                  arg/2 \\parser.any
                                                  arg/3 \\parser.any]
                                        (io.io [])))
                                  inputs))
                        {try.#Success _}
                        false
                        
                        {try.#Failure _}
                        true))))

             ..\\parser
             ))))