functest

package
v0.0.0-...-2148625 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package functest contains utilities to ease writing table-driven tests for pure functions and method.

Example:

func square(v int) int { return v * v }

func TestFunc(t *testing.T) {
	f := functest.New(square)
	f.Test(t,
		f.In(0).Want(0),
		f.In(1).Want(1),
		f.In(2).Want(4),
		f.In(3).Want(9),
	)
}

It can test whether things panic:

f := functest.New(condPanic)
f.Test(t,
	f.In(false, nil),
	f.In(true, "boom").Check(func(res functest.Result) error {
		if res.Panic != "boom" {
			return fmt.Errorf("panic = %v; want boom", res.Panic)
		}
		return nil
	}),
	f.In(true, nil).Check(func(res functest.Result) error {
		if res.Panic != nil || res.Paniked {
			return fmt.Errorf("expected panic with nil value, got: %+v", res)
		}
		return nil
	}),
)

If a test fails, functest does its best to format a useful error message. You can also name test cases:

f := functest.New(square)
f.Test(t,
	f.In(0).Want(0),
	f.In(1).Want(111),
	f.In(2).Want(4),
	f.Case("three").In(3).Want(999),
)

Which would fail like:

--- FAIL: TestSquare (0.00s)
functest.go:304: square(1) = 1; want 111
functest.go:304: three: square(3) = 9; want 999
FAIL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

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

Case is a test case to run.

Test cases can be either named or unnamed, depending on how they're created. Naming cases is optional; all failures messages aim to have useful output and include the input to the function.

Unless the function's arity is zero, all cases should have their input set with In.

The case's expected output can be set with Want and/or Check.

func (*Case) Check

func (c *Case) Check(checker func(Result) error) *Case

Check adds a function to check the result of the case's function call. It is a low-level function when Want is insufficient. For instance, it allows checking whether a function panics. If no checker functions are registered, function panics are considered a test failure.

Check modifies and returns c. Callers my use both Want and Check, and may use Check multiple times.

func (*Case) In

func (c *Case) In(args ...interface{}) *Case

In sets the arguments of c used to call f.

func (*Case) Want

func (c *Case) Want(result ...interface{}) *Case

Want sets the expected result values of the test case. Want modifies and returns c. Callers my use both Want and Check.

type Func

type Func struct {
	// Name is the name of the function to use in error messages.
	// In most cases it is initialized by New, unless the function
	// being tested is an anonymous function.
	Name string
	// contains filtered or unexported fields
}

Func is a wrapper around a func to test. It must be created with New.

func New

func New(f interface{}) *Func

New wraps a function for testing. The provided value f must be a function or method.

func (*Func) Case

func (f *Func) Case(name string) *Case

Case returns a new named case. It should be modified before use.

func (*Func) In

func (f *Func) In(args ...interface{}) *Case

In returns a new unnamed test case. It will be identified by its arguments only.

func (*Func) Test

func (f *Func) Test(t testing.TB, cases ...*Case)

Test runs the provided test cases against f. If any test cases fail, t.Errorf is called.

type Result

type Result struct {
	// Result is the return value(s) of the function.
	Result []interface{}

	// Panic is the panic value of the function.
	Panic interface{}

	// Panicked is whether the function paniced.
	// It can be used to determine whether a function
	// called panic(nil).
	Panicked bool
}

Result is the result of a function call, for use with Check.

Jump to

Keyboard shortcuts

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