Documentation ¶
Overview ¶
Package assert is a suite of helpers and convenience methods for writing tests that wrap `*testing.T`.
Package assert adds helpers to make writing tests easier.
Example Usage:
func TestFoo(t *testing.T) { // create the assertions wrapper assert := assert.New(t) assert.True(false) // this will fail the test. }
Index ¶
- Constants
- func Count() int
- func Elapsed() time.Duration
- func GetContextID(ctx context.Context) string
- func GetTestName(ctx context.Context) string
- func Increment()
- func Rate() float64
- func ReportRate()
- func Started()
- func WithContextID(ctx context.Context, id string) context.Context
- func WithTestName(ctx context.Context, id string) context.Context
- type Assertions
- func (a *Assertions) All(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool
- func (a *Assertions) AllOfFloat64(target []float64, predicate PredicateOfFloat, ...) bool
- func (a *Assertions) AllOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
- func (a *Assertions) AllOfString(target []string, predicate PredicateOfString, ...) bool
- func (a *Assertions) Any(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool
- func (a *Assertions) AnyCount(target interface{}, times int, predicate Predicate, ...) bool
- func (a *Assertions) AnyOfFloat64(target []float64, predicate PredicateOfFloat, ...) bool
- func (a *Assertions) AnyOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
- func (a *Assertions) AnyOfString(target []string, predicate PredicateOfString, ...) bool
- func (a *Assertions) Background() context.Context
- func (a *Assertions) Contains(corpus, substring string, userMessageComponents ...interface{}) bool
- func (a *Assertions) Empty(collection interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) Equal(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) Fail(userMessageComponents ...interface{}) bool
- func (a *Assertions) FailNow(userMessageComponents ...interface{})
- func (a *Assertions) False(object bool, userMessageComponents ...interface{}) bool
- func (a *Assertions) FileExists(filepath string, userMessageComponents ...interface{}) bool
- func (a *Assertions) HasPrefix(corpus, prefix string, userMessageComponents ...interface{}) bool
- func (a *Assertions) HasSuffix(corpus, suffix string, userMessageComponents ...interface{}) bool
- func (a *Assertions) InDelta(f0, f1, delta float64, userMessageComponents ...interface{}) bool
- func (a *Assertions) InTimeDelta(t1, t2 time.Time, delta time.Duration, userMessageComponents ...interface{}) bool
- func (a *Assertions) Len(collection interface{}, length int, userMessageComponents ...interface{}) bool
- func (a *Assertions) Matches(expr string, value interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) Nil(object interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) NonFatal() *Assertions
- func (a *Assertions) None(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool
- func (a *Assertions) NoneOfFloat64(target []float64, predicate PredicateOfFloat, ...) bool
- func (a *Assertions) NoneOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
- func (a *Assertions) NoneOfString(target []string, predicate PredicateOfString, ...) bool
- func (a *Assertions) NotContains(corpus, substring string, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotEmpty(collection interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotHasPrefix(corpus, prefix string, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotHasSuffix(corpus, suffix string, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotImplemented(userMessageComponents ...interface{})
- func (a *Assertions) NotInTimeDelta(t1, t2 time.Time, delta time.Duration, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotMatches(expr string, value interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotNil(object interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) NotPanic(action func(), userMessageComponents ...interface{}) bool
- func (a *Assertions) NotZero(value interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) PanicEqual(expected interface{}, action func(), userMessageComponents ...interface{}) bool
- func (a *Assertions) ReferenceEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
- func (a *Assertions) True(object bool, userMessageComponents ...interface{}) bool
- func (a *Assertions) Zero(value interface{}, userMessageComponents ...interface{}) bool
- type Failure
- type Option
- type OutputFormat
- type Predicate
- type PredicateOfFloat
- type PredicateOfInt
- type PredicateOfString
- type PredicateOfTime
Constants ¶
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" )
Variables ¶
This section is empty.
Functions ¶
func GetContextID ¶ added in v1.20201204.1
GetContextID gets the context id for a test run.
func GetTestName ¶ added in v1.20201204.1
GetTestName gets the test name for a test run context.
func WithContextID ¶ added in v1.20201204.1
WithContextID sets the test context id.
Types ¶
type Assertions ¶
type Assertions struct { Output io.Writer OutputFormat OutputFormat T *testing.T Context context.Context Optional bool Count int32 }
Assertions is the main entry point for using the assertions library.
func Empty ¶
func Empty(opts ...Option) *Assertions
Empty returns an empty assertions handler; useful when you want to apply assertions w/o hooking into the testing framework.
func New ¶
func New(t *testing.T, opts ...Option) *Assertions
New returns a new instance of `Assertions`.
func (*Assertions) All ¶
func (a *Assertions) All(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool
All applies a predicate.
func (*Assertions) AllOfFloat64 ¶
func (a *Assertions) AllOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool
AllOfFloat64 applies a predicate.
func (*Assertions) AllOfInt ¶
func (a *Assertions) AllOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
AllOfInt applies a predicate.
func (*Assertions) AllOfString ¶
func (a *Assertions) AllOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool
AllOfString applies a predicate.
func (*Assertions) Any ¶
func (a *Assertions) Any(target interface{}, predicate Predicate, userMessageComponents ...interface{}) bool
Any applies a predicate.
func (*Assertions) AnyCount ¶ added in v1.20210103.1
func (a *Assertions) AnyCount(target interface{}, times int, predicate Predicate, userMessageComponents ...interface{}) bool
AnyCount applies a predicate and passes if it fires a given number of times .
func (*Assertions) AnyOfFloat64 ¶
func (a *Assertions) AnyOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool
AnyOfFloat64 applies a predicate.
func (*Assertions) AnyOfInt ¶
func (a *Assertions) AnyOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
AnyOfInt applies a predicate.
func (*Assertions) AnyOfString ¶
func (a *Assertions) AnyOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool
AnyOfString applies a predicate.
func (*Assertions) Background ¶ added in v1.20201204.1
func (a *Assertions) Background() context.Context
Background returns the assertions context.
func (*Assertions) Contains ¶
func (a *Assertions) Contains(corpus, substring string, userMessageComponents ...interface{}) bool
Contains asserts that a substring is present in a corpus.
func (*Assertions) Empty ¶
func (a *Assertions) Empty(collection interface{}, userMessageComponents ...interface{}) bool
Empty asserts that a collection is empty.
func (*Assertions) Equal ¶
func (a *Assertions) Equal(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
Equal asserts that two objects are deeply equal.
func (*Assertions) Fail ¶ added in v1.20201204.1
func (a *Assertions) Fail(userMessageComponents ...interface{}) bool
Fail forces a test failure (useful for debugging).
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{}) bool
False asserts a boolean is false.
func (*Assertions) FileExists ¶
func (a *Assertions) FileExists(filepath string, userMessageComponents ...interface{}) bool
FileExists asserts that a file exists at a given filepath on disk.
func (*Assertions) HasPrefix ¶ added in v1.20210116.3
func (a *Assertions) HasPrefix(corpus, prefix string, userMessageComponents ...interface{}) bool
HasPrefix asserts that a corpus has a given prefix.
func (*Assertions) HasSuffix ¶ added in v1.20210116.3
func (a *Assertions) HasSuffix(corpus, suffix string, userMessageComponents ...interface{}) bool
HasSuffix asserts that a corpus has a given suffix.
func (*Assertions) InDelta ¶
func (a *Assertions) InDelta(f0, f1, delta float64, userMessageComponents ...interface{}) bool
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{}) bool
InTimeDelta asserts that times t1 and t2 are within a delta.
func (*Assertions) Len ¶
func (a *Assertions) Len(collection interface{}, length int, userMessageComponents ...interface{}) bool
Len asserts that a collection has a given length.
func (*Assertions) Matches ¶ added in v1.20201204.1
func (a *Assertions) Matches(expr string, value interface{}, userMessageComponents ...interface{}) bool
Matches returns if a given value matches a given regexp expression.
func (*Assertions) Nil ¶
func (a *Assertions) Nil(object interface{}, userMessageComponents ...interface{}) bool
Nil asserts that a reference is nil.
func (*Assertions) NonFatal ¶
func (a *Assertions) NonFatal() *Assertions
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{}) bool
None applies a predicate.
func (*Assertions) NoneOfFloat64 ¶
func (a *Assertions) NoneOfFloat64(target []float64, predicate PredicateOfFloat, userMessageComponents ...interface{}) bool
NoneOfFloat64 applies a predicate.
func (*Assertions) NoneOfInt ¶
func (a *Assertions) NoneOfInt(target []int, predicate PredicateOfInt, userMessageComponents ...interface{}) bool
NoneOfInt applies a predicate.
func (*Assertions) NoneOfString ¶
func (a *Assertions) NoneOfString(target []string, predicate PredicateOfString, userMessageComponents ...interface{}) bool
NoneOfString applies a predicate.
func (*Assertions) NotContains ¶
func (a *Assertions) NotContains(corpus, substring string, userMessageComponents ...interface{}) bool
NotContains asserts that a substring is present in a corpus.
func (*Assertions) NotEmpty ¶
func (a *Assertions) NotEmpty(collection interface{}, userMessageComponents ...interface{}) bool
NotEmpty asserts that a collection is not empty.
func (*Assertions) NotEqual ¶
func (a *Assertions) NotEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
NotEqual asserts that two objects are not deeply equal.
func (*Assertions) NotHasPrefix ¶ added in v1.20210116.3
func (a *Assertions) NotHasPrefix(corpus, prefix string, userMessageComponents ...interface{}) bool
NotHasPrefix asserts that a corpus does not have a given prefix.
func (*Assertions) NotHasSuffix ¶ added in v1.20210116.3
func (a *Assertions) NotHasSuffix(corpus, suffix string, userMessageComponents ...interface{}) bool
NotHasSuffix asserts that a corpus does not have a given suffix.
func (*Assertions) NotImplemented ¶ added in v1.20210603.3
func (a *Assertions) NotImplemented(userMessageComponents ...interface{})
NotImplemented will just error.
func (*Assertions) NotInTimeDelta ¶ added in v1.20201204.1
func (a *Assertions) NotInTimeDelta(t1, t2 time.Time, delta time.Duration, userMessageComponents ...interface{}) bool
NotInTimeDelta asserts that times t1 and t2 are not within a delta.
func (*Assertions) NotMatches ¶ added in v1.20201204.1
func (a *Assertions) NotMatches(expr string, value interface{}, userMessageComponents ...interface{}) bool
NotMatches returns if a given value does not match a given regexp expression.
func (*Assertions) NotNil ¶
func (a *Assertions) NotNil(object interface{}, userMessageComponents ...interface{}) bool
NotNil asserts that a reference is not nil.
func (*Assertions) NotPanic ¶ added in v1.20210701.2
func (a *Assertions) NotPanic(action func(), userMessageComponents ...interface{}) bool
NotPanic asserts the given action does not panic.
func (*Assertions) NotZero ¶
func (a *Assertions) NotZero(value interface{}, userMessageComponents ...interface{}) bool
NotZero asserts that a value is not equal to it's default value.
func (*Assertions) PanicEqual ¶
func (a *Assertions) PanicEqual(expected interface{}, action func(), userMessageComponents ...interface{}) bool
PanicEqual asserts the panic emitted by an action equals an expected value.
func (*Assertions) ReferenceEqual ¶
func (a *Assertions) ReferenceEqual(expected interface{}, actual interface{}, userMessageComponents ...interface{}) bool
ReferenceEqual asserts that two objects are the same reference in memory.
func (*Assertions) True ¶
func (a *Assertions) True(object bool, userMessageComponents ...interface{}) bool
True asserts a boolean is true.
func (*Assertions) Zero ¶
func (a *Assertions) Zero(value interface{}, userMessageComponents ...interface{}) bool
Zero asserts that a value is equal to it's default value.
type Failure ¶ added in v1.20201204.1
type Failure struct { Message string `json:"message"` UserMessage string `json:"userMessage,omitempty"` CallerInfo []string `json:"callerInfo"` }
Failure is an assertion failure.
func NewFailure ¶ added in v1.20201204.1
NewFailure creates a new failure.
type OutputFormat ¶ added in v1.20201204.1
type OutputFormat string
OutputFormat is an assertion error output format.
const ( OutputFormatDefault OutputFormat = "" OutputFormatText OutputFormat = "text" OutputFormatJSON OutputFormat = "json" )
OutputFormats
func OutputFormatFromEnv ¶ added in v1.20201204.1
func OutputFormatFromEnv() OutputFormat
OutputFormatFromEnv gets the output format from the env or the default.
type Predicate ¶
type Predicate func(item interface{}) bool
Predicate is a func that returns a bool.
type PredicateOfFloat ¶
PredicateOfFloat is a func that takes a float64 and returns a bool.
type PredicateOfInt ¶
PredicateOfInt is a func that takes an int and returns a bool.
type PredicateOfString ¶
PredicateOfString is a func that takes a string and returns a bool.
type PredicateOfTime ¶
PredicateOfTime is a func that takes a time.Time and returns a bool.