testing: use strings.Builder

Change-Id: Ied8a3019f1e21c0e74c798d2b1b279a606a38554
Reviewed-on: https://go-review.googlesource.com/c/go/+/428279
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
cuiweixie 2022-09-04 19:18:50 +08:00 committed by Daniel Martí
parent f4bf9bceac
commit ed530dbd07
6 changed files with 17 additions and 17 deletions

View File

@ -5,7 +5,6 @@
package testing
import (
"bytes"
"errors"
"flag"
"fmt"
@ -14,6 +13,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
)
@ -379,7 +379,7 @@ func (f *F) Fuzz(ff any) {
// fuzz worker. This would become very verbose, particularly during
// minimization. Return the error instead, and let the caller deal
// with the output.
var buf bytes.Buffer
var buf strings.Builder
if ok := run(&buf, e); !ok {
return errors.New(buf.String())
}

View File

@ -5,13 +5,12 @@
package testing
import (
"bytes"
"regexp"
"strings"
)
func TestTBHelper(t *T) {
var buf bytes.Buffer
var buf strings.Builder
ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
t1 := &T{
common: common{
@ -55,7 +54,7 @@ helperfuncs_test.go:67: 10
}
func TestTBHelperParallel(t *T) {
var buf bytes.Buffer
var buf strings.Builder
ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
t1 := &T{
common: common{

View File

@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"log"
"strings"
"testing"
)
@ -32,12 +33,12 @@ func TestWriteLogger(t *testing.T) {
log.SetOutput(olw)
}()
lOut := new(bytes.Buffer)
lOut := new(strings.Builder)
log.SetPrefix("lw: ")
log.SetOutput(lOut)
log.SetFlags(0)
lw := new(bytes.Buffer)
lw := new(strings.Builder)
wl := NewWriteLogger("write:", lw)
if _, err := wl.Write([]byte("Hello, World!")); err != nil {
t.Fatalf("Unexpectedly failed to write: %v", err)
@ -64,7 +65,7 @@ func TestWriteLogger_errorOnWrite(t *testing.T) {
log.SetOutput(olw)
}()
lOut := new(bytes.Buffer)
lOut := new(strings.Builder)
log.SetPrefix("lw: ")
log.SetOutput(lOut)
log.SetFlags(0)
@ -93,7 +94,7 @@ func TestReadLogger(t *testing.T) {
log.SetOutput(olw)
}()
lOut := new(bytes.Buffer)
lOut := new(strings.Builder)
log.SetPrefix("lr: ")
log.SetOutput(lOut)
log.SetFlags(0)
@ -130,7 +131,7 @@ func TestReadLogger_errorOnRead(t *testing.T) {
log.SetOutput(olw)
}()
lOut := new(bytes.Buffer)
lOut := new(strings.Builder)
log.SetPrefix("lr: ")
log.SetOutput(lOut)
log.SetFlags(0)

View File

@ -26,7 +26,7 @@ func TestOneByteReader_nonEmptyReader(t *testing.T) {
b = make([]byte, 3)
// Read from obr until EOF.
got := new(bytes.Buffer)
got := new(strings.Builder)
for i := 0; ; i++ {
n, err = obr.Read(b)
if err != nil {
@ -77,7 +77,7 @@ func TestHalfReader_nonEmptyReader(t *testing.T) {
}
// non empty read buffer
b = make([]byte, 2)
got := new(bytes.Buffer)
got := new(strings.Builder)
for i := 0; ; i++ {
n, err = hr.Read(b)
if err != nil {
@ -190,7 +190,7 @@ func TestDataErrReader_nonEmptyReader(t *testing.T) {
der := DataErrReader(buf)
b := make([]byte, 3)
got := new(bytes.Buffer)
got := new(strings.Builder)
var n int
var err error
for {

View File

@ -5,7 +5,7 @@
package iotest
import (
"bytes"
"strings"
"testing"
)
@ -23,7 +23,7 @@ var truncateWriterTests = []struct {
func TestTruncateWriter(t *testing.T) {
for _, tt := range truncateWriterTests {
buf := new(bytes.Buffer)
buf := new(strings.Builder)
tw := TruncateWriter(buf, tt.trunc)
n, err := tw.Write([]byte(tt.in))
if err != nil {

View File

@ -477,7 +477,7 @@ func TestTRun(t *T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *T) {
ctx := newTestContext(tc.maxPar, newMatcher(regexp.MatchString, "", ""))
buf := &bytes.Buffer{}
buf := &strings.Builder{}
root := &T{
common: common{
signal: make(chan bool),
@ -664,7 +664,7 @@ func TestBRun(t *T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *T) {
var ok bool
buf := &bytes.Buffer{}
buf := &strings.Builder{}
// This is almost like the Benchmark function, except that we override
// the benchtime and catch the failure result of the subbenchmark.
root := &B{