asserts

package
v0.0.0-...-e30cafb Latest Latest
Warning

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

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

Documentation

Overview

The asserts package provides a powerful and convenient set of assertions. Here expected and obtained values are compared. Individual descriptions as well as the output help to quickly find failed tests.

In the beginning of a test function a new assertion instance is created with:

assert := asserts.NewTestingAssertion(t, shallFail)

Inside the test an assert looks like:

assert.Equal(expected, obtained, "obtained value has to be like expected")

If shallFail is set to true a failing assert also lets fail the Go test. Otherwise the failing is printed but the tests continue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValueDescription

func ValueDescription(value interface{}) string

ValueDescription returns a description of a value as string.

Types

type Assertion

type Assertion interface {
	// Logf can be used to display useful information during testing.
	Logf(format string, args ...interface{})

	// True tests if obtained is true.
	True(obtained bool, msgs ...string) bool

	// False tests if obtained is false.
	False(obtained bool, msgs ...string) bool

	// Nil tests if obtained is nil.
	Nil(obtained interface{}, msgs ...string) bool

	// NotNil tests if obtained is not nil.
	NotNil(obtained interface{}, msgs ...string) bool

	// Equal tests if obtained and expected are equal.
	Equal(obtained, expected interface{}, msgs ...string) bool

	// Different tests if obtained and expected are different.
	Different(obtained, expected interface{}, msgs ...string) bool

	// About tests if obtained and expected are near to each other
	// (within the given extend).
	About(obtained, expected, extend float64, msgs ...string) bool

	// Substring tests if obtained is a substring of the full string.
	Substring(obtained, full string, msgs ...string) bool

	// Match tests if the obtained string matches a regular expression.
	Match(obtained, regex string, msgs ...string) bool

	// ErrorMatch tests if the obtained error as string matches a
	// regular expression.
	ErrorMatch(obtained error, regex string, msgs ...string) bool

	// Implementor tests if obtained implements the expected
	// interface variable pointer.
	Implementor(obtained, expected interface{}, msgs ...string) bool

	// Assignable tests if the types of expected and obtained are assignable.
	Assignable(obtained, expected interface{}, msgs ...string) bool

	// Unassignable tests if the types of expected and obtained are
	// not assignable.
	Unassignable(obtained, expected interface{}, msgs ...string) bool

	// Empty tests if the len of the obtained string, array, slice
	// map or channel is 0.
	Empty(obtained interface{}, msgs ...string) bool

	// NotEmpty tests if the len of the obtained string, array, slice
	// map or channel is greater than 0.
	NotEmpty(obtained interface{}, msgs ...string) bool

	// Length tests if the len of the obtained string, array, slice
	// map or channel is equal to the expected one.
	Length(obtained interface{}, expected int, msgs ...string) bool

	// Panics checks if the passed function panics.
	Panics(pf func(), msgs ...string) bool

	// Fail always fails.
	Fail(msgs ...string) bool
}

Assertion defines the available test methods.

func NewAssertion

func NewAssertion(f Failer) Assertion

NewAssertion creates a new Assertion instance.

func NewPanicAssertion

func NewPanicAssertion() Assertion

NewPanicAssertion creates a new Assertion instance which panics if a test fails.

func NewTestingAssertion

func NewTestingAssertion(f Failable, shallFail bool) Assertion

NewTestingAssertion creates a new Assertion instance for use with the testing package. The *testing.T has to be passed as failable, the first argument. shallFail controls if a failing assertion also lets fail the Go test.

type Failable

type Failable interface {
	FailNow()
}

Failable allows an assertion to signal a fail to an external instance like testing.T or testing.B.

type Failer

type Failer interface {
	// Logf can be used to display useful information during testing.
	Logf(format string, args ...interface{})

	// Fail will be called if an assert fails.
	Fail(test Test, obtained, expected interface{}, msgs ...string) bool
}

failer describes a type controlling how an assert reacts after a failure.

type Test

type Test uint

Test represents the test inside an assert.

const (
	Invalid Test = iota
	True
	False
	Nil
	NotNil
	Equal
	Different
	About
	Substring
	Match
	ErrorMatch
	Implementor
	Assignable
	Unassignable
	Empty
	NotEmpty
	Length
	Panics
	Fail
)

func (Test) String

func (t Test) String() string

type Tester

type Tester struct{}

Tester is a helper which can be used in own Assertion implementations.

func (Tester) HasPanic

func (t Tester) HasPanic(pf func()) (ok bool)

HasPanic checks if the passed function panics.

func (Tester) IsAbout

func (t Tester) IsAbout(obtained, expected, extend float64) bool

IsAbout checks if obtained and expected are to a given extend almost equal.

func (Tester) IsAssignable

func (t Tester) IsAssignable(obtained, expected interface{}) bool

IsAssignable checks if the types of obtained and expected are assignable.

func (Tester) IsEqual

func (t Tester) IsEqual(obtained, expected interface{}) bool

IsEqual checks if obtained and expected are equal.

func (Tester) IsImplementor

func (t Tester) IsImplementor(obtained, expected interface{}) (bool, error)

IsImplementor checks if obtained implements the expected interface variable pointer.

func (Tester) IsMatching

func (t Tester) IsMatching(obtained, regex string) (bool, error)

IsMatching checks if the obtained string matches a regular expression.

func (Tester) IsNil

func (t Tester) IsNil(obtained interface{}) bool

IsNil checks if obtained is nil in a safe way.

func (Tester) IsSubstring

func (t Tester) IsSubstring(obtained, full string) bool

IsSubstring checks if obtained is a substring of the full string.

func (Tester) IsTrue

func (t Tester) IsTrue(obtained bool) bool

IsTrue checks if obtained is true.

func (Tester) Len

func (t Tester) Len(obtained interface{}) (int, error)

Length checks the len of the obtained string, array, slice, map or channel.

Jump to

Keyboard shortcuts

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