Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Jan 24, 2024
1 parent 6779184 commit b01a1ac
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
16 changes: 16 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ func TestBytesBufferPool(t *testing.T) {
assert.True(t, p.Get().Cap() >= n)
})
}

func TestBytesBufferPool_Get(t *testing.T) {
var p BytesBufferPool

b := p.Get()
assert.Equal(t, 0, b.Cap())

const str = "dit is een hele lang test string met heel veel willekeurige woorden die nergens op slaan zolang de buffer maar groeit"
b.WriteString(str)
assert.Equal(t, str, b.String())

p.Put(b)
b = p.Get()
assert.Equal(t, 0, b.Len())
assert.Equal(t, 128, b.Cap())
}
38 changes: 38 additions & 0 deletions count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2024, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package writing

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestToCountingWriter(t *testing.T) {
var writer writerOnly
csw := ToCountingWriter(&writer)
assert.NotSame(t, &writer, csw)

want := []byte("test")
haveCount, haveErr := csw.Write(want)

assert.Equal(t, want, writer.buf)
assert.Equal(t, 4, haveCount)
assert.Equal(t, haveCount, csw.Count())
assert.Nil(t, haveErr)
}

func TestToCountingStringWriter(t *testing.T) {
var writer writerOnly
csw := ToCountingStringWriter(&writer)
assert.NotSame(t, &writer, csw)

const want = "test"
haveCount, haveErr := csw.WriteString(want)

assert.Equal(t, want, string(writer.buf))
assert.Equal(t, 4, haveCount)
assert.Equal(t, haveCount, csw.Count())
assert.Nil(t, haveErr)
}
25 changes: 18 additions & 7 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package writing

import (
"github.com/stretchr/testify/assert"
"io"
"strings"
"testing"
)
Expand All @@ -17,11 +16,23 @@ func TestToStringWriter(t *testing.T) {
assert.Same(t, &sb, ToStringWriter(&sb))
})
t.Run("upgraded", func(t *testing.T) {
type Writer struct {
io.Writer
}

var writer Writer
assert.NotSame(t, &writer, ToStringWriter(&writer))
var writer writerOnly
sw := ToStringWriter(&writer)
assert.NotSame(t, &writer, sw)
sw.WriteString("test")

Check failure on line 22 in string_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sw.WriteString` is not checked (errcheck)
assert.Equal(t, "test", string(writer.buf))
})
}

type writerOnly struct {
buf []byte
}

func (w *writerOnly) Write(p []byte) (n int, err error) {
if w.buf == nil {
w.buf = make([]byte, 0, len(p))
}

w.buf = append(w.buf, p...)
return len(p), nil
}

0 comments on commit b01a1ac

Please sign in to comment.