github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xerrors/join_test.go (about)

     1  package xerrors
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestJoin(t *testing.T) {
    12  	for _, tt := range []struct {
    13  		err joinErrors
    14  		iss []error
    15  		ass []interface{}
    16  		s   string
    17  	}{
    18  		{
    19  			err: Join(context.Canceled),
    20  			iss: []error{context.Canceled},
    21  			ass: nil,
    22  			s:   "[\"context canceled\"]",
    23  		},
    24  		{
    25  			err: Join(context.Canceled, context.DeadlineExceeded, Operation()),
    26  			iss: []error{context.Canceled, context.DeadlineExceeded},
    27  			ass: []interface{}{func() interface{} {
    28  				var i isYdbError
    29  
    30  				return &i
    31  			}()},
    32  			s: "[\"context canceled\",\"context deadline exceeded\",\"operation/STATUS_CODE_UNSPECIFIED (code = 0)\"]",
    33  		},
    34  	} {
    35  		t.Run("", func(t *testing.T) {
    36  			require.Equal(t, tt.s, tt.err.Error())
    37  			if len(tt.iss) > 0 {
    38  				require.True(t, Is(tt.err, tt.iss...))
    39  			}
    40  			if len(tt.ass) > 0 {
    41  				require.True(t, As(tt.err, tt.ass...))
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestUnwrapJoined(t *testing.T) {
    48  	err1 := context.Canceled
    49  	err2 := context.DeadlineExceeded
    50  
    51  	var joined error = Join(err1, err2)
    52  
    53  	unwrappable := joined.(interface{ Unwrap() []error }) //nolint:errorlint
    54  	inners := unwrappable.Unwrap()
    55  	assert.Contains(t, inners, err1)
    56  	assert.Contains(t, inners, err2)
    57  }