@@ -3,7 +3,6 @@ package errorsx_test
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "slices"
7
6
"testing"
8
7
9
8
"go-simpler.org/errorsx"
@@ -30,16 +29,17 @@ func TestIsAny(t *testing.T) {
30
29
}
31
30
}
32
31
33
- func TestHasType (t * testing.T ) {
32
+ func TestAs (t * testing.T ) {
33
+ isok := func (_ any , ok bool ) bool { return ok }
34
+
34
35
tests := map [string ]struct {
35
36
fn func (error ) bool
36
37
err error
37
38
want bool
38
39
}{
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 },
43
43
}
44
44
45
45
for name , test := range tests {
@@ -51,26 +51,6 @@ func TestHasType(t *testing.T) {
51
51
}
52
52
}
53
53
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
-
74
54
func TestClose (t * testing.T ) {
75
55
tests := map [string ]struct {
76
56
mainErr error
@@ -112,8 +92,8 @@ type barError struct{}
112
92
113
93
func (barError ) Error () string { return "bar" }
114
94
115
- func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
116
-
117
95
type errCloser struct { err error }
118
96
119
97
func (c * errCloser ) Close () error { return c .err }
98
+
99
+ func wrap (err error ) error { return fmt .Errorf ("%w" , err ) }
0 commit comments