aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro.lux
blob: 8553e6e165e1c13325d95544e5aa66043c57c2ba (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
(.module:
  [library
   [lux "*"
    ["_" test {"+" Test}]
    [abstract
     [monad {"+" do}]]
    [control
     ["[0]" try ("[1]#[0]" functor)]
     [parser
      ["<[0]>" code]]]
    [data
     ["[0]" bit ("[1]#[0]" equivalence)]
     ["[0]" text
      ["%" format {"+" format}]]
     [collection
      ["[0]" list]]]
    [math
     ["[0]" random {"+" Random}]
     [number
      ["[0]" nat]]]
    ["[0]" meta
     ["[0]" location]
     ["[0]" symbol]]]]
  [\\library
   ["[0]" /
    [syntax {"+" syntax:}]
    ["[0]" code ("[1]#[0]" equivalence)]
    ["[0]" template]]]
  ["[0]" / "_"
   ["[1][0]" code]
   ["[1][0]" local]
   ["[1][0]" syntax]
   ["[1][0]" template]])

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

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

(syntax: (pow/2 [number <code>.any])
  (in (list (` (nat.* (~ number) (~ number))))))

(syntax: (pow/4 [number <code>.any])
  (in (list (` (..pow/2 (..pow/2 (~ number)))))))

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

(syntax: (fresh_identifier [])
  (do meta.monad
    [g!fresh (/.identifier "fresh")]
    (in (list g!fresh))))

(def: random_lux
  (Random [Nat Text .Lux])
  (do [! random.monad]
    [seed random.nat
     identifier_prefix (random.ascii/upper 1)
     .let [macro_module (symbol.module (symbol /._))
           current_module (symbol.module (symbol .._))]]
    (in [seed
         identifier_prefix
         [.#info            [.#target  ""
                             .#version ""
                             .#mode    {.#Build}]
          .#source          [location.dummy 0 ""]
          .#location         location.dummy
          .#current_module  {.#Some current_module}
          .#modules         (list [macro_module
                                   [.#module_hash        0
                                    .#module_aliases     (list)
                                    .#definitions        (: (List [Text .Global])
                                                            (list (!global /.log_single_expansion!)
                                                                  (!global /.log_expansion!)
                                                                  (!global /.log_full_expansion!)))
                                    .#imports            (list)
                                    .#module_state       {.#Active}]]
                                  [current_module
                                   [.#module_hash        0
                                    .#module_aliases     (list)
                                    .#definitions        (: (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: expander
  Test
  (do [! random.monad]
    [[seed identifier_prefix lux] ..random_lux

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

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

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

                  [/.single_expansion /.log_single_expansion! single_expansion]
                  [/.expansion        /.log_expansion!        expansion]
                  [/.full_expansion   /.log_full_expansion!   full_expansion]
                  ))
            (_.cover [/.one_expansion]
                     (bit#= (not (nat.= 1 repetitions))
                            (|> (/.one_expansion (` (..repeated (~ (code.nat repetitions)) (~ pow/1))))
                                (meta.result lux)
                                (!expect {try.#Failure _}))))
            ))))

(def: .public test
  Test
  (<| (_.covering /._)
      ($_ _.and
          (do [! random.monad]
            [[seed identifier_prefix lux] ..random_lux]
            ($_ _.and
                (_.cover [/.identifier]
                         (|> (/.identifier identifier_prefix)
                             (# meta.monad each %.code)
                             (meta.result lux)
                             (!expect (^multi {try.#Success actual_identifier}
                                              (and (text.contains? identifier_prefix actual_identifier)
                                                   (text.contains? (%.nat seed) actual_identifier))))))
                (_.cover [/.wrong_syntax_error]
                         (|> (/.single_expansion (` (/.log_single_expansion!)))
                             (meta.result lux)
                             (!expect (^multi {try.#Failure error}
                                              (text.contains? (/.wrong_syntax_error (symbol /.log_single_expansion!))
                                                              error)))))
                (_.cover [/.with_identifiers]
                         (with_expansions [<expected> (fresh_identifier)]
                           (|> (/.with_identifiers [<expected>]
                                 (# meta.monad in <expected>))
                               (meta.result lux)
                               (!expect (^multi {try.#Success [_ {.#Identifier ["" actual]}]}
                                                (text.contains? (template.text [<expected>])
                                                                actual))))))
                ))

          ..expander
          
          /code.test
          /local.test
          /syntax.test
          /template.test
          )))