Skip to content

Commit

Permalink
tests: improve coverage for go/bytes2/buffer.go (#14958)
Browse files Browse the repository at this point in the history
Signed-off-by: Eshaan Aggarwal <[email protected]>
  • Loading branch information
EshaanAgg authored Jan 19, 2024
1 parent 737fdb3 commit 9af8692
Showing 1 changed file with 28 additions and 16 deletions.
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())
}

0 comments on commit 9af8692

Please sign in to comment.