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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package multierror_test
import (
"errors"
"fmt"
"testing"
"github.com/ethersphere/bee/pkg/resolver/multiresolver/multierror"
)
// nestedError implements error and is used for tests.
type nestedError struct{}
func (*nestedError) Error() string { return "" }
func TestErrorError(t *testing.T) {
t.Run("one error", func(t *testing.T) {
want := "1 error occurred: foo"
multi := multierror.New(errors.New("foo"))
if multi.Error() != want {
t.Fatalf("got: %q, want %q", multi.Error(), want)
}
})
t.Run("multiple errors", func(t *testing.T) {
want := "2 errors occurred: foo, bar"
multi := multierror.New(
errors.New("foo"),
errors.New("bar"),
)
if multi.Error() != want {
t.Fatalf("got: %q, want %q", multi.Error(), want)
}
})
}
func TestErrorErrorOrNil(t *testing.T) {
err := multierror.New()
if err.ErrorOrNil() != nil {
t.Fatalf("bad: %#v", err.ErrorOrNil())
}
err.Errors = []error{errors.New("foo")}
if got := err.ErrorOrNil(); got == nil {
t.Fatal("should not be nil")
} else if got != err {
t.Fatalf("bad: %#v", got)
}
}
func TestErrorWrapErrorOrNil(t *testing.T) {
wrapErr := errors.New("wrapper error")
err := multierror.New()
if err.WrapErrorOrNil(wrapErr) != nil {
t.Fatalf("bad: %#v", err.ErrorOrNil())
}
multi := multierror.New(
errors.New("foo"),
errors.New("bar"),
)
if got := multi.WrapErrorOrNil(wrapErr); got == nil {
t.Fatal("should not be nil")
} else if !errors.Is(got, wrapErr) {
t.Fatalf("bad: %#v", got)
}
}
func TestErrorUnwrap(t *testing.T) {
t.Run("with errors", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
errors.New("bar"),
errors.New("baz"),
)
var current error = err
for i := 0; i < len(err.Errors); i++ {
current = errors.Unwrap(current)
if !errors.Is(current, err.Errors[i]) {
t.Fatal("should be next value")
}
}
if errors.Unwrap(current) != nil {
t.Fatal("should be nil at the end")
}
})
t.Run("with no errors", func(t *testing.T) {
err := multierror.New()
if errors.Unwrap(err) != nil {
t.Fatal("should be nil")
}
})
t.Run("with nil multierror", func(t *testing.T) {
var err *multierror.Error
if errors.Unwrap(err) != nil {
t.Fatal("should be nil")
}
})
}
func TestErrorIs(t *testing.T) {
errBar := errors.New("bar")
t.Run("with errBar", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
errBar,
errors.New("baz"),
)
if !errors.Is(err, errBar) {
t.Fatal("should be true")
}
})
t.Run("with errBar wrapped by fmt.Errorf", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
fmt.Errorf("errorf: %w", errBar),
errors.New("baz"),
)
if !errors.Is(err, errBar) {
t.Fatal("should be true")
}
})
t.Run("without errBar", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
errors.New("baz"),
)
if errors.Is(err, errBar) {
t.Fatal("should be false")
}
})
}
func TestErrorAs(t *testing.T) {
match := &nestedError{}
t.Run("with the value", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
match,
errors.New("baz"),
)
var target *nestedError
if !errors.As(err, &target) {
t.Fatal("should be true")
}
if target == nil {
t.Fatal("target should not be nil")
}
})
t.Run("with the value wrapped by fmt.Errorf", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
fmt.Errorf("errorf: %w", match),
errors.New("baz"),
)
var target *nestedError
if !errors.As(err, &target) {
t.Fatal("should be true")
}
if target == nil {
t.Fatal("target should not be nil")
}
})
t.Run("without the value", func(t *testing.T) {
err := multierror.New(
errors.New("foo"),
errors.New("baz"),
)
var target *nestedError
if errors.As(err, &target) {
t.Fatal("should be false")
}
if target != nil {
t.Fatal("target should be nil")
}
})
}