-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstack_test.go
108 lines (97 loc) · 1.73 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
106
107
108
// Copyright 2015 Dave Cheney <[email protected]>. All rights reserved.
// Use of this source code (or at least parts of it) is governed by a BSD-style
// license that can be found in the LICENSE_THIRD_PARTY file.
package errors
import (
"runtime"
"testing"
)
func TestStackTrace(t *testing.T) {
tests := []struct {
stack *stack
want []string
}{
{
callers(0),
[]string{
"emperror.dev/errors.TestStackTrace\n" +
"\t.+/stack_test.go:18",
},
},
}
for i, tt := range tests {
st := tt.stack.StackTrace()
for j, want := range tt.want {
testFormatRegexp(t, i, st[j], "%+v", want)
}
}
}
func stackTrace() StackTrace {
const depth = 8
var pcs [depth]uintptr
n := runtime.Callers(1, pcs[:])
var st stack = pcs[0:n]
return st.StackTrace()
}
func TestStackTraceFormat(t *testing.T) {
tests := []struct {
StackTrace
format string
want string
}{{
nil,
"%s",
`\[\]`,
}, {
nil,
"%v",
`\[\]`,
}, {
nil,
"%+v",
"",
}, {
nil,
"%#v",
`\[\]errors.Frame\(nil\)`,
}, {
make(StackTrace, 0),
"%s",
`\[\]`,
}, {
make(StackTrace, 0),
"%v",
`\[\]`,
}, {
make(StackTrace, 0),
"%+v",
"",
}, {
make(StackTrace, 0),
"%#v",
`\[\]errors.Frame{}`,
}, {
stackTrace()[:2],
"%s",
`\[stack_test.go stack_test.go\]`,
}, {
stackTrace()[:2],
"%v",
`\[stack_test.go:39 stack_test.go:88\]`,
}, {
stackTrace()[:2],
"%+v",
"\n" +
"emperror.dev/errors.stackTrace\n" +
"\t.+/stack_test.go:39\n" +
"emperror.dev/errors.TestStackTraceFormat\n" +
"\t.+/stack_test.go:92",
}, {
stackTrace()[:2],
"%#v",
`\[\]errors.Frame{stack_test.go:39, stack_test.go:100}`,
}}
for i, tt := range tests {
testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want)
}
}