massert

package
v0.0.0-...-c20f884 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package massert implements an assertion framework which is useful in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t *testing.T, aa ...Assertion)

Assert is a convenience function which performs the Assertion and calls Error on the testing.T instance for the first Assertion which fails.

func Require

func Require(t *testing.T, aa ...Assertion)

Require is a convenience function which performs the Assertions and calls Fatal on the testing.T instance for the first Assertion which fails.

Types

type AssertErr

type AssertErr struct {
	Err       error     // The error which occurred
	Assertion Assertion // The Assertion which failed
}

AssertErr is an error returned by Assertions which have failed, containing information about both the reason for failure and the Assertion itself.

func (AssertErr) Error

func (ae AssertErr) Error() string

type Assertion

type Assertion interface {
	Assert() error
	Description() string // A description of the Assertion

	// Returns the callstack of where the Assertion was created, ordered from
	// closest to farthest. This may not necessarily contain the entire
	// callstack if that would be inconveniently cumbersome.
	Stack() []runtime.Frame
}

Assertion is an entity which will make some kind of assertion and produce an error if that assertion does not hold true. The error returned will generally be of type AssertErr.

func All

func All(aa ...Assertion) Assertion

All asserts that at all of the given Assertions succeed. Its Assert method will return the error of whichever Assertion failed.

func Any

func Any(aa ...Assertion) Assertion

Any asserts that at least one of the given Assertions succeeds.

func AnyOne

func AnyOne(aa ...Assertion) Assertion

AnyOne asserts that exactly one of the given Assertions succeeds.

func Comment

func Comment(a Assertion, msg string, args ...interface{}) Assertion

Comment prepends a formatted string to the given Assertion's string description.

func Equal

func Equal(a, b interface{}) Assertion

Equal asserts that the two values are exactly equal, and uses the reflect.DeepEqual function to determine if they are.

func Error

func Error(err error) Assertion

Error returns an Assertion which always fails with the given error.

func Errorf

func Errorf(str string, args ...interface{}) Assertion

Errorf is like Err but allows for a formatted string.

func HasKey

func HasKey(set, elem interface{}) Assertion

HasKey asserts that the given set (which must be a map type) has the given element as a key in it.

func HasValue

func HasValue(set, elem interface{}) Assertion

HasValue asserts that the given set has the given element as a value in it. The set may be an array, a slice, or a map, and if it's a map then the elem will need to be a value in it.

func Length

func Length(set interface{}, length int) Assertion

Length asserts that the given set has the given number of elements in it. The set may be an array, a slice, or a map. A nil value'd set is considered to be a length of zero.

func Nil

func Nil(i interface{}) Assertion

Nil asserts that the value is nil. This assertion works both if the value is the untyped nil value (e.g. `Nil(nil)`) or if it's a typed nil value (e.g. `Nil([]byte(nil))`).

func None

func None(aa ...Assertion) Assertion

None asserts that all of the given Assertions fail.

NOTE this is functionally equivalent to doing `Not(Any(aa...))`, but the error returned is more helpful.

func Not

func Not(a Assertion) Assertion

Not negates an Assertion, so that it fails if the given Assertion does not, and vice-versa.

func Subset

func Subset(set, subset interface{}) Assertion

Subset asserts that the given subset is a subset of the given set. Both must be of the same type and may be arrays, slices, or maps.

Jump to

Keyboard shortcuts

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