aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/exception.lux
blob: 13bde092b7c56c0a8bca6e239bbc28d3c9f41da5 (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: .public test
  Test
  (do {! random.monad}
    [expected random.nat
     wrong (|> random.nat (random.only (|>> (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 [/.except]
                     (case (/.except ..an_exception [])
                       (#try.Success _) false
                       (#try.Failure _) true))
            (_.cover [/.error]
                     (case (/.except ..an_exception [])
                       (#try.Success _)
                       false
                       
                       (#try.Failure message)
                       (text\= message (/.error ..an_exception []))))
            (_.cover [/.match?]
                     (/.match? ..an_exception
                               (/.error ..an_exception [])))
            (_.cover [/.assertion]
                     (case (/.assertion ..an_exception [] assertion_succeeded?)
                       (#try.Success _)
                       assertion_succeeded?
                       
                       (#try.Failure message)
                       (and (not assertion_succeeded?)
                            (text\= message (/.error ..an_exception [])))))
            (_.cover [/.catch]
                     (and (n.= expected
                               (|> (/.except ..an_exception [])
                                   (/.catch ..an_exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))
                          (n.= expected
                               (|> (/.except ..another_exception [])
                                   (/.catch ..an_exception (function (_ ex) wrong))
                                   (/.catch ..another_exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))))
            (_.cover [/.otherwise]
                     (n.= expected
                          (|> (/.except ..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 [/.listing]
                     (let [enumeration (/.listing %.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 (/.error ..an_exception [])))
                          (case (/.with ..an_exception []
                                  (: (Try Nat)
                                     (/.except ..another_exception [])))
                            (#try.Success _)
                            false
                            
                            (#try.Failure message)
                            (and (text.contains? (/.error ..an_exception []) message)
                                 (text.contains? (/.error ..another_exception []) message)))))
            (_.cover [/.exception:]
                     (case (/.except ..custom_exception [expected])
                       (#try.Success _)
                       false
                       
                       (#try.Failure message)
                       (and (text.contains? ..label message)
                            (text.contains? (%.nat expected) message))))
            ))))