tester

package module
v0.0.0-...-488e3e2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-tester

Go library for testing.

GoDoc Test License

Ask DeepWiki OpenSourceInsight OSS Insight

Features

  • Assertion
  • Global variable replacer (os.Stdout, os.Stderr, rand.Reader, etc)
  • io.Reader and io.Writer that can return errors

Tested Environments

Operating System:

Architecture (Using QEMU on linux):

  • x86: amd64, 386
  • arm: arm/v5, arm/v6, arm/v7, arm64
  • risc: riscv64, loong64
  • ppc: ppc64, ppc64le
  • mips: mips, mips64, mips64le, mipsle
  • ibm: s390x

Release Cycle

License

Apache-2.0

Usage

TODO

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrMaxRead tell that the read bytes reached maximum.
	ErrMaxRead = errors.New("go-tester/tester: max readable byte reached")
)
View Source
var (
	// ErrMaxWritten tell that the written bytes reached maximum.
	ErrMaxWritten = errors.New("go-tester/tester: max writable byte reached")
)

Functions

func AssertDeepEqual

func AssertDeepEqual[T any](t *testing.T, want, got T) bool

AssertDeepEqual asserts that the given values are deeply equal. reflect.DeepEqual is used for comparison. AssertDeepEqual returns true if two value are deeply equal.

func AssertEqual

func AssertEqual[T comparable](t *testing.T, want, got T) bool

AssertEqual asserts that the given values are equal. `==` operator is used for comparison. AssertEqual returns true if two value are equal.

func AssertEqualErr

func AssertEqualErr(t *testing.T, want, got error) bool

AssertEqualErr asserts that the given errors are equal. errors.Is is used for comparison. Given got will be unwrapped and compare with want in the errors.Is. AssertEqualErr returns true if two errors are equal.

func ReplaceRandReader

func ReplaceRandReader(r io.Reader) (done func())

ReplaceRandReader replaces the global variable rand.Reader with r. Call done after tests finished to set the original Reader back. Do not run tests parallel until the done was called.

Example:

done := tester.ReplaceRandReader(YourReader)
defer done()
// Your test codes here.

func ReplaceStderr

func ReplaceStderr() (r *os.File, done func())

ReplaceStderr replaces the global variable os.Stderr and return reader. Call done after tests finished to set the original Stderr back. Do not run tests parallel until the done was called.

Example:

r, done := tester.ReplaceStderr()
defer done()
// Your test codes here.

func ReplaceStdin

func ReplaceStdin() (w *os.File, done func())

ReplaceStdin replaces the global variable os.Stdin and return writer. Call done after tests finished to set the original Stdin back. Do not run tests parallel until the done was called.

Example:

w, done := tester.ReplaceStdin()
defer done()
// Your test codes here.

func ReplaceStdout

func ReplaceStdout() (r *os.File, done func())

ReplaceStdout replaces the global variable os.Stdout and return reader. Call done after tests finished to set the original Stdout back. Do not run tests parallel until the done was called.

Example:

r, done := tester.ReplaceStdout()
defer done()
// Your test codes here.

Types

type MaxReader

type MaxReader struct {
	// contains filtered or unexported fields
}

MaxReader is a io.Reader that can read n bytes at maximum. Use NewMaxReader to create a new reader.

func MaxErrorReader

func MaxErrorReader(r io.Reader, n int64) *MaxReader

MaxErrorReader is the alias for NewMaxReader(r, n, ErrMaxRead).

func MaxSilentReader

func MaxSilentReader(r io.Reader, n int64) *MaxReader

MaxSilentReader is the alias for NewMaxReader(r, n, nil).

func NewMaxReader

func NewMaxReader(r io.Reader, n int64, err error) *MaxReader

NewMaxReader returns a new MaxReader that read n bytes at maximum. When the read bytes reached n, the reader returns n and err.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/aileron-projects/go-tester"
)

func main() {
	s := strings.NewReader("1234567890")
	r := tester.NewMaxReader(s, 5, tester.ErrMaxRead)

	buf := make([]byte, 3)
	fmt.Println(r.Read(buf))
	fmt.Println(r.Read(buf))
	fmt.Println(r.Read(buf))
}
Output:
3 <nil>
2 go-tester/tester: max readable byte reached
0 go-tester/tester: max readable byte reached

func (*MaxReader) Bytes

func (w *MaxReader) Bytes() []byte

Bytes returns read bytes.

func (*MaxReader) Read

func (r *MaxReader) Read(p []byte) (n int, err error)

func (*MaxReader) String

func (w *MaxReader) String() string

String returns read bytes in string.

type MaxWriter

type MaxWriter struct {
	// contains filtered or unexported fields
}

MaxWriter is a io.Writer that accepts n bytes at maximum. Use NewMaxWriter to create a new writer.

func MaxErrorWriter

func MaxErrorWriter(n int64) *MaxWriter

MaxErrorWriter is the alias for NewMaxWriter(n, ErrMaxWritten).

func MaxSilentWriter

func MaxSilentWriter(n int64) *MaxWriter

MaxSilentWriter is the alias for NewMaxWriter(n, nil).

func NewMaxWriter

func NewMaxWriter(n int64, err error) *MaxWriter

NewMaxWriter returns a new MaxWriter that accepts n bytes at maximum. When the written bytes reached n, the writer returns n and err.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go-tester"
)

func main() {
	w := tester.NewMaxWriter(5, tester.ErrMaxWritten)

	fmt.Println(w.Write([]byte("123")))
	fmt.Println(w.Write([]byte("456")))
	fmt.Println(w.Write([]byte("789")))
}
Output:
3 <nil>
2 go-tester/tester: max writable byte reached
0 go-tester/tester: max writable byte reached

func (*MaxWriter) Bytes

func (w *MaxWriter) Bytes() []byte

Bytes returns written bytes.

func (*MaxWriter) String

func (w *MaxWriter) String() string

String returns written bytes in string.

func (*MaxWriter) Write

func (w *MaxWriter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL