aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/time/day.lux
blob: 6f1ea4b3dba5a42f6580a01466b062ebb9a50280 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" hash]
     ["$[0]" order]
     ["$[0]" enum]
     ["$[0]" codec]]]
   [control
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]
    [function
     ["[0]" predicate]]]
   [data
    [collection
     ["[0]" list]
     ["[0]" set]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]])

(def .public random
  (Random /.Day)
  (random.either (random.either (random.either (random#in {/.#Sunday})
                                               (random#in {/.#Monday}))
                                (random.either (random#in {/.#Tuesday})
                                               (random#in {/.#Wednesday})))
                 (random.either (random.either (random#in {/.#Thursday})
                                               (random#in {/.#Friday}))
                                (random#in {/.#Saturday}))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Day])
      (do random.monad
        [expected ..random
         invalid (random.only (predicate.or (n.< (/.number {/.#Sunday}))
                                            (n.> (/.number {/.#Saturday})))
                              random.nat)])
      (`` (all _.and
               (_.for [/.equivalence]
                      ($equivalence.spec /.equivalence ..random))
               (_.for [/.hash]
                      ($hash.spec /.hash ..random))
               (_.for [/.order]
                      ($order.spec /.order ..random))
               (_.for [/.enum]
                      ($enum.spec /.enum ..random))
               (_.for [/.codec]
                      ($codec.spec /.equivalence /.codec ..random))

               (,, (with_template [<before> <current> <after>]
                     [(_.coverage [<current>]
                        (and (at /.equivalence = {<before>} (at /.enum pred {<current>}))
                             (at /.equivalence = {<after>} (at /.enum succ {<current>}))))]

                     [/.#Saturday /.#Sunday /.#Monday]
                     [/.#Sunday /.#Monday /.#Tuesday]
                     [/.#Monday /.#Tuesday /.#Wednesday]
                     [/.#Tuesday /.#Wednesday /.#Thursday]
                     [/.#Wednesday /.#Thursday /.#Friday]
                     [/.#Thursday /.#Friday /.#Saturday]
                     [/.#Friday /.#Saturday /.#Sunday]
                     ))
               (do random.monad
                 [not_a_day (random.upper_cased 1)]
                 (_.coverage [/.not_a_day_of_the_week]
                   (when (at /.codec decoded not_a_day)
                     {try.#Failure error}
                     (exception.match? /.not_a_day_of_the_week error)
                     
                     {try.#Success _}
                     false)))
               (_.coverage [/.number /.by_number]
                 (|> expected
                     /.number
                     /.by_number
                     (try#each (at /.equivalence = expected))
                     (try.else false)))
               (_.coverage [/.invalid_day]
                 (when (/.by_number invalid)
                   {try.#Failure error}
                   (exception.match? /.invalid_day error)
                   
                   {try.#Success _}
                   false))
               (_.coverage [/.week]
                 (let [all (list.size /.week)
                       uniques (set.size (set.of_list /.hash /.week))]
                   (and (n.= (/.number {/.#Saturday})
                             all)
                        (n.= all
                             uniques))))
               ))))