-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace_test.go
More file actions
222 lines (199 loc) · 4.81 KB
/
stacktrace_test.go
File metadata and controls
222 lines (199 loc) · 4.81 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package logging
import (
"errors"
"fmt"
"reflect"
"strings"
"testing"
)
func TestNewStack(t *testing.T) {
type Expected struct {
frameN int
file string
name string
line int
}
type Test struct {
title string
callback func() StackTrace
expected Expected
}
// Anonymous func1
inner := func() StackTrace {
return NewStackTrace(2)
}
// Anonymous func2
outer := func() StackTrace {
return inner()
}
var tests = []Test{
{
title: "inner frame",
callback: inner,
expected: Expected{
frameN: 0,
file: "/stacktrace_test.go",
name: "func1",
line: 27,
},
},
{
title: "outer frame",
callback: outer,
expected: Expected{
frameN: 1,
file: "/stacktrace_test.go",
name: "func2",
line: 32,
},
},
}
for _, test := range tests {
tmp := test
t.Run(tmp.title, func(t *testing.T) {
t.Parallel()
stack := tmp.callback()
if len(stack.Frames)+1 < tmp.expected.frameN {
t.Fatalf("specified frameN is not within valid stack frame range\ngot: %d\nwant: <%d", len(stack.Frames), tmp.expected.frameN)
}
if stack.String() == "" {
t.Error("StackTrace string must generate string")
}
frame := stack.Frames[tmp.expected.frameN]
file := frame.File
line := frame.Line
name := frame.Func.Name()
i := strings.LastIndex(name, ".")
if i != -1 {
name = name[i+1:]
}
expect := tmp.expected.file
if expect != "" && !strings.HasSuffix(file, expect) {
t.Errorf("frame has file name suffix\ngot: %s\nwant: %s", file, expect)
}
expectI := tmp.expected.line
if expectI != 0 && line != expectI {
t.Errorf("frame has incorrect call line number\ngot: %d\nwant: %d", line, expectI)
}
expect = tmp.expected.name
if expect != "" && name != expect {
t.Errorf("frame has incorrect function name\ngot: %s\nwant: %s", name, expect)
}
})
}
}
func TestMarshalStack(t *testing.T) {
type Expected = []stackInfo
type Test struct {
title string
stacktrace error
expected Expected
}
var tests = []Test{
{
title: "wrong error",
stacktrace: fmt.Errorf("wrong error"),
expected: nil,
},
{
title: "empty stacktrace error",
stacktrace: StackTraceError{},
expected: []stackInfo{},
},
{
title: "valid stacktrace error",
stacktrace: NewStackTraceError("valid error"),
expected: []stackInfo{
[]frameInfo{
{
"file": "/stacktrace_test.go",
"function": "TestMarshalStack",
"line": "122",
},
{
"function": "tRunner",
},
{
"function": "goexit",
},
},
},
},
{
title: "joined stacktrace errors",
stacktrace: errors.Join(
NewStackTraceError("valid error 1"),
fmt.Errorf("wrong error"),
NewStackTraceError("valid error 2"),
),
expected: []stackInfo{
[]frameInfo{
{
"file": "/stacktrace_test.go",
"function": "TestMarshalStack",
"line": "142",
},
{
"function": "tRunner",
},
{
"function": "goexit",
},
},
[]frameInfo{
{
"file": "/stacktrace_test.go",
"function": "TestMarshalStack",
"line": "144",
},
{
"function": "tRunner",
},
{
"function": "goexit",
},
},
},
},
}
for _, test := range tests {
tmp := test
t.Run(tmp.title, func(t *testing.T) {
t.Parallel()
data := marshalStack(tmp.stacktrace)
if tmp.expected == nil {
if data != nil {
t.Fatalf("marshalled result value is incorrect\ngot: %+v\nwant: nil", data)
}
} else {
stackInfos, ok := data.([]stackInfo)
if !ok {
t.Fatalf("marshalled result is of the wrong type\ngot: %v\nwant: [][]map[string]string", reflect.TypeOf(data))
}
if len(tmp.expected) != len(stackInfos) {
t.Fatalf("stackInfo array has incorrect length\ngot: %d\nwant: %d", len(stackInfos), len(tmp.expected))
}
for i, info := range stackInfos {
expectInfo := tmp.expected[i]
if len(expectInfo) != len(info) {
t.Fatalf("stackInfo %d has incorrect length\ngot: %d\nwant: %d", i, len(info), len(expectInfo))
}
for j, frameInfo := range info {
expect := expectInfo[j]["file"]
if expect != "" && !strings.HasSuffix(frameInfo["file"], expect) {
t.Errorf("frameInfo has incorrect file name suffix\ngot: %s\nwant: %s", frameInfo["file"], expect)
}
expect = expectInfo[j]["line"]
if expect != "" && expect != frameInfo["line"] {
t.Errorf("frameInfo has incorrect call line number\ngot: %s\nwant: %s", frameInfo["line"], expect)
}
expect = expectInfo[j]["function"]
if expect != "" && expect != frameInfo["function"] {
t.Errorf("frameInfo has incorrect function name\ngot: %s\nwant: %s", frameInfo["function"], expect)
}
}
}
}
})
}
}