-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller_test.go
103 lines (86 loc) · 2.34 KB
/
caller_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
package xo
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetCaller(t *testing.T) {
func() {
caller := GetCaller(0, 0)
assert.Len(t, caller.Stack, 4)
assert.Equal(t, Caller{
Short: "xo.TestGetCaller.func1",
Full: "github.com/256dpi/xo.TestGetCaller.func1",
File: "github.com/256dpi/xo/caller_test.go",
Line: 12,
Stack: caller.Stack,
}, caller)
caller = GetCaller(1, 2)
assert.Len(t, caller.Stack, 2)
assert.Equal(t, Caller{
Short: "xo.TestGetCaller",
Full: "github.com/256dpi/xo.TestGetCaller",
File: "github.com/256dpi/xo/caller_test.go",
Line: 31,
Stack: caller.Stack,
}, caller)
}()
}
func TestCallerDrop(t *testing.T) {
func() {
caller := GetCaller(0, 0)
caller.Drop(1)
assert.Len(t, caller.Stack, 3)
assert.Equal(t, Caller{
Short: "xo.TestCallerDrop",
Full: "github.com/256dpi/xo.TestCallerDrop",
File: "github.com/256dpi/xo/caller_test.go",
Line: 46,
Stack: caller.Stack,
}, caller)
}()
}
func TestCallerIncludes(t *testing.T) {
parent := GetCaller(1, 0)
child := GetCaller(0, 0)
assert.False(t, parent.Includes(child, 0))
assert.True(t, parent.Includes(parent, 0))
assert.True(t, child.Includes(child, 0))
assert.True(t, child.Includes(parent, 0))
parent = GetCaller(0, 0)
child = func() Caller {
return GetCaller(0, 0)
}()
assert.False(t, parent.Includes(child, 0))
assert.True(t, parent.Includes(parent, 0))
assert.True(t, child.Includes(child, 0))
assert.False(t, child.Includes(parent, 0))
assert.True(t, child.Includes(parent, 1))
}
func TestCallerFormat(t *testing.T) {
caller := GetCaller(0, 0)
str := caller.String()
assert.Equal(t, "xo.TestCallerFormat", str)
str = fmt.Sprintf("%s", caller)
assert.Equal(t, "xo.TestCallerFormat", str)
str = fmt.Sprintf("%q", caller)
assert.Equal(t, `"xo.TestCallerFormat"`, str)
str = fmt.Sprintf("%v", caller)
assert.Equal(t, "github.com/256dpi/xo.TestCallerFormat", str)
str = fmt.Sprintf("%+v", caller)
assert.Equal(t, []string{
"> github.com/256dpi/xo.TestCallerFormat",
"> github.com/256dpi/xo/caller_test.go:LN",
"> testing.tRunner",
"> testing/testing.go:LN",
"> runtime.goexit",
"> runtime/asm_" + arch + ".s:LN",
}, splitStackTrace(str))
}
func BenchmarkGetCaller(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
GetCaller(0, 0)
}
}