You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
954 B
Go
49 lines
954 B
Go
4 years ago
|
package errorx
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
err1 = "first error"
|
||
|
err2 = "second error"
|
||
|
)
|
||
|
|
||
|
func TestBatchErrorNil(t *testing.T) {
|
||
|
var batch BatchError
|
||
|
assert.Nil(t, batch.Err())
|
||
|
assert.False(t, batch.NotNil())
|
||
|
batch.Add(nil)
|
||
|
assert.Nil(t, batch.Err())
|
||
|
assert.False(t, batch.NotNil())
|
||
|
}
|
||
|
|
||
|
func TestBatchErrorNilFromFunc(t *testing.T) {
|
||
|
err := func() error {
|
||
|
var be BatchError
|
||
|
return be.Err()
|
||
|
}()
|
||
|
assert.True(t, err == nil)
|
||
|
}
|
||
|
|
||
|
func TestBatchErrorOneError(t *testing.T) {
|
||
|
var batch BatchError
|
||
|
batch.Add(errors.New(err1))
|
||
|
assert.NotNil(t, batch)
|
||
|
assert.Equal(t, err1, batch.Err().Error())
|
||
|
assert.True(t, batch.NotNil())
|
||
|
}
|
||
|
|
||
|
func TestBatchErrorWithErrors(t *testing.T) {
|
||
|
var batch BatchError
|
||
|
batch.Add(errors.New(err1))
|
||
|
batch.Add(errors.New(err2))
|
||
|
assert.NotNil(t, batch)
|
||
|
assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error())
|
||
|
assert.True(t, batch.NotNil())
|
||
|
}
|