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

(def .public (random $ max_sub_units)
  (All (_ $)
    (-> (Currency $) Nat
        (Random (/.Money $))))
  (when max_sub_units
    0 (random#in (/.money $ max_sub_units))
    _ (random#each (|>> (n.% max_sub_units)
                        (/.money $))
                   random.nat)))

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

         expected_parameter (random.only (n.> 0) random.nat)
         expected_subject random.nat])
      (_.for [/.Money])
      (all _.and
           (_.for [/.equivalence /.=]
                  (equivalenceT.spec /.equivalence (..random currency.usd 1000,00)))
           (_.for [/.order /.<]
                  (orderT.spec /.order (..random currency.usd 1000,00)))
           
           (_.coverage [/.money /.currency /.amount]
             (let [it (/.money currency.usd expected_amount)]
               (and (same? currency.usd (/.currency it))
                    (same? expected_amount (/.amount it)))))
           (_.coverage [/.+ /.-]
             (let [parameter (/.money currency.usd expected_parameter)
                   subject (/.money currency.usd expected_subject)]
               (and (|> subject
                        (/.+ parameter)
                        (of /.equivalence = subject)
                        not)
                    (|> subject
                        (/.+ parameter)
                        (/.- parameter)
                        (maybe#each (of /.equivalence = subject))
                        (maybe.else false)))))
           (_.coverage [/.min]
             (let [expected_parameter (/.money currency.usd expected_parameter)
                   expected_subject (/.money currency.usd expected_subject)]
               (and (/.<= expected_parameter
                          (/.min expected_parameter expected_subject))
                    (/.<= expected_subject
                          (/.min expected_parameter expected_subject)))))
           (_.coverage [/.max]
             (let [expected_parameter (/.money currency.usd expected_parameter)
                   expected_subject (/.money currency.usd expected_subject)]
               (and (/.>= expected_parameter
                          (/.max expected_parameter expected_subject))
                    (/.>= expected_subject
                          (/.max expected_parameter expected_subject)))))
           (let [expected_parameter (/.money currency.usd expected_parameter)
                 expected_subject (/.money currency.usd expected_subject)]
             (all _.and
                  (_.coverage [/.>]
                    (bit#= (/.> expected_parameter expected_subject)
                           (/.< expected_subject expected_parameter)))
                  (_.coverage [/.<= /.>=]
                    (bit#= (/.<= expected_parameter expected_subject)
                           (/.>= expected_subject expected_parameter)))
                  ))
           (_.coverage [/.units /.sub_units
                        /.of_units /.of_sub_units]
             (let [expected (/.money currency.usd expected_amount)
                   actual (/.+ (/.of_units currency.usd (/.units expected))
                               (/.of_sub_units currency.usd (/.sub_units expected)))]
               (/.= expected actual)))
           (do !
             [it (..random currency.usd 1000,00)]
             (_.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
           )))