assert

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2018 License: MIT Imports: 13 Imported by: 0

README

go-assert

assert is an assertions library similar to testify/assert but with the focus on assertions being fatal vs. being permissive.

Documentation

Overview

Package assert adds helpers to make writing tests easier.

Index

Constants

View Source
const (
	// RED is the ansi escape code fragment for red.
	RED = "31"
	// BLUE is the ansi escape code fragment for blue.
	BLUE = "94"
	// GREEN is the ansi escape code fragment for green.
	GREEN = "32"
	// YELLOW is the ansi escape code fragment for yellow.
	YELLOW = "33"
	// WHITE is the ansi escape code fragment for white.
	WHITE = "37"
	// GRAY is the ansi escape code fragment for gray.
	GRAY = "90"

	// EMPTY is a constant for the empty (0 length) string.
	EMPTY = ""
)
View Source
const (
	// Unit is a filter for unit tests.
	Unit = "unit"
	// Acceptance is a filter for acceptance tests.
	Acceptance = "acceptance"
	// Integration is a filter for integration tests.
	Integration = "integration"
)

Filters

Variables

This section is empty.

Functions

func CheckFilter

func CheckFilter(t *testing.T, filter Filter)

CheckFilter checks the filter.

func Count

func Count() int

Count returns the total number of assertions.

func Elapsed

func Elapsed() time.Duration

Elapsed returns the time since `Started()`

func Increment

func Increment()

Increment increments the global assertion count.

func Main

func Main(m *testing.M)

Main wraps a testing.M.

func Rate

func Rate() float64

Rate returns the assertions per second.

func ReportRate

func ReportRate()

ReportRate writes the rate summary to stdout.

func Started

func Started()

Started marks a started time.

Types

type Any

type Any = interface{}

Any is a loose type alias to interface{}

type Assertions

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

Assertions is the main entry point for using the assertions library.

func Empty

func Empty() *Assertions

Empty returns an empty assertions class; useful when you want to apply assertions w/o hooking into the testing framework.

func Filtered

func Filtered(t *testing.T, filter Filter) *Assertions

Filtered returns a new instance of `Assertions`.

func New

func New(t *testing.T) *Assertions

New returns a new instance of `Assertions`.

func (*Assertions) All

func (a *Assertions) All(target interface{}, predicate Predicate, userMessageComponents ...interface{})

All applies a predicate.

func (*Assertions) AllOfFloat64

func (a *Assertions) AllOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{})

AllOfFloat64 applies a predicate.

func (*Assertions) AllOfInt

func (a *Assertions) AllOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{})

AllOfInt applies a predicate.

func (*Assertions) AllOfString

func (a *Assertions) AllOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{})

AllOfString applies a predicate.

func (*Assertions) Any

func (a *Assertions) Any(target interface{}, predicate Predicate, userMessageComponents ...interface{})

Any applies a predicate.

func (*Assertions) AnyOfFloat64

func (a *Assertions) AnyOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{})

AnyOfFloat64 applies a predicate.

func (*Assertions) AnyOfInt

func (a *Assertions) AnyOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{})

AnyOfInt applies a predicate.

func (*Assertions) AnyOfString

func (a *Assertions) AnyOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{})

AnyOfString applies a predicate.

func (*Assertions) Contains

func (a *Assertions) Contains(corpus, substring string, userMessageComponents ...interface{})

Contains asserts that a substring is present in a corpus.

func (*Assertions) Empty

func (a *Assertions) Empty(collection interface{}, userMessageComponents ...interface{})

Empty asserts that a collection is empty.

func (*Assertions) EndTimeout

func (a *Assertions) EndTimeout()

EndTimeout marks a timed block as complete.

func (*Assertions) Equal

func (a *Assertions) Equal(expected interface{}, actual interface{}, userMessageComponents ...interface{})

Equal asserts that two objects are deeply equal.

func (*Assertions) FailNow

func (a *Assertions) FailNow(userMessageComponents ...interface{})

FailNow forces a test failure (useful for debugging).

func (*Assertions) False

func (a *Assertions) False(object bool, userMessageComponents ...interface{})

False asserts a boolean is false.

func (*Assertions) FileExists

func (a *Assertions) FileExists(filepath string, userMessageComponents ...interface{})

FileExists asserts that a file exists at a given filepath on disk.

func (*Assertions) InDelta

func (a *Assertions) InDelta(f0, f1, delta float64, userMessageComponents ...interface{})

InDelta asserts that two floats are within a delta.

The delta is computed by the absolute of the difference betwee `f0` and `f1` and testing if that absolute difference is strictly less than `delta` if greater, it will fail the assertion, if delta is equal to or greater than difference the assertion will pass.

func (*Assertions) InTimeDelta

func (a *Assertions) InTimeDelta(t1, t2 time.Time, delta time.Duration, userMessageComponents ...interface{})

InTimeDelta asserts that times t1 and t2 are within a delta.

func (*Assertions) Len

func (a *Assertions) Len(collection interface{}, length int, userMessageComponents ...interface{})

Len asserts that a collection has a given length.

func (*Assertions) Nil

func (a *Assertions) Nil(object interface{}, userMessageComponents ...interface{})

Nil asserts that a reference is nil.

func (*Assertions) NonFatal

func (a *Assertions) NonFatal() *Optional

NonFatal transitions the assertion into a `NonFatal` assertion; that is, one that will not cause the test to abort if it fails. NonFatal assertions are useful when you want to check many properties during a test, but only on an informational basis. They will typically return a bool to indicate if the assertion succeeded, or if you should consider the overall test to still be a success.

func (*Assertions) None

func (a *Assertions) None(target interface{}, predicate Predicate, userMessageComponents ...interface{})

None applies a predicate.

func (*Assertions) NoneOfFloat64

func (a *Assertions) NoneOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{})

NoneOfFloat64 applies a predicate.

func (*Assertions) NoneOfInt

func (a *Assertions) NoneOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{})

NoneOfInt applies a predicate.

func (*Assertions) NoneOfString

func (a *Assertions) NoneOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{})

NoneOfString applies a predicate.

func (*Assertions) NotContains

func (a *Assertions) NotContains(corpus, substring string, userMessageComponents ...interface{})

NotContains asserts that a substring is present in a corpus.

func (*Assertions) NotEmpty

func (a *Assertions) NotEmpty(collection interface{}, userMessageComponents ...interface{})

NotEmpty asserts that a collection is not empty.

func (*Assertions) NotEqual

func (a *Assertions) NotEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{})

NotEqual asserts that two objects are not deeply equal.

func (*Assertions) NotNil

func (a *Assertions) NotNil(object interface{}, userMessageComponents ...interface{})

NotNil asserts that a reference is not nil.

func (*Assertions) NotZero

func (a *Assertions) NotZero(value interface{}, userMessageComponents ...interface{})

NotZero asserts that a value is not equal to it's default value.

func (*Assertions) Output

func (a *Assertions) Output() io.Writer

Output returns the underlying output writer.

func (*Assertions) PanicEqual

func (a *Assertions) PanicEqual(expected interface{}, action func(), userMessageComponents ...interface{})

PanicEqual asserts the panic emitted by an actin equals an expected value.

func (*Assertions) ReferenceEqual

func (a *Assertions) ReferenceEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{})

ReferenceEqual asserts that two objects are the same reference in memory.

func (*Assertions) StartTimeout

func (a *Assertions) StartTimeout(timeout time.Duration, userMessageComponents ...interface{})

StartTimeout starts a timed block.

func (*Assertions) True

func (a *Assertions) True(object bool, userMessageComponents ...interface{})

True asserts a boolean is true.

func (*Assertions) WithFilter

func (a *Assertions) WithFilter(filter Filter) *Assertions

WithFilter sets the filter.

func (*Assertions) WithOutput

func (a *Assertions) WithOutput(w io.Writer) *Assertions

WithOutput sets the assertions output. Error messages will be written to this in addition to the test handler.

func (*Assertions) Zero

func (a *Assertions) Zero(value interface{}, userMessageComponents ...interface{})

Zero asserts that a value is equal to it's default value.

type Filter

type Filter string

Filter is a unit test filter.

type Optional

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

Optional is an assertion type that does not stop a test if an assertion fails, simply outputs the error.

func (*Optional) All

func (o *Optional) All(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool

All applies a predicate.

func (*Optional) AllOfFloat

func (o *Optional) AllOfFloat(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool

AllOfFloat applies a predicate.

func (*Optional) AllOfInt

func (o *Optional) AllOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool

AllOfInt applies a predicate.

func (*Optional) AllOfString

func (o *Optional) AllOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool

AllOfString applies a predicate.

func (*Optional) Any

func (o *Optional) Any(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool

Any applies a predicate.

func (*Optional) AnyOfFloat

func (o *Optional) AnyOfFloat(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool

AnyOfFloat applies a predicate.

func (*Optional) AnyOfInt

func (o *Optional) AnyOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool

AnyOfInt applies a predicate.

func (*Optional) AnyOfString

func (o *Optional) AnyOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool

AnyOfString applies a predicate.

func (*Optional) Contains

func (o *Optional) Contains(corpus, substring string, userMessageComponents ...interface{}) bool

Contains checks if a substring is present in a corpus.

func (*Optional) Empty

func (o *Optional) Empty(collection interface{}, userMessageComponents ...interface{}) bool

Empty asserts that a collection is empty.

func (*Optional) Equal

func (o *Optional) Equal(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool

Equal asserts that two objects are equal.

func (*Optional) Fail

func (o *Optional) Fail(userMessageComponents ...interface{})

Fail manually injects a failure.

func (*Optional) False

func (o *Optional) False(object bool, userMessageComponents ...interface{}) bool

False asserts that a bool is false.

func (*Optional) FileExists

func (o *Optional) FileExists(filepath string, userMessageComponents ...interface{}) bool

FileExists asserts that a file exists on disk at a given filepath.

func (*Optional) InDelta

func (o *Optional) InDelta(a, b, delta float64, userMessageComponents ...interface{}) bool

InDelta returns if two float64s are separated by a given delta.

func (*Optional) InTimeDelta

func (o *Optional) InTimeDelta(a, b time.Time, delta time.Duration, userMessageComponents ...interface{}) bool

InTimeDelta returns if two times are separated by a given delta.

func (*Optional) Len

func (o *Optional) Len(collection interface{}, length int, userMessageComponents ...interface{}) bool

Len asserts that the collection has a specified length.

func (*Optional) Nil

func (o *Optional) Nil(object interface{}, userMessageComponents ...interface{}) bool

Nil asserts the object is nil.

func (*Optional) None

func (o *Optional) None(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool

None applies a predicate.

func (*Optional) NoneOfFloat

func (o *Optional) NoneOfFloat(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool

NoneOfFloat applies a predicate.

func (*Optional) NoneOfInt

func (o *Optional) NoneOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool

NoneOfInt applies a predicate.

func (*Optional) NoneOfString

func (o *Optional) NoneOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool

NoneOfString applies a predicate.

func (*Optional) NotContains

func (o *Optional) NotContains(corpus, substring string, userMessageComponents ...interface{}) bool

NotContains checks if a substring is not present in a corpus.

func (*Optional) NotEmpty

func (o *Optional) NotEmpty(collection interface{}, userMessageComponents ...interface{}) bool

NotEmpty asserts that a collection is not empty.

func (*Optional) NotEqual

func (o *Optional) NotEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool

NotEqual asserts that two objects are not equal.

func (*Optional) NotNil

func (o *Optional) NotNil(object interface{}, userMessageComponents ...interface{}) bool

NotNil asserts the object is not nil.

func (*Optional) NotZero

func (o *Optional) NotZero(value interface{}, userMessageComponents ...interface{}) bool

NotZero asserts that a value is not the default value.

func (*Optional) Output

func (o *Optional) Output() io.Writer

Output returns the underlying output writer.

func (*Optional) PanicEqual

func (o *Optional) PanicEqual(expected interface{}, action func(), userMessageComponents ...interface{}) bool

PanicEqual asserts the panic emitted by an actin equals an expected value.

func (*Optional) ReferenceEqual

func (o *Optional) ReferenceEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool

ReferenceEqual asserts that two objects are the same underlying reference in memory.

func (*Optional) True

func (o *Optional) True(object bool, userMessageComponents ...interface{}) bool

True asserts that a bool is false.

func (*Optional) WithOutput

func (o *Optional) WithOutput(w io.Writer) *Optional

WithOutput sets an output to capture error output.

func (*Optional) Zero

func (o *Optional) Zero(value interface{}, userMessageComponents ...interface{}) bool

Zero asserts that a value is the default value.

type Predicate

type Predicate func(item Any) bool

Predicate is a func that returns a bool.

type PredicateOfFloat

type PredicateOfFloat func(item float64) bool

PredicateOfFloat is a func that takes a float64 and returns a bool.

type PredicateOfInt

type PredicateOfInt func(item int) bool

PredicateOfInt is a func that takes an int and returns a bool.

type PredicateOfString

type PredicateOfString func(item string) bool

PredicateOfString is a func that takes a string and returns a bool.

type PredicateOfTime

type PredicateOfTime func(item time.Time) bool

PredicateOfTime is a func that takes a time.Time and returns a bool.

Jump to

Keyboard shortcuts

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