aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/time/month.lux
blob: 4db1303b3344532bf864923658a0e31bfa01da59 (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
(.module:
  [library
   [lux "*"
    ["_" test {"+" [Test]}]
    [abstract
     [monad {"+" [do]}]
     ["[0]" predicate]
     [\\specification
      ["$[0]" equivalence]
      ["$[0]" hash]
      ["$[0]" order]
      ["$[0]" enum]
      ["$[0]" codec]]]
    [control
     ["[0]" try ("[1]\[0]" functor)]
     ["[0]" exception]]
    [data
     [collection
      ["[0]" set]
      ["[0]" list ("[1]\[0]" functor mix)]]]
    [math
     ["[0]" random {"+" [Random]}]
     [number
      ["n" nat]]]]]
  [\\library
   ["[0]" /
    [//
     ["[0]" duration]]]])

(def: .public random
  (Random /.Month)
  (let [december (/.number #/.December)]
    (|> random.nat
        (\ random.monad each (|>> (n.% december) ++))
        (random.one (|>> /.by_number try.maybe)))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Month])
      ($_ _.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))

          (do random.monad
            [expected ..random
             invalid (random.only (predicate.or (n.< (/.number #/.January))
                                                (n.> (/.number #/.December)))
                                  random.nat)]
            ($_ _.and
                (_.cover [/.number /.by_number]
                         (|> expected
                             /.number
                             /.by_number
                             (try\each (\ /.equivalence = expected))
                             (try.else false)))
                (_.cover [/.invalid_month]
                         (case (/.by_number invalid)
                           {#try.Failure error}
                           (exception.match? /.invalid_month error)
                           
                           {#try.Success _}
                           false))
                (_.cover [/.year]
                         (let [all (list.size /.year)
                               uniques (set.size (set.of_list /.hash /.year))]
                           (and (n.= (/.number #/.December)
                                     all)
                                (n.= all
                                     uniques))))
                (_.cover [/.days]
                         (let [expected (.nat (duration.ticks duration.day duration.normal_year))]
                           (|> /.year
                               (list\each /.days)
                               (list\mix n.+ 0)
                               (n.= expected))))
                (_.cover [/.leap_year_days]
                         (let [expected (.nat (duration.ticks duration.day duration.leap_year))]
                           (|> /.year
                               (list\each /.leap_year_days)
                               (list\mix n.+ 0)
                               (n.= expected))))
                (do random.monad
                  [not_a_month (random.ascii/upper 1)]
                  (_.cover [/.not_a_month_of_the_year]
                           (case (\ /.codec decoded not_a_month)
                             {#try.Failure error}
                             (exception.match? /.not_a_month_of_the_year error)
                             
                             {#try.Success _}
                             false)))
                )))))