-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy patherror_test.go
More file actions
145 lines (136 loc) 路 3.03 KB
/
Copy patherror_test.go
File metadata and controls
145 lines (136 loc) 路 3.03 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
package oops
import (
"errors"
"io/fs"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrorsIs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
run func(is *assert.Assertions)
}{
{
name: "Errorf",
run: func(is *assert.Assertions) {
err := Errorf("Error: %w", fs.ErrExist)
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Wrap",
run: func(is *assert.Assertions) {
err := Wrap(fs.ErrExist)
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Wrap_reflexive",
run: func(is *assert.Assertions) {
err := Wrap(fs.ErrExist)
is.ErrorIs(err, err) //nolint:testifylint
},
},
{
name: "Wrapf",
run: func(is *assert.Assertions) {
err := Wrapf(fs.ErrExist, "Error: %w", assert.AnError)
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Join_first",
run: func(is *assert.Assertions) {
err := Join(fs.ErrExist, assert.AnError)
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Join_second",
run: func(is *assert.Assertions) {
err := Join(assert.AnError, fs.ErrExist)
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Recover",
run: func(is *assert.Assertions) {
err := Recover(func() { panic(fs.ErrExist) })
is.ErrorIs(err, fs.ErrExist)
},
},
{
name: "Recoverf",
run: func(is *assert.Assertions) {
err := Recoverf(func() { panic(fs.ErrExist) }, "Error: %w", assert.AnError)
is.ErrorIs(err, fs.ErrExist)
},
},
{
// Two independently created OopsErrors must not match each other.
// Previously, Is() returned true for any OopsError target regardless of identity.
name: "distinct_oops_errors_not_equal",
run: func(is *assert.Assertions) {
err1 := Errorf("error 1")
err2 := Errorf("error 2")
is.False(errors.Is(err1, err2)) //nolint:testifylint
is.False(errors.Is(err2, err1)) //nolint:testifylint
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
is := assert.New(t)
tt.run(is)
})
}
}
func TestErrorsAs(t *testing.T) {
t.Parallel()
var anError error = &fs.PathError{Err: fs.ErrExist}
tests := []struct {
name string
errFn func() error
}{
{
name: "Errorf",
errFn: func() error { return Errorf("Error: %w", anError) },
},
{
name: "Wrap",
errFn: func() error { return Wrap(anError) },
},
{
name: "Wrapf",
errFn: func() error { return Wrapf(anError, "Error: %w", assert.AnError) },
},
{
name: "Join_first",
errFn: func() error { return Join(anError, assert.AnError) },
},
{
name: "Join_second",
errFn: func() error { return Join(assert.AnError, anError) },
},
{
name: "Recover",
errFn: func() error { return Recover(func() { panic(anError) }) },
},
{
name: "Recoverf",
errFn: func() error { return Recoverf(func() { panic(anError) }, "Error: %w", assert.AnError) },
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
is := assert.New(t)
var target *fs.PathError
is.ErrorAs(tt.errFn(), &target)
})
}
}