aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/macro.lux
blob: 792ed49eb82d23216c3e7eae20314e31423a1b57 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.use "[1]#[0]" functor)]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" functor)]
    [number
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" static]
    ["[0]" location]
    ["[0]" symbol]
    ["[0]" code (.use "[1]#[0]" equivalence)
     ["<[1]>" \\parser]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   ["^" pattern]
   [syntax (.only syntax)]
   ["[0]" template]
   ["[0]" expansion]]]
 ["[0]" /
  ["[1][0]" local]
  ["[1][0]" syntax]
  ["[1][0]" template]
  ["[1][0]" pattern]
  ["[1][0]" context]])

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

(def !global
  (template (_ <definition>)
    [(is [Text .Global]
         [(template.text [<definition>]) {.#Definition [true .Macro <definition>]}])]))

(def pow/2
  (syntax (_ [number <code>.any])
    (in (list (` (n.* (, number) (, number)))))))

(def pow/4
  (syntax (_ [number <code>.any])
    (in (list (` (..pow/2 (..pow/2 (, number))))))))

(def repeated
  (syntax (_ [times <code>.nat
              token <code>.any])
    (in (list.repeated times token))))

(def fresh_symbol
  (syntax (_ [])
    (do meta.monad
      [g!fresh (/.symbol "fresh")]
      (in (list g!fresh)))))

(def random_lux
  (Random [Nat Text .Lux])
  (do [! random.monad]
    [seed random.nat
     symbol_prefix (random.upper_case 1)
     .let [macro_module (symbol.module (symbol /._))
           current_module (symbol.module (symbol .._))]]
    (in [seed
         symbol_prefix
         [.#info            [.#target ""
                             .#version ""
                             .#mode {.#Build}
                             .#configuration (list)]
          .#source          [location.dummy 0 ""]
          .#location         location.dummy
          .#current_module  {.#Some current_module}
          .#modules         (list [macro_module
                                   [.#module_hash        0
                                    .#module_aliases     (list)
                                    .#definitions        (is (List [Text .Global])
                                                             (list (!global expansion.log_single!)
                                                                   (!global expansion.log_complete!)
                                                                   (!global expansion.log_total!)))
                                    .#imports            (list)
                                    .#module_state       {.#Active}]]
                                  [current_module
                                   [.#module_hash        0
                                    .#module_aliases     (list)
                                    .#definitions        (is (List [Text .Global])
                                                             (list (!global ..pow/2)
                                                                   (!global ..pow/4)
                                                                   (!global ..repeated)))
                                    .#imports            (list)
                                    .#module_state       {.#Active}]])
          .#scopes          (list)
          .#type_context    [.#ex_counter 0
                             .#var_counter 0
                             .#var_bindings (list)]
          .#expected        {.#None}
          .#seed            seed
          .#scope_type_vars (list)
          .#extensions      []
          .#eval            (as (-> Type Code (Meta Any)) [])
          .#host            []]])))

(def iterated
  (syntax (_ [cycle <code>.nat
              it <code>.any])
    (in (list (case cycle
                0 it
                _ (` (..iterated (, (code.nat (-- cycle))) (, it))))))))

(def test|expansion
  Test
  (do [! random.monad]
    [[seed symbol_prefix lux] ..random_lux

     pow/1 (at ! each code.nat random.nat)

     repetitions (at ! each (n.% 10) random.nat)
     .let [single_expansion (` (..pow/2 (..pow/2 (, pow/1))))
           expansion (` (n.* (..pow/2 (, pow/1))
                             (..pow/2 (, pow/1))))
           full_expansion (` (n.* (n.* (, pow/1) (, pow/1))
                                  (n.* (, pow/1) (, pow/1))))]]
    (`` (all _.and
             ... (,, (with_template [<expander> <logger> <expansion>]
             ...       [(_.coverage [<expander>]
             ...          (|> (<expander> (` (..pow/4 (, pow/1))))
             ...              (meta.result lux)
             ...              (try#each (at (list.equivalence code.equivalence) =
             ...                            (list <expansion>)))
             ...              (try.else false)))

             ...        (_.coverage [<logger>]
             ...          (and (|> (expansion.single (` (<logger> "omit" (..pow/4 (, pow/1)))))
             ...                   (meta.result lux)
             ...                   (try#each (at (list.equivalence code.equivalence) = (list)))
             ...                   (try.else false))
             ...               (|> (expansion.single (` (<logger> (..pow/4 (, pow/1)))))
             ...                   (meta.result lux)
             ...                   (try#each (at (list.equivalence code.equivalence) = (list <expansion>)))
             ...                   (try.else false))))]

             ...       [expansion.single   expansion.log_single!   single_expansion]
             ...       [expansion.complete expansion.log_complete! expansion]
             ...       [expansion.total    expansion.log_total!    full_expansion]
             ...       ))
             ... (_.coverage [expansion.one]
             ...   (bit#= (not (n.= 1 repetitions))
             ...          (|> (expansion.one (` (..repeated (, (code.nat repetitions)) (, pow/1))))
             ...              (meta.result lux)
             ...              (!expect {try.#Failure _}))))
             (_.coverage [/.final]
               (with_expansions [<expected> (static.random_nat)
                                 <cycles> (static.random code.nat
                                                         (random#each (|>> (n.% 5) ++) random.nat))
                                 <actual> (/.final (..iterated <cycles> <expected>))]
                 (case (' <actual>)
                   [_ {.#Nat actual}]
                   (n.= <expected> actual)

                   _
                   false)))
             (_.coverage [/.times]
               (with_expansions [<expected> (static.random_nat)
                                 <max> (static.random code.nat
                                                      (random#each (|>> (n.% 10) (n.+ 2)) random.nat))
                                 <cycles> (static.random code.nat
                                                         (random#each (|>> (n.% <max>) ++) random.nat))
                                 <actual> (/.times <cycles> (..iterated <max> <expected>))]
                 (let [expected_remaining (n.- <cycles> <max>)]
                   (case (` <actual>)
                     (^.` (..iterated (^., [_ {.#Nat actual_remaining}]) (^., [_ {.#Nat actual}])))
                     (and (n.= expected_remaining actual_remaining)
                          (n.= <expected> actual))

                     _
                     false))))
             ))))

(def sum
  (macro (_ tokens)
    (loop (again [tokens tokens
                  output 0])
      (case tokens
        {.#End}
        (at meta.monad in (list (code.nat output)))
        
        {.#Item [_ {.#Nat head}] tail}
        (again tail (n.+ head output))

        _
        (meta.failure "")))))

(def sum'
  (macro (_ tokens lux)
    ((/.function ..sum) tokens lux)))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (do [! random.monad]
             [[seed symbol_prefix lux] ..random_lux]
             (all _.and
                  (_.coverage [/.symbol]
                    (|> (/.symbol symbol_prefix)
                        (at meta.monad each %.code)
                        (meta.result lux)
                        (!expect (^.multi {try.#Success actual_symbol}
                                          (and (text.contains? symbol_prefix actual_symbol)
                                               (text.contains? (%.nat seed) actual_symbol))))))
                  (_.coverage [/.wrong_syntax_error]
                    (|> (expansion.single (` (expansion.log_single!)))
                        (meta.result lux)
                        (!expect (^.multi {try.#Failure error}
                                          (text.contains? (/.wrong_syntax_error (symbol expansion.log_single!))
                                                          error)))))
                  (_.coverage [/.with_symbols]
                    (with_expansions [<expected> (fresh_symbol)]
                      (|> (/.with_symbols [<expected>]
                            (at meta.monad in <expected>))
                          (meta.result lux)
                          (!expect (^.multi {try.#Success [_ {.#Symbol ["" actual]}]}
                                            (text.contains? (template.text [<expected>])
                                                            actual))))))
                  (_.coverage [/.function]
                    (with_expansions [n/0 (static.random_nat)
                                      n/1 (static.random_nat)
                                      n/1 (static.random_nat)]
                      (n.= (..sum n/0 n/1 n/1)
                           (..sum' n/0 n/1 n/1))))
                  ))

           ..test|expansion
           
           /local.test
           /syntax.test
           /template.test
           /pattern.test
           /context.test
           )))