assert

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualComplexes

func EqualComplexes(t testing.TB, want, got complex128, details ...interface{})

EqualComplexes compares the two complex numbers for equality. If they are not equal then the Fatal function is called with details.

func EqualErrs

func EqualErrs(t testing.TB, want, got error, details ...interface{})

EqualErrs compares if two errors have the same error description (by calling .Error()). If they are not equal then the Fatal() function is called with details. Both errors can't be nil.

func EqualFloats

func EqualFloats(t testing.TB, want, got float64, details ...interface{})

EqualFloats compares the two floats for equality. If they are not equal then the Fatal() function is called with details.

func EqualInts

func EqualInts(t testing.TB, want int, got int, details ...interface{})

EqualInts compares the two ints for equality. If they are not equal then the Fatal() function is called with details.

func EqualStrings

func EqualStrings(t testing.TB, want string, got string, details ...interface{})

EqualStrings compares the two strings for equality. If they are not equal then the Fatal() function is called with details.

func Err

func Err(assert *Assert, message string)

Err is a FailureReport that calls t.Error().

func Error

func Error(t testing.TB, err error, details ...interface{})

Error will call Fatal with the given details if the error is nil.

func Fatal

func Fatal(assert *Assert, message string)

Fatal is a FailureReport that calls t.Fatal() to abort the test.

func IsError

func IsError(t testing.TB, got, want error, details ...interface{})

IsError will assert if the given error matches the want error. It uses the errors.Is() function to check if the error wraps the wanted error. It calls the Fatal() function if errors.Is() returns false.

func IsTrue

func IsTrue(t testing.TB, cond bool, details ...interface{})

IsTrue asserts that b is true. If it's not then Fatal() is called with details.

func NoError

func NoError(t testing.TB, err error, details ...interface{})

NoError will assert that given error is nil. If it's nil then the Fatal() function is called with details.

func Partial

func Partial(t testing.TB, obj interface{}, target interface{}, details ...interface{})

func StringContains

func StringContains(t testing.TB, s, substr string, details ...interface{})

StringContains asserts that string s contains the subst string and calls the Fatal() function with details otherwise.

func StringMatch

func StringMatch(t testing.TB, pattern string, str string, details ...interface{})

StringMatch asserts that string matches the regex pattern and calls the Fatal() function with details otherwise.

Types

type Assert

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

Assert is a custom assert helper.

func New

func New(t testing.TB, fail FailureReport, details ...interface{}) *Assert

New creates a new assert helper object with a custom fail function and an optional context detail. For calling t.Fatal() or t.Error() in case of failures, see Fatal() and Err() respectively. Example:

assert := assert.New(t, assert.Fatal)

The variadic details parameter must be a string format followed by its format arguments. Example:

v1.Name = "test"
v2.Name = "tesd"
assert := assert.New(t, assert.Err, "comparing objects %s and %s", v1, v2)
assert.EqualStrings(v1.Name, v2.Name, "Name mismatch")
...

The code above fails with message below:

wanted[test] but got[tesd].Name mismatch: comparing objects Value1 and Value2

func (*Assert) EqualBools

func (assert *Assert) EqualBools(want bool, got bool, details ...interface{})

EqualBools asserts two booleans for equality. If they are not the equal then the failure function is called.

func (*Assert) EqualComplexes

func (assert *Assert) EqualComplexes(want, got complex128, details ...interface{})

EqualComplexes compares the two complex numbers for equality. If they are not equal then the failure function is called with details.

func (*Assert) EqualErrs

func (assert *Assert) EqualErrs(want error, got error, details ...interface{})

EqualErrs compares if two errors have the same error description (by calling .Error()). If they are not equal then the failure function is called with details. Both errors can't be nil.

func (*Assert) EqualFloats

func (assert *Assert) EqualFloats(want float64, got float64, details ...interface{})

EqualFloats compares the two floats for equality. If they are not equal then the failure function is called with details.

func (*Assert) EqualInts

func (assert *Assert) EqualInts(want int, got int, details ...interface{})

EqualInts compares the two ints for equality. If they are not equal then the failure function is called with details.

func (*Assert) EqualStrings

func (assert *Assert) EqualStrings(want string, got string, details ...interface{})

EqualStrings compares the two strings for equality. If they are not equal then the failure function is called with details.

func (*Assert) EqualUints

func (assert *Assert) EqualUints(want uint64, got uint64, details ...interface{})

EqualUints compares the two uint64s for equality. If they are not equal then the failure function is called with details.

func (*Assert) Error

func (assert *Assert) Error(err error, details ...interface{})

Error will call the failure function with the given details if the error is nil.

func (*Assert) IsError

func (assert *Assert) IsError(got, want error, details ...interface{})

IsError will assert if the given error matches the want error. It uses the errors.Is() function to check if the error wraps the wanted error. It calls the failure function if errors.Is() returns false.

func (*Assert) IsFalse

func (assert *Assert) IsFalse(b bool, details ...interface{})

IsFalse asserts that b is false. If it's not then the failure function is called with details.

func (*Assert) IsTrue

func (assert *Assert) IsTrue(b bool, details ...interface{})

IsTrue asserts that b is true. If it's not then the failure function is called with details.

func (*Assert) NoError

func (assert *Assert) NoError(err error, details ...interface{})

NoError will assert that given error is nil. If it's nil then the failure function is called with details.

func (*Assert) Partial

func (assert *Assert) Partial(obj, target interface{}, details ...interface{})

Partial recursively asserts that obj partially matches target. Below are the assertion rules: - booleans, integers, floats and complex numbers must be equal. - strings, slices, array and map: the obj must contains the target. - structs: the obj fields must recursively 8match the target fields.

func (*Assert) StringContains

func (assert *Assert) StringContains(s string, substr string, details ...interface{})

StringContains asserts that string s contains the subst string and calls the failure function with details otherwise.

func (*Assert) StringMatch

func (assert *Assert) StringMatch(pattern string, str string, details ...interface{})

StringMatch asserts that string matches the regex pattern and calls the failure function with details otherwise.

type FailureReport

type FailureReport func(assert *Assert, message string)

FailureReport is the function type used to report assert errors. See assert.Fatal and assert.Err for implementations.

Jump to

Keyboard shortcuts

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