-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_test.go
80 lines (66 loc) · 1.97 KB
/
stack_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
package sterrors
import (
"fmt"
"reflect"
"testing"
)
func callFake() *Caller {
return caller(2)
}
func TestCallStack(t *testing.T) {
SetDefaultCreateErrorFunc(func() Error {
return &BaseError{}
})
initErr := fmt.Errorf("initial error")
secErr, secondCall := E("second error", initErr), callFake()
thirdErr, thirdCall := E("third error", secErr), callFake()
callStack := CallStack(thirdErr)
if len(callStack) != 3 {
t.Errorf("callstack has not lenth 3")
}
if "third error" != callStack[0].ErrMessage {
t.Errorf("third callstack error message is not equal to third error")
}
if "second error" != callStack[1].ErrMessage {
t.Errorf("second callstack error message is not equal to second error")
}
if "initial error" != callStack[2].ErrMessage {
t.Errorf("first callstack error message is not equal to initial error")
}
var nilCaller *Caller
if !reflect.DeepEqual(thirdCall, callStack[0].Caller) {
t.Errorf("third caller is not equal to actual first caller")
}
if !reflect.DeepEqual(secondCall, callStack[1].Caller) {
t.Errorf("second caller is not equal to actual second caller")
}
if !reflect.DeepEqual(nilCaller, callStack[2].Caller) {
t.Errorf("first caller is not equal to nilCaller")
}
}
func TestCallStack_WithNotTraceableErr(t *testing.T) {
err := fmt.Errorf("initial error")
callStack := CallStack(err)
if len(callStack) != 1 {
t.Errorf("callstack has not lenth 1")
}
if "initial error" != callStack[0].ErrMessage {
t.Errorf("first callstack error message is not equal to initial error")
}
}
func TestCallStack_WithErrWithoutCause(t *testing.T) {
err := E("initial error")
callStack := CallStack(err)
if len(callStack) != 1 {
t.Errorf("callstack has not lenth 1")
}
if "initial error" != callStack[0].ErrMessage {
t.Errorf("first callstack error message is not equal to initial error")
}
}
func TestCallStack_WithNil(t *testing.T) {
callStack := CallStack(nil)
if len(callStack) != 0 {
t.Errorf("callstack has not lenth 0")
}
}