-
Notifications
You must be signed in to change notification settings - Fork 1
/
join_test.go
138 lines (114 loc) · 3.92 KB
/
join_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
package errors_test
import (
stderrors "errors"
"fmt"
"testing"
"github.com/jsteenb2/errors"
)
var sentinelErr = fmt.Errorf("sentinel err")
func TestJoin(t *testing.T) {
t.Run("single error joined error can be unwrapped", func(t *testing.T) {
err := errors.Join(errors.New("first multi error"))
gotMsg := err.Error()
wantMsg := `1 error occurred:
* first multi error [ github.com/jsteenb2/errors/join_test.go:15[TestJoin.func1] ]
`
eq(t, wantMsg, gotMsg)
unwrappedErr := errors.Unwrap(err)
if unwrappedErr == nil {
t.Fatal("unexpected nil unwrapped error")
}
gotMsg = unwrappedErr.Error()
eq(t, "first multi error", gotMsg)
})
t.Run("multiple joined errors can be unwrapped", func(t *testing.T) {
err := errors.Join(
errors.New("err 1"),
errors.New("err 2"),
)
wantMsg := `2 errors occurred:
* err 1 [ github.com/jsteenb2/errors/join_test.go:34[TestJoin.func2] ]
* err 2 [ github.com/jsteenb2/errors/join_test.go:35[TestJoin.func2] ]
`
eq(t, wantMsg, err.Error())
unwrappedErr := errors.Unwrap(err)
if unwrappedErr == nil {
t.Fatal("unexpected nil unwrapped error")
}
eq(t, "err 1", unwrappedErr.Error())
unwrappedErr = errors.Unwrap(unwrappedErr)
if unwrappedErr == nil {
t.Fatal("unexpected nil unwrapped error")
}
eq(t, "err 2", unwrappedErr.Error())
})
t.Run("multiple joined errors can be used with Is and As", func(t *testing.T) {
err := errors.Join(
errors.New("err 1", errors.Kind("foo")),
sentinelErr,
)
wantMsg := `2 errors occurred:
* err 1 [ github.com/jsteenb2/errors/join_test.go:59[TestJoin.func3] ]
* sentinel err
`
eq(t, wantMsg, err.Error())
if !errors.Is(err, sentinelErr) {
t.Errorf("failed to identify sentinel error")
}
if !errors.Is(err, errors.Kind("foo")) {
t.Error("failed to find matching kind error")
}
})
t.Run("multiple joined errors can be used with Fields", func(t *testing.T) {
err := errors.Join(
errors.New("err 1", errors.Kind("foo"), errors.KVs("ki1", "vi1")),
sentinelErr,
errors.New("err 3", errors.KVs("ki3", "vi3")),
errors.Join(
errors.New("err 4"),
nil, nil, nil, // verify we don't get nil errors
),
(error)(nil), // verify we don't get nil error
errors.KVs("kj1", "vj1"),
)
wantFields := []any{
// parent Join error
"kj1", "vj1", "err_kind", "foo", "stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:78[TestJoin.func4]"},
// first err
"err_0", []any{"ki1", "vi1", "err_kind", "foo", "stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:79[TestJoin.func4]"}},
// third err
"err_2", []any{"ki3", "vi3", "stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:81[TestJoin.func4]"}},
// fourth err
"err_3", []any{
"stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:82[TestJoin.func4]"},
"err_0", []any{"stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:83[TestJoin.func4]"}},
},
}
eqFields(t, wantFields, errors.Fields(err))
unwrapped := errors.Unwrap(err)
wantFields = []any{"ki1", "vi1", "err_kind", "foo", "stack_trace", []string{"github.com/jsteenb2/errors/join_test.go:79[TestJoin.func4]"}}
eqFields(t, wantFields, errors.Fields(unwrapped))
sentinelUnwrapped := errors.Unwrap(unwrapped)
eqFields(t, nil, errors.Fields(sentinelUnwrapped))
})
}
func TestDisjoin(t *testing.T) {
t.Run("with nil error should return nil", func(t *testing.T) {
errs := errors.Disjoin(nil)
if errs != nil {
t.Fatalf("unexpected errs returned:\n\t\tgot:\t%#v", errs)
}
})
t.Run("with std errors joined errors should unwrap", func(t *testing.T) {
innerErr := fmt.Errorf("simple err")
errs := errors.Disjoin(stderrors.Join(innerErr))
must(t, eqLen(t, 1, errs))
eq(t, innerErr, errs[0])
})
t.Run("with Join error should unwrap", func(t *testing.T) {
innerErr := fmt.Errorf("simple err")
errs := errors.Disjoin(errors.Join(innerErr))
must(t, eqLen(t, 1, errs))
eq(t, innerErr, errs[0])
})
}