-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
53 lines (45 loc) · 1.17 KB
/
Copy patherrors_test.go
File metadata and controls
53 lines (45 loc) · 1.17 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
package go2errorsinspection_test
import (
"testing"
errors "github.com/sidh/go2errorsinspection"
"github.com/stretchr/testify/assert"
)
func TestIs(t *testing.T) {
sentinel := errors.New("sentinel")
// Sentinel error wrapped by another error, Is can find sentinel error
{
err := errors.WithError(sentinel, errors.New("wrapper"))
assert.True(t, errors.Is(err, sentinel))
}
// Sentinel error wraps another error, Is cannot find sentinel error
// This check is impossible to implement
{
err := errors.WithError(errors.New("wrapped"), sentinel)
assert.True(t, errors.Is(err, sentinel))
}
}
func TestAs(t *testing.T) {
type myError struct {
errors.ErrText
}
// Typed error wrapped by another error, As can find typed error
{
var me *myError
err := errors.WithError(
&myError{ErrText: errors.ErrText{
Text: "my error message",
}},
errors.New("wrapper"),
)
assert.True(t, errors.AsValue(&me, err))
}
// Typed error wraps another error, As can find typed error
{
var me *myError
err := &myError{ErrText: errors.ErrText{
Text: "my error message",
Wrapped: errors.New("wrapped"),
}}
assert.True(t, errors.AsValue(&me, err))
}
}