Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elee1766 committed Aug 5, 2023
1 parent 795d1e5 commit 043c94a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
5 changes: 5 additions & 0 deletions enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (e *Encoder) ResetWriter(out io.Writer) {
e.first = e.first[:0]
}

// Grow grows the underlying buffer
func (e *Encoder) Grow(n int) {
e.w.Grow(n)
}

// Bytes returns underlying buffer.
func (e Encoder) Bytes() []byte { return e.w.Buf }

Expand Down
16 changes: 16 additions & 0 deletions enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ func requireCompat(t *testing.T, cb func(*Encoder), v any) {
testEncoderModes(t, cb, string(buf))
}

func TestEncoderGrow(t *testing.T) {
should := require.New(t)
e := &Encoder{}
should.Equal(0, len(e.Bytes()))
should.Equal(0, cap(e.Bytes()))
e.Grow(1024)
should.Equal(0, len(e.Bytes()))
should.Equal(1024, cap(e.Bytes()))
e.Grow(512)
should.Equal(0, len(e.Bytes()))
should.Equal(1024, cap(e.Bytes()))
e.Grow(4096)
should.Equal(0, len(e.Bytes()))
should.Equal(4096, cap(e.Bytes()))
}

func TestEncoderByteShouldGrowBuffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
Expand Down
13 changes: 12 additions & 1 deletion w.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jx

import "io"
import (
"bytes"
"io"
)

// Writer writes json tokens to underlying buffer.
//
Expand Down Expand Up @@ -51,6 +54,14 @@ func (w *Writer) ResetWriter(out io.Writer) {
w.stream.Reset(out)
}

// Grow grows the underlying buffer.
// It calls (*bytes.Buffer).Grow(n int) on b.Buf
func (b *Writer) Grow(n int) {

Check warning on line 59 in w.go

View workflow job for this annotation

GitHub Actions / lint / run

receiver-naming: receiver name b should be consistent with previous receiver name w for Writer (revive)
buf := bytes.NewBuffer(b.Buf)
buf.Grow(n)
b.Buf = buf.Bytes()
}

// byte writes a single byte.
func (w *Writer) byte(c byte) (fail bool) {
if w.stream == nil {
Expand Down
11 changes: 0 additions & 11 deletions w_grow.go

This file was deleted.

16 changes: 16 additions & 0 deletions w_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ func TestWriter_String(t *testing.T) {
w.True()
require.Equal(t, "true", w.String())
}

func TestWriter_Grow(t *testing.T) {
should := require.New(t)
e := &Writer{}
should.Equal(0, len(e.Buf))
should.Equal(0, cap(e.Buf))
e.Grow(1024)
should.Equal(0, len(e.Buf))
should.Equal(1024, cap(e.Buf))
e.Grow(512)
should.Equal(0, len(e.Buf))
should.Equal(1024, cap(e.Buf))
e.Grow(4096)
should.Equal(0, len(e.Buf))
should.Equal(4096, cap(e.Buf))
}

0 comments on commit 043c94a

Please sign in to comment.