-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.go
105 lines (96 loc) · 2.1 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
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
package errors
import (
"fmt"
"github.com/stretchr/testify/assert"
"runtime"
"strconv"
"testing"
)
var testFrame = frame()
type X struct{}
// val returns a Frame pointing to itself.
func (x X) val() Frame {
return frame()
}
// ptr returns a Frame pointing to itself.
func (x *X) ptr() Frame {
return frame()
}
func TestFrame_String(t *testing.T) {
s := frame()
assert.Equal(t, "github.com/psi59/errors.TestFrame_String(stack_test.go:26)", s.String())
assert.Equal(t, "github.com/psi59/errors.init(stack_test.go:11)", testFrame.String())
var x X
assert.Equal(t, "github.com/psi59/errors.X.val(stack_test.go:17)", x.val().String())
assert.Equal(t, "github.com/psi59/errors.(*X).ptr(stack_test.go:22)", x.ptr().String())
}
func TestFrame_Format(t *testing.T) {
var x X
tests := []struct {
frame Frame
format string
want string
}{
{
frame: x.ptr(),
format: "%v",
want: "github.com/psi59/errors.(*X).ptr(stack_test.go:22)",
},
{
frame: x.ptr(),
format: "%+v",
want: "\n\tat github.com/psi59/errors.(*X).ptr(stack_test.go:22)",
},
}
for _, tt := range tests {
t.Run(tt.frame.String(), func(t *testing.T) {
got := fmt.Sprintf(tt.format, tt.frame)
assert.Equal(t, tt.want, got)
})
}
}
func frame() Frame {
pc, _, _, _ := runtime.Caller(1)
return Frame{pc: pc}
}
func TestStackTrace_Format(t *testing.T) {
var x X
tests := []struct {
stack StackTrace
format string
want string
}{
{
stack: StackTrace{
x.ptr(),
x.val(),
},
format: "%v",
want: "",
},
{
stack: StackTrace{
x.ptr(),
x.val(),
},
format: "%+v",
want: "\n\tat github.com/psi59/errors.(*X).ptr(stack_test.go:22)" +
"\n\tat github.com/psi59/errors.X.val(stack_test.go:17)",
},
{
stack: StackTrace{
x.ptr(),
testFrame,
},
format: "%+v",
want: "\n\tat github.com/psi59/errors.(*X).ptr(stack_test.go:22)" +
"\n\tat github.com/psi59/errors.init(stack_test.go:11)",
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
got := fmt.Sprintf(tt.format, tt.stack)
assert.Equal(t, tt.want, got)
})
}
}