-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
147 lines (114 loc) · 3.4 KB
/
errors_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
package errors_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/jsteenb2/errors"
)
func TestWrap(t *testing.T) {
t.Run("simple wrapped error is returned when calling std lib errors.Unwrap", func(t *testing.T) {
baseErr := errors.New("first error")
if unwrapped := errors.Unwrap(baseErr); unwrapped != nil {
t.Fatalf("recieved unexpected unwrapped error:\n\t\tgot:\t%v", unwrapped)
}
wrappedErr := errors.Wrap(baseErr)
if unwrapped := errors.Unwrap(wrappedErr); unwrapped == nil {
t.Fatalf("recieved unexpected nil unwrapped error")
}
})
t.Run("unwrapping nil error should return nil", func(t *testing.T) {
err := errors.Wrap(nil)
if err != nil {
t.Fatalf("recieved unexpected wrapped error:\n\t\tgot:\t%v", err)
}
})
}
func TestV(t *testing.T) {
type foo struct {
i int
}
t.Run("key val pairs are should be accessible", func(t *testing.T) {
err := errors.New("simple msg", errors.KVs("bool", true, "str", "string", "float", 3.14, "int", 1, "foo", foo{i: 3}))
eqV(t, err, "bool", true)
eqV(t, err, "str", "string")
eqV(t, err, "float", 3.14)
eqV(t, err, "int", 1)
eqV(t, err, "foo", foo{i: 3})
if v := errors.V(err, "non existent"); v != nil {
t.Errorf("unexpected value returned:\n\t\tgot:\t%#v", v)
}
})
t.Run("when parent error kv pair collides with wrapped error will take parent kv pair", func(t *testing.T) {
err := errors.New("simple msg", errors.KVs("str", "initial"))
err = errors.Wrap(err, errors.KVs("str", "wrapped"))
eqV(t, err, "str", "wrapped")
})
t.Run("when provided KVs should incorporate them as they are without key mapping", func(t *testing.T) {
err := errors.New("simple msg", errors.KVs(errors.KVs("unspread", "vals"), errors.KV{K: "indiv", V: "1"}))
eqV(t, err, "unspread", "vals")
eqV(t, err, "indiv", "1")
})
}
func eq[T comparable](t *testing.T, want, got T) bool {
t.Helper()
matches := want == got
if !matches {
t.Errorf("values do not match:\n\t\twant:\t%#v\n\t\tgot:\t%#v", want, got)
}
return matches
}
func eqV[T comparable](t *testing.T, err error, key string, want T) bool {
t.Helper()
got, ok := errors.V(err, key).(T)
must(t, eq(t, true, ok))
return eq(t, want, got)
}
func eqFields(t *testing.T, want, got []any) bool {
t.Helper()
defer func() {
if t.Failed() {
b, _ := json.MarshalIndent(got, "", " ")
t.Logf("got: %s", string(b))
}
}()
if matches := eqLen(t, len(want), got); !matches {
return matches
}
matches := true
// compare pairs and verify keys are strings
for i := 0; i < len(want); i += 2 {
wantKey := isT[string](t, want[i])
gotKey := isT[string](t, got[i])
vIdx := i + 1
wantVal, gotVal := want[vIdx], got[vIdx]
keysMatch := wantKey == gotKey
valsMatch := reflect.DeepEqual(wantVal, gotVal)
if !keysMatch || !valsMatch {
matches = false
t.Errorf("unexpected fields pair:\n\t\twant:\t%s: %+v\n\t\tgot:\t%s: %+v", wantKey, wantVal, gotKey, gotVal)
}
}
return matches
}
func eqLen[T any](t *testing.T, wantLen int, got []T) bool {
t.Helper()
matches := wantLen == len(got)
if !matches {
t.Fatalf("expected fields to have length %d:\n\t\tgot len: %d\n\t\tgot val: %v", wantLen, len(got), got)
}
return matches
}
func isT[T any](t *testing.T, v any) T {
t.Helper()
out, ok := v.(T)
if !ok {
t.Fatalf("unexpected type found:\n\t\tgot: %T", v)
}
return out
}
func must(t *testing.T, outcome bool) {
t.Helper()
if !outcome {
t.FailNow()
}
}