testerr

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 4 Imported by: 0

README

testerr

Go Reference

Go testing helpers to check errors from function calls – see Go reference.

Basically this:

// In plain Go:
n, err := someCallInGoTest() // returns (int, error)
if err != nil {
	t.Fatalf("illegal error: %s", err)
}
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}

// Short, but ignoring error may yield misleading results:
n, _ := someCallInGoTest()
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}

// Using testerr:
n := Shall1(someCallInGoTest()).BeNil(t) // Calls t.Fatal() if err != nil
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}

Documentation

Overview

Package testerr provides some helpers to check errors from calls in unit tests.

Especially if the errors are not the focus of the test, it is useful to also check these errors - especially those accompanying other return values - without distracting too much from the actual test.

Test functions in testerr that begin with "Should" (Should, Should1) call the error function of a test if the test fails. Test functions that begin with "Shall" (Shall, Shall1) call the fatal function of the test if the test fails.

Example
var t *testing.T
testedFunc := func() (int, error) { return 4711, nil }

// In plain Go:
n, err := testedFunc()
if err != nil {
	t.Fatalf("illegal error: %s", err)
}
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}

// Short, but ignoring error may yield misleading results:
n, _ = testedFunc()
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}

// Using testerr:
n = Shall1(testedFunc()).BeNil(t) // Calls t.Fatal() if err is not nil
if n < 0 || n > 10000 {
	t.Errorf("%d out of range", n)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Ln  lnIndicator
	Fmt fmtIndicator
)

Functions

func MsgString

func MsgString(msg ...any) string

MsgString is used to format strings throughout testerr using the fmt.Print functions without requiring the …ln() and …f() variants for all functions suppoting messages. MsgString will call fmt.Sprintln() if msg[0]==Ln and fmt.Sprintf if msg[0]==Fmt. Otherwise MsgString calls fmt.Sprint.

Example
fmt.Print("fmt", 4711, true)
fmt.Println()
io.WriteString(os.Stdout, MsgString("fmt", 4711, true))
fmt.Println()

fmt.Println("fmt", 4711, true)
io.WriteString(os.Stdout, MsgString(Ln, "fmt", 4711, true))

fmt.Printf("fmt %d & %t", 4711, true)
fmt.Println()
io.WriteString(os.Stdout, MsgString(Fmt, "fmt %d & %t", 4711, true))
fmt.Println()
Output:

fmt4711 true
fmt4711 true
fmt 4711 true
fmt 4711 true
fmt 4711 & true
fmt 4711 & true

Types

type CheckFunc

type CheckFunc func(error) error

func (CheckFunc) Check

func (f CheckFunc) Check(err error) error

type Checker

type Checker interface{ Check(error) error }

A Checker examines the error value passed to the Check method for some condition and return nil if the condition is fulfilled. Otherwise, Check returns an error that describes why the condition is not fulfilled. Basic checkers are already integrated into the RetN(Error|Fatal) types for easy use.

func Is

func Is(target error) Checker

Is returns a checker that uses errors.Is(err, target) to check the error err.

func IsNil

func IsNil(msg ...any) Checker

IsNil returns a checker that checks whether an error is nil. With msg you can add an information message in case the check fails (see MsgString).

func Msg

func Msg(msg ...any) Checker

Msg returns a checker that requires err != nil and checks the error string to be MsgString(msg).

func MsgContains

func MsgContains(msg ...any) Checker

Msg returns a checker that requires err != nil and checks the error string to contain MsgString(msg).

func MsgMatch

func MsgMatch(expr *regexp.Regexp) Checker

Msg returns a checker that requires err != nil and checks the error string to match the regular expression expr.

func MsgMatchRegexp

func MsgMatchRegexp(expr string) Checker

Msg returns a checker that requires err != nil and checks the error string to match the regular expression regexp.Compile(expr).

func MsgPrefix

func MsgPrefix(msg ...any) Checker

Msg returns a checker that requires err != nil and checks the error string to start with MsgString(msg).

func MsgSuffix

func MsgSuffix(msg ...any) Checker

Msg returns a checker that requires err != nil and checks the error string to end with MsgString(msg).

func NotNil

func NotNil(msg ...any) Checker

NotNil returns a checker that checks whether an error is not nil. With msg you can add an information message in case the check fails (see MsgString).

type Ret0Error

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

func Should

func Should(err error) Ret0Error

Should records the passed error, usually from a function call, in order to react to an unexpected error with t.Error(). The error can be analysed using the methods of Ret0Error.

Example
testedFunction := func() error { return errors.New("this is an error") }

var t *testing.T                  // Usually passed in by the test framework
Should(testedFunction()).BeNil(t) // will call t.Error()
Output:

func (Ret0Error) All

func (r Ret0Error) All(t Test, checks ...Checker) error

func (Ret0Error) Any

func (r Ret0Error) Any(t Test, checks ...Checker) error

func (Ret0Error) Be

func (r Ret0Error) Be(t Test, target error) error

func (Ret0Error) BeNil

func (r Ret0Error) BeNil(t Test, msg ...any) error

func (Ret0Error) Check

func (r Ret0Error) Check(t Test, check Checker) error

func (Ret0Error) NotNil

func (r Ret0Error) NotNil(t Test, msg ...any) error

type Ret0Fatal

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

func Shall

func Shall(err error) Ret0Fatal

Shall records the passed error, usually from a function call, in order to react to an unexpected error with t.Fatal(). The error can be analysed using the methods of Ret0Fatal.

Example
testedFunction := func() error { return errors.New("this is an error") }

var t *testing.T                 // Usually passed in by the test framework
Shall(testedFunction()).BeNil(t) // will call t.Fatal()
Output:

func (Ret0Fatal) All

func (r Ret0Fatal) All(t Test, checks ...Checker) error

func (Ret0Fatal) Any

func (r Ret0Fatal) Any(t Test, checks ...Checker) error

func (Ret0Fatal) Be

func (r Ret0Fatal) Be(t Test, target error) error

func (Ret0Fatal) BeNil

func (r Ret0Fatal) BeNil(t Test, msg ...any)

func (Ret0Fatal) Check

func (r Ret0Fatal) Check(t Test, check Checker) error

func (Ret0Fatal) NotNil

func (r Ret0Fatal) NotNil(t Test, msg ...any) error

type Ret1Error

type Ret1Error[R any] struct {
	// contains filtered or unexported fields
}

func Should1

func Should1[R any](v R, err error) Ret1Error[R]

Should1 records the passed values, usually from a function call, in order to react to an unexpected error with t.Error(). The error is evaluated using the Ret1Error methods and the remaining values are returned.

Example
testedFunction := func() (int, error) {
	return 4711, errors.New("this is an error")
}

var t *testing.T                        // Usually passed in by the test framework
n := Should1(testedFunction()).BeNil(t) // will call t.Error()
if n != 4711 {
	t.Errorf("unexpected n == %d", n)
}
Output:

func (Ret1Error[R]) All

func (r Ret1Error[R]) All(t Test, checks ...Checker) R

func (Ret1Error[R]) Any

func (r Ret1Error[R]) Any(t Test, checks ...Checker) R

func (Ret1Error[R]) Be

func (r Ret1Error[R]) Be(t Test, target error) R

func (Ret1Error[R]) BeNil

func (r Ret1Error[R]) BeNil(t Test, msg ...any) R

func (Ret1Error[R]) Check

func (r Ret1Error[R]) Check(t Test, check Checker) R

func (Ret1Error[R]) NotNil

func (r Ret1Error[R]) NotNil(t Test, msg ...any) R

type Ret1Fatal

type Ret1Fatal[R any] struct {
	// contains filtered or unexported fields
}

func Shall1

func Shall1[R any](v R, err error) Ret1Fatal[R]

Shall1 records the passed values, usually from a function call, in order to react to an unexpected error with t.Fatal(). The error is evaluated using the Ret1Fatal methods and the remaining values are returned.

Example
testedFunction := func() (int, error) {
	return 4711, errors.New("this is an error")
}

var t *testing.T                       // Usually passed in by the test framework
n := Shall1(testedFunction()).BeNil(t) // will call t.Fatal()
if n != 4711 {
	t.Errorf("unexpected n == %d", n)
}
Output:

func (Ret1Fatal[R]) All

func (r Ret1Fatal[R]) All(t Test, checks ...Checker) R

func (Ret1Fatal[R]) Any

func (r Ret1Fatal[R]) Any(t Test, checks ...Checker) R

func (Ret1Fatal[R]) Be

func (r Ret1Fatal[R]) Be(t Test, target error) R

func (Ret1Fatal[R]) BeNil

func (r Ret1Fatal[R]) BeNil(t Test, msg ...any) R

func (Ret1Fatal[R]) Check

func (r Ret1Fatal[R]) Check(t Test, check Checker) R

func (Ret1Fatal[R]) NotNil

func (r Ret1Fatal[R]) NotNil(t Test, msg ...any) R

type Test

type Test interface {
	Error(args ...any)
	Fatal(args ...any)
	Helper()
}

Test requires ony the necessary subset of methods from Go's testing.TB interface. This allows us to implement the Test interface to test the testerr package itself. Note that testing.TB cannot be implemented outside of the testing packge for good reasons.

Jump to

Keyboard shortcuts

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