@@ -2,6 +2,7 @@ package multierror
22
33import (
44 "errors"
5+ "fmt"
56 "reflect"
67 "testing"
78)
@@ -69,3 +70,134 @@ func TestErrorWrappedErrors(t *testing.T) {
6970 t .Fatalf ("bad: %s" , multi .WrappedErrors ())
7071 }
7172}
73+
74+ func TestErrorUnwrap (t * testing.T ) {
75+ t .Run ("with errors" , func (t * testing.T ) {
76+ err := & Error {Errors : []error {
77+ errors .New ("foo" ),
78+ errors .New ("bar" ),
79+ errors .New ("baz" ),
80+ }}
81+
82+ var current error = err
83+ for i := 0 ; i < len (err .Errors ); i ++ {
84+ current = errors .Unwrap (current )
85+ if ! errors .Is (current , err .Errors [i ]) {
86+ t .Fatal ("should be next value" )
87+ }
88+ }
89+
90+ if errors .Unwrap (current ) != nil {
91+ t .Fatal ("should be nil at the end" )
92+ }
93+ })
94+
95+ t .Run ("with no errors" , func (t * testing.T ) {
96+ err := & Error {Errors : nil }
97+ if errors .Unwrap (err ) != nil {
98+ t .Fatal ("should be nil" )
99+ }
100+ })
101+
102+ t .Run ("with nil multierror" , func (t * testing.T ) {
103+ var err * Error
104+ if errors .Unwrap (err ) != nil {
105+ t .Fatal ("should be nil" )
106+ }
107+ })
108+ }
109+
110+ func TestErrorIs (t * testing.T ) {
111+ errBar := errors .New ("bar" )
112+
113+ t .Run ("with errBar" , func (t * testing.T ) {
114+ err := & Error {Errors : []error {
115+ errors .New ("foo" ),
116+ errBar ,
117+ errors .New ("baz" ),
118+ }}
119+
120+ if ! errors .Is (err , errBar ) {
121+ t .Fatal ("should be true" )
122+ }
123+ })
124+
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+
137+ t .Run ("without errBar" , func (t * testing.T ) {
138+ err := & Error {Errors : []error {
139+ errors .New ("foo" ),
140+ errors .New ("baz" ),
141+ }}
142+
143+ if errors .Is (err , errBar ) {
144+ t .Fatal ("should be false" )
145+ }
146+ })
147+ }
148+
149+ func TestErrorAs (t * testing.T ) {
150+ match := & nestedError {}
151+
152+ t .Run ("with the value" , func (t * testing.T ) {
153+ err := & Error {Errors : []error {
154+ errors .New ("foo" ),
155+ match ,
156+ errors .New ("baz" ),
157+ }}
158+
159+ var target * nestedError
160+ if ! errors .As (err , & target ) {
161+ t .Fatal ("should be true" )
162+ }
163+ if target == nil {
164+ t .Fatal ("target should not be nil" )
165+ }
166+ })
167+
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+
184+ t .Run ("without the value" , func (t * testing.T ) {
185+ err := & Error {Errors : []error {
186+ errors .New ("foo" ),
187+ errors .New ("baz" ),
188+ }}
189+
190+ var target * nestedError
191+ if errors .As (err , & target ) {
192+ t .Fatal ("should be false" )
193+ }
194+ if target != nil {
195+ t .Fatal ("target should be nil" )
196+ }
197+ })
198+ }
199+
200+ // nestedError implements error and is used for tests.
201+ type nestedError struct {}
202+
203+ func (* nestedError ) Error () string { return "" }
0 commit comments