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
|
package xerrors
import (
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSentinelWrapNil(t *testing.T) {
sentinel := NewSentinel("sentinel")
assert.Panics(t, func() { assert.NoError(t, sentinel.Wrap(nil)) })
}
func TestSentinelWrap(t *testing.T) {
sentinel := NewSentinel("sentinel")
assert.EqualError(t, sentinel.Wrap(New("err")), "sentinel: err")
}
func TestSentinelMultiWrap(t *testing.T) {
top := NewSentinel("top")
middle := NewSentinel("middle")
err := top.Wrap(middle.Wrap(New("bottom")))
assert.EqualError(t, err, "top: middle: bottom")
}
func TestSentinelIs(t *testing.T) {
sentinel := NewSentinel("sentinel")
assert.True(t, Is(sentinel, sentinel))
assert.True(t, Is(sentinel.Wrap(New("err")), sentinel))
assert.True(t, Is(NewSentinel("err").Wrap(sentinel), sentinel))
assert.True(t, Is(Errorf("wrapper: %w", sentinel), sentinel))
assert.True(t, Is(sentinel.WithStackTrace(), sentinel))
assert.True(t, Is(Errorf("wrapper: %w", sentinel.WithStackTrace()), sentinel))
}
func TestSentinelMultiWrapIs(t *testing.T) {
top := NewSentinel("top")
middle := NewSentinel("middle")
err := top.Wrap(middle.Wrap(io.EOF))
assert.True(t, Is(err, top))
assert.True(t, Is(err, middle))
assert.True(t, Is(err, io.EOF))
assert.False(t, Is(err, New("random")))
}
func TestSentinelAs(t *testing.T) {
sentinel := NewSentinel("sentinel")
var target *Sentinel
assert.True(t, As(sentinel, &target))
assert.NotNil(t, target)
target = nil
assert.True(t, As(sentinel.Wrap(New("err")), &target))
assert.NotNil(t, target)
target = nil
assert.True(t, As(NewSentinel("err").Wrap(sentinel), &target))
assert.NotNil(t, target)
target = nil
assert.True(t, As(Errorf("wrapper: %w", sentinel), &target))
assert.NotNil(t, target)
target = nil
assert.True(t, As(sentinel.WithStackTrace(), &target))
assert.NotNil(t, target)
target = nil
assert.True(t, As(Errorf("wrapper: %w", sentinel.WithStackTrace()), &target))
assert.NotNil(t, target)
target = nil
}
func TestSentinelMultiWrapAs(t *testing.T) {
top := NewSentinel("top")
middle := NewSentinel("middle")
err := top.Wrap(middle.Wrap(io.EOF))
var target *Sentinel
assert.True(t, As(err, &target))
assert.NotNil(t, target)
}
func TestSentinelFormatting(t *testing.T) {
sentinel := NewSentinel("sentinel")
assert.Equal(t, "sentinel", fmt.Sprintf("%s", sentinel))
assert.Equal(t, "sentinel", fmt.Sprintf("%v", sentinel))
assert.Equal(t, "sentinel", fmt.Sprintf("%+v", sentinel))
}
|