2018-01-13 16:14:54 +00:00
|
|
|
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)
|
2021-08-13 08:11:13 +00:00
|
|
|
require.IsType(t, &Errors{}, err)
|
|
|
|
var errs = err.(*Errors).Errs
|
2018-01-13 16:14:54 +00:00
|
|
|
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)
|
2021-08-13 08:11:13 +00:00
|
|
|
require.IsType(t, &Errors{}, err)
|
|
|
|
var errs = err.(*Errors).Errs
|
2018-01-13 16:14:54 +00:00
|
|
|
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)
|
2021-08-13 08:11:13 +00:00
|
|
|
require.IsType(t, &Errors{}, err)
|
|
|
|
var errs = err.(*Errors).Errs
|
2018-01-13 16:14:54 +00:00
|
|
|
assert.Equal(t, 2, len(errs))
|
|
|
|
assert.Equal(t, err1, errs[0])
|
2021-08-13 08:11:13 +00:00
|
|
|
assert.IsType(t, &Errors{}, errs[1])
|
2018-01-13 16:14:54 +00:00
|
|
|
}
|