-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
70 lines (60 loc) · 1.4 KB
/
error_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
package httpkit
import (
"fmt"
"runtime"
"testing"
)
func TestWrapError(t *testing.T) {
t.Run("Caller", func(t *testing.T) {
err := WrapError(fmt.Errorf("test wrap caller"))
f, ok := err.Caller()
if !ok {
t.Fatal("wrap caller not exist")
}
if actual := 11; f.Line != actual {
t.Fatalf("caller line, Expected = %d, Actual = %d", actual, f.Line)
} else if actual := "github.com/joyparty/httpkit.TestWrapError.func1"; f.Function != actual {
t.Fatalf("caller function, Expected = %s, Actual = %s", f.Function, actual)
}
})
}
func TestCaller(t *testing.T) {
cases := []struct {
Caller *runtime.Frame
Function string
}{
{
Caller: calleeFoo(),
Function: "github.com/joyparty/httpkit.calleeFoo",
},
{
Caller: calleeBar(),
Function: "github.com/joyparty/httpkit.calleeBar",
},
{
Caller: calleeFoobar(),
Function: "github.com/joyparty/httpkit.calleeBar",
},
{
Caller: testCaller(),
Function: "github.com/joyparty/httpkit.TestCaller",
},
}
for _, c := range cases {
if expected, actual := c.Function, c.Caller.Function; actual != expected {
t.Fatalf("getCaller(), Expected=%s, Actual=%s", expected, actual)
}
}
}
func calleeFoobar() *runtime.Frame {
return calleeBar()
}
func calleeFoo() *runtime.Frame {
return testCaller()
}
func calleeBar() *runtime.Frame {
return testCaller()
}
func testCaller() *runtime.Frame {
return GetCaller()
}