-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_test.go
91 lines (70 loc) · 1.87 KB
/
custom_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package oops_test
import (
"errors"
"testing"
"github.com/calebcase/oops"
"github.com/stretchr/testify/require"
)
type CustomError struct {
Message string
}
func NewCustomError(msg string) *CustomError {
return &CustomError{
Message: msg,
}
}
var _ error = &CustomError{}
func (ce *CustomError) Error() string {
return ce.Message
}
func TestCustomError(t *testing.T) {
t.Run("new", func(t *testing.T) {
cerr := NewCustomError("custom")
err := oops.New("%w", cerr)
ce := &CustomError{}
require.ErrorIs(t, err, cerr)
require.ErrorAs(t, err, &ce)
require.Equal(t, cerr, ce)
require.Equal(t, cerr, errors.Unwrap(errors.Unwrap(err)))
})
t.Run("trace", func(t *testing.T) {
cerr := NewCustomError("custom")
err := oops.Trace(cerr)
ce := &CustomError{}
require.ErrorIs(t, err, cerr)
require.ErrorAs(t, err, &ce)
require.Equal(t, cerr, ce)
require.Equal(t, cerr, errors.Unwrap(err))
})
t.Run("namespace", func(t *testing.T) {
ns := oops.Namespace("test")
cerr := NewCustomError("custom")
err := ns.Wrap(cerr)
ce := &CustomError{}
require.ErrorIs(t, err, cerr)
require.ErrorAs(t, err, &ce)
require.Equal(t, ce, cerr)
require.Equal(t, ce, errors.Unwrap(err))
})
t.Run("shadow", func(t *testing.T) {
herr := errors.New("hidden")
cerr := NewCustomError("custom")
err := oops.Shadow(herr, cerr)
ce := &CustomError{}
require.ErrorIs(t, err, cerr)
require.ErrorAs(t, err, &ce)
require.Equal(t, ce, cerr)
require.Equal(t, ce, errors.Unwrap(err))
})
t.Run("chain", func(t *testing.T) {
first := errors.New("first")
cerr := NewCustomError("custom")
err := oops.Chain(first, cerr)
ce := &CustomError{}
require.ErrorIs(t, err, cerr)
require.ErrorAs(t, err, &ce)
require.Equal(t, cerr, ce)
require.Equal(t, oops.ChainError{cerr}, errors.Unwrap(err))
require.Equal(t, cerr, errors.Unwrap(errors.Unwrap(err)))
})
}