aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/exception.lux
blob: 434b2eae086198371cc1f2abf46faf37899d53eb (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["[0]" try (.only Try)]]]])

(/.def an_exception)
(/.def another_exception)

(def label "YOLO")
(/.def (custom_exception value)
  (/.Exception Nat)
  (/.report (list [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 (of ! each %.nat random.nat)]
     field0 report_element
     value0 report_element
     field1 report_element
     value1 report_element]
    (<| (_.covering /._)
        (_.for [/.Exception
                /.#constructor /.#label])
        (all _.and
             (_.coverage [/.except]
               (when (/.except ..an_exception [])
                 {try.#Success _} false
                 {try.#Failure _} true))
             (_.coverage [/.error]
               (when (/.except ..an_exception [])
                 {try.#Success _}
                 false
                 
                 {try.#Failure message}
                 (text#= message (/.error ..an_exception []))))
             (_.coverage [/.match?]
               (/.match? ..an_exception
                         (/.error ..an_exception [])))
             (_.coverage [/.assertion]
               (when (/.assertion ..an_exception [] assertion_succeeded?)
                 {try.#Success _}
                 assertion_succeeded?
                 
                 {try.#Failure message}
                 (and (not assertion_succeeded?)
                      (text#= message (/.error ..an_exception [])))))
             (_.coverage [/.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))))))
             (_.coverage [/.otherwise]
               (n.= expected
                    (|> (/.except ..another_exception [])
                        (/.when ..an_exception (function (_ ex) wrong))
                        (/.otherwise (function (_ ex) expected)))))
             (_.coverage [/.report]
               (let [report (/.report (list [field0 value0]
                                            [field1 value1]))]
                 (and (text.contains? field0 report)
                      (text.contains? value0 report)
                      (text.contains? field1 report)
                      (text.contains? value1 report))))
             (_.coverage [/.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))))
             (_.coverage [/.with]
               (and (when (/.with ..an_exception [] {try.#Success expected})
                      {try.#Success actual} (n.= expected actual)
                      {try.#Failure _} false)
                    (when (/.with ..an_exception [] {try.#Failure ""})
                      {try.#Success _} false
                      {try.#Failure message} (text#= message (/.error ..an_exception [])))
                    (when (/.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)))))
             (_.coverage [/.def]
               (when (/.except ..custom_exception [expected])
                 {try.#Success _}
                 false
                 
                 {try.#Failure message}
                 (and (text.contains? ..label message)
                      (text.contains? (%.nat expected) message))))
             ))))