Skip to content

Latest commit

 

History

History
439 lines (336 loc) · 13.5 KB

File metadata and controls

439 lines (336 loc) · 13.5 KB

IO Interface

Contents

Example: IO Read 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.go
package 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

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError ͏ ͏(0.0s)}}$
$\small{\texttt{PASS}}$
$\small{\texttt{coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$
$\small{\texttt{ok ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏github.com/dancsecs/sztest/examples/io ̲ ̲interface/read ̲ ̲error ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$

Contents

Example: IO Write 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.go
package 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

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError ͏ ͏(0.0s)}}$
$\small{\texttt{PASS}}$
$\small{\texttt{coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$
$\small{\texttt{ok ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏github.com/dancsecs/sztest/examples/io ̲ ̲interface/write ̲ ̲error ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$

Contents

Example: IO Read Seek 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.go
package 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

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError ͏ ͏(0.0s)}}$
$\small{\texttt{PASS}}$
$\small{\texttt{coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$
$\small{\texttt{ok ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏github.com/dancsecs/sztest/examples/io ̲ ̲interface/read ̲ ̲seek ̲ ̲error ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$

Contents

Example: IO Write 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.go
package 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

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError ͏ ͏(0.0s)}}$
$\small{\texttt{PASS}}$
$\small{\texttt{coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$
$\small{\texttt{ok ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏github.com/dancsecs/sztest/examples/io ̲ ̲interface/write ̲ ̲seek ̲ ̲error ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$

Contents

Example: IO Close 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.go
package 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

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲IoInterface ̲ ̲ReadError ͏ ͏(0.0s)}}$
$\small{\texttt{PASS}}$
$\small{\texttt{coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$
$\small{\texttt{ok ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏github.com/dancsecs/sztest/examples/io ̲ ̲interface/close ̲ ̲error ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏coverage: ͏ ͏100.0﹪ ͏ ͏of ͏ ͏statements}}$

Contents