aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro/code.lux
blob: 61a135c0f438da7386a0c7b5f610980cf8246ffc (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(.using
 [library
  [lux "*"
   ["_" test (.only Test)]
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]]]
   [control
    ["[0]" try (.only Try)]]
   [data
    ["[0]" product]
    ["[0]" text]
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [math
    ["[0]" random (.only Random) ("[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [meta
    ["[0]" location]]
   [tool
    [compiler
     [language
      [lux
       ["[0]" syntax]]]]]]]
 [\\library
  ["[0]" /]])

(def: random_text
  (Random Text)
  (random.alphabetic 10))

(def: random_symbol
  (Random Symbol)
  (random.and ..random_text ..random_text))

(def: (random_sequence random)
  (All (_ a) (-> (Random a) (Random (List a))))
  (do [! random.monad]
    [size (|> random.nat (# ! each (n.% 3)))]
    (random.list size random)))

(def: .public random
  (Random Code)
  (random.rec
   (function (_ random)
     (all random.either
          (random#each /.bit random.bit)
          (random#each /.nat random.nat)
          (random#each /.int random.int)
          (random#each /.rev random.rev)
          (random#each /.frac random.safe_frac)
          (random#each /.text ..random_text)
          (random#each /.symbol ..random_symbol)
          (random#each /.form (..random_sequence random))
          (random#each /.variant (..random_sequence random))
          (random#each /.tuple (..random_sequence random))
          ))))

(def: (read source_code)
  (-> Text (Try Code))
  (let [parse (syntax.parse ""
                            syntax.no_aliases
                            (text.size source_code))
        start (is Source
                  [location.dummy 0 source_code])]
    (case (parse start)
      {.#Left [end error]}
      {try.#Failure error}
      
      {.#Right [end lux_code]}
      {try.#Success lux_code})))

(def: (replacement_simulation [original substitute])
  (-> [Code Code] (Random [Code Code]))
  (random.rec
   (function (_ replacement_simulation)
     (let [for_sequence (is (-> (-> (List Code) Code) (Random [Code Code]))
                            (function (_ to_code)
                              (random.only (|>> product.left (# /.equivalence = original) not)
                                           (do [! random.monad]
                                             [parts (..random_sequence replacement_simulation)]
                                             (in [(to_code (list#each product.left parts))
                                                  (to_code (list#each product.right parts))])))))]
       (all random.either
            (random#in [original substitute])
            (do [! random.monad]
              [sample (random.only (|>> (# /.equivalence = original) not)
                                   (all random.either
                                        (random#each /.bit random.bit)
                                        (random#each /.nat random.nat)
                                        (random#each /.int random.int)
                                        (random#each /.rev random.rev)
                                        (random#each /.frac random.safe_frac)
                                        (random#each /.text ..random_text)
                                        (random#each /.symbol ..random_symbol)))]
              (in [sample sample]))
            (for_sequence /.form)
            (for_sequence /.variant)
            (for_sequence /.tuple)
            )))))

(def: for_format
  Test
  (`` (all _.and
           (~~ (template [<coverage> <random> <tag>]
                 [(do [! random.monad]
                    [expected <random>]
                    (_.coverage [<coverage>]
                      (and (case (..read (/.format (<coverage> expected)))
                             {try.#Success actual}
                             (# /.equivalence =
                                actual
                                (<coverage> expected))
                             
                             {try.#Failure error}
                             false)
                           (# /.equivalence =
                              [location.dummy {<tag> expected}]
                              (<coverage> expected)))))]

                 [/.bit random.bit .#Bit]
                 [/.nat random.nat .#Nat]
                 [/.int random.int .#Int]
                 [/.rev random.rev .#Rev]
                 [/.frac random.safe_frac .#Frac]
                 [/.text ..random_text .#Text]
                 [/.symbol ..random_symbol .#Symbol]
                 [/.form (..random_sequence ..random) .#Form]
                 [/.variant (..random_sequence ..random) .#Variant]
                 [/.tuple (..random_sequence ..random) .#Tuple]))
           (~~ (template [<coverage> <random> <tag>]
                 [(do [! random.monad]
                    [expected <random>]
                    (_.coverage [<coverage>]
                      (and (case (..read (/.format (<coverage> expected)))
                             {try.#Success actual}
                             (# /.equivalence =
                                actual
                                (<coverage> expected))
                             
                             {try.#Failure error}
                             false)
                           (# /.equivalence =
                              [location.dummy {<tag> ["" expected]}]
                              (<coverage> expected)))
                      ))]

                 [/.local ..random_text .#Symbol]
                 )))))

(def: .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence ..random))
           
           (_.for [/.format]
                  ..for_format)
           
           (do [! random.monad]
             [[original substitute] (random.only (function (_ [original substitute])
                                                   (not (# /.equivalence = original substitute)))
                                                 (random.and ..random ..random))
              [sample expected] (random.only (function (_ [sample expected])
                                               (not (# /.equivalence = sample expected)))
                                             (..replacement_simulation [original substitute]))]
             (_.coverage [/.replaced]
               (# /.equivalence =
                  expected
                  (/.replaced original substitute sample))))
           )))