Skip to content

Commit b43c30b

Browse files
authored
test: rewrite with table tests (#17)
1 parent ceee6f9 commit b43c30b

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

errorsx_test.go

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,93 @@ import (
1010
)
1111

1212
func TestIsAny(t *testing.T) {
13-
test := func(name string, err error, targets []error, want bool) {
14-
t.Helper()
13+
tests := map[string]struct {
14+
err error
15+
targets []error
16+
want bool
17+
}{
18+
"no matches": {err: errFoo, targets: []error{errBar}, want: false},
19+
"single target match": {err: errFoo, targets: []error{errFoo}, want: true},
20+
"single target match (wrapped)": {err: wrap(errFoo), targets: []error{errFoo}, want: true},
21+
"multiple targets match (wrapped)": {err: wrap(errFoo), targets: []error{errBar, errFoo}, want: true},
22+
}
23+
24+
for name, test := range tests {
1525
t.Run(name, func(t *testing.T) {
16-
t.Helper()
17-
if got := errorsx.IsAny(err, targets[0], targets[1:]...); got != want {
18-
t.Errorf("got %t; want %t", got, want)
26+
if got := errorsx.IsAny(test.err, test.targets[0], test.targets[1:]...); got != test.want {
27+
t.Errorf("got %t; want %t", got, test.want)
1928
}
2029
})
2130
}
22-
23-
test("no matches", errFoo, []error{errBar}, false)
24-
test("single target match", errFoo, []error{errFoo}, true)
25-
test("single target match (wrapped)", wrap(errFoo), []error{errFoo}, true)
26-
test("multiple targets match (wrapped)", wrap(errFoo), []error{errBar, errFoo}, true)
2731
}
2832

2933
func TestHasType(t *testing.T) {
30-
test := func(name string, fn func(error) bool, err error, want bool) {
31-
t.Helper()
34+
tests := map[string]struct {
35+
fn func(error) bool
36+
err error
37+
want bool
38+
}{
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},
43+
}
44+
45+
for name, test := range tests {
3246
t.Run(name, func(t *testing.T) {
33-
t.Helper()
34-
if got := fn(err); got != want {
35-
t.Errorf("got %t; want %t", got, want)
47+
if got := test.fn(test.err); got != test.want {
48+
t.Errorf("got %t; want %t", got, test.want)
3649
}
3750
})
3851
}
39-
40-
test("no match", errorsx.HasType[barError], errFoo, false)
41-
test("match (exact)", errorsx.HasType[fooError], errFoo, true)
42-
test("match (wrapped)", errorsx.HasType[fooError], wrap(errFoo), true)
43-
test("match (interface)", errorsx.HasType[interface{ Error() string }], errFoo, true)
4452
}
4553

4654
func TestSplit(t *testing.T) {
47-
test := func(name string, err error, wantErrs []error) {
48-
t.Helper()
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 {
4966
t.Run(name, func(t *testing.T) {
50-
t.Helper()
51-
if gotErrs := errorsx.Split(err); !slices.Equal(gotErrs, wantErrs) {
52-
t.Errorf("got %v; want %v", gotErrs, wantErrs)
67+
if gotErrs := errorsx.Split(test.err); !slices.Equal(gotErrs, test.wantErrs) {
68+
t.Errorf("got %v; want %v", gotErrs, test.wantErrs)
5369
}
5470
})
5571
}
56-
57-
test("nil error", nil, nil)
58-
test("single error", errFoo, nil)
59-
test("joined errors (errors.Join)", errors.Join(errFoo, errBar), []error{errFoo, errBar})
60-
test("joined errors (fmt.Errorf)", fmt.Errorf("%w; %w", errFoo, errBar), []error{errFoo, errBar})
6172
}
6273

6374
func TestClose(t *testing.T) {
64-
test := func(name string, mainErr, closeErr error, wantErrs []error) {
65-
t.Helper()
75+
tests := map[string]struct {
76+
mainErr error
77+
closeErr error
78+
wantErrs []error
79+
}{
80+
"main: ok; close: ok": {mainErr: nil, closeErr: nil, wantErrs: []error{}},
81+
"main: ok; close: error": {mainErr: nil, closeErr: errBar, wantErrs: []error{errBar}},
82+
"main: error; close: ok": {mainErr: errFoo, closeErr: nil, wantErrs: []error{errFoo}},
83+
"main: error; close: error": {mainErr: errFoo, closeErr: errBar, wantErrs: []error{errFoo, errBar}},
84+
}
85+
86+
for name, test := range tests {
6687
t.Run(name, func(t *testing.T) {
67-
t.Helper()
6888
gotErr := func() (err error) {
69-
c := errCloser{err: closeErr}
89+
c := errCloser{err: test.closeErr}
7090
defer errorsx.Close(&c, &err)
71-
return mainErr
91+
return test.mainErr
7292
}()
73-
for _, wantErr := range wantErrs {
93+
for _, wantErr := range test.wantErrs {
7494
if !errors.Is(gotErr, wantErr) {
75-
t.Errorf("got %v; want %v", gotErr, wantErrs)
95+
t.Errorf("got %v; want %v", gotErr, wantErr)
7696
}
7797
}
7898
})
7999
}
80-
81-
test("main: ok; close: ok", nil, nil, []error{})
82-
test("main: ok; close: error", nil, errBar, []error{errBar})
83-
test("main: error; close: ok", errFoo, nil, []error{errFoo})
84-
test("main: error; close: error", errFoo, errBar, []error{errFoo, errBar})
85100
}
86101

87102
var (

0 commit comments

Comments
 (0)