aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/exception.lux
blob: f62ad927116accdad34e61af9c26c5245f1700a4 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]]
    [data
     ["." text ("#\." equivalence)
      ["%" format (#+ format)]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ exception:)
    [//
     ["." try (#+ Try)]]]])

(exception: an-exception)
(exception: another-exception)

(def: label "YOLO")
(exception: (custom-exception {value Nat})
  (/.report [label (%.nat value)]))

(def: #export test
  Test
  (do {! random.monad}
    [expected random.nat
     wrong (|> random.nat (random.filter (|>> (n.= expected) not)))
     assertion-succeeded? random.bit
     #let [report-element (\ ! map %.nat random.nat)]
     field0 report-element
     value0 report-element
     field1 report-element
     value1 report-element]
    (<| (_.covering /._)
        (_.for [/.Exception])
        ($_ _.and
            (_.cover [/.return]
                     (case (/.return expected)
                       (#try.Success actual) (n.= expected actual)
                       (#try.Failure _) false))
            (_.cover [/.throw]
                     (case (/.throw ..an-exception [])
                       (#try.Success _) false
                       (#try.Failure _) true))
            (_.cover [/.construct]
                     (case (/.throw ..an-exception [])
                       (#try.Success _)
                       false
                       
                       (#try.Failure message)
                       (text\= message (/.construct ..an-exception []))))
            (_.cover [/.match?]
                     (/.match? ..an-exception
                               (/.construct ..an-exception [])))
            (_.cover [/.assert]
                     (case (/.assert ..an-exception [] assertion-succeeded?)
                       (#try.Success _)
                       assertion-succeeded?
                       
                       (#try.Failure message)
                       (and (not assertion-succeeded?)
                            (text\= message (/.construct ..an-exception [])))))
            (_.cover [/.catch]
                     (and (n.= expected
                               (|> (/.throw ..an-exception [])
                                   (/.catch ..an-exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))
                          (n.= expected
                               (|> (/.throw ..another-exception [])
                                   (/.catch ..an-exception (function (_ ex) wrong))
                                   (/.catch ..another-exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))))
            (_.cover [/.otherwise]
                     (n.= expected
                          (|> (/.throw ..another-exception [])
                              (/.catch ..an-exception (function (_ ex) wrong))
                              (/.otherwise (function (_ ex) expected)))))
            (_.cover [/.report]
                     (let [report (/.report [field0 value0]
                                            [field1 value1])]
                       (and (text.contains? field0 report)
                            (text.contains? value0 report)
                            (text.contains? field1 report)
                            (text.contains? value1 report))))
            (_.cover [/.enumerate]
                     (let [enumeration (/.enumerate %.text (list field0 value0 field1 value1))]
                       (and (text.contains? field0 enumeration)
                            (text.contains? value0 enumeration)
                            (text.contains? field1 enumeration)
                            (text.contains? value1 enumeration))))
            (_.cover [/.with]
                     (and (case (/.with ..an-exception [] (#try.Success expected))
                            (#try.Success actual) (n.= expected actual)
                            (#try.Failure _) false)
                          (case (/.with ..an-exception [] (#try.Failure ""))
                            (#try.Success _) false
                            (#try.Failure message) (text\= message (/.construct ..an-exception [])))
                          (case (/.with ..an-exception []
                                  (: (Try Nat)
                                     (/.throw ..another-exception [])))
                            (#try.Success _)
                            false
                            
                            (#try.Failure message)
                            (and (text.contains? (/.construct ..an-exception []) message)
                                 (text.contains? (/.construct ..another-exception []) message)))))
            (_.cover [/.exception:]
                     (case (/.throw ..custom-exception [expected])
                       (#try.Success _)
                       false
                       
                       (#try.Failure message)
                       (and (text.contains? ..label message)
                            (text.contains? (%.nat expected) message))))
            ))))