Skip to content

Commit bcb7871

Browse files
felixgemitchellh
authored andcommitted
add test case for Unwrap + Errorf
1 parent 0c3d827 commit bcb7871

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

multierror_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package multierror
22

33
import (
44
"errors"
5+
"fmt"
56
"reflect"
67
"testing"
78
)
@@ -121,6 +122,18 @@ func TestErrorIs(t *testing.T) {
121122
}
122123
})
123124

125+
t.Run("with errBar wrapped by fmt.Errorf", func(t *testing.T) {
126+
err := &Error{Errors: []error{
127+
errors.New("foo"),
128+
fmt.Errorf("errorf: %w", errBar),
129+
errors.New("baz"),
130+
}}
131+
132+
if !errors.Is(err, errBar) {
133+
t.Fatal("should be true")
134+
}
135+
})
136+
124137
t.Run("without errBar", func(t *testing.T) {
125138
err := &Error{Errors: []error{
126139
errors.New("foo"),
@@ -152,6 +165,22 @@ func TestErrorAs(t *testing.T) {
152165
}
153166
})
154167

168+
t.Run("with the value wrapped by fmt.Errorf", func(t *testing.T) {
169+
err := &Error{Errors: []error{
170+
errors.New("foo"),
171+
fmt.Errorf("errorf: %w", match),
172+
errors.New("baz"),
173+
}}
174+
175+
var target *nestedError
176+
if !errors.As(err, &target) {
177+
t.Fatal("should be true")
178+
}
179+
if target == nil {
180+
t.Fatal("target should not be nil")
181+
}
182+
})
183+
155184
t.Run("without the value", func(t *testing.T) {
156185
err := &Error{Errors: []error{
157186
errors.New("foo"),

0 commit comments

Comments
 (0)