Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: improve coverage for go/bytes2/buffer.go #14958

Merged
merged 6 commits into from
Jan 19, 2024
Merged
Changes from 1 commit
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
48 changes: 35 additions & 13 deletions go/bytes2/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,45 @@ limitations under the License.

package bytes2

import (
"testing"
)
import "testing"

// Checks if the actual value is equal to the expected value for the given function
func assertEqual(t *testing.T, got any, want any, funcName string) {
if got != want {
t.Errorf("%s: got %v, want %v", funcName, got, want)
}
}
EshaanAgg marked this conversation as resolved.
Show resolved Hide resolved

func TestBuffer(t *testing.T) {
// Initialize a new buffer
b := NewBuffer(nil)

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

// Test WriteString function
b.WriteString("cd")
assertEqual(t, string(b.Bytes()), "abcd", "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)
}
assertEqual(t, string(b.Bytes()), "abcde", "WriteByte()")

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

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

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

// Test Len function
assertEqual(t, b.Len(), 5, "Len()")

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