-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat_test.go
More file actions
99 lines (83 loc) · 2.35 KB
/
Copy pathcompat_test.go
File metadata and controls
99 lines (83 loc) · 2.35 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
package faultline
import (
"errors"
"fmt"
"testing"
)
// sentinel is a custom error type for errors.Is / errors.As testing.
type sentinel struct {
msg string
}
func (s *sentinel) Error() string { return s.msg }
func TestErrorsIsWrap(t *testing.T) {
base := &sentinel{"base"}
wrapped := Wrap(base, "layer1")
wrapped2 := Wrap(wrapped, "layer2")
if !errors.Is(wrapped, base) {
t.Error("errors.Is(Wrap(base), base) should be true")
}
if !errors.Is(wrapped2, base) {
t.Error("errors.Is(Wrap(Wrap(base)), base) should be true")
}
}
func TestErrorsAsWrap(t *testing.T) {
base := &sentinel{"base"}
wrapped := Wrap(base, "layer1")
wrapped2 := Wrap(wrapped, "layer2")
var target *sentinel
if !errors.As(wrapped, &target) {
t.Error("errors.As(Wrap(base), *sentinel) should be true")
}
if target != base {
t.Errorf("errors.As target: got %v, want %v", target, base)
}
target = nil
if !errors.As(wrapped2, &target) {
t.Error("errors.As(Wrap(Wrap(base)), *sentinel) should be true")
}
if target != base {
t.Errorf("errors.As target: got %v, want %v", target, base)
}
}
func TestErrorsIsWithStack(t *testing.T) {
base := &sentinel{"base"}
wrapped := WithStack(base)
if !errors.Is(wrapped, base) {
t.Error("errors.Is(WithStack(base), base) should be true")
}
}
func TestErrorsIsWithMessage(t *testing.T) {
base := &sentinel{"base"}
wrapped := WithMessage(base, "context")
if !errors.Is(wrapped, base) {
t.Error("errors.Is(WithMessage(base), base) should be true")
}
}
func TestErrorsIsNew(t *testing.T) {
err := New("test")
// New errors are unique, so errors.Is with a different error should be false.
other := errors.New("other")
if errors.Is(err, other) {
t.Error("errors.Is(New, other) should be false")
}
}
func TestErrorsAsMixedChain(t *testing.T) {
base := &sentinel{"root"}
chain := WithMessage(Wrap(WithStack(base), "wrap"), "msg")
var target *sentinel
if !errors.As(chain, &target) {
t.Error("errors.As through mixed chain should find sentinel")
}
if target != base {
t.Errorf("errors.As target: got %v, want %v", target, base)
}
}
func TestErrorsIsThroughFmtErrorf(t *testing.T) {
base := &sentinel{"base"}
// Test that Cause() also walks stdlib Unwrap chains.
stdWrapped := fmt.Errorf("stdlib: %w", base)
cause := Cause(stdWrapped)
if cause != base {
t.Errorf("Cause through fmt.Errorf: got %v, want %v", cause, base)
}
}