Documentation
¶
Overview ¶
Package assert implements assertion functions for verifying invariants to extend the built-in testing package.
Index ¶
- Variables
- func ContainsElement(t *testing.T, source, expect any, message ...any) error
- func ContainsElementNow(t *testing.T, source, expect any, message ...any) error
- func ContainsString(t *testing.T, str, substr string, message ...any) error
- func ContainsStringNow(t *testing.T, str, substr string, message ...any) error
- func DeepEqual(t *testing.T, actual, expect any, message ...any) error
- func DeepEqualNow(t *testing.T, actual, expect any, message ...any) error
- func Equal(t *testing.T, actual, expect any, message ...any) error
- func EqualNow(t *testing.T, actual, expect any, message ...any) error
- func FloatEqual(t *testing.T, actual, expect, epsilon any, message ...any) error
- func FloatEqualNow(t *testing.T, actual, expect, epsilon any, message ...any) error
- func FloatNotEqual(t *testing.T, actual, expect, epsilon any, message ...any) error
- func FloatNotEqualNow(t *testing.T, actual, expect, epsilon any, message ...any) error
- func Gt(t *testing.T, v1, v2 any, message ...string) error
- func GtNow(t *testing.T, v1, v2 any, message ...string) error
- func Gte(t *testing.T, v1, v2 any, message ...string) error
- func GteNow(t *testing.T, v1, v2 any, message ...string) error
- func HasPrefixString(t *testing.T, str, prefix string, message ...any) error
- func HasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error
- func HasSuffixString(t *testing.T, str, suffix string, message ...any) error
- func HasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error
- func IsError(t *testing.T, err, expected error, message ...any) error
- func IsErrorNow(t *testing.T, err, expected error, message ...any) error
- func Lt(t *testing.T, v1, v2 any, message ...string) error
- func LtNow(t *testing.T, v1, v2 any, message ...string) error
- func Lte(t *testing.T, v1, v2 any, message ...string) error
- func LteNow(t *testing.T, v1, v2 any, message ...string) error
- func MapHasKey(t *testing.T, m, key any, message ...any) error
- func MapHasKeyNow(t *testing.T, m, key any, message ...any) error
- func MapHasValue(t *testing.T, m, value any, message ...any) error
- func MapHasValueNow(t *testing.T, m, value any, message ...any) error
- func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error
- func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error
- func MatchString(t *testing.T, val, pattern string, message ...any) error
- func MatchStringNow(t *testing.T, val, pattern string, message ...any) error
- func Nil(t *testing.T, val any, message ...any) error
- func NilNow(t *testing.T, val any, message ...any) error
- func NotContainsElement(t *testing.T, source, expect any, message ...any) error
- func NotContainsElementNow(t *testing.T, source, expect any, message ...any) error
- func NotContainsString(t *testing.T, str, substr string, message ...any) error
- func NotContainsStringNow(t *testing.T, str, substr string, message ...any) error
- func NotDeepEqual(t *testing.T, actual, expect any, message ...any) error
- func NotDeepEqualNow(t *testing.T, actual, expect any, message ...any) error
- func NotEqual(t *testing.T, actual, expect any, message ...any) error
- func NotEqualNow(t *testing.T, actual, expect any, message ...any) error
- func NotHasPrefixString(t *testing.T, str, prefix string, message ...any) error
- func NotHasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error
- func NotHasSuffixString(t *testing.T, str, suffix string, message ...any) error
- func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error
- func NotIsError(t *testing.T, err, unexpected error, message ...any) error
- func NotIsErrorNow(t *testing.T, err, unexpected error, message ...any) error
- func NotMapHasKey(t *testing.T, m, key any, message ...any) error
- func NotMapHasKeyNow(t *testing.T, m, key any, message ...any) error
- func NotMapHasValue(t *testing.T, m, value any, message ...any) error
- func NotMapHasValueNow(t *testing.T, m, value any, message ...any) error
- func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error
- func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error
- func NotMatchString(t *testing.T, val, pattern string, message ...any) error
- func NotMatchStringNow(t *testing.T, val, pattern string, message ...any) error
- func NotNil(t *testing.T, val any, message ...any) error
- func NotNilNow(t *testing.T, val any, message ...any) error
- func NotPanic(t *testing.T, fn func(), message ...any) error
- func NotPanicNow(t *testing.T, fn func(), message ...any) error
- func NotPanicOf(t *testing.T, fn func(), unexpectedErr any, message ...any) error
- func NotPanicOfNow(t *testing.T, fn func(), unexpectedErr any, message ...any) error
- func NotTrue(t *testing.T, val any, message ...any) error
- func NotTrueNow(t *testing.T, val any, message ...any) error
- func Panic(t *testing.T, fn func(), message ...any) error
- func PanicNow(t *testing.T, fn func(), message ...any) error
- func PanicOf(t *testing.T, fn func(), expectErr any, message ...any) error
- func PanicOfNow(t *testing.T, fn func(), expectErr any, message ...any) error
- func True(t *testing.T, val any, message ...any) error
- func TrueNow(t *testing.T, val any, message ...any) error
- type Assertion
- func (a *Assertion) ContainsElement(source, expect any, message ...any) error
- func (a *Assertion) ContainsElementNow(source, expect any, message ...any) error
- func (a *Assertion) ContainsString(str, substr string, message ...any) error
- func (a *Assertion) ContainsStringNow(str, substr string, message ...any) error
- func (a *Assertion) DeepEqual(actual, expect any, message ...any) error
- func (a *Assertion) DeepEqualNow(actual, expect any, message ...any) error
- func (a *Assertion) Equal(actual, expect any, message ...any) error
- func (a *Assertion) EqualNow(actual, expect any, message ...any) error
- func (a *Assertion) FloatEqual(actual, expect, epsilon any, message ...any) error
- func (a *Assertion) FloatEqualNow(actual, expect, epsilon any, message ...any) error
- func (a *Assertion) FloatNotEqual(actual, expect, epsilon any, message ...any) error
- func (a *Assertion) FloatNotEqualNow(actual, expect, epsilon any, message ...any) error
- func (a *Assertion) Gt(v1, v2 any, message ...string) error
- func (a *Assertion) GtNow(v1, v2 any, message ...string) error
- func (a *Assertion) Gte(v1, v2 any, message ...string) error
- func (a *Assertion) GteNow(v1, v2 any, message ...string) error
- func (a *Assertion) HasPrefixString(str, prefix string, message ...any) error
- func (a *Assertion) HasPrefixStringNow(str, prefix string, message ...any) error
- func (a *Assertion) HasSuffixString(str, suffix string, message ...any) error
- func (a *Assertion) HasSuffixStringNow(str, suffix string, message ...any) error
- func (a *Assertion) IsError(err, expected error, message ...any) error
- func (a *Assertion) IsErrorNow(err, expected error, message ...any) error
- func (a *Assertion) Lt(v1, v2 any, message ...string) error
- func (a *Assertion) LtNow(v1, v2 any, message ...string) error
- func (a *Assertion) Lte(v1, v2 any, message ...string) error
- func (a *Assertion) LteNow(v1, v2 any, message ...string) error
- func (a *Assertion) MapHasKey(m, key any, message ...any) error
- func (a *Assertion) MapHasKeyNow(m, key any, message ...any) error
- func (a *Assertion) MapHasValue(m, value any, message ...any) error
- func (a *Assertion) MapHasValueNow(m, value any, message ...any) error
- func (a *Assertion) Match(val string, pattern *regexp.Regexp, message ...any) error
- func (a *Assertion) MatchNow(val string, pattern *regexp.Regexp, message ...any) error
- func (a *Assertion) MatchString(val, pattern string, message ...any) error
- func (a *Assertion) MatchStringNow(val, pattern string, message ...any) error
- func (a *Assertion) Nil(val any, message ...any) error
- func (a *Assertion) NilNow(val any, message ...any) error
- func (a *Assertion) NotContainsElement(source, expect any, message ...any) error
- func (a *Assertion) NotContainsElementNow(source, expect any, message ...any) error
- func (a *Assertion) NotContainsString(str, substr string, message ...any) error
- func (a *Assertion) NotContainsStringNow(str, substr string, message ...any) error
- func (a *Assertion) NotDeepEqual(actual, expect any, message ...any) error
- func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...any) error
- func (a *Assertion) NotEqual(actual, expect any, message ...any) error
- func (a *Assertion) NotEqualNow(actual, expect any, message ...any) error
- func (a *Assertion) NotHasPrefixString(str, prefix string, message ...any) error
- func (a *Assertion) NotHasPrefixStringNow(str, prefix string, message ...any) error
- func (a *Assertion) NotHasSuffixString(str, suffix string, message ...any) error
- func (a *Assertion) NotHasSuffixStringNow(str, suffix string, message ...any) error
- func (a *Assertion) NotIsError(err, unexpected error, message ...any) error
- func (a *Assertion) NotIsErrorNow(err, unexpected error, message ...any) error
- func (a *Assertion) NotMapHasKey(m, key any, message ...any) error
- func (a *Assertion) NotMapHasKeyNow(m, key any, message ...any) error
- func (a *Assertion) NotMapHasValue(m, value any, message ...any) error
- func (a *Assertion) NotMapHasValueNow(m, value any, message ...any) error
- func (a *Assertion) NotMatch(val string, pattern *regexp.Regexp, message ...any) error
- func (a *Assertion) NotMatchNow(val string, pattern *regexp.Regexp, message ...any) error
- func (a *Assertion) NotMatchString(val, pattern string, message ...any) error
- func (a *Assertion) NotMatchStringNow(val, pattern string, message ...any) error
- func (a *Assertion) NotNil(val any, message ...any) error
- func (a *Assertion) NotNilNow(val any, message ...any) error
- func (a *Assertion) NotPanic(fn func(), message ...any) error
- func (a *Assertion) NotPanicNow(fn func(), message ...any) error
- func (a *Assertion) NotPanicOf(fn func(), unexpectedErr any, message ...any) error
- func (a *Assertion) NotPanicOfNow(fn func(), unexpectedErr any, message ...any) error
- func (a *Assertion) NotTrue(val any, message ...any) error
- func (a *Assertion) NotTrueNow(val any, message ...any) error
- func (a *Assertion) Panic(fn func(), message ...any) error
- func (a *Assertion) PanicNow(fn func(), message ...any) error
- func (a *Assertion) PanicOf(fn func(), expectErr any, message ...any) error
- func (a *Assertion) PanicOfNow(fn func(), expectErr any, message ...any) error
- func (assertion *Assertion) Run(name string, f func(a *Assertion)) bool
- func (a *Assertion) True(val any, message ...any) error
- func (a *Assertion) TrueNow(val any, message ...any) error
- type AssertionError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotArray indicates that the value must be a slice or an array. ErrNotArray error = errors.New("the value must be a slice or an array") // ErrNotFloat indicates that the value must be a floating number. ErrNotFloat error = errors.New("the value must be a floating number") // ErrNotMap indicates that the value must be a map. ErrNotMap error = errors.New("the value must be a map") // ErrNotOrderable indicates that the value must be orderable. ErrNotOrderable error = errors.New("the value must be orderable") // ErrNotSameType indicates that both values must be the same type. ErrNotSameType error = errors.New("the values must be the same type") // ErrRequireT indicates that the instance of testing.T is a required parameter. ErrRequireT error = errors.New("testing.T is required") )
Functions ¶
func ContainsElement ¶ added in v0.2.0
NotContainsElement tests whether the array or slice contains the specified element or not, and it set the result to fail if the array or slice does not contain the specified element. It'll panic if the `source` is not an array or a slice.
assert.ContainsElement(t, []int{1, 2, 3}, 1) // success assert.ContainsElement(t, []int{1, 2, 3}, 3) // success assert.ContainsElement(t, []int{1, 2, 3}, 4) // fail
func ContainsElementNow ¶ added in v0.2.0
ContainsElementNow tests whether the array or slice contains the specified element or not, and it will terminate the execution if the array or slice does not contain the specified element. It'll panic if the `source` is not an array or a slice.
assert.ContainsElementNow(t, []int{1, 2, 3}, 1) // success assert.ContainsElementNow(t, []int{1, 2, 3}, 3) // success assert.ContainsElementNow(t, []int{1, 2, 3}, 4) // fail and stop the execution // never runs
func ContainsString ¶ added in v0.1.7
ContainsString tests whether the string contains the substring or not, and it set the result to fail if the string does not contains the substring.
assert.ContainsString(t, "Hello world", "") // success assert.ContainsString(t, "Hello world", "Hello") // success assert.ContainsString(t, "Hello world", "world") // success assert.ContainsString(t, "Hello world", "hello") // fail
func ContainsStringNow ¶ added in v0.1.7
ContainsStringNow tests whether the string contains the substring or not, and it will terminate the execution if the string does not contains the substring.
assert.ContainsStringNow(t, "Hello world", "") // success assert.ContainsStringNow(t, "Hello world", "Hello") // success assert.ContainsStringNow(t, "Hello world", "world") // success assert.ContainsStringNow(t, "Hello world", "hello") // fail and stop the execution // never runs
func DeepEqual ¶
DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to fail if they are not deeply equal, and it doesn't stop the execution.
assert.DeepEqual(t, 1, 1) // success assert.DeepEqual(t, "ABC", "ABC") // success assert.DeepEqual(t, 1, 0) // fail assert.DeepEqual(t, 1, int64(1)) // fail
func DeepEqualNow ¶ added in v0.1.2
DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the execution if they are not deeply equal.
assert.DeepEqualNow(t, 1, 1) // success assert.DeepEqualNow(t, "ABC", "ABC") // success assert.DeepEqualNow(t, 1, int64(1)) // fail and terminate // never run
func Equal ¶ added in v0.1.5
Equal tests the equality between actual and expect parameters. It'll set the result to fail if they are not equal, and it doesn't stop the execution.
assert.Equal(t, 1, 1) // success assert.Equal(t, "ABC", "ABC") // success assert.Equal(t, 1, int64(1)) // success assert.Equal(t, 1, uint64(1)) // fail assert.Equal(t, 1, 0) // fail
func EqualNow ¶ added in v0.1.5
EqualNow tests the equality between actual and expect parameters, and it'll stop the execution if they are not equal.
assert.EqualNow(t, 1, 1) // success assert.EqualNow(t, "ABC", "ABC") // success assert.EqualNow(t, 1, int64(1)) // success assert.EqualNow(t, 1, 0) // fail and terminate never run
func FloatEqual ¶ added in v1.1.1
FloatEqual tests the equality between actual and expect floating numbers with epsilon. It'll set the result to fail if they are not equal, and it doesn't stop the execution.
FloatEqual(t, 1.0, 1.0, 0.1) // success FloatEqual(t, 1.0, 1.01, 0.1) // success FloatEqual(t, 1.0, 1.2, 0.1) // fail
func FloatEqualNow ¶ added in v1.1.1
FloatEqualNow tests the equality between actual and expect floating numbers with epsilon, and it'll stop the execution if they are not equal.
FloatEqualNow(t, 1.0, 1.0, 0.1) // success FloatEqualNow(t, 1.0, 1.01, 0.1) // success FloatEqualNow(t, 1.0, 1.2, 0.1) // fail and terminate
func FloatNotEqual ¶ added in v1.1.1
FloatNotEqual tests the inequality between actual and expect floating numbers with epsilon. It'll set the result to fail if they are equal, but it doesn't stop the execution.
FloatNotEqual(t, 1.0, 1.2, 0.1) // success FloatNotEqual(t, 1.0, 1.1, 0.1) // success FloatNotEqual(t, 1.0, 1.0, 0.1) // fail
func FloatNotEqualNow ¶ added in v1.1.1
FloatNotEqualNow tests the inequality between actual and expect floating numbers with epsilon, and it'll stop the execution if they are equal.
FloatNotEqualNow(t, 1.0, 1.2, 0.1) // success FloatNotEqualNow(t, 1.0, 1.1, 0.1) // success FloatNotEqualNow(t, 1.0, 1.0, 0.1) // fail and terminate
func Gt ¶ added in v1.0.0
Gt compares the values and sets the result to false if the first value is not greater than to the second value.
assert.Gt(t, 2, 1) // success assert.Gt(t, 3.14, 1.68) // success assert.Gt(t, "BCD", "ABC") // success assert.Gt(t, 2, 2) // fail assert.Gt(t, 1, 2) // fail
func GtNow ¶ added in v1.0.0
GtNow compares the values and sets the result to false if the first value is not greater than to the second value. It will panic if they do not match the expected result.
assert.GtNow(t, 2, 1) // success assert.GtNow(t, 3.14, 1.68) // success assert.GtNow(t, "BCD", "ABC") // success assert.GtNow(t, 1, 2) // fail and terminate // never runs
func Gte ¶ added in v1.0.0
Gte compares the values and sets the result to false if the first value is not greater than or equal to the second value.
assert.Gte(t, 2, 1) // success assert.Gte(t, 3.14, 1.68) // success assert.Gte(t, "BCD", "ABC") // success assert.Gte(t, 2, 2) // success assert.Gte(t, 1, 2) // fail
func GteNow ¶ added in v1.0.0
GteNow compares the values and sets the result to false if the first value is not greater than or equal to the second value. It will panic if they do not match the expected result.
assert.GteNow(t, 2, 1) // success assert.GteNow(t, 3.14, 1.68) // success assert.GteNow(t, "BCD", "ABC") // success assert.GteNow(t, 2, 2) // success assert.GteNow(t, 1, 2) // fail and terminate // never runs
func HasPrefixString ¶ added in v0.1.7
HasPrefixString tests whether the string has the prefix string or not, and it set the result to fail if the string does not have the prefix string.
assert.HasPrefixString(t, "Hello world", "") // success assert.HasPrefixString(t, "Hello world", "Hello") // success assert.HasPrefixString(t, "Hello world", "world") // fail assert.HasPrefixString(t, "Hello world", "hello") // fail
func HasPrefixStringNow ¶ added in v0.1.7
HasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate the execution if the string does not have the prefix string.
assert.HasPrefixStringNow(t, "Hello world", "") // success assert.HasPrefixStringNow(t, "Hello world", "Hello") // success assert.HasPrefixStringNow(t, "Hello world", "hello") // fail and stop the execution // never runs
func HasSuffixString ¶ added in v0.1.7
HasSuffixString tests whether the string has the suffix string or not, and it set the result to fail if the string does not have the suffix string.
assert.HasSuffixString(t, "Hello world", "") // success assert.HasSuffixString(t, "Hello world", "world") // success assert.HasSuffixString(t, "Hello world", "World") // fail assert.HasSuffixString(t, "Hello world", "hello") // fail
func HasSuffixStringNow ¶ added in v0.1.7
HasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate the execution if the string does not have the suffix string.
assert.HasSuffixStringNow(t, "Hello world", "") // success assert.HasSuffixStringNow(t, "Hello world", "world") // success assert.HasSuffixStringNow(t, "Hello world", "World") // fail and stop the execution // never runs
func IsError ¶ added in v1.1.0
IsError tests whether the error matches the target or not. It'll set the result to fail if the error does not match to the target error, and it doesn't stop the execution.
err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.New("error 3") assert.IsError(t, err1, err1) // success assert.IsError(t, err1, err2) // fail assert.IsError(t, errors.Join(err1, err2), err1) // success assert.IsError(t, errors.Join(err1, err2), err2) // success assert.IsError(t, errors.Join(err1, err2), err3) // fail
func IsErrorNow ¶ added in v1.1.0
IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and stop the execution if the error does not match to the target error.
err1 := errors.New("error 1") err2 := errors.New("error 2") assert.IsErrorNow(t, errors.Join(err1, err2), err1) // success assert.IsErrorNow(t, errors.Join(err1, err2), err2) // success assert.IsErrorNow(t, err1, err1) // success assert.IsErrorNow(t, err1, err2) // fail // never runs
func Lt ¶ added in v1.0.0
Lt compares the values and sets the result to false if the first value is not less than the second value.
assert.Lt(t, 1, 2) // success assert.Lt(t, 1.68, 3.14) // success assert.Lt(t, "ABC", "BCD") // success assert.Lt(t, 2, 2) // fail assert.Lt(t, 2, 1) // fail
func LtNow ¶ added in v1.0.0
LtNow compares the values and sets the result to false if the first value is not less than the second value. It will panic if they do not match the expected result.
assert.LtNow(t, 1, 2) // success assert.LtNow(t, 1.68, 3.14) // success assert.LtNow(t, "ABC", "BCD") // success assert.LtNow(t, 2, 1) // fail and terminate // never runs
func Lte ¶ added in v1.0.0
Lte compares the values and sets the result to false if the first value is not less than or equal to the second value.
assert.Lte(t, 1, 2) // success assert.Lte(t, 1.68, 3.14) // success assert.Lte(t, "ABC", "BCD") // success assert.Lte(t, 2, 2) // success assert.Lte(t, 2, 1) // fail
func LteNow ¶ added in v1.0.0
LteNow compares the values and sets the result to false if the first value is not less than or equal to the second value. It will panic if they do not match the expected result.
assert.LteNow(t, 1, 2) // success assert.LteNow(t, 1.68, 3.14) // success assert.LteNow(t, "ABC", "BCD") // success assert.LteNow(t, 2, 2) // success assert.LteNow(t, 2, 1) // fail and terminate // never runs
func MapHasKey ¶ added in v0.2.1
MapHasKey tests whether the map contains the specified key or not, it will fail if the map does not contain the key, or the type of the key cannot assign to the type of the key of the map.
assert.MapHasKey(t, map[string]int{"a":1}, "a") // success assert.MapHasKey(t, map[string]int{"a":1}, "b") // fail assert.MapHasKey(t, map[string]int{"a":1}, 1) // fail
func MapHasKeyNow ¶ added in v0.2.1
MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the execution if the test fails. It will fail if the map does not contain the key, or the type of the key cannot assign to the type of the key of the map.
assert.MapHasKeyNow(t, map[string]int{"a":1}, "a") // success assert.MapHasKeyNow(t, map[string]int{"a":1}, "b") // fail and terminate // never run
func MapHasValue ¶ added in v0.2.1
MapHasValue tests whether the map contains the specified value or not, it will fail if the map does not contain the value, or the type of the value cannot assign to the type of the values of the map.
assert.MapHasValue(t, map[string]int{"a":1}, 1) // success assert.MapHasValue(t, map[string]int{"a":1}, 2) // fail assert.MapHasValue(t, map[string]int{"a":1}, "a") // fail
func MapHasValueNow ¶ added in v0.2.1
MapHasValueNow tests whether the map contains the specified value or not, and it will terminate the execution if the test fails. It will fail if the map does not contain the value, or the type of the value cannot assign to the type of the value of the map.
assert.MapHasValueNow(t, map[string]int{"a":1}, 1) // success assert.MapHasValueNow(t, map[string]int{"a":1}, 2) // fail and terminate // never run
func Match ¶ added in v0.1.5
Match tests whether the string matches the regular expression or not.
pattern := regexp.MustCompile(`^https?:\/\/`) assert.Match(t, "http://example.com", pattern) // success assert.Match(t, "example.com", pattern) // fail
func MatchNow ¶ added in v0.1.5
MatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if it does not match.
pattern := regexp.MustCompile(`^https?:\/\/`) assert.MatchNow(t, "http://example.com", pattern) // success assert.MatchNow(t, "example.com", pattern) // fail and terminate // never run
func MatchString ¶ added in v0.1.5
MatchString will compile the pattern and test whether the string matches the regular expression or not. It will panic if the pattern is not a valid regular expression.
assert.MatchString(t, "http://example.com", `^https?:\/\/`) // success assert.MatchString(t, "example.com", `^https?:\/\/`) // fail
func MatchStringNow ¶ added in v0.1.5
MatchStringNow will compile the pattern and test whether the string matches the regular expression or not. It will terminate the execution if it does not match, and it will panic if the pattern is not a valid regular expression.
assert.MatchStringNow(t, "http://example.com", `^https?:\/\/`) // success assert.MatchStringNow(t, "example.com", `^https?:\/\/`) // fail and terminate // never run
func Nil ¶ added in v0.1.1
Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.
var err error // nil assert.Nil(t, err) // success err = errors.New("some error") assert.Nil(t, err) // fail
func NilNow ¶ added in v0.1.2
NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.
This function will set the result to fail, and stop the execution if the value is not nil.
var err error // nil assert.NilNow(t, err) // success err = errors.New("some error") assert.NilNow(t, err) // fail and terminate // never run
func NotContainsElement ¶ added in v0.2.0
NotContainsElement tests whether the array or slice contains the specified element or not, and it set the result to fail if the array or slice contains the specified element. It'll panic if the `source` is not an array or a slice.
assert.NotContainsElement(t, []int{1, 2, 3}, 4) // success assert.NotContainsElement(t, []int{1, 2, 3}, 0) // success assert.NotContainsElement(t, []int{1, 2, 3}, 1) // fail
func NotContainsElementNow ¶ added in v0.2.0
NotContainsElementNow tests whether the array or slice contains the specified element or not, and it will terminate the execution if the array or slice contains the specified element. It'll panic if the `source` is not an array or a slice.
assert.NotContainsElementNow(t, []int{1, 2, 3}, 4) // success assert.NotContainsElementNow(t, []int{1, 2, 3}, 0) // success assert.NotContainsElementNow(t, []int{1, 2, 3}, 1) // fail and stop the execution // never runs
func NotContainsString ¶ added in v0.1.7
NotContainsString tests whether the string contains the substring or not, and it set the result to fail if the string contains the substring.
assert.NotContainsString(t, "Hello world", "") // fail assert.NotContainsString(t, "Hello world", "Hello") // fail assert.NotContainsString(t, "Hello world", "world") // fail assert.NotContainsString(t, "Hello world", "hello") // success
func NotContainsStringNow ¶ added in v0.1.7
NotContainsStringNow tests whether the string contains the substring or not, and it will terminate the execution if the string contains the substring.
assert.NotContainsStringNow(t, "Hello world", "hello") // success assert.NotContainsStringNow(t, "Hello world", "Hello") // fail and stop the execution // never runs
func NotDeepEqual ¶
NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the result to fail if they are deeply equal, but it doesn't stop the execution.
assert.NotDeepEqual(t, 1, 0) // success assert.NotDeepEqual(t, 1, int64(1)) // success assert.NotDeepEqual(t, 1, 1) // fail assert.NotDeepEqual(t, "ABC", "ABC") // fail
func NotDeepEqualNow ¶ added in v0.1.2
NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop the execution if they are deeply equal.
assert.NotDeepEqual(t, 1, 0) // success assert.NotDeepEqual(t, 1, int64(1)) // success assert.NotDeepEqual(t, "ABC", "ABC") // fail and terminate // never run
func NotEqual ¶ added in v0.1.5
NotEqual tests the inequality between actual and expected parameters. It'll set the result to fail if they are equal, but it doesn't stop the execution.
assert.NotEqual(t, 1, 0) // success assert.NotEqual(t, "ABC", "CBA") // success assert.NotEqual(t, 1, uint64(1)) // success assert.NotEqual(t, 1, 1) // fail assert.NotEqual(t, "ABC", "ABC") // fail assert.NotEqual(t, 1, int64(1)) // fail
func NotEqualNow ¶ added in v0.1.5
NotEqualNow tests the inequality between actual and expected parameters, and it'll stop the execution if they are equal.
assert.NotEqualNow(t, 1, 0) // success assert.NotEqualNow(t, "ABC", "CBA") // success assert.NotEqualNow(t, 1, 1) // fail and terminate // never run
func NotHasPrefixString ¶ added in v0.1.7
NotHasPrefixString tests whether the string has the prefix string or not, and it set the result to fail if the string have the prefix string.
assert.NotHasPrefixString(t, "Hello world", "hello") // success assert.NotHasPrefixString(t, "Hello world", "world") // success assert.NotHasPrefixString(t, "Hello world", "") // fail assert.NotHasPrefixString(t, "Hello world", "Hello") // fail
func NotHasPrefixStringNow ¶ added in v0.1.7
NotHasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate the execution if the string have the prefix string.
assert.NotHasPrefixStringNow(t, "Hello world", "hello") // success assert.NotHasPrefixStringNow(t, "Hello world", "world") // success assert.NotHasPrefixStringNow(t, "Hello world", "Hello") // fail and stop the execution // never runs
func NotHasSuffixString ¶ added in v0.1.7
NotHasSuffixString tests whether the string has the suffix string or not, and it set the result to fail if the string have the suffix string.
assert.NotHasSuffixString(t, "Hello world", "Hello") // success assert.NotHasSuffixString(t, "Hello world", "World") // success assert.NotHasSuffixString(t, "Hello world", "") // fail assert.NotHasSuffixString(t, "Hello world", "world") // fail
func NotHasSuffixStringNow ¶ added in v0.1.7
NotHasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate the execution if the string have the suffix string.
assert.NotHasSuffixStringNow(t, "Hello world", "hello") // success assert.NotHasSuffixStringNow(t, "Hello world", "World") // success assert.NotHasSuffixStringNow(t, "Hello world", "world") // fail and stop the execution // never runs
func NotIsError ¶ added in v1.1.0
NotIsError tests whether the error matches the target or not. It'll set the result to fail if the error matches to the target error, and it doesn't stop the execution.
err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.New("error 3") assert.NotIsError(t, err1, err2) // success assert.NotIsError(t, err1, err1) // fail assert.NotIsError(t, errors.Join(err1, err2), err3) // success assert.NotIsError(t, errors.Join(err1, err2), err1) // fail assert.NotIsError(t, errors.Join(err1, err2), err2) // fail
func NotIsErrorNow ¶ added in v1.1.0
NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail and stop the execution if the error matches to the target error.
err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.new("error 3") assert.NotIsErrorNow(t, errors.Join(err1, err2), err3) // success assert.NotIsErrorNow(t, err1, err2) // fail assert.NotIsErrorNow(t, err1, err1) // fail and terminate // never runs
func NotMapHasKey ¶ added in v0.2.1
NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map contain the key. It will also set the test result to success if the type of the key cannot assign to the type of the key of the map.
assert.NotMapHasKey(t, map[string]int{"a":1}, "b") // success assert.NotMapHasKey(t, map[string]int{"a":1}, 1) // success assert.NotMapHasKey(t, map[string]int{"a":1}, "a") // fail
func NotMapHasKeyNow ¶ added in v0.2.1
NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map contain the key, and it will terminate the execution if the test fails. It will also set the test result to success if the type of the key cannot assign to the type of the key of the map.
assert.NotMapHasKeyNow(t, map[string]int{"a":1}, "b") // success assert.NotMapHasKeyNow(t, map[string]int{"a":1}, 1) // success assert.NotMapHasKeyNow(t, map[string]int{"a":1}, "a") // fail and terminate // never run
func NotMapHasValue ¶ added in v0.2.1
NotMapHasValue tests whether the map contains the specified value or not, it will fail if the map contain the value. It will also set the test result to success if the type of the value cannot assign to the type of the value of the map.
assert.NotMapHasValue(t, map[string]int{"a":1}, 2) // success assert.NotMapHasValue(t, map[string]int{"a":1}, "a") // success assert.NotMapHasValue(t, map[string]int{"a":1}, 1) // fail
func NotMapHasValueNow ¶ added in v0.2.1
NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the map contain the value, and it will terminate the execution if the test fails. It will also set the test result to success if the type of the value cannot assign to the type of the value of the map.
assert.NotMapHasValueNow(t, map[string]int{"a":1}, 2) // success assert.NotMapHasValueNow(t, map[string]int{"a":1}, "a") // success assert.NotMapHasValueNow(t, map[string]int{"a":1}, 1) // fail and terminate // never run
func NotMatch ¶ added in v0.1.5
NotMatch tests whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern.
pattern := regexp.MustCompile(`^https?:\/\/`) assert.NotMatch(t, "example.com", pattern) // success assert.NotMatch(t, "http://example.com", pattern) // fail
func NotMatchNow ¶ added in v0.1.5
NotMatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if the string matches the pattern.
pattern := regexp.MustCompile(`^https?:\/\/`) assert.NotMatchNow(t, "example.com", pattern) // success assert.NotMatchNow(t, "http://example.com", pattern) // fail and terminate // never run
func NotMatchString ¶ added in v0.1.5
MatchString will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will also panic if the pattern is not a valid regular expression.
assert.NotMatchString(t, "example.com", `^https?:\/\/`) // success assert.NotMatchString(t, "http://example.com", `^https?:\/\/`) // fail
func NotMatchStringNow ¶ added in v0.1.5
NotMatchStringNow will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will terminate the execution if the string matches the pattern, and it will panic if the pattern is not a valid regular expression.
assert.NotMatchStringNow(t, "example.com", `^https?:\/\/`) // success assert.NotMatchStringNow(t, "http://example.com", `^https?:\/\/`) // fail and terminate // never run
func NotNil ¶ added in v0.1.1
NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.
var err error // nil assert.NotNil(t, err) // fail err = errors.New("some error") assert.NotNil(t, err) // success
func NotNilNow ¶ added in v0.1.2
NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.
This function will set the result to fail, and stop the execution if the value is nil.
var err error = errors.New("some error") assert.NotNilNow(t, err) // success err = nil assert.NotNilNow(t, err) // fail and terminate // never run
func NotPanic ¶
NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the function panic.
assert.NotPanic(t, func() { // no panic }) // success assert.NotPanic(t, func() { panic("some error") }) // fail
func NotPanicNow ¶ added in v0.1.2
NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the function panic, and it also stops the execution.
assert.NotPanicNow(t, func() { // no panic }) // success assert.NotPanicNow(t, func() { panic("some error") }) // fail and terminate // never run
func NotPanicOf ¶ added in v1.0.2
NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected error. If the function panics by the unexpected error, it will set the result to fail.
assert.NotPanicOf(t, func() { panic("other error") }, "unexpected error") // success assert.NotPanicOf(t, func() { // ..., no panic }, "unexpected error") // success assert.NotPanicOf(t, func() { panic("unexpected error") }, "unexpected error") // fail
func NotPanicOfNow ¶ added in v1.0.2
NotPanicOfNow expects the function fn not panic, or the function does not panic by the unexpected error. If the function panics by the unexpected error, it will set the result to fail and stop the execution.
assert.NotPanicOfNow(t, func() { panic("other error") }, "unexpected error") // success assert.NotPanicOfNow(t, func() { // ..., no panic }, "unexpected error") // success assert.NotPanicOfNow(t, func() { panic("unexpected error") }, "unexpected error") // fail and terminate // never runs
func NotTrue ¶ added in v0.1.4
NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
assert.NotTrue(t, 0) // success assert.NotTrue(t, "") // success assert.NotTrue(t, 1) // fail assert.NotTrue(t, "test") // fail
func NotTrueNow ¶ added in v0.1.4
NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
The function will stop the execution if the value is truthy.
assert.NotTrueNow(t, 0) // success assert.NotTrueNow(t, "") // success assert.NotTrueNow(t, "test") // fail and terminate // never run
func Panic ¶
Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't panic.
assert.Panic(t, func() { panic("some error") }) // success assert.Panic(t, func() { // no panic }) // fail
func PanicNow ¶ added in v0.1.2
PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't panic, and stop the execution.
assert.PanicNow(t, func() { panic("some error") }) // success assert.PanicNow(t, func() { // no panic }) // fail // never run
func PanicOf ¶ added in v1.0.2
PanicOf expects the function fn to panic by the expected error. If the function does not panic or panic for another reason, it will set the result to fail.
assert.PanicOf(t, func() { panic("expected error") }, "expected error") // success assert.PanicOf(t, func() { panic("unexpected error") }, "expected error") // fail assert.PanicOf(t, func() { // ..., no panic }, "expected error") // fail
func PanicOfNow ¶ added in v1.0.2
PanicOfNow expects the function fn to panic by the expected error. If the function does not panic or panic for another reason, it will set the result to fail and terminate the execution.
assert.PanicOfNow(t, func() { panic("expected error") }, "expected error") // success assert.PanicOfNow(t, func() { panic("unexpected error") }, "expected error") // fail and terminated // never runs
func True ¶ added in v0.1.4
True tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
assert.True(t, 1) // success assert.True(t, "test") // success assert.True(t, 0) // fail assert.True(t, "") // fail
func TrueNow ¶ added in v0.1.4
TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
The function will stop the execution if the value is falsy.
assert.TrueNow(t, 1) // success assert.TrueNow(t, "test") // success assert.TrueNow(t, "") // fail and terminate // never run
Types ¶
type Assertion ¶
Assertion is the extension of the Go builtin `testing.T`.
Please do not create an Assertion instance without the New function, every assertion function will panic if no inner testing.T set.
func New ¶
New returns an assertion instance for verifying invariants.
assertion := assert.New(t) assertion.Equal(actual, expect) // ...
func (*Assertion) ContainsElement ¶ added in v0.2.0
NotContainsElement tests whether the array or slice contains the specified element or not, and it set the result to fail if the array or slice does not contain the specified element. It'll panic if the `source` is not an array or a slice.
a := assert.New(t) a.ContainsElement([]int{1, 2, 3}, 1) // success a.ContainsElement([]int{1, 2, 3}, 3) // success a.ContainsElement([]int{1, 2, 3}, 4) // fail
func (*Assertion) ContainsElementNow ¶ added in v0.2.0
ContainsElementNow tests whether the array or slice contains the specified element or not, and it will terminate the execution if the array or slice does not contain the specified element. It'll panic if the `source` is not an array or a slice.
a := assert.New(t) a.ContainsElementNow([]int{1, 2, 3}, 1) // success a.ContainsElementNow([]int{1, 2, 3}, 3) // success a.ContainsElementNow([]int{1, 2, 3}, 4) // fail and stop the execution // never runs
func (*Assertion) ContainsString ¶ added in v0.1.7
ContainsString tests whether the string contains the substring or not, and it set the result to fail if the string does not contains the substring.
a := assert.New(t) a.ContainsString("Hello world", "") // success a.ContainsString("Hello world", "Hello") // success a.ContainsString("Hello world", "world") // success a.ContainsString("Hello world", "hello") // fail
func (*Assertion) ContainsStringNow ¶ added in v0.1.7
ContainsStringNow tests whether the string contains the substring or not, and it will terminate the execution if the string does not contains the substring.
a := assert.New(t) a.ContainsStringNow("Hello world", "") // success a.ContainsStringNow("Hello world", "Hello") // success a.ContainsStringNow("Hello world", "world") // success a.ContainsStringNow("Hello world", "hello") // fail and stop the execution // never runs
func (*Assertion) DeepEqual ¶
DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to fail if they are not deeply equal, and it doesn't stop the execution.
a := assert.New(t) a.DeepEqual(1, 1) // success a.DeepEqual("ABC", "ABC") // success a.DeepEqual(1, 0) // fail a.DeepEqual(1, int64(1)) // fail
func (*Assertion) DeepEqualNow ¶ added in v0.1.2
DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the execution if they are not deeply equal.
a := assert.New(t) a.DeepEqualNow(1, 1) // success a.DeepEqualNow("ABC", "ABC") // success a.DeepEqualNow(1, int64(1)) // fail and terminate // never run
func (*Assertion) Equal ¶ added in v0.1.5
Equal tests the equality between actual and expect parameters. It'll set the result to fail if they are not equal, and it doesn't stop the execution.
a := assert.New(t) a.Equal(1, 1) // success a.Equal("ABC", "ABC") // success a.Equal(1, int64(1)) // success a.Equal(1, uint64(1)) // fail a.Equal(1, 0) // fail
func (*Assertion) EqualNow ¶ added in v0.1.5
EqualNow tests the equality between actual and expect parameters, and it'll stop the execution if they are not equal.
a := assert.New(t) a.EqualNow(1, 1) // success a.EqualNow("ABC", "ABC") // success a.EqualNow(1, int64(1)) // success a.EqualNow(1, 0) // fail and terminate never run
func (*Assertion) FloatEqual ¶ added in v1.1.1
FloatEqual tests the equality between actual and expect floating numbers with epsilon. It'll set the result to fail if they are not equal, and it doesn't stop the execution.
a := assert.New(t) a.FloatEqual(1.0, 1.0, 0.1) // success a.FloatEqual(1.0, 1.01, 0.1) // success a.FloatEqual(1.0, 1.2, 0.1) // fail
func (*Assertion) FloatEqualNow ¶ added in v1.1.1
FloatEqualNow tests the equality between actual and expect floating numbers with epsilon, and it'll stop the execution if they are not equal.
a := assert.New(t) a.FloatEqualNow(1.0, 1.0, 0.1) // success a.FloatEqualNow(1.0, 1.01, 0.1) // success a.FloatEqualNow(1.0, 1.2, 0.1) // fail and terminate
func (*Assertion) FloatNotEqual ¶ added in v1.1.1
FloatNotEqual tests the inequality between actual and expect floating numbers with epsilon. It'll set the result to fail if they are equal, but it doesn't stop the execution.
a := assert.New(t) a.FloatNotEqual(1.0, 1.2, 0.1) // success a.FloatNotEqual(1.0, 1.1, 0.1) // success a.FloatNotEqual(1.0, 1.0, 0.1) // fail
func (*Assertion) FloatNotEqualNow ¶ added in v1.1.1
FloatNotEqualNow tests the inequality between actual and expect floating numbers with epsilon, and it'll stop the execution if they are equal.
a := assert.New(t) a.FloatNotEqualNow(1.0, 1.2, 0.1) // success a.FloatNotEqualNow(1.0, 1.1, 0.1) // success a.FloatNotEqualNow(1.0, 1.0, 0.1) // fail and terminate
func (*Assertion) Gt ¶ added in v1.0.0
Gt compares the values and sets the result to false if the first value is not greater than to the second value.
a := assert.New(t) a.Gt(2, 1) // success a.Gt(3.14, 1.68) // success a.Gt("BCD", "ABC") // success a.Gt(2, 2) // fail a.Gt(1, 2) // fail
func (*Assertion) GtNow ¶ added in v1.0.0
GtNow compares the values and sets the result to false if the first value is not greater than to the second value. It will panic if they do not match the expected result.
a := assert.New(t) a.GtNow(2, 1) // success a.GtNow(3.14, 1.68) // success a.GtNow("BCD", "ABC") // success a.GtNow(1, 2) // fail and terminate // never runs
func (*Assertion) Gte ¶ added in v1.0.0
Gte compares the values and sets the result to false if the first value is not greater than or equal to the second value.
a := assert.New(t) a.Gte(2, 1) // success a.Gte(3.14, 1.68) // success a.Gte("BCD", "ABC") // success a.Gte(2, 2) // success a.Gte(1, 2) // fail
func (*Assertion) GteNow ¶ added in v1.0.0
GteNow compares the values and sets the result to false if the first value is not greater than or equal to the second value. It will panic if they do not match the expected result.
a := assert.New(t) a.GteNow(2, 1) // success a.GteNow(3.14, 1.68) // success a.GteNow("BCD", "ABC") // success a.GteNow(2, 2) // success a.GteNow(1, 2) // fail and terminate // never runs
func (*Assertion) HasPrefixString ¶ added in v0.1.7
HasPrefixString tests whether the string has the prefix string or not, and it set the result to fail if the string does not have the prefix string.
a := assert.New(t) a.HasPrefixString("Hello world", "") // success a.HasPrefixString("Hello world", "Hello") // success a.HasPrefixString("Hello world", "world") // fail a.HasPrefixString("Hello world", "hello") // fail
func (*Assertion) HasPrefixStringNow ¶ added in v0.1.7
HasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate the execution if the string does not have the prefix string.
a := assert.New(t) a.HasPrefixStringNow("Hello world", "") // success a.HasPrefixStringNow("Hello world", "Hello") // success a.HasPrefixStringNow("Hello world", "hello") // fail and stop the execution // never runs
func (*Assertion) HasSuffixString ¶ added in v0.1.7
HasSuffixString tests whether the string has the suffix string or not, and it set the result to fail if the string does not have the suffix string.
a := assert.New(t) a.HasSuffixString("Hello world", "") // success a.HasSuffixString("Hello world", "world") // success a.HasSuffixString("Hello world", "World") // fail a.HasSuffixString("Hello world", "hello") // fail
func (*Assertion) HasSuffixStringNow ¶ added in v0.1.7
HasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate the execution if the string does not have the suffix string.
a := assert.New(t) a.HasSuffixStringNow("Hello world", "") // success a.HasSuffixStringNow("Hello world", "world") // success a.HasSuffixStringNow("Hello world", "World") // fail and stop the execution // never runs
func (*Assertion) IsError ¶ added in v1.1.0
IsError tests whether the error matches the target or not. It'll set the result to fail if the error does not match to the target error, and it doesn't stop the execution.
err1 := errors.New("error 1") err2 := errors.New("error 2") a := assert.New(t) a.IsError(err1, err1) // success a.IsError(err1, err2) // fail a.IsError(errors.Join(err1, err2), err1) // success a.IsError(errors.Join(err1, err2), err2) // success
func (*Assertion) IsErrorNow ¶ added in v1.1.0
IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and stop the execution if the error does not match to the target error.
err1 := errors.New("error 1") err2 := errors.New("error 2") a := assert.New(t) a.IsErrorNow(errors.Join(err1, err2), err1) // success a.IsErrorNow(errors.Join(err1, err2), err2) // success a.IsErrorNow(err1, err1) // success a.IsErrorNow(err1, err2) // fail // never runs
func (*Assertion) Lt ¶ added in v1.0.0
Lt compares the values and sets the result to false if the first value is not less than the second value.
a := assert.New(t) a.Lt(1, 2) // success a.Lt(1.68, 3.14) // success a.Lt("ABC", "BCD") // success a.Lt(2, 2) // fail a.Lt(2, 1) // fail
func (*Assertion) LtNow ¶ added in v1.0.0
LtNow compares the values and sets the result to false if the first value is not less than the second value. It will panic if they do not match the expected result.
a := assert.New(t) a.LtNow(1, 2) // success a.LtNow(1.68, 3.14) // success a.LtNow("ABC", "BCD") // success a.LtNow(2, 1) // fail and terminate // never runs
func (*Assertion) Lte ¶ added in v1.0.0
Lte compares the values and sets the result to false if the first value is not less than or equal to the second value.
a := assert.New(t) a.Lte(1, 2) // success a.Lte(1.68, 3.14) // success a.Lte("ABC", "BCD") // success a.Lte(2, 2) // success a.Lte(2, 1) // fail
func (*Assertion) LteNow ¶ added in v1.0.0
LteNow compares the values and sets the result to false if the first value is not less than or equal to the second value. It will panic if they do not match the expected result.
a := assert.New(t) a.LteNow(1, 2) // success a.LteNow(1.68, 3.14) // success a.LteNow("ABC", "BCD") // success a.LteNow(2, 2) // success a.LteNow(2, 1) // fail and terminate // never runs
func (*Assertion) MapHasKey ¶ added in v0.2.1
MapHasKey tests whether the map contains the specified key or not, it will fail if the map does not contain the key, or the type of the key cannot assign to the type of the key of the map.
a := assert.New(t) a.MapHasKey(map[string]int{"a":1}, "a") // success a.MapHasKey(map[string]int{"a":1}, "b") // fail a.MapHasKey(map[string]int{"a":1}, 1) // fail
func (*Assertion) MapHasKeyNow ¶ added in v0.2.1
MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the execution if the test fails. It will fail if the map does not contain the key, or the type of the key cannot assign to the type of the key of the map.
a := assert.New(t) a.MapHasKeyNow(map[string]int{"a":1}, "a") // success a.MapHasKeyNow(map[string]int{"a":1}, "b") // fail and terminate // never run
func (*Assertion) MapHasValue ¶ added in v0.2.1
MapHasValue tests whether the map contains the specified value or not, it will fail if the map does not contain the value, or the type of the value cannot assign to the type of the values of the map.
a := assert.New(t) a.MapHasValue(map[string]int{"a":1}, 1) // success a.MapHasValue(map[string]int{"a":1}, 2) // fail a.MapHasValue(map[string]int{"a":1}, "a") // fail
func (*Assertion) MapHasValueNow ¶ added in v0.2.1
MapHasValueNow tests whether the map contains the specified value or not, and it will terminate the execution if the test fails. It will fail if the map does not contain the value, or the type of the value cannot assign to the type of the value of the map.
a := assert.New(t) a.MapHasValueNow(map[string]int{"a":1}, 1) // success a.MapHasValueNow(map[string]int{"a":1}, 2) // fail and terminate // never run
func (*Assertion) Match ¶ added in v0.1.5
Match tests whether the string matches the regular expression or not.
a := assert.New(t) pattern := regexp.MustCompile(`^https?:\/\/`) a.Match("http://example.com", pattern) // success a.Match("example.com", pattern) // fail
func (*Assertion) MatchNow ¶ added in v0.1.5
MatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if it does not match.
a := assert.New(t) pattern := regexp.MustCompile(`^https?:\/\/`) a.MatchNow("http://example.com", pattern) // success a.MatchNow("example.com", pattern) // fail and terminate // never run
func (*Assertion) MatchString ¶ added in v0.1.5
MatchString will compile the pattern and test whether the string matches the regular expression or not. It will panic if the pattern is not a valid regular expression.
a := assert.New(t) a.MatchString("http://example.com", `^https?:\/\/`) // success a.MatchString("example.com", `^https?:\/\/`) // fail
func (*Assertion) MatchStringNow ¶ added in v0.1.5
MatchStringNow will compile the pattern and test whether the string matches the regular expression or not. It will terminate the execution if it does not match, and it will panic if the pattern is not a valid regular expression.
a := assert.New(t) a.MatchStringNow("http://example.com", `^https?:\/\/`) // success a.MatchStringNow("example.com", `^https?:\/\/`) // fail and terminate // never run
func (*Assertion) Nil ¶ added in v0.1.1
Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.
a := assert.New(t) var err error // nil a.Nil(err) // success err = errors.New("some error") a.Nil(err) // fail
func (*Assertion) NilNow ¶ added in v0.1.2
NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.
This function will set the result to fail, and stop the execution if the value is not nil.
a := assert.New(t) var err error // nil a.NilNow(err) // success err = errors.New("some error") a.NilNow(err) // fail and terminate // never run
func (*Assertion) NotContainsElement ¶ added in v0.2.0
NotContainsElement tests whether the array or slice contains the specified element or not, and it set the result to fail if the array or slice contains the specified element. It'll panic if the `source` is not an array or a slice.
a := assert.New(t) a.NotContainsElement([]int{1, 2, 3}, 4) // success a.NotContainsElement([]int{1, 2, 3}, 0) // success a.NotContainsElement([]int{1, 2, 3}, 1) // fail
func (*Assertion) NotContainsElementNow ¶ added in v0.2.0
NotContainsElementNow tests whether the array or slice contains the specified element or not, and it will terminate the execution if the array or slice contains the specified element. It'll panic if the `source` is not an array or a slice.
a := assert.New(t) a.NotContainsElementNow([]int{1, 2, 3}, 4) // success a.NotContainsElementNow([]int{1, 2, 3}, 0) // success a.NotContainsElementNow([]int{1, 2, 3}, 1) // fail and stop the execution // never runs
func (*Assertion) NotContainsString ¶ added in v0.1.7
NotContainsString tests whether the string contains the substring or not, and it set the result to fail if the string contains the substring.
a := assert.New(t) a.NotContainsString("Hello world", "hello") // success a.NotContainsString("Hello world", "") // fail a.NotContainsString("Hello world", "Hello") // fail a.NotContainsString("Hello world", "world") // fail
func (*Assertion) NotContainsStringNow ¶ added in v0.1.7
NotContainsStringNow tests whether the string contains the substring or not, and it will terminate the execution if the string contains the substring.
a := assert.New(t) a.NotContainsStringNow("Hello world", "hello") // success a.NotContainsStringNow("Hello world", "Hello") // fail and stop the execution // never runs
func (*Assertion) NotDeepEqual ¶
NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the result to fail if they are deeply equal, but it doesn't stop the execution.
a := assert.New(t) a.NotDeepEqual(1, 0) // success a.NotDeepEqual(1, int64(1)) // success a.NotDeepEqual(1, 1) // fail a.NotDeepEqual("ABC", "ABC") // fail
func (*Assertion) NotDeepEqualNow ¶ added in v0.1.2
NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop the execution if they are deeply equal.
a := assert.New(t) a.NotDeepEqual1, 0) // success a.NotDeepEqual1, int64(1)) // success a.NotDeepEqual"ABC", "ABC") // fail and terminate // never run
func (*Assertion) NotEqual ¶ added in v0.1.5
NotEqual tests the inequality between actual and expected parameters. It'll set the result to fail if they are equal, but it doesn't stop the execution.
a := assert.New(t) a.NotEqual(1, 0) // success a.NotEqual("ABC", "CBA") // success a.NotEqual(1, uint64(1)) // success a.NotEqual(1, 1) // fail a.NotEqual("ABC", "ABC") // fail a.NotEqual(1, int64(1)) // fail
func (*Assertion) NotEqualNow ¶ added in v0.1.5
NotEqualNow tests the inequality between actual and expected parameters, and it'll stop the execution if they are equal.
a := assert.New(t) a.NotEqualNow(1, 0) // success a.NotEqualNow("ABC", "CBA") // success a.NotEqualNow(1, 1) // fail and terminate // never run
func (*Assertion) NotHasPrefixString ¶ added in v0.1.7
NotHasPrefixString tests whether the string has the prefix string or not, and it set the result to fail if the string have the prefix string.
a := assert.New(t) a.NotHasPrefixString("Hello world", "hello") // success a.NotHasPrefixString("Hello world", "world") // success a.NotHasPrefixString("Hello world", "") // fail a.NotHasPrefixString("Hello world", "Hello") // fail
func (*Assertion) NotHasPrefixStringNow ¶ added in v0.1.7
NotHasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate the execution if the string have the prefix string.
a := assert.New(t) a.NotHasPrefixStringNow("Hello world", "hello") // success a.NotHasPrefixStringNow("Hello world", "world") // success a.NotHasPrefixStringNow("Hello world", "Hello") // fail and stop the execution // never runs
func (*Assertion) NotHasSuffixString ¶ added in v0.1.7
NotHasSuffixString tests whether the string has the suffix string or not, and it set the result to fail if the string have the suffix string.
a := assert.New(t) a.NotHasSuffixString("Hello world", "Hello") // success a.NotHasSuffixString("Hello world", "World") // success a.NotHasSuffixString("Hello world", "") // fail a.NotHasSuffixString("Hello world", "world") // fail
func (*Assertion) NotHasSuffixStringNow ¶ added in v0.1.7
NotHasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate the execution if the string have the suffix string.
a := assert.New(t) a.NotHasSuffixStringNow("Hello world", "hello") // success a.NotHasSuffixStringNow("Hello world", "World") // success a.NotHasSuffixStringNow("Hello world", "world") // fail and stop the execution // never runs
func (*Assertion) NotIsError ¶ added in v1.1.0
NotIsError tests whether the error matches the target or not. It'll set the result to fail if the error matches to the target error, and it doesn't stop the execution.
a := assert.New(t) err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.New("error 3") a.NotIsError(err1, err2) // success a.NotIsError(err1, err1) // fail a.NotIsError(errors.Join(err1, err2), err3) // success a.NotIsError(errors.Join(err1, err2), err1) // fail a.NotIsError(errors.Join(err1, err2), err2) // fail
func (*Assertion) NotIsErrorNow ¶ added in v1.1.0
NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail and stop the execution if the error matches to the target error.
a := assert.New(t) err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.new("error 3") a.NotIsErrorNow(errors.Join(err1, err2), err3) // success a.NotIsErrorNow(err1, err2) // fail a.NotIsErrorNow(err1, err1) // fail and terminate // never runs
func (*Assertion) NotMapHasKey ¶ added in v0.2.1
NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map contain the key. It will also set the test result to success if the type of the key cannot assign to the type of the key of the map.
a := assert.New(t) a.NotMapHasKey(map[string]int{"a":1}, "b") // success a.NotMapHasKey(map[string]int{"a":1}, 1) // success a.NotMapHasKey(map[string]int{"a":1}, "a") // fail
func (*Assertion) NotMapHasKeyNow ¶ added in v0.2.1
NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map contain the key, and it will terminate the execution if the test fails. It will also set the test result to success if the type of the key cannot assign to the type of the key of the map.
a := assert.New(t) a.NotMapHasKeyNow(map[string]int{"a":1}, "b") // success a.NotMapHasKeyNow(map[string]int{"a":1}, 1) // success a.NotMapHasKeyNow(map[string]int{"a":1}, "a") // fail and terminate // never run
func (*Assertion) NotMapHasValue ¶ added in v0.2.1
NotMapHasValue tests whether the map contains the specified value or not, it will fail if the map contain the value. It will also set the test result to success if the type of the value cannot assign to the type of the value of the map.
a := assert.New(t) a.NotMapHasValue(map[string]int{"a":1}, 2) // success a.NotMapHasValue(map[string]int{"a":1}, "a") // success a.NotMapHasValue(map[string]int{"a":1}, 1) // fail
func (*Assertion) NotMapHasValueNow ¶ added in v0.2.1
NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the map contain the value, and it will terminate the execution if the test fails. It will also set the test result to success if the type of the value cannot assign to the type of the value of the map.
a := assert.New(t) a.NotMapHasValueNow(map[string]int{"a":1}, 2) // success a.NotMapHasValueNow(map[string]int{"a":1}, "a") // success a.NotMapHasValueNow(map[string]int{"a":1}, 1) // fail and terminate // never run
func (*Assertion) NotMatch ¶ added in v0.1.5
NotMatch tests whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern.
a := assert.New(t) pattern := regexp.MustCompile(`^https?:\/\/`) a.NotMatch("example.com", pattern) // success a.NotMatch("http://example.com", pattern) // fail
func (*Assertion) NotMatchNow ¶ added in v0.1.5
NotMatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if the string matches the pattern.
a := assert.New(t) pattern := regexp.MustCompile(`^https?:\/\/`) a.NotMatchNow("example.com", pattern) // success a.NotMatchNow("http://example.com", pattern) // fail and terminate // never run
func (*Assertion) NotMatchString ¶ added in v0.1.5
MatchString will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will also panic if the pattern is not a valid regular expression.
a := assert.New(t) a.NotMatchString("example.com", `^https?:\/\/`) // success a.NotMatchString("http://example.com", `^https?:\/\/`) // fail
func (*Assertion) NotMatchStringNow ¶ added in v0.1.5
NotMatchStringNow will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will terminate the execution if the string matches the pattern, and it will panic if the pattern is not a valid regular expression.
a := assert.New(t) a.NotMatchStringNow("example.com", `^https?:\/\/`) // success a.NotMatchStringNow("http://example.com", `^https?:\/\/`) // fail and terminate // never run
func (*Assertion) NotNil ¶ added in v0.1.1
NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.
a := assert.New(t) var err error // nil a.NotNil(err) // fail err = errors.New("some error") a.NotNil(err) // success
func (*Assertion) NotNilNow ¶ added in v0.1.2
NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.
This function will set the result to fail, and stop the execution if the value is nil.
a := assert.New(t) var err error = errors.New("some error") a.NotNilNow(err) // success err = nil a.NotNilNow(err) // fail and terminate // never run
func (*Assertion) NotPanic ¶
NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the function panic.
a := assert.New(t) a.NotPanic(func() { // no panic }) // success a.NotPanic(func() { panic("some error") }) // fail
func (*Assertion) NotPanicNow ¶ added in v0.1.2
NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the function panic, and it also stops the execution.
a := assert.New(t) a.NotPanicNow(func() { // no panic }) // success a.NotPanicNow(func() { panic("some error") }) // fail and terminate // never run
func (*Assertion) NotPanicOf ¶ added in v1.0.2
NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected error. If the function panics by the unexpected error, it will set the result to fail.
a := assert.New(t) a.NotPanicOf(func() { panic("other error") }, "unexpected error") // success a.NotPanicOf(func() { // ..., no panic }, "unexpected error") // success a.NotPanicOf(func() { panic("unexpected error") }, "unexpected error") // fail
func (*Assertion) NotPanicOfNow ¶ added in v1.0.2
NotPanicOfNow expects the function fn not panic, or the function does not panic by the unexpected error. If the function panics by the unexpected error, it will set the result to fail and stop the execution.
a := assert.New(t) a.NotPanicOfNow(func() { panic("other error") }, "unexpected error") // success a.NotPanicOfNow(func() { // ..., no panic }, "unexpected error") // success a.NotPanicOfNow(func() { panic("unexpected error") }, "unexpected error") // fail and terminate // never runs
func (*Assertion) NotTrue ¶ added in v0.1.4
NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
a := assert.New(t) a.NotTrue(0) // success a.NotTrue("") // success a.NotTrue(1) // fail a.NotTrue("test") // fail
func (*Assertion) NotTrueNow ¶ added in v0.1.4
NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
The function will stop the execution if the value is truthy.
a := assert.New(t) a.NotTrueNow(0) // success a.NotTrueNow("") // success a.NotTrueNow("test") // fail and terminate // never run
func (*Assertion) Panic ¶
Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't panic.
a := assert.New(t) a.Panic(func() { panic("some error") }) // success a.Panic(func() { // no panic }) // fail
func (*Assertion) PanicNow ¶ added in v0.1.2
PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't panic, and stop the execution.
a := assert.New(t) a.PanicNow(func() { panic("some error") }) // success a.PanicNow(func() { // no panic }) // fail // never run
func (*Assertion) PanicOf ¶ added in v1.0.2
PanicOf expects the function fn to panic by the expected error. If the function does not panic or panic for another reason, it will set the result to fail.
a := assert.New(t) a.PanicOf(func() { panic("expected error") }, "expected error") // success a.PanicOf(func() { panic("unexpected error") }, "expected error") // fail a.PanicOf(func() { // ..., no panic }, "expected error") // fail
func (*Assertion) PanicOfNow ¶ added in v1.0.2
PanicOfNow expects the function fn to panic by the expected error. If the function does not panic or panic for another reason, it will set the result to fail and terminate the execution.
a := assert.New(t) a.PanicOfNow(func() { panic("expected error") }, "expected error") // success a.PanicOfNow(func() { panic("unexpected error") }, "expected error") // fail and terminated // never runs
func (*Assertion) Run ¶ added in v0.1.1
Run runs f as a subtest of a called name. It runs f in a separate goroutine and blocks until f returns or calls a.Parallel to become a parallel test. Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
Run may be called simultaneously from multiple goroutines, but all such calls must return before the outer test function for a returns.
assertion := assert.New(t) assertion.Run("SubTest", func (a *assert.Assertion) bool { // TODO... })
func (*Assertion) True ¶ added in v0.1.4
True tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
a := assert.New(t) a.True(1) // success a.True("test") // success a.True(0) // fail a.True("") // fail
func (*Assertion) TrueNow ¶ added in v0.1.4
TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.
The function will stop the execution if the value is falsy.
a := assert.New(t) a.TrueNow(1) // success a.TrueNow("test") // success a.TrueNow("") // fail and terminate // never run
type AssertionError ¶
type AssertionError struct {
// contains filtered or unexported fields
}
AssertionError indicates the failure of an assertion.
func (AssertionError) Error ¶
func (err AssertionError) Error() string
Error returns the message of the error.