src.elv.shsrc.elv.sh/pkg/tt Index | Files

package tt

import "src.elv.sh/pkg/tt"

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

Variables

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

func It(desc string) *Case

It returns a Case with the given text description.

func (*Case) Args

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

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

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

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.

Source Files

cmpopt.go tt.go

Version
v0.21.0 (latest)
Published
Aug 13, 2024
Platform
linux/amd64
Imports
11 packages
Last checked
1 day ago

Tools for package owners.