-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
88 lines (71 loc) · 2.03 KB
/
writer_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
package mock
import (
"testing"
"io"
"errors"
assertFactory "github.com/stretchr/testify/assert"
)
func TestNewWriter(t *testing.T) {
assert := assertFactory.New(t)
w := NewWriter()
assert.Equal(&writer{}, w)
}
func TestNewWriteCloser(t *testing.T) {
assert := assertFactory.New(t)
wc := NewWriteCloser()
assert.Implements((*io.Writer)(nil), wc)
assert.Implements((*io.Closer)(nil), wc)
}
func TestWriter_Write(t *testing.T) {
assert := assertFactory.New(t)
t.Run("should append data multiple times, and return added length and empty error", func(t *testing.T) {
w := &writer{}
n, err := w.Write([]byte("<foo>"))
assert.Equal(5, n)
assert.Nil(err)
assert.Equal("<foo>", w.WrittenData())
n, err = w.Write([]byte("<en>"))
assert.Equal(4, n)
assert.Nil(err)
assert.Equal("<foo><en>", w.WrittenData())
})
t.Run("should return specified length and error", func(t *testing.T) {
expectedN := 7
expectedError := errors.New("<err>")
w := &writer{returnN: expectedN, returnErr: expectedError}
n, err := w.Write([]byte("foo"))
assert.Equal(expectedN, n)
assert.Equal(expectedError, err)
assert.Equal("foo", w.WrittenData())
})
}
func TestWriter_WriteShouldReturn(t *testing.T) {
assert := assertFactory.New(t)
var logMsg string
w := &writer{
// mock log-function to store log to a local variable
log: func(v ...interface{}) {
logMsg = v[0].(string)
},
}
t.Run("should set specified values", func(t *testing.T) {
expectedN := int(6)
expectedError := errors.New("<err>")
w.WriteShouldReturn(expectedN, expectedError)
assert.Equal(expectedN, w.returnN)
assert.Equal(expectedError, w.returnErr)
assert.Empty(logMsg)
})
t.Run("should set nil values", func(t *testing.T) {
w.WriteShouldReturn(nil, nil)
assert.Nil(w.returnN)
assert.Nil(w.returnErr)
assert.Empty(logMsg)
})
t.Run("should fail if 'n' is not int or nil", func(t *testing.T) {
w.WriteShouldReturn(int64(7), errors.New("<err>"))
assert.Nil(w.returnN)
assert.Nil(w.returnErr)
assert.Contains(logMsg, "int or nil")
})
}