gitlab.com/SiaPrime/SiaPrime@v1.4.1/build/errors_test.go (about)

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