-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexception_test.go
180 lines (158 loc) · 4.77 KB
/
exception_test.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package exception
import (
"fmt"
"testing"
)
func TestTryGetsExecuted(t *testing.T) {
var hasTryExecuted bool
Try(func() {
hasTryExecuted = true
}).
Catch(nil, func(excep *Exception) {}).
Run()
if hasTryExecuted == false {
t.Fatal("Try was not executed")
}
}
func TestCatchGetsExecutedForAllErrorsIfExceptionThrownInTry(t *testing.T) {
var exceptionType ExceptionType
var hasCaughtException bool
Try(func() {
Throw(ReferenceError("Dummy error text"))
}).
Catch(nil, func(excep *Exception) {
hasCaughtException = true
exceptionType = excep.Type
}).Run()
if hasCaughtException == false {
t.Fatal("Try was not executed")
}
if exceptionType != ReferenceErrorType {
t.Fatalf("Expecting %s but found %s", ReferenceErrorType, exceptionType)
}
}
func TestFinallyGetsExecutedAlways(t *testing.T) {
var hasFinallyExecuted bool
Try(func() {
Throw(NetworkError())
}).
Catch(nil, func(excep *Exception) {}).
Finally(func() {
hasFinallyExecuted = true
}).
Run()
if hasFinallyExecuted == false {
t.Fatal("Finally was not executed")
}
}
func TestExpectedCatchBlockGetsExecutedForDefinedExceptionType(t *testing.T) {
var thrownExceptionType ExceptionType
var caughtFrom string
Try(func() {
user := map[string]string{
"name": "John Doe",
}
_, ok := user["email"]
if !ok {
Throw(LookupError("Email doesn't exist"))
}
}).
Catch(In(LookupErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "LookupErrorHandler"
}).
Catch(In(ReferenceErrorType, IndexErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "ReferenceErrorHandler"
}).
Catch(nil, func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "DefaultExceptionHandler"
}).Run()
if thrownExceptionType != LookupErrorType {
t.Fatalf("Expecting Exception type to be %s but found %s", LookupErrorType, thrownExceptionType)
}
if caughtFrom != "LookupErrorHandler" {
t.Fatalf("Expecting Catch block to be %s but found %s", "LookupErrorHandler", caughtFrom)
}
}
func TestDefaultCatchBlockGetsExecutedForUnmatchedException(t *testing.T) {
var thrownExceptionType ExceptionType
var caughtFrom string
Try(func() {
Throw(New(UnknownErrorType, "Unknown Error"))
}).
Catch(In(LookupErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "LookupErrorHandler"
}).
Catch(In(ReferenceErrorType, IndexErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "ReferenceErrorHandler"
}).
Catch(nil, func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "DefaultExceptionHandler"
}).Run()
if thrownExceptionType != UnknownErrorType {
t.Fatalf("Expecting Exception type to be %s but found %s", UnknownErrorType, thrownExceptionType)
}
if caughtFrom != "DefaultExceptionHandler" {
t.Fatalf("Expecting Catch block to be %s but found %s", "LookupErrorHandler", caughtFrom)
}
}
func TestAllPanicGetsRecoveredWithinTryCatch(t *testing.T) {
var panicAttack = "Something went very wrong!"
var caughtMessage string
Try(func() {
panic(panicAttack)
}).
Catch(nil, func(excep *Exception) {
caughtMessage = excep.Message
}).
Run()
if caughtMessage != panicAttack {
t.Fatal("Could not recover panic attack!")
}
}
func TestNestedExceptionWasHandledAsExpected(t *testing.T) {
var firstThrownExceptionType ExceptionType
var secondThrownExceptionType ExceptionType
var hasCaughtNestedException bool
var CustomExceptionType ExceptionType = "CustomException"
Try(func() {
Throw(ReferenceError("Dummy error text"))
}).
Catch(nil, func(excep1 *Exception) {
Try(func() {
// trying to save the world
Throw(New(CustomExceptionType, "Custome Error Message"))
}).Catch(In(CustomExceptionType), func(excep2 *Exception) {
firstThrownExceptionType = excep1.Type
secondThrownExceptionType = excep2.Type
hasCaughtNestedException = true
}).Run()
}).Run()
if hasCaughtNestedException == false {
t.Fatal("Nested exception was not handled")
}
if firstThrownExceptionType != ReferenceErrorType {
t.Fatalf("Expecting first exception to be %s but found %s", ReferenceErrorType, firstThrownExceptionType)
}
if secondThrownExceptionType != CustomExceptionType {
t.Fatalf("Expecting second exception to be %s but found %s", CustomExceptionType, secondThrownExceptionType)
}
}
func TestPanicIsRecoveredInDefaultCatch(t *testing.T) {
errorMsg := "I'm gonna panic but don't worry"
var caughtErrorMsg string
Try(func() {
panic(errorMsg)
}).Catch(nil, func(excep *Exception) {
caughtErrorMsg = excep.Message
fmt.Println("I knew you are gonna catch me :p", excep.Message)
}).Run()
if caughtErrorMsg != errorMsg {
t.Fatalf("Expecting error message to be %s but found %s", errorMsg, caughtErrorMsg)
}
}