aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/time/instant.lux
blob: f2e44cead0d43a3f6244cef2cdce2a3b9992d02e (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
(.module:
  [lux #*
   ["%" data/text/format (#+ format)]
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do Monad)]
    {[0 #spec]
     [/
      ["$." equivalence]
      ["$." order]
      ["$." enum]
      ["$." codec]]}]
   [control
    pipe]
   [data
    ["." text]
    [number
     ["i" int]]]
   [math
    ["r" random (#+ Random)]]
   [time
    ["@d" duration]
    ["@date" date]]]
  [//
   ["_." duration]]
  {1
   ["." / (#+ Instant)]})

(def: boundary Int +99,999,999,999,999)

(def: #export instant
  (Random Instant)
  (:: r.monad map (|>> (i.% boundary) /.from-millis) r.int))

(def: #export test
  Test
  (<| (_.context (%.name (name-of /._)))
      ($_ _.and
          ($equivalence.spec /.equivalence ..instant)
          ($order.spec /.order ..instant)
          ($enum.spec /.enum ..instant)
          ## TODO; Uncomment ASAP
          ## ($codec.spec /.equivalence /.codec ..instant)

          (do r.monad
            [millis r.int]
            (_.test "Can convert from/to milliseconds."
                    (|> millis /.from-millis /.to-millis (i.= millis))))
          (do r.monad
            [sample instant
             span _duration.duration
             #let [(^open "@/.") /.equivalence
                   (^open "@d/.") @d.equivalence]]
            ($_ _.and
                (_.test "The span of a instant and itself has an empty duration."
                        (|> sample (/.span sample) (@d/= @d.empty)))
                (_.test "Can shift a instant by a duration."
                        (|> sample (/.shift span) (/.span sample) (@d/= span)))
                (_.test "Can obtain the time-span between the epoch and an instant."
                        (|> sample /.relative /.absolute (@/= sample)))
                (_.test "All instants are relative to the epoch."
                        (|> /.epoch (/.shift (/.relative sample)) (@/= sample)))))
          )))