-
Notifications
You must be signed in to change notification settings - Fork 5
/
werror_printer_test.go
91 lines (87 loc) · 3.09 KB
/
werror_printer_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
package werror
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
const stackTraceString = ".*github.com/palantir/witchcraft-go-error.TestErrorFormatting\n" +
".*/werror_printer_test.*\n" +
"testing.tRunner\n" +
".*src/testing/testing.go.*\n" +
"runtime.goexit\n" +
".*src/runtime.*"
func TestErrorFormatting(t *testing.T) {
for _, currCase := range []struct {
name string
err error
expectedRegex string
outputEveryCallingStack bool
}{
{
name: "simple error",
err: ErrorWithContextParams(context.Background(), "simple_error"),
expectedRegex: "" +
"simple_error\n\n" +
stackTraceString,
},
{
name: "simple error with param",
err: ErrorWithContextParams(context.Background(), "simple_error", SafeParam("safeParamKey", "safeParamValue")),
expectedRegex: "" +
"simple_error safeParamKey:safeParamValue\n\n" +
stackTraceString,
},
{
name: "simple error with many params",
err: ErrorWithContextParams(context.Background(), "simple_error", SafeParam("safeParamKey", "safeParamValue"), SafeParam("safeParamKey2", "safeParamValue2")),
expectedRegex: "" +
"simple_error safeParamKey:safeParamValue, safeParamKey2:safeParamValue2\n\n" +
stackTraceString,
},
{
name: "simple wrapped error",
err: WrapWithContextParams(context.Background(), ErrorWithContextParams(context.Background(), "simple_error"), "simple_error_2"),
expectedRegex: "" +
"simple_error_2\n" +
"simple_error\n\n" +
stackTraceString,
},
{
name: "simple wrapped error with forced stacks",
err: WrapWithContextParams(context.Background(), ErrorWithContextParams(context.Background(), "simple_error"), "simple_error_2"),
expectedRegex: "" +
"simple_error_2\n" +
"simple_error\n\n" +
stackTraceString +
"\n" +
stackTraceString,
outputEveryCallingStack: true,
},
{
name: "double wrapped error with params",
err: WrapWithContextParams(context.Background(), WrapWithContextParams(context.Background(),
ErrorWithContextParams(context.Background(), "inner0Message", SafeParam("inner0ParamKey", "inner0VParamValue"), SafeParam("inner0ParamKey1", "inner0VParamValue1"), SafeParam("inner0ParamKey2", "inner0VParamValue2")),
"inner1Message", SafeParam("inner1ParamKey", "inner1ValueKey")), "inner2Message"),
expectedRegex: "" +
"inner2Message\n" +
"inner1Message inner1ParamKey:inner1ValueKey\n" +
"inner0Message inner0ParamKey:inner0VParamValue, inner0ParamKey1:inner0VParamValue1, inner0ParamKey2:inner0VParamValue2\n\n" +
stackTraceString,
},
{
name: "error with pointer params",
err: ErrorWithContextParams(context.Background(), "simple_error",
SafeParam("key1", &[]string{"value"}[0]),
SafeParam("key2", &[]int{42}[0]),
SafeParam("key3", (*string)(nil)),
),
expectedRegex: "" +
"simple_error key1:value, key2:42, key3:<nil>\n\n" +
stackTraceString,
},
} {
t.Run(currCase.name, func(t *testing.T) {
assert.Regexp(t, currCase.expectedRegex, GenerateErrorString(currCase.err, currCase.outputEveryCallingStack))
})
}
}