diff --git a/testio/testio.go b/testio/testio.go index 8d75911..151e91b 100644 --- a/testio/testio.go +++ b/testio/testio.go @@ -169,6 +169,26 @@ type BufCloser struct { buf *bytes.Buffer } +// NewBufCloser creates and initializes a new BufCloser using buf as +// its initial contents. It is intended to prepare a BufCloser to read +// existing data. It can also be used to size the internal buffer for +// writing. To do that, buf should have the desired capacity but a +// length of zero. +func NewBufCloser(buf []byte) *BufCloser { + bc := new(BufCloser) + bc.buf = bytes.NewBuffer(buf) + return bc +} + +// NewBufCloserString creates and initializes a new Buffer using +// string s as its initial contents. It is intended to prepare a +// buffer to read an existing string. +func NewBufCloserString(s string) *BufCloser { + buf := new(BufCloser) + buf.buf = bytes.NewBufferString(s) + return buf +} + // Write writes the data to the BufCloser. func (buf *BufCloser) Write(p []byte) (int, error) { return buf.buf.Write(p) @@ -199,26 +219,6 @@ func (buf *BufCloser) Len() int { return buf.buf.Len() } -// NewBufCloser creates and initializes a new BufCloser using buf as -// its initial contents. It is intended to prepare a BufCloser to read -// existing data. It can also be used to size the internal buffer for -// writing. To do that, buf should have the desired capacity but a -// length of zero. -func NewBufCloser(buf []byte) *BufCloser { - bc := new(BufCloser) - bc.buf = bytes.NewBuffer(buf) - return bc -} - -// NewBufCloserString creates and initializes a new Buffer using -// string s as its initial contents. It is intended to prepare a -// buffer to read an existing string. -func NewBufCloserString(s string) *BufCloser { - buf := new(BufCloser) - buf.buf = bytes.NewBufferString(s) - return buf -} - // A LoggingBuffer is an io.ReadWriter that prints the hex value of // the data for all reads and writes. type LoggingBuffer struct { @@ -323,6 +323,26 @@ type BrokenCloser struct { buf *bytes.Buffer } +// NewBrokenCloser creates and initializes a new BrokenCloser using buf as +// its initial contents. It is intended to prepare a BrokenCloser to read +// existing data. It can also be used to size the internal buffer for +// writing. To do that, buf should have the desired capacity but a +// length of zero. +func NewBrokenCloser(buf []byte) *BrokenCloser { + bc := new(BrokenCloser) + bc.buf = bytes.NewBuffer(buf) + return bc +} + +// NewBrokenCloserString creates and initializes a new Buffer using +// string s as its initial contents. It is intended to prepare a +// buffer to read an existing string. +func NewBrokenCloserString(s string) *BrokenCloser { + buf := new(BrokenCloser) + buf.buf = bytes.NewBufferString(s) + return buf +} + // Write writes the data to the BrokenCloser. func (buf *BrokenCloser) Write(p []byte) (int, error) { return buf.buf.Write(p) @@ -347,23 +367,3 @@ func (buf *BrokenCloser) Reset() { func (buf *BrokenCloser) Bytes() []byte { return buf.buf.Bytes() } - -// NewBrokenCloser creates and initializes a new BrokenCloser using buf as -// its initial contents. It is intended to prepare a BrokenCloser to read -// existing data. It can also be used to size the internal buffer for -// writing. To do that, buf should have the desired capacity but a -// length of zero. -func NewBrokenCloser(buf []byte) *BrokenCloser { - bc := new(BrokenCloser) - bc.buf = bytes.NewBuffer(buf) - return bc -} - -// NewBrokenCloserString creates and initializes a new Buffer using -// string s as its initial contents. It is intended to prepare a -// buffer to read an existing string. -func NewBrokenCloserString(s string) *BrokenCloser { - buf := new(BrokenCloser) - buf.buf = bytes.NewBufferString(s) - return buf -} diff --git a/testio/testio_test.go b/testio/testio_test.go index d819e62..b0f898a 100644 --- a/testio/testio_test.go +++ b/testio/testio_test.go @@ -1,13 +1,15 @@ -package testio +package testio_test import ( "bytes" "os" "testing" + + "git.wntrmute.dev/kyle/goutils/testio" ) func TestBrokenWriter(t *testing.T) { - buf := NewBrokenWriter(2) + buf := testio.NewBrokenWriter(2) data := []byte{1, 2} n, err := buf.Write(data) @@ -39,7 +41,7 @@ func TestBufCloser(t *testing.T) { var data = []byte{1, 2} var read = make([]byte, 2) - buf := NewBufCloser(data) + buf := testio.NewBufCloser(data) _, err := buf.Read(read) if err != nil { t.Fatalf("%v", err) @@ -54,7 +56,7 @@ func TestBufCloser(t *testing.T) { buf.Reset() s := "hi" - buf = NewBufCloserString(s) + buf = testio.NewBufCloserString(s) read = buf.Bytes() if string(read) != s { @@ -65,7 +67,7 @@ func TestBufCloser(t *testing.T) { func TestLoggingBuffer(t *testing.T) { src := &bytes.Buffer{} data := []byte("AB") - lb := NewLoggingBuffer(src) + lb := testio.NewLoggingBuffer(src) _, err := lb.Write(data) if err != nil { t.Fatalf("%v", err) @@ -82,8 +84,8 @@ func TestLoggingBuffer(t *testing.T) { } expected := "[TEST] [WRITE] 4142\n" - if string(out.Bytes()) != expected { - t.Fatalf("expected '%s', have '%s'", expected, string(out.Bytes())) + if out.String() != expected { + t.Fatalf("expected '%s', have '%s'", expected, out.String()) } out.Reset() @@ -96,8 +98,8 @@ func TestLoggingBuffer(t *testing.T) { } expected = "[TEST] [READ] 4142\n" - if string(out.Bytes()) != expected { - t.Fatalf("expected '%s', have '%s'", expected, string(out.Bytes())) + if out.String() != expected { + t.Fatalf("expected '%s', have '%s'", expected, out.String()) } out.Reset() @@ -112,8 +114,8 @@ func TestLoggingBuffer(t *testing.T) { } expected = "[READ] 4142\n" - if string(out.Bytes()) != expected { - t.Fatalf("expected '%s', have '%s'", expected, string(out.Bytes())) + if out.String() != expected { + t.Fatalf("expected '%s', have '%s'", expected, out.String()) } src.Reset() @@ -124,8 +126,8 @@ func TestLoggingBuffer(t *testing.T) { } func TestBrokenReadWriter(t *testing.T) { - brw := NewBrokenReadWriter(0, 0) - lb := NewLoggingBuffer(brw) + brw := testio.NewBrokenReadWriter(0, 0) + lb := testio.NewLoggingBuffer(brw) var p = make([]byte, 2) var data = []byte("HI") @@ -177,7 +179,7 @@ func TestBrokenReadWriter(t *testing.T) { } func TestBufferConn(t *testing.T) { - bc := NewBufferConn() + bc := testio.NewBufferConn() client := []byte("AB") peer := []byte("XY")