Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions go/bytes2/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@ package bytes2

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuffer(t *testing.T) {
b := NewBuffer(nil)

// Test Write function
b.Write([]byte("ab"))
assert.Equal(t, "ab", string(b.Bytes()), "Write()")

// Test WriteString function
b.WriteString("cd")
assert.Equal(t, "abcd", string(b.Bytes()), "WriteString()")

// Test WriteByte function
b.WriteByte('e')
want := "abcde"
if got := string(b.Bytes()); got != want {
t.Errorf("b.Bytes(): %s, want %s", got, want)
}
if got := b.String(); got != want {
t.Errorf("b.String(): %s, want %s", got, want)
}
if got := b.Len(); got != 5 {
t.Errorf("b.Len(): %d, want 5", got)
}
if got := b.StringUnsafe(); got != want {
t.Errorf("b.StringUnsafe(): %s, want %s", got, want)
}
if b.Reset(); len(b.bytes) != 0 {
t.Errorf("b.Reset(): got %s, want \"\"", b.bytes)
}
assert.Equal(t, "abcde", string(b.Bytes()), "WriteByte()")

// Test Bytes function
assert.Equal(t, "abcde", string(b.Bytes()))

// Test String function
assert.Equal(t, "abcde", b.String())

// Test StringUnsafe function
assert.Equal(t, "abcde", b.StringUnsafe())

// Test Len function
assert.Equal(t, 5, b.Len())

// Test Reset function
b.Reset()
assert.Equal(t, "", string(b.Bytes()))
assert.Equal(t, 0, b.Len())
}