@@ -3,7 +3,6 @@ package errorsx_test
33import (
44 "errors"
55 "fmt"
6- "slices"
76 "testing"
87
98 "go-simpler.org/errorsx"
@@ -30,16 +29,17 @@ func TestIsAny(t *testing.T) {
3029 }
3130}
3231
33- func TestHasType (t * testing.T ) {
32+ func TestAs (t * testing.T ) {
33+ isok := func (_ any , ok bool ) bool { return ok }
34+
3435 tests := map [string ]struct {
3536 fn func (error ) bool
3637 err error
3738 want bool
3839 }{
39- "no match" : {fn : errorsx .HasType [barError ], err : errFoo , want : false },
40- "match (exact)" : {fn : errorsx .HasType [fooError ], err : errFoo , want : true },
41- "match (wrapped)" : {fn : errorsx .HasType [fooError ], err : wrap (errFoo ), want : true },
42- "match (interface)" : {fn : errorsx.HasType [interface { Error () string }], err : errFoo , want : true },
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 },
4343 }
4444
4545 for name , test := range tests {
@@ -51,26 +51,6 @@ func TestHasType(t *testing.T) {
5151 }
5252}
5353
54- func TestSplit (t * testing.T ) {
55- tests := map [string ]struct {
56- err error
57- wantErrs []error
58- }{
59- "nil error" : {err : nil , wantErrs : nil },
60- "single error" : {err : errFoo , wantErrs : nil },
61- "joined errors (errors.Join)" : {err : errors .Join (errFoo , errBar ), wantErrs : []error {errFoo , errBar }},
62- "joined errors (fmt.Errorf)" : {err : fmt .Errorf ("%w; %w" , errFoo , errBar ), wantErrs : []error {errFoo , errBar }},
63- }
64-
65- for name , test := range tests {
66- t .Run (name , func (t * testing.T ) {
67- if gotErrs := errorsx .Split (test .err ); ! slices .Equal (gotErrs , test .wantErrs ) {
68- t .Errorf ("got %v; want %v" , gotErrs , test .wantErrs )
69- }
70- })
71- }
72- }
73-
7454func TestClose (t * testing.T ) {
7555 tests := map [string ]struct {
7656 mainErr error
@@ -112,8 +92,8 @@ type barError struct{}
11292
11393func (barError ) Error () string { return "bar" }
11494
115- func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
116-
11795type errCloser struct { err error }
11896
11997func (c * errCloser ) Close () error { return c .err }
98+
99+ func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
0 commit comments