mirror of https://github.com/q191201771/naza
新增 pkg/mockwriter 用于测试其它使用 Writer 接口的代码
parent
1fb1a840b7
commit
23806813af
@ -0,0 +1,58 @@
|
||||
package mockwriter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// TODO chef: 可以添加一个接口,获取内部 buffer 的内容
|
||||
|
||||
type WriterType uint8
|
||||
|
||||
const (
|
||||
WriterTypeDoNothing WriterType = iota
|
||||
WriterTypeReturnError
|
||||
WriterTypeIntoBuffer
|
||||
)
|
||||
|
||||
var (
|
||||
mockWriterErr = errors.New("mockwriter: a mock error")
|
||||
)
|
||||
|
||||
type MockWriter struct {
|
||||
t WriterType
|
||||
ts map[uint32]WriterType
|
||||
count uint32
|
||||
b bytes.Buffer
|
||||
}
|
||||
|
||||
func NewMockWriter(t WriterType) *MockWriter {
|
||||
return &MockWriter{
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
// 为某些写操作指定特定的类型,次数从 0 开始计数
|
||||
func (w *MockWriter) SetSpecificType(ts map[uint32]WriterType) {
|
||||
w.ts = ts
|
||||
}
|
||||
|
||||
func (w *MockWriter) Write(b []byte) (int, error) {
|
||||
t, exist := w.ts[w.count]
|
||||
w.count++
|
||||
if !exist {
|
||||
t = w.t
|
||||
}
|
||||
switch t {
|
||||
case WriterTypeDoNothing:
|
||||
return len(b), nil
|
||||
case WriterTypeReturnError:
|
||||
return 0, mockWriterErr
|
||||
//case WriterTypeIntoBuffer:
|
||||
// return w.b.Write(b)
|
||||
}
|
||||
|
||||
return w.b.Write(b)
|
||||
|
||||
//panic("never reach here.")
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package mockwriter
|
||||
|
||||
import (
|
||||
"github.com/q191201771/nezha/pkg/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewMockWriter(t *testing.T) {
|
||||
_ = NewMockWriter(WriterTypeDoNothing)
|
||||
}
|
||||
|
||||
func TestMockWriter_Write(t *testing.T) {
|
||||
var (
|
||||
w *MockWriter
|
||||
n int
|
||||
err error
|
||||
b = []byte("hello")
|
||||
)
|
||||
|
||||
w = NewMockWriter(WriterTypeDoNothing)
|
||||
n, err = w.Write(b)
|
||||
assert.Equal(t, 5, n)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
w = NewMockWriter(WriterTypeReturnError)
|
||||
n, err = w.Write(b)
|
||||
assert.Equal(t, 0, n)
|
||||
assert.Equal(t, mockWriterErr, err)
|
||||
|
||||
w = NewMockWriter(WriterTypeIntoBuffer)
|
||||
n, err = w.Write(b)
|
||||
assert.Equal(t, 5, n)
|
||||
assert.Equal(t, nil, err)
|
||||
}
|
||||
|
||||
func TestMockWriter_SetSpecificType(t *testing.T) {
|
||||
var (
|
||||
w *MockWriter
|
||||
n int
|
||||
err error
|
||||
b = []byte("hello")
|
||||
)
|
||||
w = NewMockWriter(WriterTypeDoNothing)
|
||||
w.SetSpecificType(map[uint32]WriterType{
|
||||
0: WriterTypeReturnError,
|
||||
2: WriterTypeReturnError,
|
||||
4: WriterTypeDoNothing,
|
||||
})
|
||||
|
||||
expectedLen := map[int]int{
|
||||
0: 0,
|
||||
1: 5,
|
||||
2: 0,
|
||||
3: 5,
|
||||
4: 5,
|
||||
5: 5,
|
||||
}
|
||||
expectedErr := map[int]error{
|
||||
0: mockWriterErr,
|
||||
1: nil,
|
||||
2: mockWriterErr,
|
||||
3: nil,
|
||||
4: nil,
|
||||
5: nil,
|
||||
}
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
n, err = w.Write(b)
|
||||
assert.Equal(t, expectedLen[i], n)
|
||||
assert.Equal(t, expectedErr[i], err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue