@@ -14,7 +14,7 @@ func TestIsAny(t *testing.T) {
1414 targets []error
1515 want bool
1616 }{
17- "no matches" : {err : errFoo , targets : []error {errBar }, want : false },
17+ "no match" : {err : errFoo , targets : []error {errBar }, want : false },
1818 "single target match" : {err : errFoo , targets : []error {errFoo }, want : true },
1919 "single target match (wrapped)" : {err : wrap (errFoo ), targets : []error {errFoo }, want : true },
2020 "multiple targets match (wrapped)" : {err : wrap (errFoo ), targets : []error {errBar , errFoo }, want : true },
@@ -33,41 +33,41 @@ func TestAs(t *testing.T) {
3333 isok := func (_ any , ok bool ) bool { return ok }
3434
3535 tests := map [string ]struct {
36- fn func (error ) bool
3736 err error
37+ as func (error ) bool
3838 want bool
3939 }{
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 },
4343 }
4444
4545 for name , test := range tests {
4646 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 {
4848 t .Errorf ("got %t; want %t" , got , test .want )
4949 }
5050 })
5151 }
5252}
5353
54- func TestClose (t * testing.T ) {
54+ func TestDo (t * testing.T ) {
5555 tests := map [string ]struct {
5656 mainErr error
57- closeErr error
57+ deferErr error
5858 wantErrs []error
5959 }{
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 }},
6464 }
6565
6666 for name , test := range tests {
6767 t .Run (name , func (t * testing.T ) {
6868 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 )
7171 return test .mainErr
7272 }()
7373 for _ , wantErr := range test .wantErrs {
@@ -92,8 +92,4 @@ type barError struct{}
9292
9393func (barError ) Error () string { return "bar" }
9494
95- type errCloser struct { err error }
96-
97- func (c * errCloser ) Close () error { return c .err }
98-
9995func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
0 commit comments