github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/utils/err_array_test.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"errors"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/livekit/psrpc"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestErrArray(t *testing.T) {
    27  	err1 := errors.New("error 1")
    28  	err2 := errors.New("error 2")
    29  	err3 := psrpc.NewErrorf(psrpc.NotFound, "error 3")
    30  	err4 := psrpc.NewErrorf(psrpc.Internal, "error 4")
    31  
    32  	errArray := &ErrArray{}
    33  	assert.Nil(t, errArray.ToError())
    34  
    35  	errArray.AppendErr(err1)
    36  	assert.Equal(t, psrpc.Unknown, errArray.ToError().Code())
    37  	assert.Equal(t, err1.Error(), errArray.ToError().Error())
    38  
    39  	errArray.AppendErr(err2)
    40  	assert.Equal(t, psrpc.Unknown, errArray.ToError().Code())
    41  	assert.Equal(t, 2, len(strings.Split(errArray.ToError().Error(), "\n")))
    42  
    43  	errArray.AppendErr(err3)
    44  	assert.Equal(t, psrpc.NotFound, errArray.ToError().Code())
    45  	assert.Equal(t, 3, len(strings.Split(errArray.ToError().Error(), "\n")))
    46  
    47  	errArray.AppendErr(err4)
    48  	assert.Equal(t, psrpc.NotFound, errArray.ToError().Code())
    49  	assert.Equal(t, 4, len(strings.Split(errArray.ToError().Error(), "\n")))
    50  }