aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/time/day.lux
blob: f1bcc932d9b47c6fd65aca9d3e2c289ff9cc3b52 (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
(.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]" list]
      ["[0]" set]]]
    [math
     ["[0]" random {"+" [Random]} ("[1]\[0]" monad)]
     [number
      ["n" nat]]]]]
  [\\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)]
        ($_ _.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
              [not_a_day (random.ascii/upper 1)]
              (_.cover [/.not_a_day_of_the_week]
                       (case (\ /.codec decoded not_a_day)
                         (#try.Failure error)
                         (exception.match? /.not_a_day_of_the_week error)
                         
                         (#try.Success _)
                         false)))
            (_.cover [/.number /.by_number]
                     (|> expected
                         /.number
                         /.by_number
                         (try\each (\ /.equivalence = expected))
                         (try.else false)))
            (_.cover [/.invalid_day]
                     (case (/.by_number invalid)
                       (#try.Failure error)
                       (exception.match? /.invalid_day error)
                       
                       (#try.Success _)
                       false))
            (_.cover [/.week]
                     (let [all (list.size /.week)
                           uniques (set.size (set.of_list /.hash /.week))]
                       (and (n.= (/.number #/.Saturday)
                                 all)
                            (n.= all
                                 uniques))))
            ))))