github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/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  		join error
    14  		iss  []error
    15  		ass  []interface{}
    16  		s    string
    17  	}{
    18  		{
    19  			join: Join(context.Canceled),
    20  			iss:  []error{context.Canceled},
    21  			ass:  nil,
    22  			s:    "context canceled",
    23  		},
    24  		{
    25  			join: 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  			join: Join(context.Canceled, context.DeadlineExceeded, nil),
    36  			iss:  []error{context.Canceled, context.DeadlineExceeded},
    37  			s:    "[\"context canceled\",\"context deadline exceeded\"]",
    38  		},
    39  		{
    40  			join: Join(nil, context.DeadlineExceeded, nil),
    41  			iss:  []error{context.DeadlineExceeded},
    42  			s:    "context deadline exceeded",
    43  		},
    44  	} {
    45  		t.Run("", func(t *testing.T) {
    46  			require.Equal(t, tt.s, tt.join.Error())
    47  			if len(tt.iss) > 0 {
    48  				require.True(t, Is(tt.join, tt.iss...))
    49  			}
    50  			if len(tt.ass) > 0 {
    51  				require.True(t, As(tt.join, tt.ass...))
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestUnwrapJoined(t *testing.T) {
    58  	err1 := context.Canceled
    59  	err2 := context.DeadlineExceeded
    60  
    61  	joined := Join(err1, err2)
    62  
    63  	unwrappable := joined.(interface{ Unwrap() []error })
    64  	inners := unwrappable.Unwrap()
    65  	assert.Contains(t, inners, err1)
    66  	assert.Contains(t, inners, err2)
    67  }