aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/money.lux
blob: 4ebd46bf2fd5c1b23b4ec57c6bc6134f8ba4a9ec (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["[0]S" equivalence]]]
   [control
    ["[0]" maybe (.use "[1]#[0]" functor)]]
   [data
    ["[0]" text (.only)
     ["%" \\format]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   ["[0]" currency]]]
 ["[0]" /
  ["[1][0]" currency]])

(def .public random
  (Random (Ex (_ of)
            (/.Money of)))
  (do random.monad
    [expected_currency /currency.random
     expected_amount random.nat]
    (in (/.money expected_currency expected_amount))))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [.let [expected_currency currency.usd]
         expected_amount random.nat

         expected_parameter (random.only (n.> 0) random.nat)
         expected_subject random.nat])
      (_.for [/.Money])
      (all _.and
           (_.for [/.equivalence]
                  (equivalenceS.spec /.equivalence ..random))
           
           (_.coverage [/.money /.currency /.amount]
             (let [it (/.money expected_currency expected_amount)]
               (and (same? expected_currency (/.currency it))
                    (same? expected_amount (/.amount it)))))
           (_.coverage [/.+ /.-]
             (let [parameter (/.money expected_currency expected_parameter)
                   subject (/.money expected_currency expected_subject)]
               (and (|> subject
                        (/.+ parameter)
                        (of /.equivalence = subject)
                        not)
                    (|> subject
                        (/.+ parameter)
                        (/.- parameter)
                        (maybe#each (of /.equivalence = subject))
                        (maybe.else false)))))
           (do !
             [it ..random]
             (_.coverage [/.format]
               (and (text.starts_with? (%.nat (/.amount it))
                                       (text.replaced_once "." "" (/.format it)))
                    (text.ends_with? (currency.alphabetic_code (/.currency it))
                                     (/.format it)))))

           /currency.test
           )))