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

feat: replacing bytes.Buffer instances used only to generate strings with strings.Builder. #224

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions internal/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package color

import (
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -182,13 +181,13 @@ func FromEnv(env, defaultColor string) (string, string, string) {
}

// AppendTestNameOn enables test name color in b.
func AppendTestNameOn(b *bytes.Buffer) {
func AppendTestNameOn(b *strings.Builder) {
Init()
b.WriteString(TestNameOn)
}

// AppendTestNameOff disables test name color in b.
func AppendTestNameOff(b *bytes.Buffer) {
func AppendTestNameOff(b *strings.Builder) {
Init()
b.WriteString(TestNameOff)
}
Expand All @@ -211,7 +210,7 @@ func Bad(s string, args ...any) string {
func BadUsage(usage string, param any, pos int, kind bool) string {
Init()

var b bytes.Buffer
var b strings.Builder
fmt.Fprintf(&b, "%susage: %s, but received ", BadOnBold, usage)

if param == nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package color_test

import (
"bytes"
"os"
"strings"
"testing"

"github.com/maxatome/go-testdeep/internal/color"
Expand All @@ -27,7 +27,7 @@ func TestColor(t *testing.T) {
test.EqualStr(t, bold, "")
test.EqualStr(t, off, "")

var b bytes.Buffer
var b strings.Builder
color.AppendTestNameOn(&b)
test.EqualInt(t, b.Len(), 0)
color.AppendTestNameOff(&b)
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestColor(t *testing.T) {

// Color test name
_, color.TestNameOn, color.TestNameOff = color.FromEnv(color.EnvColorTitle, "yellow")
var b bytes.Buffer
var b strings.Builder
color.AppendTestNameOn(&b)
test.EqualStr(t, b.String(), "\x1b[1;33m")
color.AppendTestNameOff(&b)
Expand Down
7 changes: 3 additions & 4 deletions internal/ctxerr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package ctxerr

import (
"bytes"
"reflect"
"strings"

Expand Down Expand Up @@ -92,7 +91,7 @@ func TypeMismatch(got, expected reflect.Type) *Error {

// Error implements error interface.
func (e *Error) Error() string {
buf := bytes.Buffer{}
buf := strings.Builder{}

e.Append(&buf, "")

Expand All @@ -101,7 +100,7 @@ func (e *Error) Error() string {

// Append appends the a contents to buf using prefix prefix for each
// line.
func (e *Error) Append(buf *bytes.Buffer, prefix string) {
func (e *Error) Append(buf *strings.Builder, prefix string) {
if e == BooleanError {
return
}
Expand Down Expand Up @@ -209,7 +208,7 @@ func (e *Error) SummaryString() string {
return ""
}

var buf bytes.Buffer
var buf strings.Builder
e.Summary.AppendSummary(&buf, "")
return buf.String()
}
9 changes: 4 additions & 5 deletions internal/ctxerr/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package ctxerr

import (
"bytes"
"strings"

"github.com/maxatome/go-testdeep/internal/color"
Expand All @@ -17,7 +16,7 @@ import (
// ErrorSummary is the interface used to render error summaries. See
// Error.Summary.
type ErrorSummary interface {
AppendSummary(buf *bytes.Buffer, prefix string)
AppendSummary(buf *strings.Builder, prefix string)
}

// ErrorSummaryItem implements the [ErrorSummary] interface and allows
Expand All @@ -40,7 +39,7 @@ type ErrorSummaryItem struct {
var _ ErrorSummary = ErrorSummaryItem{}

// AppendSummary implements the [ErrorSummary] interface.
func (s ErrorSummaryItem) AppendSummary(buf *bytes.Buffer, prefix string) {
func (s ErrorSummaryItem) AppendSummary(buf *strings.Builder, prefix string) {
color.Init()

buf.WriteString(prefix)
Expand Down Expand Up @@ -72,7 +71,7 @@ type ErrorSummaryItems []ErrorSummaryItem
var _ ErrorSummary = (ErrorSummaryItems)(nil)

// AppendSummary implements [ErrorSummary] interface.
func (s ErrorSummaryItems) AppendSummary(buf *bytes.Buffer, prefix string) {
func (s ErrorSummaryItems) AppendSummary(buf *strings.Builder, prefix string) {
maxLen := 0
for _, item := range s {
if len(item.Label) > maxLen {
Expand All @@ -95,7 +94,7 @@ type errorSummaryString string

var _ ErrorSummary = errorSummaryString("")

func (s errorSummaryString) AppendSummary(buf *bytes.Buffer, prefix string) {
func (s errorSummaryString) AppendSummary(buf *strings.Builder, prefix string) {
color.Init()

buf.WriteString(prefix)
Expand Down
3 changes: 1 addition & 2 deletions internal/ctxerr/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package ctxerr_test

import (
"bytes"
"strings"
"testing"

Expand All @@ -17,7 +16,7 @@ import (
)

func errorSummaryToString(s ctxerr.ErrorSummary, prefix string) string {
var buf bytes.Buffer
var buf strings.Builder
s.AppendSummary(&buf, prefix)
return buf.String()
}
Expand Down
5 changes: 3 additions & 2 deletions internal/json/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ func hex(b []byte) (rune, bool) {
func (j *json) parseString() (string, bool) {
// j.buf[j.pos.bpos] == '"' → caller responsibility

var b *bytes.Buffer
var b *strings.Builder

from := j.pos.bpos + 1
savePos := j.pos

appendBuffer := func(r rune) {
if b == nil {
b = bytes.NewBuffer(j.buf[from : j.pos.bpos-1])
b = &strings.Builder{}
b.Write(j.buf[from : j.pos.bpos-1])
}
b.WriteRune(r)
}
Expand Down
20 changes: 9 additions & 11 deletions internal/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func ToString(val any) string {
}

case []reflect.Value:
var buf bytes.Buffer
SliceToBuffer(&buf, tval)
var buf strings.Builder
SliceToString(&buf, tval)
return buf.String()

// no "(string) " prefix for printable strings
Expand Down Expand Up @@ -79,26 +79,24 @@ func IndentStringIn(w io.Writer, str, indent, colOn, colOff string) {
repl.WriteString(w, str) //nolint: errcheck
}

// SliceToBuffer stringifies items slice into buf then returns buf.
func SliceToBuffer(buf *bytes.Buffer, items []reflect.Value) *bytes.Buffer {
// SliceToString stringifies items slice into buf then returns buf.
func SliceToString(buf *strings.Builder, items []reflect.Value) *strings.Builder {
buf.WriteByte('(')

begLine := bytes.LastIndexByte(buf.Bytes(), '\n') + 1
begLine := strings.LastIndexByte(buf.String(), '\n') + 1
prefix := strings.Repeat(" ", buf.Len()-begLine)

if len(items) < 2 {
if len(items) > 0 {
buf.WriteString(IndentString(ToString(items[0]), prefix))
}
} else {
for idx, item := range items {
if idx != 0 {
buf.WriteString(prefix)
}
buf.WriteString(IndentString(ToString(item), prefix))
buf.WriteString(IndentString(ToString(items[0]), prefix))
for _, item := range items[1:] {
buf.WriteString(",\n")
buf.WriteString(prefix)
buf.WriteString(IndentString(ToString(item), prefix))
}
buf.Truncate(buf.Len() - 2)
}
buf.WriteByte(')')

Expand Down
5 changes: 3 additions & 2 deletions internal/util/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ items:
}
}

buf := bytes.NewBufferString(curTest.BufInit)
test.EqualStr(t, util.SliceToBuffer(buf, items).String(),
var buf strings.Builder
buf.WriteString(curTest.BufInit)
test.EqualStr(t, util.SliceToString(&buf, items).String(),
curTest.Expected)
}
}
Expand Down
3 changes: 1 addition & 2 deletions td/cmp_deeply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package td

import (
"bytes"
"reflect"
"strings"

Expand Down Expand Up @@ -119,7 +118,7 @@ func formatError(t TestingT, isFatal bool, err *ctxerr.Error, args ...any) {

args = flat.Interfaces(args...)

var buf bytes.Buffer
var buf strings.Builder
color.AppendTestNameOn(&buf)
if len(args) == 0 {
buf.WriteString(failedTest)
Expand Down
6 changes: 4 additions & 2 deletions td/td_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package td

import (
"bytes"
"reflect"
"strings"

"github.com/maxatome/go-testdeep/internal/flat"
"github.com/maxatome/go-testdeep/internal/util"
Expand All @@ -27,6 +27,8 @@ func newList(items ...any) tdList {
}

func (l *tdList) String() string {
return util.SliceToBuffer(bytes.NewBufferString(l.GetLocation().Func), l.items).
var b strings.Builder
b.WriteString(l.GetLocation().Func)
return util.SliceToString(&b, l.items).
String()
}
7 changes: 4 additions & 3 deletions td/td_set_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package td

import (
"bytes"
"reflect"
"strings"

"github.com/maxatome/go-testdeep/internal/ctxerr"
"github.com/maxatome/go-testdeep/internal/flat"
Expand Down Expand Up @@ -162,8 +162,9 @@ func (s *tdSetBase) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
}

func (s *tdSetBase) String() string {
return util.SliceToBuffer(
bytes.NewBufferString(s.GetLocation().Func), s.expectedItems).String()
var b strings.Builder
b.WriteString(s.GetLocation().Func)
return util.SliceToString(&b, s.expectedItems).String()
}

func (s *tdSetBase) TypeBehind() reflect.Type {
Expand Down