-
Notifications
You must be signed in to change notification settings - Fork 1
/
try_catch_test.go
58 lines (52 loc) · 1.25 KB
/
try_catch_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
package m3lsh
import (
"reflect"
"testing"
)
func TestTryCatch(t *testing.T) {
TryCatch(func() {
Throw(&testException{}, "TEST")
}, Catcher(&testException{}, func(ex interface{}) {
if reflect.TypeOf(ex) != reflect.TypeOf(&testException{}) {
t.Fatal("Wrong exception type")
}
e := ex.(*testException)
if e.Message != "TEST" {
t.Error("Exception has wrong message")
}
if len(e.Stacktrace()) <= 1 {
t.Error("Stack trace too short")
}
}), Catcher(&BaseException{}, func(ex interface{}) {
t.Error("Called wrong catcher")
}))
}
func TestTryCatchPanic(t *testing.T) {
TryCatch(func() {
panic("NO")
}, Catcher(&testException{}, func(ex interface{}) {
t.Error("Called wrong catcher")
}), Catcher(&BaseException{}, func(ex interface{}) {
if reflect.TypeOf(ex) != reflect.TypeOf(&BaseException{}) {
t.Fatal("Wrong exception type")
}
e := ex.(*BaseException)
if e.Message != "NO" {
t.Error("Exception has wrong message")
}
if len(e.Stacktrace()) <= 1 {
t.Error("Stack trace too short")
}
}))
}
func TestTryCatchNoCatcher(t *testing.T) {
defer func() {
recover()
}()
TryCatch(func() {
panic("WO?")
}, Catcher(&testException{}, func(ex interface{}) {
t.Fatal("Called wrong catcher")
}))
t.Fatal("Did not panic")
}