check

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package check contains assertions to assist with unit testing.

Index

Constants

View Source
const DefaultWaitCheckInterval = 1 * time.Millisecond

DefaultWaitCheckInterval is the default value of the optional check interval passed to Wait().

View Source
const EnvGolabels = "GOLABELS"

EnvGolabels is the name of the environment variable used by RequireLabel.

Variables

View Source
var ErrSimulated = errors.New("simulated")

ErrSimulated is a pre-canned error, useful in simulating faults.

Functions

func PrintStack

func PrintStack(depth int) string

PrintStack prints the call stack, starting from the given depth.

func RequireLabel

func RequireLabel(t TestSkipper, required string)

RequireLabel ensures that the given label is present in the value of the GOLABELS environment variable, where the latter is a comma-separated list of arbitrary labels, e.g. GOLABELS=prod,test. If the required label is absent, the test will be skipped.

func RunTargetted

func RunTargetted(t TestSkipper, r Runnable)

RunTargetted runs a given function if it the test case was specified using a narrow-matching regular expression; for example, by running 'debug.test -test.run ^...$' or 'go test -run ^...$'. If tests were run without a regex, or with a regex designed to match several tests, the example will be skipped.

The purpose of RunTargetted is to enable the selective running of 'go doc' example snippets. These should not normally be run as part of package tests, CI builds and so on, but may occasionally be run from the IDE.

func ThatDoesNotPanic

func ThatDoesNotPanic(t Tester, f func())

ThatDoesNotPanic ensures that the given function f returns without panicking. This is useful in tests that must perform multiple assertions without terminating the test. (Otherwise, if we let the panic through, the test will not run to completion.)

func ThatPanicsAsExpected

func ThatPanicsAsExpected(t Tester, assertion PanicAssertion, f func())

ThatPanicsAsExpected checks that the given function f panics, where the trapped panic complies with the supplied assertion.

Types

type Assertion

type Assertion func(t Tester)

Assertion is a check that must pass for Timesert.UntilAsserted to return without raising an error.

type Interceptor

type Interceptor interface {
	Tester
}

Interceptor represents a mechanism for transforming the result of invoking Tester.Errorf(). This is useful when you need to modify the output of a Tester, for instance, to enrich it with additional information that is not available to the assertion framework at the point where an assertion fails.

type InterceptorStub

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

InterceptorStub is an element in a fluid chain.

func Intercept

func Intercept(t Tester) InterceptorStub

Intercept starts a fluid chain for specifying testing mutations.

func (InterceptorStub) Mutate

func (is InterceptorStub) Mutate(m Mutation) Interceptor

Mutate sets up the given mutation that will be triggered on Tester.Errorf(). To chain mutations, use the Then() method.

type MockTester

type MockTester struct {
	ErrorFunc func(format string, args ...interface{})
}

MockTester aids in the mocking of the Tester interface.

func (*MockTester) Errorf

func (m *MockTester) Errorf(format string, args ...interface{})

Errorf feeds a formatted error message to the mocked tester.

type Mutation

type Mutation func(original string) string

Mutation represents a transformation of the captured Tester.Errorf() invocation, where the formatted message is fed to a Mutation function, and the output is some transformation of the original.

func AddStack

func AddStack() Mutation

AddStack adds a stack trace to the end of the capture.

func Append

func Append(suffix interface{}) Mutation

Append adds a string to the end of the original captured message. The appended suffixed is delimited from the original capture with a single whitespace character.

func Appendf

func Appendf(format string, args ...interface{}) Mutation

Appendf adds a (printf-style) formmatted message to the end of the capture. This is a convenience for calling Append(), having applied fmt.Sprintf().

func (Mutation) Then

func (before Mutation) Then(after Mutation) Mutation

Then chains mutations, feeding the outcome of the 'before' mutation to the after' mutation. It allows you to do things like:

Append("Hello").Then(AddStack())

type PanicAssertion

type PanicAssertion func(t Tester, cause interface{})

PanicAssertion checks a given panic cause. It is used by ThatPanicsAsExpected.

func AnyCause

func AnyCause() PanicAssertion

AnyCause satisfies the panic assertion irrespective of the cause. Use to test that a function panics without caring as to the nature of the panic.

func CauseEqual

func CauseEqual(expected interface{}) PanicAssertion

CauseEqual checks that the panic is equal to the given cause.

func ErrorContaining

func ErrorContaining(substr string) PanicAssertion

ErrorContaining checks that the panic is of the built-in error type, where the result of calling err.Error() contains the given substring.

func ErrorWithValue

func ErrorWithValue(value string) PanicAssertion

ErrorWithValue checks that the panic is of the built-in error type, where the result of calling err.Error() matches the given value.

type Predicate

type Predicate func() bool

Predicate is a condition that must be satisfied for Timesert.Until to return.

func Equal

func Equal(supplier func() interface{}, expected interface{}) Predicate

Equal tests that the value returned by the given supplied matches the expected value.

func Not

func Not(p Predicate) Predicate

Not inverts a given predicate.

type Runnable

type Runnable func()

Runnable in any no-arg function.

Typically, this is a 'go doc' example function, of the form —

func Example_suffix() { ... }
func ExampleF_suffix() { ... }
func ExampleT_suffix() { ... }
func ExampleT_M_suffix() { ... }

type SingleCapture

type SingleCapture interface {
	Captured() *string
	CapturedLines() []string
	NumCapturedLines() int
	AssertNil(t Tester)
	AssertNotNil(t Tester)
	AssertFirstLineEqual(t Tester, expected string)
	AssertFirstLineContains(t Tester, substr string)
	AssertContains(t Tester, substr string)
}

SingleCapture represents one instance of the invocation of TestCapture.Errorf.

type TestCapture

type TestCapture interface {
	Tester
	First() SingleCapture
	Capture(index int) SingleCapture
	Captures() []SingleCapture
	Length() int
	Reset()
}

TestCapture provides a mechanism for capturing the results of tests. This is useful when testing assertion libraries. TestCapture is thread-safe; it may be invoked concurrently from multiple goroutines to capture test results.

func NewTestCapture

func NewTestCapture() TestCapture

NewTestCapture creates a new TestCapture object.

type TestSkipper

type TestSkipper interface {
	Skip(args ...interface{})
}

TestSkipper is the API contract for testing.T.Skip().

type Tester

type Tester interface {
	Errorf(format string, args ...interface{})
}

Tester is an API-compatible stand-in for *testing.T.

type Timesert

type Timesert interface {
	Until(p Predicate) bool
	UntilAsserted(a Assertion) bool
}

Timesert provides a mechanism for awaiting an assertion or a condition from a test.

func Wait

func Wait(t Tester, timeout time.Duration, interval ...time.Duration) Timesert

Wait returns a Timesert object will block for up to the given timeout. The third argument is optional, specifying the upper bound on the check interval (defaults to DefaultWaitCheckInterval).

Jump to

Keyboard shortcuts

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