aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/analysis.lux
blob: a3f401643f0f18ad1c9d891789112f930dabda74 (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
149
(.using
  [library
   [lux "*"
    ["_" test {"+" Test}]
    [abstract
     [monad {"+" do}]]
    [control
     [pipe {"+" case>}]
     ["[0]" try]
     ["[0]" exception]
     ["<>" parser]]
    [data
     ["[0]" bit ("[1]#[0]" equivalence)]
     ["[0]" text ("[1]#[0]" equivalence)]
     [collection
      ["[0]" list]]]
    [math
     ["[0]" random {"+" Random}]
     [number
      ["n" nat]
      ["i" int]
      ["f" frac]
      ["r" rev]]]
    [meta
     ["[0]" symbol ("[1]#[0]" equivalence)]]
    [tool
     [compiler
      [reference {"+" Constant}
       [variable {"+"}]]
      [language
       [lux
        ["[0]" analysis]]]]]]]
  [\\library
   ["[0]" /]])

(template: (!expect <expectation> <computation>)
  [(case <computation>
     <expectation>
     true

     _
     false)])

(def: constant
  (Random Constant)
  (random.and (random.unicode 10)
              (random.unicode 10)))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      (do [! random.monad]
        []
        (`` ($_ _.and
                (do [! random.monad]
                  [expected (# ! each (|>> analysis.bit) random.bit)]
                  (_.cover [/.result /.any]
                           (|> (list expected)
                               (/.result /.any)
                               (case> {try.#Success actual}
                                      (# analysis.equivalence = expected actual)

                                      {try.#Failure _}
                                      false))))
                (~~ (template [<query> <check> <random> <analysis> <=>]
                      [(do [! random.monad]
                         [expected <random>]
                         (_.cover [<query>]
                                  (|> (list (<analysis> expected))
                                      (/.result <query>)
                                      (case> {try.#Success actual}
                                             (<=> expected actual)

                                             {try.#Failure _}
                                             false))))
                       (do [! random.monad]
                         [expected <random>]
                         (_.cover [<check>]
                                  (|> (list (<analysis> expected))
                                      (/.result (<check> expected))
                                      (!expect {try.#Success _}))))]
                      
                      [/.bit /.bit! random.bit analysis.bit bit#=]
                      [/.nat /.nat! random.nat analysis.nat n.=]
                      [/.int /.int! random.int analysis.int i.=]
                      [/.frac /.frac! random.safe_frac analysis.frac f.=]
                      [/.rev /.rev! random.rev analysis.rev r.=]
                      [/.text /.text! (random.unicode 10) analysis.text text#=]
                      [/.local /.local! random.nat analysis.local n.=]
                      [/.foreign /.foreign! random.nat analysis.foreign n.=]
                      [/.constant /.constant! ..constant analysis.constant symbol#=]
                      ))
                (do [! random.monad]
                  [expected random.bit]
                  (_.cover [/.tuple]
                           (|> (list (analysis.tuple (list (analysis.bit expected))))
                               (/.result (/.tuple /.bit))
                               (case> {try.#Success actual}
                                      (bit#= expected actual)

                                      {try.#Failure _}
                                      false))))
                (do [! random.monad]
                  [dummy random.bit]
                  (_.cover [/.end?]
                           (and (|> (/.result /.end? (list))
                                    (!expect {try.#Success #1}))
                                (|> (/.result (do <>.monad
                                                [verdict /.end?
                                                 _ /.bit]
                                                (in verdict))
                                              (list (analysis.bit dummy)))
                                    (!expect {try.#Success #0})))))
                (do [! random.monad]
                  [dummy random.bit]
                  (_.cover [/.end!]
                           (and (|> (/.result /.end! (list))
                                    (!expect {try.#Success _}))
                                (|> (/.result /.end! (list (analysis.bit dummy)))
                                    (!expect {try.#Failure _})))))
                (do [! random.monad]
                  [expected random.bit]
                  (_.cover [/.cannot_parse]
                           (and (|> (list (analysis.bit expected))
                                    (/.result /.nat)
                                    (case> {try.#Success _}
                                           false

                                           {try.#Failure error}
                                           (exception.match? /.cannot_parse error)))
                                (|> (list)
                                    (/.result /.bit)
                                    (case> {try.#Success _}
                                           false

                                           {try.#Failure error}
                                           (exception.match? /.cannot_parse error))))))
                (do [! random.monad]
                  [expected random.bit]
                  (_.cover [/.unconsumed_input]
                           (|> (list (analysis.bit expected) (analysis.bit expected))
                               (/.result /.bit)
                               (case> {try.#Success _}
                                      false

                                      {try.#Failure error}
                                      (exception.match? /.unconsumed_input error)))))
                )))))