1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
package build
import (
"errors"
"testing"
)
// TestJoinErrors tests that JoinErrors only returns non-nil when there are
// non-nil elements in errs. And tests that the returned error's string the
// concatenation of all the strings of the elements in errs, in order and
// separated by sep.
func TestJoinErrors(t *testing.T) {
tests := []struct {
errs []error
sep string
wantNil bool
errStrWant string
}{
// Test that JoinErrors returns nil when errs is nil.
{
wantNil: true,
},
// Test that JoinErrors returns nil when errs is an empty slice.
{
errs: []error{},
wantNil: true,
},
// Test that JoinErrors returns nil when errs has only nil elements.
{
errs: []error{nil},
wantNil: true,
},
{
errs: []error{nil, nil, nil},
wantNil: true,
},
// Test that JoinErrors returns non-nil with the expected string when errs has only one non-nil element.
{
errs: []error{errors.New("foo")},
sep: ";",
errStrWant: "foo",
},
// Test that JoinErrors returns non-nil with the expected string when errs has multiple non-nil elements.
{
errs: []error{errors.New("foo"), errors.New("bar"), errors.New("baz")},
sep: ";",
errStrWant: "foo;bar;baz",
},
// Test that nil errors are ignored.
{
errs: []error{nil, errors.New("foo"), nil, nil, nil, errors.New("bar"), errors.New("baz"), nil, nil, nil},
sep: ";",
errStrWant: "foo;bar;baz",
},
}
for _, tt := range tests {
err := JoinErrors(tt.errs, tt.sep)
if tt.wantNil && err != nil {
t.Errorf("expected nil error, got '%v'", err)
} else if err != nil && err.Error() != tt.errStrWant {
t.Errorf("expected '%v', got '%v'", tt.errStrWant, err)
}
}
}
|