This repository has been archived by the owner on Aug 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap_context_test.go
92 lines (81 loc) · 2.14 KB
/
wrap_context_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
package emperror
import (
"fmt"
"io"
"testing"
"github.com/pkg/errors"
)
func TestWrapWith_Format(t *testing.T) {
testError := WrapWith(io.EOF, "error1", "key", "value")
tests := []struct {
error
format string
want string
}{{
WrapWith(errors.New("error"), "error2", "key", "value"),
"%s",
"error2: error",
}, {
WrapWith(errors.New("error"), "error2", "key", "value"),
"%v",
"error2: error",
}, {
WrapWith(errors.New("error"), "error2", "key", "value"),
"%+v",
"error\n" +
"github.com/goph/emperror.TestWrapWith_Format\n" +
"\t.+/wrap_context_test.go:27",
}, {
WrapWith(io.EOF, "error", "key", "value"),
"%s",
"error: EOF",
}, {
WrapWith(io.EOF, "error", "key", "value"),
"%v",
"error: EOF",
}, {
WrapWith(io.EOF, "error", "key", "value"),
"%+v",
"EOF\n" +
"error\n" +
"github.com/goph/emperror.TestWrapWith_Format\n" +
"\t.+/wrap_context_test.go:41",
}, {
WrapWith(WrapWith(io.EOF, "error1"), "error2", "key", "value"),
"%+v",
"EOF\n" +
"error1\n" +
"github.com/goph/emperror.TestWrapWith_Format\n" +
"\t.+/wrap_context_test.go:48\n",
}, {
WrapWith(fmt.Errorf("error with space"), "context", "key", "value"),
"%q",
`"context: error with space"`,
}, {
WrapWith(testError, "error2", "key", "value"),
"%+v",
"EOF\n" +
"error1\n" +
"github.com/goph/emperror.TestWrapWith_Format\n" +
"\t.+/wrap_context_test.go:12\n",
}}
for i, tt := range tests {
testFormatRegexp(t, i, tt.error, tt.format, tt.want)
}
}
func TestWrapWith_Context(t *testing.T) {
err := errors.New("error")
kvs := []interface{}{"a", 123}
err = WrapWith(err, "error2", kvs...)
kvs[1] = 0 // WrapWith should copy its key values
ctx := Context(err)
if got, want := ctx[0], "a"; got != want {
t.Errorf("context value does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
if got, want := ctx[1], 123; got != want {
t.Errorf("context value does not match the expected one\nactual: %s\nexpected: %d", got, want)
}
if got, want := err.Error(), "error2: error"; got != want {
t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
}