This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
any_error_test.go
117 lines (93 loc) · 3.09 KB
/
any_error_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package utils
import (
stdErr "errors"
"fmt"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
var anyNotFoundError, anyNotFoundErrorSupport = NewAnyError(¬FoundError{})
type notFoundError struct {
*AnyErrorSupport
field string
}
func newNotFoundError(field string) *notFoundError {
return ¬FoundError{
AnyErrorSupport: anyNotFoundErrorSupport,
field: field,
}
}
func (e *notFoundError) Unwrap() error {
return e.AnyErrorSupport
}
func (e *notFoundError) Error() string {
return fmt.Sprintf("%s not found", e.field)
}
func TestAnyErrorSupport_DoesNotAffectDefaultIsComparison(t *testing.T) {
notFound := newNotFoundError("something")
notFoundCopy := notFound
require.ErrorIs(t, notFound, notFoundCopy)
require.ErrorIs(t, errors.WithStack(notFound), notFoundCopy)
randomError := stdErr.New("random error")
require.NotErrorIs(t, notFound, randomError)
otherNotFound := newNotFoundError("something else")
require.NotErrorIs(t, notFound, otherNotFound)
}
func TestAnyError_WorksWithDefaultIsComparison(t *testing.T) {
notFound := newNotFoundError("something")
require.ErrorIs(t, notFound, anyNotFoundError)
require.ErrorIs(t, errors.WithStack(notFound), anyNotFoundError)
randomError := stdErr.New("random error")
require.NotErrorIs(t, randomError, anyNotFoundError)
}
type notFoundErrorWithIs struct {
notFoundError
ignoredField string
}
func newNotFoundErrorWithIs(field, ignoredField string) *notFoundErrorWithIs {
return ¬FoundErrorWithIs{
notFoundError: *newNotFoundError(field),
ignoredField: ignoredField,
}
}
func (a *notFoundErrorWithIs) Is(other error) bool {
otherError, ok := other.(*notFoundErrorWithIs)
if !ok {
return false
}
return a.field == otherError.field
}
func TestAnyErrorSupport_DoesNotAffectOverriddenIsComparison(t *testing.T) {
notFound := newNotFoundErrorWithIs("some value", "some ignored value")
notFoundCopy := notFound
notFoundCopy.ignoredField = "other ignored value"
require.ErrorIs(t, notFound, notFoundCopy)
randomError := stdErr.New("random error")
require.NotErrorIs(t, notFound, randomError)
otherNotFound := newNotFoundErrorWithIs("other value", "some ignored value")
require.NotErrorIs(t, notFound, otherNotFound)
}
func TestAnyError_WorksWithOverriddenIsComparison(t *testing.T) {
notFound := newNotFoundErrorWithIs("some value", "some ignored value")
require.ErrorIs(t, notFound, anyNotFoundError)
require.ErrorIs(t, errors.WithStack(notFound), anyNotFoundError)
}
var anyInvalidArgumentError, anyInvalidArgumentErrorSupport = NewAnyError(&invalidArgumentError{})
type invalidArgumentError struct {
*AnyErrorSupport
arg string
}
func newInvalidArgumentError(arg string) *invalidArgumentError {
return &invalidArgumentError{
AnyErrorSupport: anyInvalidArgumentErrorSupport,
arg: arg,
}
}
func (e *invalidArgumentError) Unwrap() error {
return e.AnyErrorSupport
}
func TestAnyError_TakesIntoAccountErrorType(t *testing.T) {
invalidArg := newInvalidArgumentError("something")
require.ErrorIs(t, invalidArg, anyInvalidArgumentError)
require.NotErrorIs(t, invalidArg, anyNotFoundError)
}