-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
150 lines (132 loc) · 3.65 KB
/
errors_test.go
File metadata and controls
150 lines (132 loc) · 3.65 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package attest_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/cboone/attest"
)
func TestNoError(t *testing.T) {
t.Run("nil error passes", func(t *testing.T) {
mt := newMockT(t)
attest.NoError(mt, nil)
if mt.failed {
t.Error("NoError(nil) should not fail")
}
})
t.Run("non-nil error fails", func(t *testing.T) {
mt := newMockT(t)
attest.NoError(mt, errors.New("something went wrong"))
if !mt.failed {
t.Error("NoError(err) should fail")
}
if len(mt.messages) > 0 && !strings.Contains(mt.messages[0], "something went wrong") {
t.Errorf("expected error message in output, got: %s", mt.messages[0])
}
})
}
func TestIsError(t *testing.T) {
t.Run("non-nil error passes", func(t *testing.T) {
mt := newMockT(t)
attest.IsError(mt, errors.New("err"))
if mt.failed {
t.Error("IsError(err) should not fail")
}
})
t.Run("nil error fails", func(t *testing.T) {
mt := newMockT(t)
attest.IsError(mt, nil)
if !mt.failed {
t.Error("IsError(nil) should fail")
}
})
}
func TestErrorIs(t *testing.T) {
var errSentinel = errors.New("sentinel")
t.Run("wrapped error matches sentinel", func(t *testing.T) {
mt := newMockT(t)
wrapped := fmt.Errorf("wrap: %w", errSentinel)
attest.ErrorIs(mt, wrapped, errSentinel)
if mt.failed {
t.Error("ErrorIs should pass for wrapped sentinel")
}
})
t.Run("different sentinel fails", func(t *testing.T) {
mt := newMockT(t)
attest.ErrorIs(mt, errors.New("other"), errSentinel)
if !mt.failed {
t.Error("ErrorIs should fail for non-matching sentinel")
}
})
t.Run("nil error fails", func(t *testing.T) {
mt := newMockT(t)
attest.ErrorIs(mt, nil, errSentinel)
if !mt.failed {
t.Error("ErrorIs should fail for nil error")
}
if len(mt.messages) > 0 && !strings.Contains(mt.messages[0], "<nil>") {
t.Errorf("expected '<nil>' in message for nil error, got: %s", mt.messages[0])
}
})
}
type validationError struct {
Field string
Message string
}
func (e *validationError) Error() string {
return fmt.Sprintf("validation error on %s: %s", e.Field, e.Message)
}
func TestErrorAs(t *testing.T) {
t.Run("custom error type extracted", func(t *testing.T) {
mt := newMockT(t)
err := fmt.Errorf("wrap: %w", &validationError{Field: "name", Message: "required"})
var target *validationError
attest.ErrorAs(mt, err, &target)
if mt.failed {
t.Error("ErrorAs should pass for matching error type")
}
if target != nil && target.Field != "name" {
t.Errorf("expected Field=name, got %s", target.Field)
}
})
t.Run("wrong type fails", func(t *testing.T) {
mt := newMockT(t)
err := errors.New("plain error")
var target *validationError
attest.ErrorAs(mt, err, &target)
if !mt.failed {
t.Error("ErrorAs should fail for non-matching error type")
}
})
t.Run("nil error fails", func(t *testing.T) {
mt := newMockT(t)
var target *validationError
attest.ErrorAs(mt, nil, &target)
if !mt.failed {
t.Error("ErrorAs should fail for nil error")
}
})
}
func TestErrorContains(t *testing.T) {
t.Run("substring present passes", func(t *testing.T) {
mt := newMockT(t)
attest.ErrorContains(mt, errors.New("connection refused"), "refused")
if mt.failed {
t.Error("ErrorContains should pass when substring is present")
}
})
t.Run("substring absent fails", func(t *testing.T) {
mt := newMockT(t)
attest.ErrorContains(mt, errors.New("connection refused"), "timeout")
if !mt.failed {
t.Error("ErrorContains should fail when substring is absent")
}
})
t.Run("nil error fails", func(t *testing.T) {
mt := newMockT(t)
attest.ErrorContains(mt, nil, "anything")
if !mt.failed {
t.Error("ErrorContains should fail for nil error")
}
})
}