-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_test.go
81 lines (69 loc) · 1.24 KB
/
call_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
package errors
import (
"io"
"reflect"
"testing"
)
func TestCall(t *testing.T) {
var err error
e := New("appended error")
AppendCall(&err, func() error {
return e
})
if err != e {
t.Errorf("%v != %v", err, e)
}
}
type mock struct {
w, c error
}
func OpenMock(w, c error) (io.WriteCloser, error) {
return &mock{w, c}, nil
}
func (m *mock) Write(p []byte) (n int, err error) {
return 0, m.w
}
func (m *mock) Close() error {
return m.c
}
func doCall(w, c error) (err error) {
f, err := OpenMock(w, c)
if err != nil {
return err
}
defer AppendCall(&err, f.Close)
_, err = f.Write(nil)
if err != nil {
return err
}
return nil
}
func TestCallNilNil(t *testing.T) {
err := doCall(nil, nil)
if err != nil {
t.Errorf("%v != nil", err)
}
}
func TestCallErrNil(t *testing.T) {
w := New("write error")
err := doCall(w, nil)
if err != w {
t.Errorf("%v != %v", err, w)
}
}
func TestCallNilErr(t *testing.T) {
c := New("close error")
err := doCall(nil, c)
if err != c {
t.Errorf("%v != %v", err, c)
}
}
func TestCallErrErr(t *testing.T) {
w := New("write error")
c := New("close error")
err := doCall(w, c)
correct := &errorList{[]error{w, c}}
if !reflect.DeepEqual(err, correct) {
t.Errorf("%#v != %#v", err, correct)
}
}