github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/utils/err_array_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/livekit/psrpc"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestErrArray(t *testing.T) {
    13  	err1 := errors.New("error 1")
    14  	err2 := errors.New("error 2")
    15  	err3 := psrpc.NewErrorf(psrpc.NotFound, "error 3")
    16  	err4 := psrpc.NewErrorf(psrpc.Internal, "error 4")
    17  
    18  	errArray := &ErrArray{}
    19  	assert.Nil(t, errArray.ToError())
    20  
    21  	errArray.AppendErr(err1)
    22  	assert.Equal(t, psrpc.Unknown, errArray.ToError().Code())
    23  	assert.Equal(t, err1.Error(), errArray.ToError().Error())
    24  
    25  	errArray.AppendErr(err2)
    26  	assert.Equal(t, psrpc.Unknown, errArray.ToError().Code())
    27  	assert.Equal(t, 2, len(strings.Split(errArray.ToError().Error(), "\n")))
    28  
    29  	errArray.AppendErr(err3)
    30  	assert.Equal(t, psrpc.NotFound, errArray.ToError().Code())
    31  	assert.Equal(t, 3, len(strings.Split(errArray.ToError().Error(), "\n")))
    32  
    33  	errArray.AppendErr(err4)
    34  	assert.Equal(t, psrpc.NotFound, errArray.ToError().Code())
    35  	assert.Equal(t, 4, len(strings.Split(errArray.ToError().Error(), "\n")))
    36  }