@@ -14,7 +14,7 @@ func TestIsAny(t *testing.T) {
14
14
targets []error
15
15
want bool
16
16
}{
17
- "no matches" : {err : errFoo , targets : []error {errBar }, want : false },
17
+ "no match" : {err : errFoo , targets : []error {errBar }, want : false },
18
18
"single target match" : {err : errFoo , targets : []error {errFoo }, want : true },
19
19
"single target match (wrapped)" : {err : wrap (errFoo ), targets : []error {errFoo }, want : true },
20
20
"multiple targets match (wrapped)" : {err : wrap (errFoo ), targets : []error {errBar , errFoo }, want : true },
@@ -33,41 +33,41 @@ func TestAs(t *testing.T) {
33
33
isok := func (_ any , ok bool ) bool { return ok }
34
34
35
35
tests := map [string ]struct {
36
- fn func (error ) bool
37
36
err error
37
+ as func (error ) bool
38
38
want bool
39
39
}{
40
- "no match" : {fn : func (err error ) bool { return isok (errorsx.As [barError ](err )) }, err : errFoo , want : false },
41
- "match (exact)" : {fn : func (err error ) bool { return isok (errorsx.As [fooError ](err )) }, err : errFoo , want : true },
42
- "match (wrapped)" : {fn : func (err error ) bool { return isok (errorsx.As [fooError ](err )) }, err : wrap ( errFoo ) , want : true },
40
+ "no match" : {err : errFoo , as : func (err error ) bool { return isok (errorsx.As [barError ](err )) }, want : false },
41
+ "match (exact)" : {err : errFoo , as : func (err error ) bool { return isok (errorsx.As [fooError ](err )) }, want : true },
42
+ "match (wrapped)" : {err : wrap ( errFoo ), as : func (err error ) bool { return isok (errorsx.As [fooError ](err )) }, want : true },
43
43
}
44
44
45
45
for name , test := range tests {
46
46
t .Run (name , func (t * testing.T ) {
47
- if got := test .fn (test .err ); got != test .want {
47
+ if got := test .as (test .err ); got != test .want {
48
48
t .Errorf ("got %t; want %t" , got , test .want )
49
49
}
50
50
})
51
51
}
52
52
}
53
53
54
- func TestClose (t * testing.T ) {
54
+ func TestDo (t * testing.T ) {
55
55
tests := map [string ]struct {
56
56
mainErr error
57
- closeErr error
57
+ deferErr error
58
58
wantErrs []error
59
59
}{
60
- "main: ok; close : ok" : {mainErr : nil , closeErr : nil , wantErrs : []error {}},
61
- "main: ok; close : error" : {mainErr : nil , closeErr : errBar , wantErrs : []error {errBar }},
62
- "main: error; close : ok" : {mainErr : errFoo , closeErr : nil , wantErrs : []error {errFoo }},
63
- "main: error; close : error" : {mainErr : errFoo , closeErr : errBar , wantErrs : []error {errFoo , errBar }},
60
+ "main: ok; defer : ok" : {mainErr : nil , deferErr : nil , wantErrs : []error {}},
61
+ "main: ok; defer : error" : {mainErr : nil , deferErr : errBar , wantErrs : []error {errBar }},
62
+ "main: error; defer : ok" : {mainErr : errFoo , deferErr : nil , wantErrs : []error {errFoo }},
63
+ "main: error; defer : error" : {mainErr : errFoo , deferErr : errBar , wantErrs : []error {errFoo , errBar }},
64
64
}
65
65
66
66
for name , test := range tests {
67
67
t .Run (name , func (t * testing.T ) {
68
68
gotErr := func () (err error ) {
69
- c := errCloser { err : test .closeErr }
70
- defer errorsx .Close ( & c , & err )
69
+ fn := func () error { return test .deferErr }
70
+ defer errorsx .Do ( fn , & err )
71
71
return test .mainErr
72
72
}()
73
73
for _ , wantErr := range test .wantErrs {
@@ -92,8 +92,4 @@ type barError struct{}
92
92
93
93
func (barError ) Error () string { return "bar" }
94
94
95
- type errCloser struct { err error }
96
-
97
- func (c * errCloser ) Close () error { return c .err }
98
-
99
95
func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
0 commit comments