aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/program.lux
blob: 1f31b3874e42ecff3b54a1034bafac13aa0fb74e (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
140
141
142
143
144
145
146
147
148
(.require
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [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]]]]]
 ["[0]" \\parser]
 [\\library
  ["[0]" /]])

(def !expect
  (template (_ <pattern> <value>)
    [(case <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 actual_program
  (syntax (_ [actual_program (<| <code>.form
                                 (<>.after (<code>.this_text "lux def program"))
                                 <code>.any)])
    (in (list actual_program))))

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

             ..\\parser
             ))))