Skip to content

Commit

Permalink
group: fix testing to be deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
nickethier committed Nov 11, 2019
1 parent d06a4d7 commit 67c4202
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multierror

import (
"errors"
"strings"
"testing"
)

Expand All @@ -10,14 +11,14 @@ func TestGroup(t *testing.T) {
err2 := errors.New("group_test: 2")

cases := []struct {
errs []error
result error
errs []error
nilResult bool
}{
{errs: []error{}},
{errs: []error{nil}},
{errs: []error{err1}, result: Append(nil, err1)},
{errs: []error{err1, nil}, result: Append(nil, err1)},
{errs: []error{err1, nil, err2}, result: Append(err2, err1)},
{errs: []error{}, nilResult: true},
{errs: []error{nil}, nilResult: true},
{errs: []error{err1}},
{errs: []error{err1, nil}},
{errs: []error{err1, nil, err2}},
}

for _, tc := range cases {
Expand All @@ -30,14 +31,14 @@ func TestGroup(t *testing.T) {
}

gErr := g.Wait()
if gErr == nil && tc.result != nil {
t.Fatalf("Group.Wait() was nil but expected: %v", tc.result)
return
} else if tc.result == nil && gErr != nil {
t.Fatalf("Group.Wait() was expected to be nil, actual: %v", gErr)
return
} else if gErr != nil && tc.result != nil && gErr.Error() != tc.result.Error() {
t.Errorf("Group.Wait() returned the wrong error, expected: %v, actual: %v", tc.result, gErr)
if gErr != nil {
for i := range tc.errs {
if tc.errs[i] != nil && !strings.Contains(gErr.Error(), tc.errs[i].Error()) {
t.Fatalf("expected error to contain %q, actual: %v", tc.errs[i].Error(), gErr)
}
}
} else if !tc.nilResult {
t.Fatalf("Group.Wait() should not have returned nil for errs: %v", tc.errs)
}
}
}

0 comments on commit 67c4202

Please sign in to comment.