aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/exception.lux
blob: ad9ef59fb262e1ef3faca4e0be9cb13f8782a4e6 (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
(.using
 [library
  [lux "*"
   ["_" test {"+" Test}]
   [abstract
    [monad {"+" do}]]
   [data
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format {"+" format}]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]]]
 [\\library
  ["[0]" / {"+" exception:}
   [//
    ["[0]" 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 (# ! each %.nat random.nat)]
     field0 report_element
     value0 report_element
     field1 report_element
     value1 report_element]
    (<| (_.covering /._)
        (_.for [/.Exception])
        ($_ _.and
            (_.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 [/.when]
                     (and (n.= expected
                               (|> (/.except ..an_exception [])
                                   (/.when ..an_exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))
                          (n.= expected
                               (|> (/.except ..another_exception [])
                                   (/.when ..an_exception (function (_ ex) wrong))
                                   (/.when ..another_exception (function (_ ex) expected))
                                   (/.otherwise (function (_ ex) wrong))))))
            (_.cover [/.otherwise]
                     (n.= expected
                          (|> (/.except ..another_exception [])
                              (/.when ..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 []
                                  (is (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))))
            ))))