-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcept_test.go
More file actions
280 lines (248 loc) · 8.06 KB
/
except_test.go
File metadata and controls
280 lines (248 loc) · 8.06 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package except
import (
"testing"
)
// getValue is a mock function for testing
func getValue() int {
return 50 // Not 100, will trigger assertion error
}
func TestTryGetsExecuted(t *testing.T) {
var hasTryExecuted bool
e := New()
e.Try(func() {
hasTryExecuted = true
}).
Catch(nil, func(excep *Exception) {}).
Run()
if !hasTryExecuted {
t.Fatal("Try was not executed")
}
}
func TestCatchGetsExecutedForAllErrorsIfExceptionThrownInTry(t *testing.T) {
var exceptionType ExceptionType
var hasCaughtException bool
e := New()
e.Try(func() {
e.Throw(e.ReferenceError("Dummy error text"))
}).
Catch(nil, func(excep *Exception) {
hasCaughtException = true
exceptionType = excep.Type
}).Run()
if !hasCaughtException {
t.Fatal("Exception was not caught")
}
if exceptionType != ReferenceErrorType {
t.Fatalf("Expecting %s but found %s", ReferenceErrorType, exceptionType)
}
}
func TestFinallyGetsExecutedAlways(t *testing.T) {
var hasFinallyExecuted bool
e := New()
e.Try(func() {
e.Throw(e.NetworkError())
}).
Catch(nil, func(excep *Exception) {}).
Finally(func() {
hasFinallyExecuted = true
}).
Run()
if !hasFinallyExecuted {
t.Fatal("Finally was not executed")
}
}
func TestExpectedCatchBlockGetsExecutedForDefinedExceptionType(t *testing.T) {
var thrownExceptionType ExceptionType
var caughtFrom string
e := New()
e.Try(func() {
user := map[string]string{
"name": "John Doe",
}
_, ok := user["email"]
if !ok {
e.Throw(e.LookupError("Email doesn't exist"))
}
}).
Catch(e.In(LookupErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "LookupErrorHandler"
}).
Catch(e.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
e := New()
e.Try(func() {
e.Throw(e.NewException(UnknownErrorType, "Unknown Error"))
}).
Catch(e.In(LookupErrorType), func(excep *Exception) {
thrownExceptionType = excep.Type
caughtFrom = "LookupErrorHandler"
}).
Catch(e.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", "DefaultExceptionHandler", caughtFrom)
}
}
func TestAllPanicGetsRecoveredWithinTryCatch(t *testing.T) {
var panicAttack = "Something went very wrong!"
var caughtMessage string
e := New()
e.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"
e1 := New()
e1.Try(func() {
e1.Throw(e1.ReferenceError("Dummy error text"))
}).
Catch(nil, func(excep1 *Exception) {
e2 := New()
e2.Try(func() {
// trying to save the world
e2.Throw(e2.NewException(CustomExceptionType, "Custom Error Message"))
}).Catch(e2.In(CustomExceptionType), func(excep2 *Exception) {
firstThrownExceptionType = excep1.Type
secondThrownExceptionType = excep2.Type
hasCaughtNestedException = true
}).Run()
}).Run()
if !hasCaughtNestedException {
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
e := New()
e.Try(func() {
panic(errorMsg)
}).Catch(nil, func(excep *Exception) {
caughtErrorMsg = excep.Message
// Debug: Panic successfully caught and recovered
}).Run()
if caughtErrorMsg != errorMsg {
t.Fatalf("Expecting error message to be %s but found %s", errorMsg, caughtErrorMsg)
}
}
// Test the exact example from the user's requirement
func TestUserExampleFromRequirement(t *testing.T) {
var assertionErrorCaught bool
var finallyExecuted bool
var caughtMessage string
var caughtType ExceptionType
e := New()
e.Try(func() {
data := getValue() // get me the value from Allions
if data != 100 {
e.Throw(e.AssertionError("Expected value is not the same as 100"))
}
}).
Catch(e.In(AssertionErrorType, ValueErrorType), func(excep *Exception) {
// Debug: Assertion error caught successfully
// Message: excep.Message
// Type: excep.Type
// Stack trace available in excep.StackTrace
assertionErrorCaught = true
caughtMessage = excep.Message
caughtType = excep.Type
}).
Catch(nil, func(excep *Exception) {
// Debug: Fallback handler executed
}).
Finally(func() {
// Debug: Finally block executed for cleanup
finallyExecuted = true
}).
Run()
if !assertionErrorCaught {
t.Fatal("AssertionError was not caught")
}
if !finallyExecuted {
t.Fatal("Finally block was not executed")
}
if caughtType != AssertionErrorType {
t.Fatalf("Expected AssertionErrorType but got %s", caughtType)
}
if caughtMessage != "Expected value is not the same as 100" {
t.Fatalf("Expected specific error message but got: %s", caughtMessage)
}
}
// Test all exception types
func TestAllExceptionTypes(t *testing.T) {
e := New()
tests := []struct {
name string
exceptionFunc func() *Exception
expectedType ExceptionType
}{
{"AssertionError", func() *Exception { return e.AssertionError("test") }, AssertionErrorType},
{"IndexError", func() *Exception { return e.IndexError("test") }, IndexErrorType},
{"RuntimeError", func() *Exception { return e.RuntimeError("test") }, RuntimeErrorType},
{"ValueError", func() *Exception { return e.ValueError("test") }, ValueErrorType},
{"NetworkError", func() *Exception { return e.NetworkError("test") }, NetworkErrorType},
{"SyntaxError", func() *Exception { return e.SyntaxError("test") }, SyntaxErrorType},
{"PermissionError", func() *Exception { return e.PermissionError("test") }, PermissionErrorType},
{"TimeoutError", func() *Exception { return e.TimeoutError("test") }, TimeoutErrorType},
{"TypeError", func() *Exception { return e.TypeError("test") }, TypeErrorType},
{"ConnectionError", func() *Exception { return e.ConnectionError("test") }, ConnectionErrorType},
{"ReferenceError", func() *Exception { return e.ReferenceError("test") }, ReferenceErrorType},
{"EOFError", func() *Exception { return e.EOFError("test") }, EOFErrorType},
{"LookupError", func() *Exception { return e.LookupError("test") }, LookupErrorType},
{"UnknownError", func() *Exception { return e.UnknownError("test") }, UnknownErrorType},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exception := tt.exceptionFunc()
if exception.Type != tt.expectedType {
t.Errorf("Expected type %s, got %s", tt.expectedType, exception.Type)
}
if exception.Message != "test" {
t.Errorf("Expected message 'test', got %s", exception.Message)
}
})
}
}