mv pkg/mockwriter/ -> pkg/fake/

pull/2/head
q191201771 5 years ago
parent 4e9609f337
commit 34957541d3

@ -6,15 +6,13 @@
//
// Author: Chef (191201771@qq.com)
package mockwriter
package fake
import (
"bytes"
"errors"
)
// TODO chef: 可以添加一个接口,获取内部 buffer 的内容
type WriterType uint8
const (
@ -24,28 +22,28 @@ const (
)
var (
ErrMockWriter = errors.New("naza.mockwriter: a mock error")
ErrFakeWriter = errors.New("naza.fake: a fake writer error")
)
type MockWriter struct {
type Writer struct {
t WriterType
ts map[uint32]WriterType
count uint32
b bytes.Buffer
B bytes.Buffer
}
func NewMockWriter(t WriterType) *MockWriter {
return &MockWriter{
func NewWriter(t WriterType) *Writer {
return &Writer{
t: t,
}
}
// 为某些写操作指定特定的类型,次数从 0 开始计数
func (w *MockWriter) SetSpecificType(ts map[uint32]WriterType) {
func (w *Writer) SetSpecificType(ts map[uint32]WriterType) {
w.ts = ts
}
func (w *MockWriter) Write(b []byte) (int, error) {
func (w *Writer) Write(b []byte) (int, error) {
t, exist := w.ts[w.count]
w.count++
if !exist {
@ -55,11 +53,8 @@ func (w *MockWriter) Write(b []byte) (int, error) {
case WriterTypeDoNothing:
return len(b), nil
case WriterTypeReturnError:
return 0, ErrMockWriter
//case WriterTypeIntoBuffer:
// return w.b.Write(b)
return 0, ErrFakeWriter
}
return w.b.Write(b)
//panic("never reach here.")
return w.B.Write(b)
}

@ -0,0 +1,83 @@
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package fake_test
import (
"testing"
"github.com/q191201771/naza/pkg/fake"
"github.com/q191201771/naza/pkg/assert"
)
func TestNewWriter(t *testing.T) {
_ = fake.NewWriter(fake.WriterTypeDoNothing)
}
func TestWriter_Write(t *testing.T) {
var (
w *fake.Writer
n int
err error
b = []byte("hello")
)
w = fake.NewWriter(fake.WriterTypeDoNothing)
n, err = w.Write(b)
assert.Equal(t, 5, n)
assert.Equal(t, nil, err)
w = fake.NewWriter(fake.WriterTypeReturnError)
n, err = w.Write(b)
assert.Equal(t, 0, n)
assert.Equal(t, fake.ErrFakeWriter, err)
w = fake.NewWriter(fake.WriterTypeIntoBuffer)
n, err = w.Write(b)
assert.Equal(t, 5, n)
assert.Equal(t, nil, err)
}
func TestWriter_SetSpecificType(t *testing.T) {
var (
w *fake.Writer
n int
err error
b = []byte("hello")
)
w = fake.NewWriter(fake.WriterTypeDoNothing)
w.SetSpecificType(map[uint32]fake.WriterType{
0: fake.WriterTypeReturnError,
2: fake.WriterTypeReturnError,
4: fake.WriterTypeDoNothing,
})
expectedLen := map[int]int{
0: 0,
1: 5,
2: 0,
3: 5,
4: 5,
5: 5,
}
expectedErr := map[int]error{
0: fake.ErrFakeWriter,
1: nil,
2: fake.ErrFakeWriter,
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)
}
}

@ -1,105 +0,0 @@
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package mockwriter
import (
"testing"
"github.com/q191201771/naza/pkg/assert"
)
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, ErrMockWriter, 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: ErrMockWriter,
1: nil,
2: ErrMockWriter,
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)
}
}
func BenchmarkNewMockWriter(b *testing.B) {
b.ReportAllocs()
var tmp uint32
for i := 0; i < b.N; i++ {
mw := NewMockWriter(WriterTypeDoNothing)
tmp = tmp + mw.count
}
}
func newMockWriter2(t WriterType) MockWriter {
return MockWriter{
t: t,
}
}
func BenchmarkNewMockWriter2(b *testing.B) {
b.ReportAllocs()
var tmp uint32
for i := 0; i < b.N; i++ {
mw := newMockWriter2(WriterTypeDoNothing)
tmp = tmp + mw.count
}
}
Loading…
Cancel
Save