-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
197 lines (165 loc) · 3.35 KB
/
main_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package goadapt
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
)
func passert(t *testing.T, f func()) {
t.Helper()
r := func() {
if recover() == nil {
t.Errorf("missing panic")
}
}
defer r()
f()
}
// test boolean condition
func tassert(t *testing.T, cond bool, txt string, args ...interface{}) {
t.Helper()
if !cond {
t.Fatalf(txt, args...)
}
}
func massert(t *testing.T, str string, substr string) {
t.Helper()
tassert(t, strings.Contains(str, substr), "can't find %s in %s", substr, str)
}
func TestAssert(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() { log.SetOutput(os.Stderr) }()
f := func() {
Assert(false)
}
passert(t, f)
f = func() {
Assert(false, "foo %s", "bar")
}
passert(t, f)
f = func() {
Assert(false, "foobar")
}
passert(t, f)
}
func TestCheck(t *testing.T) {
f := func() {
Ck(errors.New("foo"))
}
passert(t, f)
g := func() (err error) {
defer Return(&err)
Ck(errors.New("bar"), "some message")
return
}
err := g()
massert(t, err.Error(), "some message")
h := func() (err error) {
defer Return(&err)
Ck(errors.New("bar"), "some message with %s", "formatting")
return
}
err = h()
massert(t, err.Error(), "some message with formatting")
}
func TestReturn(t *testing.T) {
f := func() (err error) {
defer Return(&err)
Ck(errors.New("foo"))
return
}
err := f()
massert(t, err.Error(), "foo")
g := func() (err error) {
defer Return(&err)
// noop
return
}
err = g()
tassert(t, err == nil, "err not nil")
h := func() (err error) {
defer Return(&err)
panic("aliens")
}
j := func() {
err := h()
tassert(t, err == nil, "err not nil")
}
passert(t, j)
k := func() (err error) {
defer Return(&err, "annotation")
Ck(errors.New("foo"))
return
}
err = k()
massert(t, err.Error(), "annotation: ")
m := func() (err error) {
defer Return(&err, "annotation with %s", "formatting")
Ck(errors.New("foo"))
return
}
err = m()
massert(t, err.Error(), "annotation with formatting: ")
q := func() (err error) {
defer Return(&err)
Ck(errors.New("foo"), "ck annotation")
return
}
err = q()
massert(t, err.Error(), "ck annotation")
}
type miderr struct{}
func (e miderr) Error() string {
return "lower error"
}
func (e miderr) Unwrap() error {
return fmt.Errorf("bottom error")
}
func TestUnwrap(t *testing.T) {
f := func() (err error) {
defer Return(&err)
Ck(&miderr{})
return
}
err := f()
x := err
loop:
for i := 0; i < 10; i++ {
switch y := x.(type) {
case *AdaptErr:
// fmt.Printf("%d AdaptErr x %T y %T\n", i, x, y)
x = y.Unwrap()
case *miderr:
// fmt.Printf("%d miderr x %T y %T\n", i, x, y)
x = y.Unwrap()
default:
// fmt.Printf("%d default x %T y %T\n", i, x, y)
tassert(t, i == 3, "unwrap depth %d: %#v", i, y)
tassert(t, x.Error() == "bottom error", "failed bottom unwrap")
break loop
}
}
var e *AdaptErr
tassert(t, errors.As(err, &e), "err not unwrapping to AdaptErr")
var l *miderr
tassert(t, errors.As(err, &l), "err %T not unwrapping to miderr: %v", err, err)
}
func TestUerr(t *testing.T) {
f := func() {
Uerr("foo")
}
passert(t, f)
}
func TestInfo(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() { log.SetOutput(os.Stderr) }()
Info("foo%s", "bar")
massert(t, buf.String(), "foobar")
Info("foo", "bar")
massert(t, buf.String(), "foo bar")
}