github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/sync/multierror/multierror_test.go (about)

     1  package multierror
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  func TestMultiError(t *testing.T) {
     9  	me2a := NewBuilder(2)
    10  	me2a.Add(errors.New("a"))
    11  	me1ab := NewBuilder(1)
    12  	me1ab.Add(errors.New("a"))
    13  	me1ab.Add(errors.New("b"))
    14  
    15  	for _, test := range []struct {
    16  		errs     []error
    17  		expected error
    18  	}{
    19  		{
    20  			[]error{},
    21  			nil,
    22  		},
    23  		{
    24  			[]error{errors.New("FAIL")},
    25  			errors.New("FAIL"),
    26  		},
    27  		{
    28  			[]error{errors.New("1"), errors.New("2"), errors.New("3")},
    29  			errors.New(`[1
    30  2] [plus 1 other error(s)]`),
    31  		},
    32  		{
    33  			[]error{errors.New("1"), me2a.Err()},
    34  			errors.New(`[1
    35  a]`),
    36  		},
    37  	} {
    38  		errs := NewBuilder(2)
    39  
    40  		for _, e := range test.errs {
    41  			errs.Add(e)
    42  		}
    43  
    44  		got := errs.Err()
    45  
    46  		if test.expected == nil && got == nil {
    47  			continue
    48  		}
    49  
    50  		if test.expected == nil || got == nil {
    51  			t.Fatalf("error mismatch: %v vs %v", test.expected, got)
    52  		}
    53  
    54  		if test.expected.Error() != got.Error() {
    55  			t.Fatalf("error mismatch: %q vs %q", test.expected, got)
    56  		}
    57  	}
    58  }