-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderror_test.go
96 lines (78 loc) · 2.32 KB
/
derror_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
package errors
import (
"errors"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDerror_Format(t *testing.T) {
var f2 = func() error {
return New(testCode(true), MD{K: "var1", V: "a string"}, MD{K: "var2", V: 10})
}
// nolint:gocritic
var f1 = func() error {
return f2()
}
var (
err = f1()
derr = err.(derror)
)
t.Run("code and message (%s)", func(t *testing.T) {
var (
s = fmt.Sprintf("%s", err)
exp = fmt.Sprintf("%s: %s", derr.c.String(), derr.c.Message())
)
assert.Equal(t, exp, s)
})
t.Run("code, message, id and metadata (%v)", func(t *testing.T) {
var (
s = fmt.Sprintf("%v", err)
exp = fmt.Sprintf("%s: %s\n\tid: %s\n\tmetadata: %v",
derr.c.String(), derr.c.Message(), derr.id, derr.mds,
)
)
assert.Equal(t, exp, s)
})
t.Run("code, message, id, metadata and call stack (%+v)", func(t *testing.T) {
var (
s = fmt.Sprintf("%+v", err)
exp = fmt.Sprintf("%s: %s\n\tid: %s\n\tmetadata: %v\n\tcall stack:\n%+v",
derr.c.String(), derr.c.Message(), derr.id, derr.mds, derr.cs,
)
)
assert.Equal(t, exp, s)
})
t.Run("code, message, id, metadata and call stack (compacted) (%+-v)", func(t *testing.T) {
var (
s = fmt.Sprintf("%+-v", err)
exp = fmt.Sprintf("%s: %s\n\tid: %s\n\tmetadata: %v\n\tcall stack (compacted):\n%-v",
derr.c.String(), derr.c.Message(), derr.id, derr.mds, derr.cs,
)
)
assert.Equal(t, exp, s)
})
t.Run("code, message, id, metadata, wrapped error and call stack (%+v)", func(t *testing.T) {
var (
err = Wrap(errors.New("some external error"), testCode(true), MD{K: "var1", V: "a string"})
e = err.(derror)
s = fmt.Sprintf("%+v", e)
exp = fmt.Sprintf("%s: %s\n\tid: %s\n\tmetadata: %v\n\twrapped error: %+v\n\tcall stack:\n%+v",
e.c.String(), e.c.Message(), e.id, e.mds, e.werr, e.cs,
)
)
assert.Equal(t, exp, s)
})
t.Run("any other verb", func(t *testing.T) {
var verbs = [...]string{
"t", "b", "c", "d", "o", "q", "x", "X", "U", "e", "E", "f", "F", "g", "G", "q", "p",
}
var v = fmt.Sprintf("%%%s", verbs[rand.Intn(len(verbs))])
var s = fmt.Sprintf(v, err)
assert.Empty(t, s)
})
}
func TestDerror_Error(t *testing.T) {
var err = New(testCode(true), MD{K: "var1", V: "a string"}, MD{K: "var2", V: 10})
assert.Equal(t, fmt.Sprintf("%s", err), err.Error())
}