-
Notifications
You must be signed in to change notification settings - Fork 2
/
scenarios_test.exs
146 lines (131 loc) · 3 KB
/
scenarios_test.exs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
defmodule ErrorHandlingBlogpostTest do
@moduledoc """
Running this test file will result in a lot of errors and failing tests and
that is expected. Most of these tests are about proving that certain
combinations of errors and error handling are not compatible.
This is just a way to organize the handlers (rescue, catch) against the
triggers (raise, throw, etc).
"""
use ExUnit.Case
describe "catch/2" do
# raise & catch - compatible
test "is compatible with raise" do
try do
raise "raise error"
catch
:error, %RuntimeError{message: "raise error"} ->
:ok
end
end
# throw & catch - compatible
test "is compatible with throw" do
try do
throw "throw error"
catch
:throw, "throw error" ->
:ok
end
end
# exit & catch - compatible
test "is compatible with exit" do
try do
exit("exiting")
catch
:exit, "exiting" ->
:ok
end
end
# :erlang.error & catch - compatible
test "is compatible with :erlang.error" do
try do
:erlang.error("erlang error")
catch
:error, "erlang error" ->
:ok
end
end
end
describe "rescue/1" do
# raise & rescue - compatible
test "is compatible with raise" do
try do
raise "raise error"
rescue
e ->
%RuntimeError{message: "raise error"} = e
:ok
end
end
# throw & rescue - not compatible
test "is not compatible with throw" do
try do
throw "throw error"
rescue
_e ->
# Never going to reach here
nil
end
end
# exit & rescue - not compatible
test "is not compatible with exit" do
try do
exit("exiting")
rescue
_e ->
# Never going to reach here
nil
end
end
# :erlang.error & rescue - compatible
test "is compatible with :erlang.error" do
try do
:erlang.error("erlang error")
rescue
e ->
%ErlangError{original: "erlang error"} = e
:ok
end
end
end
describe "catch/1" do
# raise & catch - not compatible
test "is not compatible with raise" do
try do
raise "raise error"
catch
_e ->
# Never going to reach here
nil
end
end
# throw & catch - compatible
test "is compatible with throw" do
try do
throw "throw error"
catch
"throw error" ->
:ok
end
end
# exit & catch - not compatible
test "is not compatible with exit" do
try do
exit("exiting")
catch
_e ->
# Never going to reach here
nil
end
end
# :erlang.error & catch - not compatible
test "is not compatible with :erlang.error" do
try do
:erlang.error("erlang error")
catch
_e ->
# Never going to reach here
nil
end
end
end
end