github.com/searKing/golang/go@v1.2.117/errors/multi_go1.19_test.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !go1.20
     6  
     7  package errors_test
     8  
     9  import (
    10  	"errors"
    11  	"reflect"
    12  	"testing"
    13  
    14  	errors_ "github.com/searKing/golang/go/errors"
    15  )
    16  
    17  func TestMulti(t *testing.T) {
    18  	err1 := errors.New("err1")
    19  	err2 := errors.New("err2")
    20  	for _, test := range []struct {
    21  		errs []error
    22  		want []error
    23  	}{{
    24  		errs: []error{err1},
    25  		want: []error{err1},
    26  	}, {
    27  		errs: []error{err1, err2},
    28  		want: []error{err1},
    29  	}, {
    30  		errs: []error{err1, nil, err2},
    31  		want: []error{err1},
    32  	}, {
    33  		errs: []error{nil, err1, nil, err2},
    34  		want: []error{err1},
    35  	}} {
    36  		me := errors_.Multi(test.errs...)
    37  		var got []error
    38  		if i, ok := me.(interface{ Unwrap() []error }); ok {
    39  			got = i.Unwrap()
    40  		} else if i, ok := me.(interface{ Unwrap() error }); ok {
    41  			got = append(got, i.Unwrap())
    42  		}
    43  		if !reflect.DeepEqual(got, test.want) {
    44  			t.Errorf("Multi(%v) = %v; want %v", test.errs, got, test.want)
    45  		}
    46  		if len(got) != cap(got) {
    47  			t.Errorf("Multi(%v) returns errors with len=%v, cap=%v; want len==cap", test.errs, len(got), cap(got))
    48  		}
    49  	}
    50  }