batcherror_test.go 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package errorx
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const (
  9. err1 = "first error"
  10. err2 = "second error"
  11. )
  12. func TestBatchErrorNil(t *testing.T) {
  13. var batch BatchError
  14. assert.Nil(t, batch.Err())
  15. assert.False(t, batch.NotNil())
  16. batch.Add(nil)
  17. assert.Nil(t, batch.Err())
  18. assert.False(t, batch.NotNil())
  19. }
  20. func TestBatchErrorNilFromFunc(t *testing.T) {
  21. err := func() error {
  22. var be BatchError
  23. return be.Err()
  24. }()
  25. assert.True(t, err == nil)
  26. }
  27. func TestBatchErrorOneError(t *testing.T) {
  28. var batch BatchError
  29. batch.Add(errors.New(err1))
  30. assert.NotNil(t, batch)
  31. assert.Equal(t, err1, batch.Err().Error())
  32. assert.True(t, batch.NotNil())
  33. }
  34. func TestBatchErrorWithErrors(t *testing.T) {
  35. var batch BatchError
  36. batch.Add(errors.New(err1))
  37. batch.Add(errors.New(err2))
  38. assert.NotNil(t, batch)
  39. assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error())
  40. assert.True(t, batch.NotNil())
  41. }