-
Notifications
You must be signed in to change notification settings - Fork 0
/
err_queue_test.go
140 lines (104 loc) · 3.07 KB
/
err_queue_test.go
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
package xerrorz
import (
"errors"
"fmt"
"io"
"strings"
"testing"
"golang.org/x/xerrors"
)
func TestErrQueue0(t *testing.T) {
e1 := errors.New("e1")
eq := ErrQueue{Curr: e1, Queue: nil} // Queue is nil
errStr := fmt.Sprintf("%+v", eq)
errLines := strings.Split(errStr, "\n")
if len(errLines) != 1 {
t.Errorf("Invalid length: %d\n", len(errLines))
}
if !strings.Contains(errLines[0], "e1") {
t.Errorf("l.1 should contain \"e1\"\n")
}
}
func TestErrQueue1(t *testing.T) {
e1 := errors.New("e1")
eq := ErrQueue{Curr: e1, Queue: []*error{}} // Empty queue
errStr := fmt.Sprintf("%+v", eq)
errLines := strings.Split(errStr, "\n")
if len(errLines) != 1 {
t.Errorf("Invalid length: %d\n", len(errLines))
}
if !strings.Contains(errLines[0], "e1") {
t.Errorf("l.1 should contain \"e1\"\n")
}
}
func TestErrQueue2(t *testing.T) {
e1 := errors.New("e1")
e2 := errors.New("e2")
eq := ErrQueue{Curr: e1, Queue: []*error{&e2}} // Queue has a single element
errStr := fmt.Sprintf("%+v", eq)
errLines := strings.Split(errStr, "\n")
if len(errLines) != 2 {
t.Errorf("Invalid length: %d\n", len(errLines))
}
if !strings.Contains(errLines[0], "e1") {
t.Errorf("l.1 should contain \"e1\"\n")
}
if !strings.Contains(errLines[1], "e2") {
t.Errorf("l.2 should contain \"e2\"\n")
}
}
func TestErrQueue3(t *testing.T) {
e1 := errors.New("e1")
e2 := errors.New("e2")
e3 := errors.New("e3")
eq := ErrQueue{Curr: e1, Queue: []*error{&e2, &e3}} // Queue has multiple elements
errStr := fmt.Sprintf("%+v", eq)
errLines := strings.Split(errStr, "\n")
if len(errLines) != 3 {
t.Errorf("Invalid length: %d\n", len(errLines))
}
if !strings.Contains(errLines[0], "e1") {
t.Errorf("l.1 should contain \"e1\"\n")
}
if !strings.Contains(errLines[1], "e2") {
t.Errorf("l.2 should contain \"e2\"\n")
}
if !strings.Contains(errLines[2], "e3") {
t.Errorf("l.3 should contain \"e3\"\n")
}
}
func TestErrQueue4(t *testing.T) {
// Nested errors by xerrors.Wrapper
e1 := xerrors.Errorf("e1: %w", io.ErrClosedPipe)
e2 := xerrors.Errorf("e2: %w", e1)
e3 := xerrors.Errorf("e3: %w", e2)
f1 := xerrors.Errorf("f1: %w", io.ErrNoProgress)
g1 := xerrors.Errorf("g1")
eq := ErrQueue{Curr: e3, Queue: []*error{&f1, &g1}}
errStr := fmt.Sprintf("%+v", eq)
errLines := strings.Split(errStr, "\n")
if len(errLines) != 17 {
t.Errorf("Invalid length: %d\n", len(errLines))
}
if !strings.Contains(errLines[0], "e3") {
t.Errorf("l.1 should contain \"e3\"\n")
}
if !strings.Contains(errLines[3], "e2") {
t.Errorf("l.4 should contain \"e2\"\n")
}
if !strings.Contains(errLines[6], "e1") {
t.Errorf("l.7 should contain \"e1\"\n")
}
if !strings.Contains(errLines[9], io.ErrClosedPipe.Error()) {
t.Errorf("l.10 should contain \"%s\"\n", io.ErrClosedPipe.Error())
}
if !strings.Contains(errLines[10], "f1") {
t.Errorf("l.11 should contain \"f1\"\n")
}
if !strings.Contains(errLines[13], io.ErrNoProgress.Error()) {
t.Errorf("l.14 should contain \"%s\"\n", io.ErrNoProgress.Error())
}
if !strings.Contains(errLines[14], "g1") {
t.Errorf("l.15 should contain \"g1\"\n")
}
}