-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_err_test.go
75 lines (62 loc) · 1.49 KB
/
t_err_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
package sqlb
import (
"fmt"
"io"
"testing"
)
type FakeTracedErr string
func (self FakeTracedErr) Error() string { return string(self) }
func (self FakeTracedErr) Format(out fmt.State, _ rune) {
try1(io.WriteString(out, self.Error()))
if out.Flag('+') {
if self != `` {
try1(io.WriteString(out, `; `))
}
try1(io.WriteString(out, `fake stack trace`))
return
}
}
func Benchmark_errf(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
_ = errf(`error %v`, `message`)
}
}
func Benchmark_fmt_Errorf(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
_ = fmt.Errorf(`error %v`, `message`)
}
}
func TestErr_formatting(t *testing.T) {
test := func(src Err, expBase, expPlus string) {
eq(t, expBase, src.Error())
eq(t, expBase, fmt.Sprintf(`%v`, src))
eq(t, expPlus, fmt.Sprintf(`%+v`, src))
}
test(Err{}, ``, ``)
test(
Err{While: `doing some operation`},
`[sqlb] error while doing some operation`,
`[sqlb] error while doing some operation`,
)
test(
Err{Cause: ErrStr(`some cause`)},
`[sqlb] error: some cause`,
`[sqlb] error: some cause`,
)
test(
Err{
While: `doing some operation`,
Cause: ErrStr(`some cause`),
},
`[sqlb] error while doing some operation: some cause`,
`[sqlb] error while doing some operation: some cause`,
)
test(
Err{
While: `doing some operation`,
Cause: FakeTracedErr(`some cause`),
},
`[sqlb] error while doing some operation: some cause`,
`[sqlb] error while doing some operation: some cause; fake stack trace`,
)
}