-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
159 lines (136 loc) · 3.87 KB
/
errors_test.go
File metadata and controls
159 lines (136 loc) · 3.87 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
151
152
153
154
155
156
157
158
159
//go:build !debug
package errors
import (
"fmt"
"io"
"testing"
)
func TestMarshal(t *testing.T) {
// Single error. No user is set, so we will have a zero-length field inside.
e1 := E(Op("Get"), Serve, "caching in progress")
// Nested error.
e2 := E(Op("Read"), Undefined, e1)
b := MarshalError(e2)
e3 := UnmarshalError(b)
in := e2.(*Error)
out := e3.(*Error)
// Compare elementwise.
if in.Op != out.Op {
t.Errorf("expected Op %q; got %q", in.Op, out.Op)
}
if in.Kind != out.Kind {
t.Errorf("expected kind %d; got %d", in.Kind, out.Kind)
}
// Note that error will have lost type information, so just check its Error string.
if in.Err.Error() != out.Err.Error() {
t.Errorf("expected Err %q; got %q", in.Err, out.Err)
}
// time
if in.Raised != out.Raised {
t.Errorf("expected time %s; got %s", in.Raised, out.Raised)
}
}
func TestSeparator(t *testing.T) {
defer func(prev string) {
Separator = prev
}(Separator)
Separator = ":: "
// Single error. No user is set, so we will have a zero-length field inside.
e1 := E(Op("Get"), Serve, "Serve error")
// Nested error.
e2 := E(Op("Get"), Serve, e1)
want := "Get: Serve error:: Get: Serve error"
if errorAsString(e2) != want {
t.Errorf("expected %q; got %q", want, e2)
}
}
func TestDoesNotChangePreviousError(t *testing.T) {
err := E(Serve)
err2 := E(Op("I will NOT modify err"), err)
expected := "I will NOT modify err: Serve error"
if errorAsString(err2) != expected {
t.Fatalf("Expected %q, got %q", expected, err2)
}
kind := err.(*Error).Kind
if kind != Serve {
t.Fatalf("Expected kind %v, got %v", Serve, kind)
}
}
type matchTest struct {
err1, err2 error
matched bool
}
const (
op = Op("Op")
op1 = Op("Op1")
op2 = Op("Op2")
)
var matchTests = []matchTest{
// Errors not of type *Error fail outright.
{nil, nil, false},
{io.EOF, io.EOF, false},
{E(io.EOF), io.EOF, false},
{io.EOF, E(io.EOF), false},
// Success. We can drop fields from the first argument and still match.
{E(io.EOF), E(io.EOF), true},
{E(op, Init, io.EOF), E(op, Init, io.EOF), true},
{E(op, Init, io.EOF, "test"), E(op, Init, io.EOF, "test", "test"), true},
{E(op, Init), E(op, Init, io.EOF, "test", "test"), true},
{E(op), E(op, Init, io.EOF, "test", "test"), true},
// Failure.
{E(io.EOF), E(io.ErrClosedPipe), false},
{E(op1), E(op2), false},
{E(Init), E(Serve), false},
{E("test"), E("test1"), false},
{E(fmt.Errorf("error")), E(fmt.Errorf("error1")), false},
{E(op, Init, io.EOF, "test", "test1"), E(op, Init, io.EOF, "test", "test"), false},
{E("test", Str("something")), E("test"), false}, // Test nil error on rhs.
// Nested *Errors.
{E(op1, E("test")), E(op1, "1", E(op2, "2", "test")), true},
{E(op1, "test"), E(op1, "1", E(op2, "2", "test")), false},
{E(op1, E("test")), E(op1, "1", Str(E(op2, "2", "test").Error())), false},
}
func TestMatch(t *testing.T) {
for _, test := range matchTests {
matched := Match(test.err1, test.err2)
if matched != test.matched {
t.Errorf("Match(%q, %q)=%t; want %t", test.err1, test.err2, matched, test.matched)
}
}
}
type kindTest struct {
err error
kind Kind
want bool
}
var kindTests = []kindTest{
// Non-Error errors.
{nil, Serve, false},
{Str("not an *Error"), Serve, false},
// Basic comparisons.
{E(Serve), Serve, true},
{E(Init), Serve, false},
{E("no kind"), Serve, false},
{E("no kind"), Logger, false},
// Nested *Error values.
{E("Nesting", E(Serve)), Serve, true},
{E("Nesting", E(Logger)), Serve, false},
{E("Nesting", E("no kind")), Serve, false},
{E("Nesting", E("no kind")), Logger, false},
}
func TestKind(t *testing.T) {
for _, test := range kindTests {
got := Is(test.kind, test.err)
if got != test.want {
t.Errorf("Is(%q, %q)=%t; want %t", test.kind, test.err, got, test.want)
}
}
}
func errorAsString(err error) string {
if e, ok := err.(*Error); ok {
e2 := *e
e2.stack = stack{}
return e2.Error()
}
return err.Error()
}