Skip to content

Commit

Permalink
✨ feat: byte - Buffer add method for implements more common interface
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 16, 2023
1 parent f929fe1 commit 1350962
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
15 changes: 15 additions & 0 deletions byteutil/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ func (b *Buffer) MustWriteString(ss ...string) {
}
}

// ResetGet buffer string. alias of ResetAndGet()
func (b *Buffer) ResetGet() string {
return b.ResetAndGet()
}

// ResetAndGet buffer string.
func (b *Buffer) ResetAndGet() string {
s := b.String()
b.Reset()
return s
}

// Close buffer
func (b *Buffer) Close() error {
return nil
}

// Flush buffer
func (b *Buffer) Flush() error {
return nil
}
5 changes: 4 additions & 1 deletion byteutil/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ func TestBuffer_WriteAny(t *testing.T) {
assert.Eq(t, "ab-cd-ef", buf.ResetAndGet())

buf.WriteAny(23, "abc")
assert.Eq(t, "23abc", buf.ResetAndGet())
assert.Eq(t, "23abc", buf.ResetGet())

buf.Writeln("abc")
assert.Eq(t, "abc\n", buf.ResetAndGet())

assert.NoErr(t, buf.Close())
assert.NoErr(t, buf.Flush())
}
38 changes: 25 additions & 13 deletions testutil/fakeobj/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Writer struct {
ErrOnClose bool
}

// NewBuffer instance. alias of NewWriter()
func NewBuffer() *Writer {
return NewWriter()
}

// NewWriter instance
func NewWriter() *Writer {
return &Writer{}
Expand All @@ -39,28 +44,35 @@ func (w *Writer) SetErrOnClose() *Writer {
return w
}

// Flush implements
func (w *Writer) Flush() error {
if w.ErrOnFlush {
return errors.New("flush error")
}

// ResetGet buffer string.
func (w *Writer) ResetGet() string {
s := w.String()
w.Reset()
return nil
return s
}

// Write implements
func (w *Writer) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errors.New("fake write error")
}
return w.Buffer.Write(p)
}

// Close implements
func (w *Writer) Close() error {
if w.ErrOnClose {
return errors.New("close error")
return errors.New("fake close error")
}
return nil
}

// Write implements
func (w *Writer) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errors.New("write error")
// Flush implements
func (w *Writer) Flush() error {
if w.ErrOnFlush {
return errors.New("fake flush error")
}
return w.Buffer.Write(p)

w.Reset()
return nil
}

0 comments on commit 1350962

Please sign in to comment.