2020-11-05 12:20:57 +00:00
|
|
|
package agherr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-16 16:11:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-05 12:20:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestError_Error(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-03-16 16:11:32 +00:00
|
|
|
err error
|
2020-11-05 12:20:57 +00:00
|
|
|
name string
|
|
|
|
want string
|
|
|
|
}{{
|
2021-03-16 16:11:32 +00:00
|
|
|
err: Many("a"),
|
2020-11-05 12:20:57 +00:00
|
|
|
name: "simple",
|
|
|
|
want: "a",
|
|
|
|
}, {
|
2021-03-16 16:11:32 +00:00
|
|
|
err: Many("a", errors.New("b")),
|
2020-11-05 12:20:57 +00:00
|
|
|
name: "wrapping",
|
|
|
|
want: "a: b",
|
|
|
|
}, {
|
2021-03-16 16:11:32 +00:00
|
|
|
err: Many("a", errors.New("b"), errors.New("c"), errors.New("d")),
|
2020-11-05 12:20:57 +00:00
|
|
|
name: "wrapping several",
|
|
|
|
want: "a: b (hidden: c, d)",
|
|
|
|
}, {
|
2021-03-16 16:11:32 +00:00
|
|
|
err: Many("a", Many("b", errors.New("c"), errors.New("d"))),
|
2020-11-05 12:20:57 +00:00
|
|
|
name: "wrapping wrapper",
|
|
|
|
want: "a: b: c (hidden: d)",
|
|
|
|
}}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
assert.Equal(t, tc.want, tc.err.Error(), tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError_Unwrap(t *testing.T) {
|
2020-12-08 13:01:13 +00:00
|
|
|
var _ wrapper = &manyError{}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
const (
|
|
|
|
errSimple = iota
|
|
|
|
errWrapped
|
|
|
|
errNil
|
|
|
|
)
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
errs := []error{
|
|
|
|
errSimple: errors.New("a"),
|
2020-11-18 12:43:28 +00:00
|
|
|
errWrapped: fmt.Errorf("err: %w", errors.New("nested")),
|
2020-11-05 12:20:57 +00:00
|
|
|
errNil: nil,
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
testCases := []struct {
|
|
|
|
want error
|
|
|
|
wrapped error
|
2021-03-16 16:11:32 +00:00
|
|
|
name string
|
2020-11-05 12:20:57 +00:00
|
|
|
}{{
|
|
|
|
want: errs[errSimple],
|
|
|
|
wrapped: Many("a", errs[errSimple]),
|
2021-03-16 16:11:32 +00:00
|
|
|
name: "simple",
|
2020-11-05 12:20:57 +00:00
|
|
|
}, {
|
|
|
|
want: errs[errWrapped],
|
|
|
|
wrapped: Many("b", errs[errWrapped]),
|
2021-03-16 16:11:32 +00:00
|
|
|
name: "nested",
|
2020-11-05 12:20:57 +00:00
|
|
|
}, {
|
|
|
|
want: errs[errNil],
|
|
|
|
wrapped: Many("c", errs[errNil]),
|
2021-03-16 16:11:32 +00:00
|
|
|
name: "nil passed",
|
2020-11-05 12:20:57 +00:00
|
|
|
}, {
|
|
|
|
want: nil,
|
|
|
|
wrapped: Many("d"),
|
2021-03-16 16:11:32 +00:00
|
|
|
name: "nil not passed",
|
2020-11-05 12:20:57 +00:00
|
|
|
}}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
assert.Equal(t, tc.want, errors.Unwrap(tc.wrapped), tc.name)
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
func TestAnnotate(t *testing.T) {
|
|
|
|
const s = "1234"
|
|
|
|
const wantMsg = `bad string "1234": test`
|
|
|
|
|
|
|
|
// Don't use const, because we can't take a pointer of a constant.
|
|
|
|
var errTest error = Error("test")
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var errPtr *error
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
Annotate("bad string %q: %w", errPtr, s)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non_nil", func(t *testing.T) {
|
|
|
|
errPtr := &errTest
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
Annotate("bad string %q: %w", errPtr, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NotNil(t, errPtr)
|
|
|
|
|
|
|
|
err := *errPtr
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, wantMsg, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("defer", func(t *testing.T) {
|
|
|
|
f := func() (err error) {
|
|
|
|
defer Annotate("bad string %q: %w", &errTest, s)
|
|
|
|
|
|
|
|
return errTest
|
|
|
|
}
|
|
|
|
|
|
|
|
err := f()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, wantMsg, err.Error())
|
|
|
|
})
|
|
|
|
}
|