aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/exception.lux
blob: 46d495a4b75c0d187e82b140d5024b0dd0d4be98 (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
(.module:
  [lux #*
   [abstract/monad (#+ do)]
   [data
    [text
     ["%" format (#+ format)]]]
   [math
    ["r" random]]
   ["_" test (#+ Test)]]
  {1
   ["." / (#+ exception:)]})

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

(def: #export test
  (do r.monad
    [right r.nat
     wrong (r.filter (|>> (n/= right) not) r.nat)]
    (<| (_.context (%.name (name-of /.Exception)))
        ($_ _.and
            (_.test "Can catch exceptions."
                    (n/= right
                         (|> (/.throw an-exception [])
                             (/.catch an-exception (function (_ ex) right))
                             (/.otherwise (function (_ ex) wrong)))))
            (_.test "Can catch multiple exceptions."
                    (n/= right
                         (|> (/.throw another-exception [])
                             (/.catch an-exception (function (_ ex) wrong))
                             (/.catch another-exception (function (_ ex) right))
                             (/.otherwise (function (_ ex) wrong)))))
            (_.test "Can handle uncaught exceptions."
                    (n/= right
                         (|> (/.throw another-exception [])
                             (/.catch an-exception (function (_ ex) wrong))
                             (/.otherwise (function (_ ex) right)))))))))