aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/test/property.lux
blob: a8a1fe1d59bbc15208a510db603a7c3e97a4ccad (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
... 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 and for)
   ["[0]" debug]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception (.only Exception)]
    ["[0]" io]
    [concurrency
     ["[0]" atom (.only Atom)]
     ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set]
     ["[0]" dictionary
      ["[1]" ordered (.only Dictionary)]]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number (.only hex)
     ["n" nat]
     ["f" frac]]]
   [meta
    ["[0]" symbol]
    ["[0]" code
     ["<[1]>" \\parser]]
    [compiler
     ["@" target]]
    [macro
     [syntax (.only syntax)]]]
   [world
    ["[0]" environment]
    ["[0]" console]
    [time
     [duration (.only Duration)]
     ["[0]" instant]]]]]
 [//
  ["//" unit]
  ["[0]" coverage (.only Coverage)]
  ["[0]" tally (.only Tally)]])

(type .public Test
  (Random //.Test))

(def .public (and left right)
  (-> Test Test Test)
  (do [! random.monad]
    [left left]
    (of ! each (//.and left) right)))

(def .public context
  (-> Text Test Test)
  (|>> %.text
       //.context
       random#each))

(def .public failure
  (-> Text Test)
  (|>> //.failure
       random#in))

(def .public success
  (-> Text Test)
  (|>> //.success
       random#in))

(def .public (test message condition)
  (-> Text Bit Test)
  (random#in (//.test message condition)))

(def .public (lifted message random)
  (-> Text (Random Bit) Test)
  (do random.monad
    [it random]
    (test message it)))

(def pcg_32_magic_inc
  Nat
  (hex "FEDCBA9876543210"))

(type .public Seed
  Nat)

(def .public (seed value test)
  (-> Seed Test Test)
  (function (_ prng)
    (let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc value])
                                    test)]
      [prng result])))

(def separator
  text.new_line)

(def (times_failure seed documentation)
  (-> Seed Text Text)
  (format documentation ..separator ..separator
          "Failed with this seed: " (%.nat seed)))

(exception.def .public must_try_test_at_least_once)

(type .public Success_Policy
  Bit)

(def .public ignore_success
  Success_Policy
  #0)

(def .public announce_success
  Success_Policy
  #1)

(def .public (times amount announce_success? test)
  (-> Nat Success_Policy Test Test)
  (when amount
    0 (..failure (exception.error ..must_try_test_at_least_once []))
    _ (do random.monad
        [seed random.nat]
        (function (again prng)
          (let [[prng' instance] (random.result (random.pcg_32 [..pcg_32_magic_inc seed]) test)]
            [prng' (do [! async.monad]
                     [[tally documentation] instance]
                     (if (tally.failed? tally)
                       (in [tally (times_failure seed documentation)])
                       (exec
                         (if announce_success?
                           (debug.log! (format "Succeeded with this seed: " (%.nat seed)))
                           [])
                         (when amount
                           1 instance
                           _ (|> test
                                 (times (-- amount) announce_success?)
                                 (random.result prng')
                                 product.right)))))])))))

(def (description duration tally)
  (-> Duration Tally Text)
  (let [successes (the tally.#successes tally)
        failures (the tally.#failures tally)
        missing (set.difference (the tally.#actual tally)
                                (the tally.#expected tally))
        unexpected (set.difference (the tally.#expected tally)
                                   (the tally.#actual tally))
        report (is (-> Coverage Text)
                   (|>> set.list
                        (list.sorted (of symbol.order <))
                        (exception.listing %.symbol)))
        expected_coverage (set.size (the tally.#expected tally))
        unexpected_coverage (set.size unexpected)
        actual_coverage (n.- unexpected_coverage
                             (set.size (the tally.#actual tally)))
        coverage (when expected_coverage
                   0 "N/A"
                   expected (let [missing_ratio (f./ (n.frac expected)
                                                     (n.frac (set.size missing)))
                                  max_percent +100.0
                                  done_percent (|> +1.0
                                                   (f.- missing_ratio)
                                                   (f.* max_percent))]
                              (if (f.= max_percent done_percent)
                                "100%"
                                (let [raw (|> done_percent
                                              %.frac
                                              (text.replaced_once "+" ""))]
                                  (|> raw
                                      (text.clip 0 (if (f.< +10.0 done_percent)
                                                     4 ...  X.XX
                                                     5 ... XX.XX
                                                     ))
                                      (maybe.else raw)
                                      (text.suffix "%"))))))]
    (exception.report
     (list ["Duration" (%.duration duration)]

           ["Tests" (%.nat (n.+ successes failures))]
           ["Successes" (%.nat successes)]
           ["Failures" (%.nat failures)]
           
           ["Expected coverage" (%.nat expected_coverage)]
           ["Actual coverage" (%.nat actual_coverage)]
           ["Pending coverage" (%.nat (n.- actual_coverage
                                           expected_coverage))]
           ["Unexpected coverage" (%.nat unexpected_coverage)]
           
           ["Coverage" coverage]
           ["Pending" (report missing)]
           ["Unexpected" (report unexpected)]))))

(def failure_exit_code +1)
(def success_exit_code +0)

(def .public (run! test)
  (-> Test (Async Nothing))
  (do async.monad
    [pre (async.future instant.now)
     .let [seed (instant.millis pre)
           prng (random.pcg_32 [..pcg_32_magic_inc seed])]
     [tally documentation] (|> test (random.result prng) product.right)
     post (async.future instant.now)
     .let [duration (instant.span pre post)
           report (format documentation
                          text.new_line text.new_line
                          (..description duration tally)
                          text.new_line)]
     _ (with_expansions [<else> (in {try.#Success (debug.log! report)})]
         (.for @.js (when console.default
                      {.#None}
                      <else>

                      {.#Some console}
                      (console.write_line report console))
               <else>))]
    (async.future (of environment.default exit
                      (when (the tally.#failures tally)
                        0 ..success_exit_code
                        _ ..failure_exit_code)))))

(def .public coverage
  (syntax (_ [coverage <code>.any
              condition <code>.any])
    (in (list (` (of random.monad (,' in) (//.coverage (, coverage) (, condition))))))))

(def .public for
  (syntax (_ [coverage <code>.any
              test <code>.any])
    (in (list (` (of random.functor
                     (,' each)
                     (|>> (//.for (, coverage)))
                     (, test)))))))

(def .public covering
  (syntax (_ [module <code>.any
              test <code>.any])
    (in (list (` (of random.functor
                     (,' each)
                     (|>> (//.covering (, module)))
                     (, test)))))))

(exception.def .public (error_during_execution error)
  (Exception Text)
  (exception.report
   (list ["Error" (%.text error)])))

(def .public (in_parallel tests)
  (-> (List Test) Test)
  (when (list.size tests)
    0
    (random#in (async#in [tally.empty ""]))
    
    expected_tests
    (do random.monad
      [seed random.nat
       .let [prng (random.pcg_32 [..pcg_32_magic_inc seed])
             run! (is (-> Test //.Test)
                      (|>> (random.result prng)
                           product.right
                           try
                           (pipe.when
                             {try.#Success output}
                             output
                             
                             {try.#Failure error}
                             (//.test (exception.error ..error_during_execution [error]) false))))
             state (is (Atom (Dictionary Nat [Tally Text]))
                       (atom.atom (dictionary.empty n.order)))
             [read! write!] (is [//.Test
                                 (async.Resolver [Tally Text])]
                                (async.async []))
             _ (list#mix (function (_ test index)
                           (exec
                             (|> (run! test)
                                 (async.upon! (function (_ assertion)
                                                (do io.monad
                                                  [[_ results] (atom.update! (dictionary.has index assertion) state)]
                                                  (if (n.= expected_tests (dictionary.size results))
                                                    (let [assertions (|> results
                                                                         dictionary.entries
                                                                         (list#each product.right))]
                                                      (write! [(|> assertions
                                                                   (list#each product.left)
                                                                   (list#mix tally.and tally.empty))
                                                               (|> assertions
                                                                   (list#each product.right)
                                                                   (text.interposed ..separator))]))
                                                    (in [])))))
                                 io.run!)
                             (++ index)))
                         0
                         tests)]]
      (in read!))))