-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
153 lines (130 loc) · 3.49 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
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
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
"bytes"
stderrors "errors"
"fmt"
"strings"
"testing"
"time"
"github.com/go-pogo/errors/internal"
"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"
)
func TestWithFormatter(t *testing.T) {
t.Run("nil", func(t *testing.T) {
assert.Exactly(t, nil, WithFormatter(nil))
})
t.Run("std error", func(t *testing.T) {
err := stderrors.New("some err")
have := WithFormatter(err)
assert.Implements(t, (*xerrors.Formatter)(nil), have)
assert.Same(t, err, Unembed(have))
})
tests := map[string]error{
"error": New("whoops"),
"multiErr": newMultiErr([]error{New("hi"), stderrors.New("there")}, 0),
"with exit code": WithExitCode(New("cause"), 1),
"with formatter": WithFormatter(New("cause")),
// "with stack": WithStack(fmt.Errorf("failure: %w", stderrors.New("my bad"))),
"with time": WithTime(New("cause"), time.Now()),
}
for name, have := range tests {
t.Run(name, func(t *testing.T) {
assert.Implements(t, (*xerrors.Formatter)(nil), have)
})
}
}
func TestFormatError(t *testing.T) {
internal.DisableTraceStack()
defer internal.EnableTraceStack()
tests := map[string]struct {
err error
want1, want2 string
}{
"nil": {},
"std error": {
err: stderrors.New("some err"),
want1: "some err",
want2: "some err",
},
"error": {
err: New("oh noes"),
want1: "oh noes",
want2: "oh noes",
},
"wrap wrap": {
err: Wrap(Wrap(New("some err"), "failure"), "whoops"),
want1: "whoops: failure: some err",
want2: "whoops:\n - failure:\n - some err",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// without details
t.Run("plain", func(t *testing.T) {
var state fmtStateHelper
FormatError(tc.err, &state, 'v')
assert.Exactly(t, tc.want1, state.String())
if tc.err != nil {
assert.Exactly(t, tc.want1, fmt.Sprintf("%v", tc.err))
}
})
// with details
t.Run("details", func(t *testing.T) {
state := fmtStateHelper{flags: "+"}
FormatError(tc.err, &state, 'v')
assert.Exactly(t, tc.want2, state.String())
if tc.err != nil {
assert.Exactly(t, tc.want2, fmt.Sprintf("%+v", tc.err))
}
})
})
}
}
func TestPrintError(t *testing.T) {
tests := map[string]struct {
err error
want string
}{
"nil": {},
"std error": {
err: stderrors.New("some err"),
want: "some err",
},
"error": {
err: New("oh noes"),
want: "oh noes",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var printer fmtStateHelper
PrintError(&printer, tc.err)
assert.Exactly(t, tc.want, printer.String())
printer.Reset()
printer.flags = "+"
PrintError(&printer, tc.err)
have := printer.String()
assert.Truef(t, strings.HasPrefix(have, tc.want), "should have prefix `%s`", tc.want)
})
}
}
type fmtStateHelper struct {
flags string
bytes.Buffer
}
func (ts *fmtStateHelper) Width() (int, bool) { return 0, false }
func (ts *fmtStateHelper) Precision() (int, bool) { return 0, false }
func (ts *fmtStateHelper) Flag(f int) bool {
return strings.ContainsRune(ts.flags, rune(f))
}
func (ts *fmtStateHelper) Detail() bool { return ts.Flag('+') }
func (ts *fmtStateHelper) Print(args ...interface{}) {
_, _ = fmt.Fprint(ts, args...)
}
func (ts *fmtStateHelper) Printf(f string, args ...interface{}) {
_, _ = fmt.Fprintf(ts, f, args...)
}