-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctxerr_test.go
More file actions
153 lines (145 loc) · 3.21 KB
/
ctxerr_test.go
File metadata and controls
153 lines (145 loc) · 3.21 KB
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
package ctxerr
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
)
var ErrCause = errors.New("original error")
func TestWith(t *testing.T) {
tests := []struct {
name string
err error
ctx map[string]any
expected error
shouldMatch CtxErr
}{
{
name: "basic error wrapping",
err: ErrCause,
ctx: map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
},
expected: ErrCause,
shouldMatch: CtxErr{
Err: ErrCause,
Ctx: map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
},
},
},
{
name: "nil error",
err: nil,
ctx: map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
},
expected: nil,
},
{
name: "override existing context",
err: With(ErrCause, map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
}),
ctx: map[string]any{
"newfield": true,
"string": "new value",
"slice_string": []string{"one"},
},
expected: ErrCause,
shouldMatch: CtxErr{
Err: ErrCause,
Ctx: map[string]any{
"int": 42, // Original
"string": "new value", // Overridden
"slice_string": []string{"one"}, // Overridden
"newfield": true, // New
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := With(tt.err, tt.ctx)
if tt.expected == nil {
require.Nil(t, result)
return
}
require.ErrorIs(t, result, tt.expected)
actual, ok := result.(CtxErr)
require.True(t, ok)
require.Equal(t, tt.shouldMatch, actual)
})
}
}
func TestCtx(t *testing.T) {
tests := []struct {
name string
ctx context.Context
err error
expected CtxErr
}{
{
name: "context without error",
ctx: context.Background(),
err: nil,
expected: CtxErr{
Err: nil,
Ctx: nil,
},
},
{
name: "basic error in context",
ctx: context.Background(),
err: With(ErrCause, map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
}),
expected: CtxErr{
Err: ErrCause,
Ctx: map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
},
},
},
{
name: "override existing context error",
ctx: Ctx(context.Background(), With(ErrCause, map[string]any{
"string": "bar",
"int": 42,
"slice_string": []string{"one", "two", "three"},
})),
err: With(ErrCause, map[string]any{
"new": true,
"int": 404,
}),
expected: CtxErr{
Err: ErrCause,
Ctx: map[string]any{
"string": "bar",
"slice_string": []string{"one", "two", "three"},
"int": 404, // Overridden
"new": true, // New
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resultCtx := Ctx(tt.ctx, tt.err)
actual := From(resultCtx)
require.Equal(t, tt.expected, actual)
})
}
}