src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/errutil/multi_error_test.go (about)

     1  package errutil
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	err1 = errors.New("error 1")
    10  	err2 = errors.New("error 2")
    11  	err3 = errors.New("error 3")
    12  )
    13  
    14  var errorsTests = []struct {
    15  	e          error
    16  	wantString string
    17  }{
    18  	{Multi(), ""},
    19  	{Multi(errors.New("some error")), "some error"},
    20  	{
    21  		Multi(err1, err2),
    22  		"multiple errors: error 1; error 2",
    23  	},
    24  	{
    25  		Multi(err1, err2, err3),
    26  		"multiple errors: error 1; error 2; error 3",
    27  	},
    28  	{
    29  		Multi(err1, Multi(err2, err3)),
    30  		"multiple errors: error 1; error 2; error 3",
    31  	},
    32  	{
    33  		Multi(Multi(err1, err2), err3),
    34  		"multiple errors: error 1; error 2; error 3",
    35  	},
    36  }
    37  
    38  func TestErrors(t *testing.T) {
    39  	for _, test := range errorsTests {
    40  		if test.e == nil {
    41  			if test.wantString != "" {
    42  				t.Errorf("got nil, want %q", test.wantString)
    43  			}
    44  		} else {
    45  			gotString := test.e.Error()
    46  			if gotString != test.wantString {
    47  				t.Errorf("got %q, want %q", gotString, test.wantString)
    48  			}
    49  		}
    50  	}
    51  }