-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
411 lines (345 loc) · 9.47 KB
/
util_test.go
File metadata and controls
411 lines (345 loc) · 9.47 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package errors
import (
"fmt"
"testing"
)
// --- renderStackByPolicy ---
func TestRenderStackByPolicy_Detailed(t *testing.T) {
SetPolicy(PolicyDetailed)
raw := []byte("goroutine 1 [running]:\nsome.func()\n\t/path/file.go:10 +0x1")
result := renderStackByPolicy(raw)
if result == "" {
t.Error("expected non-empty stack")
}
SetPolicy(PolicyNormal)
}
func TestRenderStackByPolicy_Normal(t *testing.T) {
SetPolicy(PolicyNormal)
raw := []byte("goroutine 1 [running]:\nsome.func()\n\t/path/file.go:10 +0x1")
result := renderStackByPolicy(raw)
if result == "" {
t.Error("expected non-empty stack")
}
}
func TestRenderStackByPolicy_Native(t *testing.T) {
SetPolicy(PolicyNative)
raw := []byte("goroutine 1 [running]:\nsome.func()\n\t/path/file.go:10 +0x1")
result := renderStackByPolicy(raw)
_ = result // may be empty after filtering
SetPolicy(PolicyNormal)
}
func TestRenderStackByPolicy_Default(t *testing.T) {
// Use an invalid policy value to hit the default branch
policy.Store(999)
raw := []byte("some stack")
result := renderStackByPolicy(raw)
if result == "" {
t.Error("expected non-empty stack for default policy")
}
SetPolicy(PolicyNormal)
}
// --- normalizeStack ---
func TestNormalizeStack_Empty(t *testing.T) {
result := normalizeStack([]byte(" "))
if result != "<empty>" {
t.Errorf("expected '<empty>', got '%s'", result)
}
}
func TestNormalizeStack_NonEmpty(t *testing.T) {
result := normalizeStack([]byte("some stack trace"))
if result != "some stack trace" {
t.Errorf("expected 'some stack trace', got '%s'", result)
}
}
// --- filterBoilerplateFrames ---
func TestFilterBoilerplateFrames_NoFrames(t *testing.T) {
raw := []byte("no frames here")
result := filterBoilerplateFrames(raw, 0)
// fallback: returns raw
if string(result) != "no frames here" {
t.Errorf("expected raw fallback, got '%s'", result)
}
}
func TestFilterBoilerplateFrames_WithBoilerplate(t *testing.T) {
raw := []byte("runtime.goexit()\n\t/usr/local/go/src/runtime/asm.s:1650 +0x1\nmyapp.myFunc()\n\t/app/main.go:42 +0x1")
result := filterBoilerplateFrames(raw, 0)
_ = result
}
func TestFilterBoilerplateFrames_KeepLast(t *testing.T) {
raw := []byte("myapp.funcA()\n\t/app/a.go:10 +0x1\nmyapp.funcB()\n\t/app/b.go:20 +0x1\nmyapp.funcC()\n\t/app/c.go:30 +0x1")
result := filterBoilerplateFrames(raw, 1)
_ = result
}
// --- looksLikeFileLine ---
func TestLooksLikeFileLine_True(t *testing.T) {
if !looksLikeFileLine("\t/path/to/file.go:42 +0x1") {
t.Error("expected true")
}
}
func TestLooksLikeFileLine_False(t *testing.T) {
if looksLikeFileLine("some.func()") {
t.Error("expected false")
}
}
// --- isBoilerplate ---
func TestIsBoilerplate_Runtime(t *testing.T) {
if !isBoilerplate("runtime.goexit()", "\t/usr/local/go/src/runtime/asm.s:1650") {
t.Error("expected true for runtime boilerplate")
}
}
func TestIsBoilerplate_UserCode(t *testing.T) {
if isBoilerplate("myapp.myFunc()", "\t/app/main.go:42") {
t.Error("expected false for user code")
}
}
func TestIsBoilerplate_Testing(t *testing.T) {
if !isBoilerplate("testing.tRunner()", "\t/usr/local/go/src/testing/testing.go:1234") {
t.Error("expected true for testing boilerplate")
}
}
// --- escapeSpecialChars ---
func TestEscapeSpecialChars(t *testing.T) {
input := "line1\nline2\r\nline3\ttab"
result := escapeSpecialChars(input)
expected := `line1\nline2\r\nline3\ttab`
if result != expected {
t.Errorf("expected '%s', got '%s'", expected, result)
}
}
// --- callerInfos ---
func TestCallerInfos(t *testing.T) {
file, line, funcName := callerInfos(1)
if file == "" {
t.Error("expected non-empty file")
}
if line == "" {
t.Error("expected non-empty line")
}
if funcName == "" {
t.Error("expected non-empty funcName")
}
}
// --- buildMessage ---
func TestBuildMessage_Normal(t *testing.T) {
result := buildMessage("hello", "world")
if result == "" {
t.Error("expected non-empty message")
}
}
func TestBuildMessage_Empty(t *testing.T) {
result := buildMessage()
if result != "<empty>" {
t.Errorf("expected '<empty>', got '%s'", result)
}
}
func TestBuildMessage_WithError(t *testing.T) {
err := New("inner error")
result := buildMessage(err)
if result == "" {
t.Error("expected non-empty message from error")
}
}
// --- buildMessageByFormat ---
func TestBuildMessageByFormat(t *testing.T) {
result := buildMessageByFormat("value is %s", "42")
if result != "value is 42" {
t.Errorf("expected 'value is 42', got '%s'", result)
}
}
// --- cleanMessage ---
func TestCleanMessage_RemovesKeywords(t *testing.T) {
input := "error [CODE] [METADATA] [STACK] [CAUSE] here"
result := cleanMessage(input)
for _, kw := range []string{"[CODE]", "[METADATA]", "[STACK]", "[CAUSE]"} {
if contains(result, kw) {
t.Errorf("expected keyword '%s' to be removed", kw)
}
}
}
func TestCleanMessage_ReplacesNewlines(t *testing.T) {
result := cleanMessage("line1\nline2")
if contains(result, "\n") {
t.Error("expected newlines to be replaced")
}
}
func contains(s, sub string) bool {
return len(s) >= len(sub) && (s == sub || len(s) > 0 && containsStr(s, sub))
}
func containsStr(s, sub string) bool {
for i := 0; i <= len(s)-len(sub); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
// --- filterMsg ---
func TestFilterMsg_WithString(t *testing.T) {
result := filterMsg("hello")
if len(result) != 1 {
t.Fatalf("expected 1 result, got %d", len(result))
}
}
func TestFilterMsg_WithError(t *testing.T) {
err := New("filter error")
result := filterMsg(err)
if len(result) != 1 {
t.Fatalf("expected 1 result, got %d", len(result))
}
}
func TestFilterMsg_WithNilError(t *testing.T) {
var err error
result := filterMsg(err)
if len(result) != 1 {
t.Fatalf("expected 1 result, got %d", len(result))
}
}
// --- formatFuncName ---
func TestFormatFuncName(t *testing.T) {
result := formatFuncName("github.com/user/pkg.MyFunc")
if result != "MyFunc" {
t.Errorf("expected 'MyFunc', got '%s'", result)
}
}
// --- toString / toStringWithErr ---
func TestToString_String(t *testing.T) {
result := toString("hello")
if result != "hello" {
t.Errorf("expected 'hello', got '%s'", result)
}
}
func TestToString_Int(t *testing.T) {
result := toString(42)
if result != "42" {
t.Errorf("expected '42', got '%s'", result)
}
}
func TestToString_Float(t *testing.T) {
result := toString(3.14)
if result == "" {
t.Error("expected non-empty float string")
}
}
func TestToString_Bool(t *testing.T) {
result := toString(true)
if result != "true" {
t.Errorf("expected 'true', got '%s'", result)
}
}
func TestToString_Bytes(t *testing.T) {
result := toString([]byte("bytes"))
if result != "bytes" {
t.Errorf("expected 'bytes', got '%s'", result)
}
}
func TestToString_Map(t *testing.T) {
result := toString(map[string]int{"a": 1})
if result == "" {
t.Error("expected non-empty map string")
}
}
func TestToString_Struct(t *testing.T) {
type S struct{ X int }
result := toString(S{X: 1})
if result == "" {
t.Error("expected non-empty struct string")
}
}
func TestToString_Nil(t *testing.T) {
result := toString(nil)
_ = result // should not panic
}
func TestToString_Stringer(t *testing.T) {
type myStringer struct{}
// use fmt.Stringer via a known type
result := toString(fmt.Errorf("stringer error"))
if result != "stringer error" {
t.Errorf("expected 'stringer error', got '%s'", result)
}
}
func TestToString_NilPointer(t *testing.T) {
var p *int
result := toString(p)
_ = result // should not panic
}
func TestToString_IntVariants(t *testing.T) {
cases := []any{int8(1), int16(2), int32(3), int64(4)}
for _, c := range cases {
result := toString(c)
if result == "" {
t.Errorf("expected non-empty string for %T", c)
}
}
}
func TestToString_UintVariants(t *testing.T) {
cases := []any{uint(1), uint8(2), uint16(3), uint32(4), uint64(5), uintptr(6)}
for _, c := range cases {
result := toString(c)
if result == "" {
t.Errorf("expected non-empty string for %T", c)
}
}
}
func TestToString_Complex(t *testing.T) {
result := toString(complex(1, 2))
if result == "" {
t.Error("expected non-empty complex string")
}
}
func TestToString_Array(t *testing.T) {
result := toString([3]int{1, 2, 3})
if result == "" {
t.Error("expected non-empty array string")
}
}
func TestToString_ByteArray(t *testing.T) {
result := toString([3]byte{'a', 'b', 'c'})
if result != "abc" {
t.Errorf("expected 'abc', got '%s'", result)
}
}
func TestToStringWithErr_Nil(t *testing.T) {
_, err := toStringWithErr(nil)
if err == nil {
t.Error("expected error for nil input")
}
}
func TestToStringWithErr_NilPointer(t *testing.T) {
var p *int
_, err := toStringWithErr(p)
if err == nil {
t.Error("expected error for nil pointer")
}
}
func TestToStringWithErr_UnsupportedType(t *testing.T) {
// channel is unsupported
ch := make(chan int)
_, err := toStringWithErr(ch)
if err == nil {
t.Error("expected error for unsupported type")
}
}
// --- implementsStringer / implementsError ---
func TestImplementsStringer_Nil(t *testing.T) {
if implementsStringer(nil) {
t.Error("expected false for nil type")
}
}
func TestImplementsError_Nil(t *testing.T) {
if implementsError(nil) {
t.Error("expected false for nil type")
}
}
// --- optionalField ---
func TestOptionalField_Empty(t *testing.T) {
result := optionalField("label: ", "")
if result != "" {
t.Errorf("expected empty string, got '%s'", result)
}
}
func TestOptionalField_NonEmpty(t *testing.T) {
result := optionalField("label: ", "value")
if result != "label: value" {
t.Errorf("expected 'label: value', got '%s'", result)
}
}