ut/errors_test.go

85 lines
1.9 KiB
Go
Raw Permalink Normal View History

package ut
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestErrCombineNil(t *testing.T) {
assert.Nil(t, ErrCombine(nil))
}
func TestErrCombineEmpty(t *testing.T) {
assert.Nil(t, ErrCombine())
}
func TestErrCombineErrIsKept(t *testing.T) {
var err = errors.New("err")
assert.Equal(t, err, ErrCombine(err))
}
func TestErrCombineErrIsAssigned(t *testing.T) {
var err = errors.New("err")
assert.Equal(t, err, ErrCombine(nil, err))
}
func TestErrCombineAggregates(t *testing.T) {
var err1 = errors.New("err1")
var err2 = errors.New("err2")
var err = ErrCombine(err1, err2)
assert.NotNil(t, err)
require.IsType(t, &Errors{}, err)
var errs = err.(*Errors).Errs
assert.Equal(t, 2, len(errs))
assert.Equal(t, err1, errs[0])
assert.Equal(t, err2, errs[1])
}
func TestErrCombineErrorContainsBothMessages(t *testing.T) {
var err1 = errors.New("err1")
var err2 = errors.New("err2")
var err = ErrCombine(err1, err2).Error()
assert.Contains(t, err, err1.Error())
assert.Contains(t, err, err2.Error())
}
func TestErrCombineExpandsFirstArgument(t *testing.T) {
var err1 = errors.New("err1")
var err2 = errors.New("err2")
var err3 = errors.New("err3")
var err = ErrCombine(ErrCombine(err1, err2), err3)
assert.NotNil(t, err)
require.IsType(t, &Errors{}, err)
var errs = err.(*Errors).Errs
assert.Equal(t, 3, len(errs))
assert.Equal(t, err1, errs[0])
assert.Equal(t, err2, errs[1])
assert.Equal(t, err3, errs[2])
}
func TestErrCombineDoesNotExpandConsecutiveArguments(t *testing.T) {
var err1 = errors.New("err1")
var err2 = errors.New("err2")
var err3 = errors.New("err3")
var err = ErrCombine(err1, ErrCombine(err2, err3))
assert.NotNil(t, err)
require.IsType(t, &Errors{}, err)
var errs = err.(*Errors).Errs
assert.Equal(t, 2, len(errs))
assert.Equal(t, err1, errs[0])
assert.IsType(t, &Errors{}, errs[1])
}