tt

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: BSD-2-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package tt supports table-driven tests with little boilerplate.

A typical use of this package looks like this:

// Function being tested
func Neg(i int) { return -i }

func TestNeg(t *testing.T) {
	Test(t, Neg,
		// Unnamed test case
		Args(1).Rets(-1),
		// Named test case
		It("returns 0 for 0").Args(0).Rets(0),
	)
}

Index

Constants

This section is empty.

Variables

View Source
var CommonCmpOpt = cmp.Options([]cmp.Option{
	cmp.Transformer("transformList", transformList),
	cmp.Transformer("transformMap", transformMap),
	cmp.Comparer(func(x, y *big.Int) bool { return x.Cmp(y) == 0 }),
	cmp.Comparer(func(x, y *big.Rat) bool { return x.Cmp(y) == 0 }),
})

CommonCmpOpt is cmp.Option shared between tt and evaltest.

Functions

func Test

func Test(t *testing.T, fn any, tests ...*Case)

Test tests fn against the given Case instances.

The fn argument may be the function itself or an explicit FnDescriptor, the former case being equivalent to passing Fn(fn).

Types

type Case

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

Case represents a test case. It has setter methods that augment and return itself, so they can be chained like It(...).Args(...).Rets(...).

func Args

func Args(args ...any) *Case

Args is equivalent to It("").args(...). It is useful when the test case is trivial and doesn't need a description; for more complex or interesting test cases, use It instead.

func It added in v0.20.0

func It(desc string) *Case

It returns a Case with the given text description.

func (*Case) Args added in v0.20.0

func (c *Case) Args(args ...any) *Case

Args modifies the Case to pass the given arguments. It returns the receiver.

func (*Case) Rets

func (c *Case) Rets(matchers ...any) *Case

Rets modifies the Case to expect the given return values. It returns the receiver.

The arguments may implement the Matcher interface, in which case its Match method is called with the actual return value. Otherwise, reflect.DeepEqual is used to determine matches.

type FnDescriptor added in v0.20.0

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

FnDescriptor describes a function to test. It has setter methods that augment and return itself, so they can be chained like Fn(...).Named(...).ArgsFmt(...).

func Fn

func Fn(body any) *FnDescriptor

Fn creates a FnDescriptor for the given function.

func (*FnDescriptor) ArgsFmt added in v0.20.0

func (fn *FnDescriptor) ArgsFmt(s string) *FnDescriptor

ArgsFmt sets the string for formatting arguments in test error messages. It returns the receiver.

func (*FnDescriptor) Named added in v0.20.0

func (fn *FnDescriptor) Named(name string) *FnDescriptor

Named sets the name of the function. This is only necessary for methods and local closures; package-level functions will have their name automatically inferred via reflection. It returns the receiver.

type Matcher

type Matcher interface {
	// Match reports whether a return value is considered a match. The argument
	// is of type RetValue so that it cannot be implemented accidentally.
	Match(RetValue) bool
}

Matcher wraps the Match method.

Values that implement this interface can be passed to *Case.Rets to control the matching algorithm for return values.

var Any Matcher = anyMatcher{}

Any is a Matcher that matches any value.

type RetValue

type RetValue any

RetValue is an empty interface used in the Matcher interface.

Jump to

Keyboard shortcuts

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