- Example: IO Read Error
- Example: IO Write Error
- Example: IO Read Seek Error
- Example: IO Write Seek Error
- Example: IO Close Error
cat ./read_error/example.go// Package example shows various test options.
package example
import (
"errors"
"io"
)
func readFile(r io.Reader) (string, error) {
// This example will attempt to read 10 bytes from r read until an error or
// eof is returned.
const bufSize = 10
bytes := make([]byte, bufSize)
c, err := r.Read(bytes)
if err == nil && c < bufSize {
return string(bytes), errors.New("not enough bytes")
}
if errors.Is(err, io.EOF) {
return "", errors.New("unexpected EOF")
}
if err != nil {
return "", err
}
return string(bytes), nil
}cat ./read_error/example_test.gopackage example
import (
"errors"
"testing"
"github.com/dancsecs/sztest"
)
func Test_IoInterface_ReadError(t *testing.T) {
chk := sztest.CaptureNothing(t)
defer chk.Release()
// Read without any data set will cause immediate EOF.
_, err := readFile(chk)
chk.Err(err, "unexpected EOF")
// Read all the data
chk.SetIOReaderData("0123456789")
str, err := readFile(chk)
chk.NoErr(err)
chk.Str(str, "0123456789")
str, err = readFile(chk)
chk.Str(str, "")
chk.Err(err, "unexpected EOF")
// Not enough data
chk.SetIOReaderData("01234")
str, err = readFile(chk)
chk.Err(err, "not enough bytes")
chk.Str(str, "01234\x00\x00\x00\x00\x00")
// Fail after a certain number of bytes is read.
chk.SetIOReaderData("01234567890")
chk.SetIOReaderError(2, errors.New("only two bytes read"))
str, err = readFile(chk)
chk.Err(err, "not enough bytes")
chk.Str(str, "01\x00\x00\x00\x00\x00\x00\x00\x00")
str, err = readFile(chk)
chk.Err(err, "only two bytes read")
chk.Str(str, "")
// Setup a direct error to be returned on next read. No other data needs
// to be setup
chk.SetReadError(2962, errors.New("example error on returning 2962"))
n, err := chk.Read(nil)
chk.Int(n, 2962)
chk.Err(err, "example error on returning 2962")
chk.SetReadError(2963, nil) // no error just a forced count.
n, err = chk.Read(nil)
chk.NoErr(err)
chk.Int(n, 2963)
}go test -v -cover ./read_error
cat ./write_error/example.go// Package example shows various test options.
package example
import (
"io"
)
func writeFile(w io.Writer) (int, error) {
// Attempt to write 10 characters to the io.Writer.
n, err := w.Write([]byte("0123456789"))
return n, err
}cat ./write_error/example_test.gopackage example
import (
"errors"
"testing"
"github.com/dancsecs/sztest"
)
func Test_IoInterface_ReadError(t *testing.T) {
chk := sztest.CaptureNothing(t)
defer chk.Release()
// Read without anything .
c, err := writeFile(chk)
chk.NoErr(err)
chk.Int(c, 10)
chk.Str(string(chk.GetIOWriterData()), "0123456789")
chk.SetIOWriterError(8, errors.New("Run out of space after 8 chars"))
c, err = writeFile(chk)
chk.Err(err, "Run out of space after 8 chars")
chk.Int(c, 8)
chk.Str(string(chk.GetIOWriterData()), "01234567")
// Just set a write error and count to be returned on the next write.
chk.SetWriteError(37, errors.New("the write error"))
c, err = writeFile(chk)
chk.Err(err, "the write error")
chk.Int(c, 37)
}go test -v -cover ./write_error
cat ./read_seek_error/example.go// Package example shows various test options.
package example
import (
"io"
)
func seekFile(r io.ReadSeeker, pos int64) (int64, error) {
// This example will attempt position the io.ReadSeeker to the position
// provided.
return r.Seek(pos, io.SeekStart)
}cat ./read_seek_error/example_test.gopackage example
import (
"errors"
"testing"
"github.com/dancsecs/sztest"
)
func Test_IoInterface_ReadError(t *testing.T) {
chk := sztest.CaptureNothing(t)
defer chk.Release()
chk.SetSeekError(34, errors.New("past end of file"))
p, err := seekFile(chk, 962)
chk.Err(err, "past end of file")
chk.Int64(p, 34)
}go test -v -cover ./read_seek_error
cat ./write_seek_error/example.go// Package example shows various test options.
package example
import (
"io"
)
func seekFile(w io.WriteSeeker, pos int64) (int64, error) {
// This example will attempt position the io.WriteSeeker to the position
// provided.
return w.Seek(pos, io.SeekStart)
}cat ./write_seek_error/example_test.gopackage example
import (
"errors"
"testing"
"github.com/dancsecs/sztest"
)
func Test_IoInterface_ReadError(t *testing.T) {
chk := sztest.CaptureNothing(t)
defer chk.Release()
chk.SetSeekError(34, errors.New("past end of file"))
p, err := seekFile(chk, 962)
chk.Err(err, "past end of file")
chk.Int64(p, 34)
}go test -v -cover ./write_seek_error
cat ./close_error/example.go// Package example shows various test options.
package example
import (
"io"
)
func closeFile(r io.Closer) error {
// This example will attempt to close the closer returning any error
// generated by the close action.
return r.Close()
}cat ./close_error/example_test.gopackage example
import (
"errors"
"testing"
"github.com/dancsecs/sztest"
)
func Test_IoInterface_ReadError(t *testing.T) {
chk := sztest.CaptureNothing(t)
defer chk.Release()
chk.SetCloseError(errors.New("the close error"))
chk.Err(closeFile(chk), "the close error")
}go test -v -cover ./close_error