aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/debug.lux
blob: 8c89a23b34b246340c0b9a2d9d311ee3727e1548 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   ["[0]" ffi]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]
    ["[0]" io]
    [concurrency
     ["[0]" atom]]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]]
    [format
     [json (.only JSON)]
     [xml (.only XML)]]]
   [math
    ["[0]" random (.only Random)]
    [number
     [ratio (.only Ratio)]]]
   [meta
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["[0]" expansion]]
    [compiler
     ["@" target]]]
   [world
    [time (.only Time)
     [instant (.only Instant)]
     [date (.only Date)]
     [duration (.only Duration)]
     [month (.only Month)]
     [day (.only Day)]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]]
 ["$[0]" //
  [data
   [format
    ["[1][0]" json]
    ["[1][0]" xml]]]
  [math
   [number
    ["[1][0]" ratio]]]
  [meta
   ["[1][0]" code]
   ["[1][0]" location]
   ["[1][0]" symbol]
   ["[1][0]" type]]])

(def can_represent_simple_types
  (Random Bit)
  (do random.monad
    [sample_bit random.bit
     sample_int random.int
     sample_frac random.frac
     sample_text (random.upper_cased 10)
     sample_nat random.nat
     sample_rev random.rev]
    (in (`` (and (,, (with_template [<type> <format> <sample>]
                       [(|> (/.representation <type> <sample>)
                            (try#each (text#= (<format> <sample>)))
                            (try.else false))]

                       [Bit %.bit sample_bit]
                       [Nat %.nat sample_nat]
                       [Int %.int sample_int]
                       [Rev %.rev sample_rev]
                       [Frac %.frac sample_frac]
                       [Text %.text sample_text]))
                 )))))

(def can_represent_structure_types
  (Random Bit)
  (do random.monad
    [sample_bit random.bit
     sample_int random.int
     sample_frac random.frac]
    (in (`` (and (when (/.representation (type_literal [Bit Int Frac])
                                         [sample_bit sample_int sample_frac])
                   {try.#Success actual}
                   (text#= (format "[" (%.bit sample_bit)
                                   " " (%.int sample_int)
                                   " " (%.frac sample_frac)
                                   "]")
                           actual)

                   {try.#Failure error}
                   false)
                 ... TODO: Uncomment after switching from the old (tag+last?) to the new (lefts+right?) representation for variants 
                 ... (,, (with_template [<lefts> <right?> <value> <format>]
                 ...       [(|> (/.representation (type_literal (Or Bit Int Frac))
                 ...                         (is (Or Bit Int Frac)
                 ...                            (<lefts> <right?> <value>)))
                 ...            (try#each (text#= (format "(" (%.nat <lefts>)
                 ...                                     " " (%.bit <right?>)
                 ...                                     " " (<format> <value>) ")")))
                 ...            (try.else false))]

                 ...       [0 #0 sample_bit %.bit]
                 ...       [1 #0 sample_int %.int]
                 ...       [1 #1 sample_frac %.frac]
                 ...       ))
                 )))))

(def can_represent_complex_types
  (Random Bit)
  (do random.monad
    [sample_ratio $//ratio.random
     sample_symbol ($//symbol.random 5 5)
     sample_location $//location.random
     sample_type ($//type.random 0)
     sample_code $//code.random
     sample_xml $//xml.random
     sample_json $//json.random]
    (in (`` (and (,, (with_template [<type> <format> <sample>]
                       [(|> (/.representation <type> <sample>)
                            (try#each (text#= (<format> <sample>)))
                            (try.else false))]

                       [Ratio %.ratio sample_ratio]
                       [Symbol %.symbol sample_symbol]
                       [Location %.location sample_location]
                       [Code %.code sample_code]
                       [Type %.type sample_type]
                       [XML %.xml sample_xml]
                       [JSON %.json sample_json]))
                 )))))

(def can_represent_time_types
  (Random Bit)
  (do random.monad
    [sample_instant random.instant
     sample_duration random.duration
     sample_date random.date
     sample_month random.month
     sample_time random.time
     sample_day random.day]
    (in (`` (and (,, (with_template [<type> <format> <sample>]
                       [(|> (/.representation <type> <sample>)
                            (try#each (text#= (<format> <sample>)))
                            (try.else false))]

                       [Instant %.instant sample_instant]
                       [Duration %.duration sample_duration]
                       [Date %.date sample_date]
                       [Month %.month sample_month]
                       [Time %.time sample_time]
                       [Day %.day sample_day]))
                 )))))

(def representation
  Test
  (do random.monad
    [sample_bit random.bit
     sample_nat random.nat
     sample_int random.int
     sample_frac random.frac

     can_represent_simple_types! ..can_represent_simple_types
     can_represent_structure_types! ..can_represent_structure_types
     can_represent_complex_types! ..can_represent_complex_types
     can_represent_time_types! ..can_represent_time_types]
    (all _.and
         (_.coverage [/.representation]
           (`` (and can_represent_simple_types!
                    can_represent_structure_types!
                    can_represent_complex_types!
                    can_represent_time_types!
                    
                    (|> (/.representation .Any sample_frac)
                        (try#each (text#= "[]"))
                        (try.else false))
                    (|> (/.representation (type_literal (List Nat)) (is (List Nat) (list sample_nat)))
                        (try#each (text#= (%.list %.nat (list sample_nat))))
                        (try.else false))
                    (,, (with_template [<sample>]
                          [(|> (/.representation (type_literal (Maybe Nat)) (is (Maybe Nat) <sample>))
                               (try#each (text#= (%.maybe %.nat <sample>)))
                               (try.else false))]
                          
                          [{.#Some sample_nat}]
                          [{.#None}]
                          ))
                    )))
         (_.coverage [/.cannot_represent_value]
           (when (/.representation (-> Nat Nat) (|>>))
             {try.#Success representation}
             false

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

(def inspection
  Test
  (do random.monad
    [sample_bit random.bit
     sample_int random.int
     sample_frac random.frac
     sample_text (random.upper_cased 10)]
    (_.coverage [/.inspection]
      (`` (and (,, (with_template [<format> <sample>]
                     [(text#= (<format> <sample>) (/.inspection <sample>))]

                     [%.bit sample_bit]
                     [%.int sample_int]
                     [%.frac sample_frac]
                     [%.text sample_text]
                     ))
               (text#= (|> (list sample_bit sample_int sample_frac sample_text)
                           (is (List Any))
                           (list#each /.inspection)
                           (text.interposed " ")
                           (text.enclosed ["[" "]"]))
                       (/.inspection [sample_bit sample_int sample_frac sample_text]))
               )))))

(def macro_error
  (syntax (_ [macro <code>.any])
    (function (_ compiler)
      (when ((expansion.complete macro) compiler)
        {try.#Failure error}
        {try.#Success [compiler (list (code.text error))]}
        
        {try.#Success _}
        {try.#Failure "OOPS!"}))))

(type My_Text
  Text)

(for @.jvm (these (ffi.import java/lang/String
                    "[1]::[0]")

                  (ffi.import java/io/ByteArrayOutputStream
                    "[1]::[0]"
                    (new [])
                    (toString [] java/lang/String))

                  (ffi.import java/io/OutputStream
                    "[1]::[0]")

                  (ffi.import java/io/PrintStream
                    "[1]::[0]"
                    (new [java/io/OutputStream]))

                  (ffi.import java/lang/System
                    "[1]::[0]"
                    ("static" out java/io/PrintStream)
                    ("static" setOut [java/io/PrintStream] void))

                  (def system_output
                    java/io/PrintStream
                    (io.run! (java/lang/System::out))))
     @.js (these (ffi.import console
                   "[1]::[0]"
                   ("static" log (-> Text Any))))
     @.python (these (ffi.import io/StringIO
                       "[1]::[0]"
                       (new [])
                       (getvalue [] Text))

                     (ffi.import sys
                       "[1]::[0]"
                       ("static" stdout io/StringIO)))
     ... else
     (these))

(def with_out
  (template (_ <body>)
    [(for @.jvm (ffi.synchronized ..system_output
                  (let [buffer (java/io/ByteArrayOutputStream::new)
                        _ (java/lang/System::setOut (java/io/PrintStream::new buffer))
                        output <body>
                        _ (java/lang/System::setOut ..system_output)]
                    [(ffi.of_string (java/io/ByteArrayOutputStream::toString buffer))
                     output]))
          @.js (let [old (io.run! (console::log))
                     buffer (atom.atom "")
                     _ (io.run! (console::log (function (_ it)
                                                (io.run! (atom.write! (format it text.\n) buffer)))))
                     output <body>
                     _ (io.run! (console::log old))]
                 [(io.run! (atom.read! buffer))
                  output])
          @.python (let [old (io.run! (sys::stdout))
                         buffer (io/StringIO::new [])
                         _ (io.run! (sys::stdout buffer))
                         output <body>
                         _ (io.run! (sys::stdout old))]
                     [(io/StringIO::getvalue buffer)
                      output])
          @.lua [""
                 <body>]
          @.ruby [""
                  <body>])]))

(def .public test
  Test
  (<| (_.covering /._)
      (do random.monad
        [expected_message (random.lower_cased 5)]
        (all _.and
             ..inspection
             ..representation
             (_.coverage [/.hole /.type_hole]
               (let [error (is My_Text (..macro_error (/.hole)))]
                 (and (exception.match? /.type_hole error)
                      (text.contains? (%.type My_Text) error))))
             (do random.monad
               [foo (random.upper_cased 10)
                bar random.nat
                baz random.bit]
               (_.coverage [/.here]
                 (with_expansions [<no_parameters> (for @.js (,, (these))
                                                        (,, (these (/.here))))]
                   (`` (exec
                         <no_parameters>
                         (/.here foo
                                 [bar %.nat])
                         true)))))
             (_.coverage [/.unknown_local_binding]
               (exception.match? /.unknown_local_binding
                                 (..macro_error (/.here yolo))))
             (_.coverage [/.private]
               (exec
                 (is (/.private /.Inspector)
                     /.inspection)
                 true))
             (_.coverage [/.log!]
               (let [[actual_message _] (with_out
                                          (/.log! expected_message))]
                 (text#= (format expected_message text.\n)
                         actual_message)))
             ))))