assert

package
v0.77.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

testcase/assert

This package meant to provide a small and dependency lightweight implementation for common assertion related requirements.

Example:

assert.Should(tb).True(true)
assert.Must(tb).Equal(expected, actual, "error message")
assert.Must(tb).NotEqual(true, false, "exp")
assert.Must(tb).Contain([]int{1, 2, 3}, 3, "exp")
assert.Must(tb).Contain([]int{1, 2, 3}, []int{1, 2}, "exp")
assert.Must(tb).Contain(map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 42}, "exp")

For more examples, check out the example_test.go file.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyOf added in v0.60.0

type AnyOf struct {
	TB testing.TB
	Fn func(...interface{})
	// contains filtered or unexported fields
}

AnyOf is an assertion helper that allows you run AnyOf.Test assertion blocks, that can fail, as lone at least one of them succeeds. common usage use-cases:

  • list of interface, where test order, or the underlying structure's implementation is irrelevant for the behavior.
  • list of big structures, where not all field value relevant, only a subset, like a structure it wraps under a field.
  • list of structures with fields that has dynamic state values, which is irrelevant for the given test.
  • structure that can have various state scenario, and you want to check all of them, and you expect to find one match with the input.
  • fan out scenario, where you need to check in parallel that at least one of the worker received the event.
Example (FanOutPublishing)
Output:

Example (ListOfCompositedStructuresWhereOnlyTheEmbededValueIsRelevant)
Output:

Example (ListOfInterface)
Output:

Example (ListOfStructuresWithIrrelevantValues)
Output:

Example (StructWithManyAcceptableState)
Output:

func (*AnyOf) Finish added in v0.60.0

func (ao *AnyOf) Finish(msg ...interface{})

Finish will check if any of the assertion succeeded.

func (*AnyOf) Test added in v0.60.0

func (ao *AnyOf) Test(blk func(it It))

Test will test a block of assertion that must succeed in order to make AnyOf pass. You can have as much AnyOf.Test calls as you need, but if any of them pass with success, the rest will be skipped. Using Test is safe for concurrently.

type Asserter

type Asserter struct {
	TB testing.TB
	Fn func(args ...interface{})
}

func Must

func Must(tb testing.TB) Asserter
Example
Output:

func Should

func Should(tb testing.TB) Asserter
Example
Output:

func (Asserter) AnyOf added in v0.60.0

func (a Asserter) AnyOf(blk func(a *AnyOf), msg ...interface{})
Example
Output:

func (Asserter) Contain

func (a Asserter) Contain(src, has interface{}, msg ...interface{})
Example
Output:

func (Asserter) ContainExactly

func (a Asserter) ContainExactly(expected, actual interface{}, msg ...interface{})
Example
Output:

func (Asserter) Empty added in v0.61.0

func (a Asserter) Empty(v interface{}, msg ...interface{})

Empty gets whether the specified value is considered empty.

Example
Output:

func (Asserter) Equal

func (a Asserter) Equal(expected, actually interface{}, msg ...interface{})
Example
Output:

Example (IsEqualFunctionThatSupportsErrorReturning)
Output:

Example (IsEqualFunctionUsedForComparison)
Output:

func (Asserter) ErrorIs added in v0.68.0

func (a Asserter) ErrorIs(expected, actual error, msg ...interface{})

ErrorIs allows you to assert an error value by an expectation. if the implementation of the test subject later changes, and for example, it starts to use wrapping, this should not be an issue as the IsEqualErr's error chain is also matched against the expectation.

Example
Output:

func (Asserter) False added in v0.61.0

func (a Asserter) False(v bool, msg ...interface{})
Example
Output:

func (Asserter) Nil

func (a Asserter) Nil(v interface{}, msg ...interface{})
Example
Output:

func (Asserter) NotContain

func (a Asserter) NotContain(source, oth interface{}, msg ...interface{})
Example
Output:

func (Asserter) NotEmpty added in v0.61.0

func (a Asserter) NotEmpty(v interface{}, msg ...interface{})

NotEmpty gets whether the specified value is considered empty.

Example
Output:

func (Asserter) NotEqual added in v0.58.0

func (a Asserter) NotEqual(v, oth interface{}, msg ...interface{})
Example
Output:

func (Asserter) NotNil

func (a Asserter) NotNil(v interface{}, msg ...interface{})
Example
Output:

func (Asserter) NotPanic

func (a Asserter) NotPanic(blk func(), msg ...interface{})
Example
Output:

func (Asserter) Panic

func (a Asserter) Panic(blk func(), msg ...interface{}) (panicValue interface{})
Example
Output:

func (Asserter) True

func (a Asserter) True(v bool, msg ...interface{})
Example
Output:

type It added in v0.60.0

type It struct {
	testing.TB
	// Must Asserter will use FailNow on a failed assertion.
	// This will make test exit early on.
	Must Asserter
	// Should Asserter's will allow to continue the test scenario,
	// but mark test failed on a failed assertion.
	Should Asserter
}

func MakeIt added in v0.63.0

func MakeIt(tb testing.TB) It

Jump to

Keyboard shortcuts

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