aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/time.lux
blob: 33ee81f879a0de546e1a2fb42e61de577147d6a1 (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
... 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)
   [abstract
    [monad (.only do)]
    ["[0]" enum
     ["[1]T" \\test]]
    ["[0]" codec
     ["[1]T" \\test]]
    ["[0]" equivalence
     ["[1]T" \\test]]
    ["[0]" order
     ["[1]T" \\test]]]
   [control
    ["[0]" pipe]
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]
     ["<[1]>" \\parser]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 ["[0]" /
  ["[1][0]" date]
  ["[1][0]" day]
  ["[1][0]" duration]
  ["[1][0]" instant]
  ["[1][0]" month]
  ["[1][0]" year]
  ["[1][0]" solar]
  ["[1][0]" series]]
 [\\library
  ["[0]" / (.only)
   ["[0]" duration]]])

(def for_implementation
  Test
  (all _.and
       (_.for [/.equivalence]
              (equivalenceT.spec /.equivalence random.time))
       (_.for [/.order]
              (orderT.spec /.order random.time))
       (_.for [/.enum]
              (enumT.spec /.enum random.time))
       (_.for [/.codec]
              (codecT.spec /.equivalence /.codec random.time))))

(def for_clock
  Test
  (do [! random.monad]
    [expected random.time]
    (all _.and
         (_.coverage [/.clock /.time]
           (|> expected
               /.clock
               /.time
               (try#each (of /.equivalence = expected))
               (try.else false)))
         (let [expected (/.clock expected)]
           (`` (all _.and
                    (,, (with_template [<limit> <slot>]
                          [(_.coverage [<slot>]
                             (n.< <limit> (the <slot> expected)))]

                          [0024 /.#hour]
                          [0060 /.#minute]
                          [0060 /.#second]
                          [1000 /.#milli_second]
                          )))))
         )))

(def for_ranges
  Test
  (do [! random.monad]
    [valid_hour (of ! each (|>> (n.% /.hours) (n.max 10)) random.nat)
     valid_minute (of ! each (|>> (n.% /.minutes) (n.max 10)) random.nat)
     valid_second (of ! each (|>> (n.% /.seconds) (n.max 10)) random.nat)
     valid_milli_second (of ! each (n.% /.milli_seconds) random.nat)

     .let [invalid_hour (|> valid_hour (n.+ /.hours))
           invalid_minute (|> valid_minute (n.+ /.minutes) (n.min 99))
           invalid_second (|> valid_second (n.+ /.seconds) (n.min 99))]]
    (`` (all _.and
             (,, (with_template [<cap> <exception> <prefix> <suffix> <valid> <invalid>]
                   [(_.coverage [<cap> <exception>]
                      (let [valid!
                            (|> <valid>
                                %.nat
                                (text.prefix <prefix>)
                                (text.suffix <suffix>)
                                (of /.codec decoded)
                                (pipe.when
                                  {try.#Success _} true
                                  {try.#Failure error} false))
                            
                            invalid!
                            (|> <invalid>
                                %.nat
                                (text.prefix <prefix>)
                                (text.suffix <suffix>)
                                (of /.codec decoded)
                                (pipe.when
                                  {try.#Success _}
                                  false
                                  
                                  {try.#Failure error}
                                  (exception.match? <exception> error)))]
                        (and valid!
                             invalid!)))]

                   [/.hours /.invalid_hour "" ":00:00.000" valid_hour invalid_hour]
                   [/.minutes /.invalid_minute "00:" ":00.000" valid_minute invalid_minute]
                   [/.seconds /.invalid_second "00:00:" ".000" valid_second invalid_second]
                   ))
             (_.coverage [/.milli_seconds]
               (|> valid_milli_second
                   %.nat
                   (format "00:00:00.")
                   (of /.codec decoded)
                   (pipe.when
                     {try.#Success _} true
                     {try.#Failure error} false)))
             ))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Time])
      (do [! random.monad]
        [.let [day (.nat (duration.millis duration.day))]
         expected random.time

         out_of_bounds (of ! each (|>> /.millis (n.+ day))
                           random.time)]
        (`` (all _.and
                 ..for_implementation

                 (_.coverage [/.millis /.of_millis]
                   (|> expected
                       /.millis
                       /.of_millis
                       (try#each (of /.equivalence = expected))
                       (try.else false)))
                 (_.coverage [/.time_exceeds_a_day]
                   (when (/.of_millis out_of_bounds)
                     {try.#Success _}
                     false
                     
                     {try.#Failure error}
                     (exception.match? /.time_exceeds_a_day error)))
                 (_.coverage [/.midnight]
                   (|> /.midnight
                       /.millis
                       (n.= 0)))
                 (_.coverage [/.parser]
                   (|> expected
                       (of /.codec encoded)
                       (<text>.result /.parser)
                       (try#each (of /.equivalence = expected))
                       (try.else false)))
                 ..for_ranges
                 (_.for [/.Clock]
                        ..for_clock)

                 /date.test
                 /day.test
                 /duration.test
                 /instant.test
                 /month.test
                 /year.test
                 /solar.test
                 /series.test
                 )))))