-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcallstack_test.go
108 lines (90 loc) · 2.35 KB
/
callstack_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
package failure_test
import (
"fmt"
"testing"
"github.com/morikuni/failure/v2"
)
func X() failure.CallStack {
return failure.Callers(0)
}
func TestCallers(t *testing.T) {
fs := X().Frames()
contain(t, fs[0].Path(), "/failure/callstack_test.go")
contain(t, fs[0].File(), "callstack_test.go")
equal(t, fs[0].Func(), "X")
equal(t, fs[0].Line(), 11)
equal(t, fs[0].Pkg(), "v2_test") // is this bug?
equal(t, fs[0].PkgPath(), "github.com/morikuni/failure/v2_test")
contain(t, fs[1].Path(), "/failure/callstack_test.go")
contain(t, fs[1].File(), "callstack_test.go")
equal(t, fs[1].Func(), "TestCallers")
equal(t, fs[1].Line(), 15)
equal(t, fs[1].Pkg(), "v2_test")
equal(t, fs[1].PkgPath(), "github.com/morikuni/failure/v2_test")
}
func TestCallStack_Format(t *testing.T) {
cs := X()
match(t,
fmt.Sprintf("%v", cs),
`v2_test.X: v2_test.TestCallStack_Format: .*`,
)
match(t,
fmt.Sprintf("%s", cs),
`v2_test.X: v2_test.TestCallStack_Format: .*`,
)
match(t,
fmt.Sprintf("%#v", cs),
`\[\]failure.Frame{/.+/failure/callstack_test.go:11, /.+/failure/callstack_test.go:33, .*}`,
)
match(t,
fmt.Sprintf("%+v", cs),
`\[v2_test.X\] /.+/failure/callstack_test.go:11
\[v2_test.TestCallStack_Format\] /.+/failure/callstack_test.go:33
\[.*`,
)
}
func TestFrame_Format(t *testing.T) {
f := X().HeadFrame()
match(t,
fmt.Sprintf("%v", f),
`/.+/failure/callstack_test.go:11`,
)
match(t,
fmt.Sprintf("%s", f),
`/.+/failure/callstack_test.go:11`,
)
match(t,
fmt.Sprintf("%#v", f),
`/.+/failure/callstack_test.go:11`,
)
match(t,
fmt.Sprintf("%+v", f),
`\[v2_test.X\] /.+/failure/callstack_test.go:11`,
)
}
func TestCallStack_Frames(t *testing.T) {
cs := X()
fs := cs.Frames()
equal(t, cs.Frames(), fs)
equal(t, fs[0].Line(), 11)
equal(t, fs[0].Func(), "X")
equal(t, fs[1].Line(), 77)
equal(t, fs[1].Func(), "TestCallStack_Frames")
}
func TestCallStack_HeadFrame(t *testing.T) {
cs := X()
equal(t, cs.Frames()[0], cs.HeadFrame())
}
func TestFrame(t *testing.T) {
f := func() failure.Frame {
return func() failure.Frame {
return failure.Callers(0).HeadFrame()
}()
}()
equal(t, f.Func(), "TestFrame.TestFrame.func1.func2")
equal(t, f.Line(), 98)
equal(t, f.File(), "callstack_test.go")
contain(t, f.Path(), "/failure/callstack_test.go")
equal(t, f.Pkg(), "v2_test")
equal(t, f.PkgPath(), "github.com/morikuni/failure/v2_test")
}