-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
91 lines (83 loc) · 2.45 KB
/
format_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 serrors_test
import (
"errors"
"fmt"
"runtime"
"strings"
"testing"
"github.com/Eun/serrors"
)
func TestError_Error(t *testing.T) {
testCases := []struct {
name string
error error
expectedErrorText string
}{
{
name: "normal error",
error: serrors.New("some error"),
expectedErrorText: "some error",
},
{
name: "normal error with fields",
error: serrors.New("some error").With("k", "v"),
expectedErrorText: "some error",
},
{
name: "wrapped",
error: fmt.Errorf("error1: %w", serrors.New("error2").With("k", "v")),
expectedErrorText: "error1: error2[k=v]",
},
{
name: "wraps another error",
error: serrors.Wrap(errors.New("error2"), "error1"),
expectedErrorText: "error1: error2",
},
{
name: "no error text",
error: serrors.New(""),
expectedErrorText: "error",
},
{
name: "wraps a nil error",
error: serrors.Wrap(nil, "some error"),
expectedErrorText: "some error",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Equal(t, tc.expectedErrorText, tc.error.Error())
})
}
}
func TestError_Format(t *testing.T) {
_, filename, _, ok := runtime.Caller(0)
Equal(t, true, ok)
cause := serrors.New("error 1").With("k1", "v1") // [TestError_Format01]
err := serrors.Wrap(cause, "error 2").With("k2", "v2") // [TestError_Format00]
t.Run("normal string", func(t *testing.T) {
Equal(t, "error 2: error 1", fmt.Sprintf("%s", err))
})
t.Run("quoted string", func(t *testing.T) {
Equal(t, `"error 2: error 1"`, fmt.Sprintf("%q", err))
})
t.Run("verbose", func(t *testing.T) {
Equal(t, "error 2: error 1[k1=v1 k2=v2]", fmt.Sprintf("%v", err))
})
t.Run("extra verbose", func(t *testing.T) {
expected := fmt.Sprintf("error 2\n[k2=v2]\n%s\nerror 1\n[k1=v1]\n%s\n",
generateExpectedStack(t, filename, "TestError_Format00"),
generateExpectedStack(t, filename, "TestError_Format01"),
)
Equal(t, expected, fmt.Sprintf("%+v", err))
})
}
func generateExpectedStack(t *testing.T, filename string, markers ...string) string {
parts := make([]string, len(markers))
for i, marker := range markers {
frame := buildStackFrameFromMarker(t, filename, marker)
parts[i] = fmt.Sprintf("%s\n\t%s:%d", frame.Func, frame.File, frame.Line)
}
return strings.Join(parts, "\n")
}