aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/time/instant.lux
blob: d81a5ad272a33aa1258fe121837e64e6143d5f0a (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" hash]
     ["$[0]" order]
     ["$[0]" enum]
     ["$[0]" codec]]]
   [control
    ["[0]" function]
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" io]]
   [data
    ["[0]" text
     ["?[1]" \\parser]]
    [collection
     ["[0]" list (.use "[1]#[0]" mix)]]]
   [math
    ["[0]" random]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["[0]" duration (.only Duration)]
    ["[0]" day (.only Day) (.use "[1]#[0]" enum)]]]])

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Instant])
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence random.instant))
           (_.for [/.hash]
                  ($hash.spec /.hash random.instant))
           (_.for [/.order]
                  ($order.spec /.order random.instant))
           (_.for [/.enum]
                  ($enum.spec /.enum random.instant))
           (_.for [/.codec]
                  ($codec.spec /.equivalence /.codec random.instant))

           (do random.monad
             [.let [(open "#[0]") /.equivalence]
              expected random.instant]
             (all _.and
                  (_.coverage [/.millis /.of_millis]
                    (|> expected /.millis /.of_millis (#= expected)))
                  (_.coverage [/.relative /.absolute]
                    (|> expected /.relative /.absolute (#= expected)))
                  (_.coverage [/.date /.time /.of_date_time]
                    (#= expected
                        (/.of_date_time (/.date expected)
                                        (/.time expected))))
                  ))
           (do random.monad
             [.let [(open "#[0]") /.equivalence
                    (open "duration#[0]") duration.equivalence]
              from random.instant
              to random.instant]
             (all _.and
                  (_.coverage [/.span]
                    (|> from (/.span from) (duration#= duration.empty)))
                  (_.coverage [/.after]
                    (|> from (/.after (/.span from to)) (#= to)))
                  (_.coverage [/.epoch]
                    (duration#= (/.relative to)
                                (/.span /.epoch to)))
                  ))
           (do random.monad
             [instant random.instant
              .let [d0 (/.day_of_week instant)]]
             (_.coverage [/.day_of_week]
               (let [apply (is (-> (-> Duration Duration) (-> Day Day) Nat Bit)
                               (function (_ polarity move steps)
                                 (let [day_shift (list#mix (function.constant move)
                                                           d0
                                                           (list.repeated steps []))
                                       instant_shift (|> instant
                                                         (/.after (polarity (duration.up steps duration.day)))
                                                         /.day_of_week)]
                                   (day#= day_shift
                                          instant_shift))))]
                 (and (apply function.identity day#succ 0)
                      (apply function.identity day#succ 1)
                      (apply function.identity day#succ 2)
                      (apply function.identity day#succ 3)
                      (apply function.identity day#succ 4)
                      (apply function.identity day#succ 5)
                      (apply function.identity day#succ 6)
                      (apply function.identity day#succ 7)

                      (apply duration.inverse day#pred 0)
                      (apply duration.inverse day#pred 1)
                      (apply duration.inverse day#pred 2)
                      (apply duration.inverse day#pred 3)
                      (apply duration.inverse day#pred 4)
                      (apply duration.inverse day#pred 5)
                      (apply duration.inverse day#pred 6)
                      (apply duration.inverse day#pred 7)))))
           (_.coverage [/.now]
             (when (try (io.run! /.now))
               {try.#Success _}
               true
               
               {try.#Failure _}
               false))
           (do random.monad
             [expected random.instant]
             (_.coverage [/.format /.parser]
               (|> expected
                   /.format
                   (?text.result /.parser)
                   (try#each (at /.equivalence = expected))
                   (try.else false))))
           )))