Documentation ¶
Overview ¶
Package assert Provides commonly asserts functions for help write Go testing.
inspired the package: github.com/stretchr/testify/assert
Index ¶
- Variables
- func Contains(t TestingT, src, elem any, fmtAndArgs ...any) bool
- func ContainsElems[T comdef.ScalarType](t TestingT, list, sub []T, fmtAndArgs ...any) bool
- func ContainsKey(t TestingT, mp, key any, fmtAndArgs ...any) bool
- func ContainsKeys(t TestingT, mp any, keys any, fmtAndArgs ...any) bool
- func DisableColor()
- func Empty(t TestingT, give any, fmtAndArgs ...any) bool
- func Eq(t TestingT, want, give any, fmtAndArgs ...any) bool
- func Equal(t TestingT, want, give any, fmtAndArgs ...any) bool
- func Err(t TestingT, err error, fmtAndArgs ...any) bool
- func ErrIs(t TestingT, err, wantErr error, fmtAndArgs ...any) bool
- func ErrMsg(t TestingT, err error, wantMsg string, fmtAndArgs ...any) bool
- func ErrSubMsg(t TestingT, err error, subMsg string, fmtAndArgs ...any) bool
- func Error(t TestingT, err error, fmtAndArgs ...any) bool
- func Fail(t TestingT, failMsg string, fmtAndArgs ...any) bool
- func FailNow(t TestingT, failMsg string, fmtAndArgs ...any) bool
- func False(t TestingT, give bool, fmtAndArgs ...any) bool
- func Gt(t TestingT, give, min any, fmtAndArgs ...any) bool
- func Gte(t TestingT, give, min any, fmtAndArgs ...any) bool
- func HideFullPath()
- func IsKind(t TestingT, wantKind reflect.Kind, give any, fmtAndArgs ...any) bool
- func IsType(t TestingT, wantType, give any, fmtAndArgs ...any) bool
- func Len(t TestingT, give any, wantLn int, fmtAndArgs ...any) bool
- func LenGt(t TestingT, give any, minLn int, fmtAndArgs ...any) bool
- func Lt(t TestingT, give, max any, fmtAndArgs ...any) bool
- func Lte(t TestingT, give, max any, fmtAndArgs ...any) bool
- func Neq(t TestingT, want, give any, fmtAndArgs ...any) bool
- func Nil(t TestingT, give any, fmtAndArgs ...any) bool
- func NoErr(t TestingT, err error, fmtAndArgs ...any) bool
- func NoError(t TestingT, err error, fmtAndArgs ...any) bool
- func NotContains(t TestingT, src, elem any, fmtAndArgs ...any) bool
- func NotContainsKey(t TestingT, mp, key any, fmtAndArgs ...any) bool
- func NotContainsKeys(t TestingT, mp any, keys any, fmtAndArgs ...any) bool
- func NotEmpty(t TestingT, give any, fmtAndArgs ...any) bool
- func NotEq(t TestingT, want, give any, fmtAndArgs ...any) bool
- func NotEqual(t TestingT, want, give any, fmtAndArgs ...any) bool
- func NotNil(t TestingT, give any, fmtAndArgs ...any) bool
- func NotPanics(t TestingT, fn PanicRunFunc, fmtAndArgs ...any) bool
- func NotSame(t TestingT, want, actual any, fmtAndArgs ...any) bool
- func Panics(t TestingT, fn PanicRunFunc, fmtAndArgs ...any) bool
- func PanicsErrMsg(t TestingT, fn PanicRunFunc, errMsg string, fmtAndArgs ...any) bool
- func PanicsMsg(t TestingT, fn PanicRunFunc, wantVal any, fmtAndArgs ...any) bool
- func Same(t TestingT, wanted, actual any, fmtAndArgs ...any) bool
- func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool
- func StrCount(t TestingT, s, sub string, count int, fmtAndArgs ...any) bool
- func StrNotContains(t TestingT, s, sub string, fmtAndArgs ...any) bool
- func True(t TestingT, give bool, fmtAndArgs ...any) bool
- type Assertions
- func (as *Assertions) Contains(src, elem any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) ContainsKey(mp, key any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Empty(give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Eq(want, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Equal(want, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Err(err error, fmtAndArgs ...any) *Assertions
- func (as *Assertions) ErrIs(err, wantErr error, fmtAndArgs ...any) *Assertions
- func (as *Assertions) ErrMsg(err error, errMsg string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) ErrSubMsg(err error, subMsg string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Error(err error, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Fail(failMsg string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) FailNow(failMsg string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) False(give bool, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Gt(give, min any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Gte(give, min any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) IsFail() bool
- func (as *Assertions) IsOk() bool
- func (as *Assertions) IsType(wantType, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Len(give any, wantLn int, fmtAndArgs ...any) *Assertions
- func (as *Assertions) LenGt(give any, minLn int, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Lt(give, max any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Lte(give, max any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Neq(want, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Nil(give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NoErr(err error, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NoError(err error, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotContains(src, elem any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotEmpty(give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotEq(want, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotEqual(want, give any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotNil(val any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) NotPanics(fn PanicRunFunc, fmtAndArgs ...any) *Assertions
- func (as *Assertions) Panics(fn PanicRunFunc, fmtAndArgs ...any) *Assertions
- func (as *Assertions) PanicsErrMsg(fn PanicRunFunc, errMsg string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) PanicsMsg(fn PanicRunFunc, wantVal any, fmtAndArgs ...any) *Assertions
- func (as *Assertions) StrContains(s, sub string, fmtAndArgs ...any) *Assertions
- func (as *Assertions) True(give bool, fmtAndArgs ...any) *Assertions
- func (as *Assertions) WithMsg(msg string) *Assertions
- type PanicRunFunc
- type TestingT
Constants ¶
This section is empty.
Variables ¶
var ( // ShowFullPath on show error trace ShowFullPath = true // EnableColor on show error trace EnableColor = true )
Functions ¶
func Contains ¶
Contains asserts that the given data(string,slice,map) should contain element
TIP: only support types: string, map, array, slice
map - check key exists string - check sub-string exists array,slice - check sub-element exists
func ContainsElems ¶ added in v0.6.16
func ContainsElems[T comdef.ScalarType](t TestingT, list, sub []T, fmtAndArgs ...any) bool
ContainsElems asserts that the given list should contains sub elements.
func ContainsKey ¶
ContainsKey asserts that the given map is contains key
func ContainsKeys ¶ added in v0.5.8
ContainsKeys asserts that the map is contains all given keys
Usage:
ContainsKeys(t, map[string]any{...}, []string{"key1", "key2"})
func ErrSubMsg ¶
ErrSubMsg asserts that the given is a not nil error and the error message contains subMsg
func Gte ¶ added in v0.6.7
Gte asserts that the give(intX,uintX,floatX) should not be greater than or equals to min
func IsKind ¶ added in v0.5.9
IsKind assert data reflect.Kind equals. If `give` is ptr or interface, will get real kind.
Usage:
assert.IsKind(t, reflect.Int, val) // assert type is int kind.
func IsType ¶ added in v0.5.8
IsType assert data type equals
Usage:
assert.IsType(t, 0, val) // assert type is int
func Lte ¶ added in v0.6.7
Lte asserts that the give(intX,uintX,floatX) should not be less than or equals to max
func NotContains ¶ added in v0.5.7
NotContains asserts that the given data(string,slice,map) should not contain element
TIP: only support types: string, map, array, slice
map - check key exists string - check sub-string exists array,slice - check sub-element exists
func NotContainsKey ¶ added in v0.6.9
NotContainsKey asserts that the given map is not contains key
func NotContainsKeys ¶ added in v0.6.9
NotContainsKeys asserts that the map is not contains all given keys
Usage:
NotContainsKeys(t, map[string]any{...}, []string{"key1", "key2"})
func NotEqual ¶ added in v0.6.0
NotEqual asserts that the want should not be equal to the given.
alias of NotEq()
func NotPanics ¶
func NotPanics(t TestingT, fn PanicRunFunc, fmtAndArgs ...any) bool
NotPanics asserts that the code inside the specified func NOT panics.
func NotSame ¶ added in v0.5.8
NotSame asserts that two pointers do not reference the same object.
assert.NotSame(t, ptr1, ptr2)
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func Panics ¶
func Panics(t TestingT, fn PanicRunFunc, fmtAndArgs ...any) bool
Panics asserts that the code inside the specified func panics.
func PanicsErrMsg ¶
func PanicsErrMsg(t TestingT, fn PanicRunFunc, errMsg string, fmtAndArgs ...any) bool
PanicsErrMsg should panic and with error message
func PanicsMsg ¶
func PanicsMsg(t TestingT, fn PanicRunFunc, wantVal any, fmtAndArgs ...any) bool
PanicsMsg should panic and with a value
func Same ¶ added in v0.5.8
Same asserts that two pointers reference the same object.
assert.Same(t, ptr1, ptr2)
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func StrContains ¶
StrContains asserts that the given string should contain sub-string
func StrCount ¶ added in v0.6.9
StrCount asserts that the given string should contain sub-string and count
func StrNotContains ¶ added in v0.6.16
StrNotContains asserts that the given string should not contain sub-string
Types ¶
type Assertions ¶
type Assertions struct { // prefix message for each assert TODO Msg string // contains filtered or unexported fields }
Assertions provides assertion methods around the TestingT interface.
func New ¶
func New(t TestingT) *Assertions
New makes a new Assertions object for the specified TestingT.
func (*Assertions) Contains ¶
func (as *Assertions) Contains(src, elem any, fmtAndArgs ...any) *Assertions
Contains asserts that the given data(string,slice,map) should contain element
func (*Assertions) ContainsKey ¶
func (as *Assertions) ContainsKey(mp, key any, fmtAndArgs ...any) *Assertions
ContainsKey asserts that the given map is contains key
func (*Assertions) Empty ¶
func (as *Assertions) Empty(give any, fmtAndArgs ...any) *Assertions
Empty check, please see Empty()
func (*Assertions) Eq ¶
func (as *Assertions) Eq(want, give any, fmtAndArgs ...any) *Assertions
Eq asserts that the want should equal to the given
func (*Assertions) Equal ¶ added in v0.6.7
func (as *Assertions) Equal(want, give any, fmtAndArgs ...any) *Assertions
Equal asserts that the want should equal to the given
Alias of Eq()
func (*Assertions) Err ¶
func (as *Assertions) Err(err error, fmtAndArgs ...any) *Assertions
Err asserts that the given is a not nil error
func (*Assertions) ErrIs ¶ added in v0.5.10
func (as *Assertions) ErrIs(err, wantErr error, fmtAndArgs ...any) *Assertions
ErrIs asserts that the given error is equals wantErr
func (*Assertions) ErrMsg ¶
func (as *Assertions) ErrMsg(err error, errMsg string, fmtAndArgs ...any) *Assertions
ErrMsg asserts that the given is a not nil error and error message equals wantMsg
func (*Assertions) ErrSubMsg ¶
func (as *Assertions) ErrSubMsg(err error, subMsg string, fmtAndArgs ...any) *Assertions
ErrSubMsg asserts that the given is a not nil error and the error message contains subMsg
func (*Assertions) Error ¶ added in v0.6.7
func (as *Assertions) Error(err error, fmtAndArgs ...any) *Assertions
Error asserts that the given is a not nil error
func (*Assertions) Fail ¶
func (as *Assertions) Fail(failMsg string, fmtAndArgs ...any) *Assertions
Fail reports a failure through
func (*Assertions) FailNow ¶
func (as *Assertions) FailNow(failMsg string, fmtAndArgs ...any) *Assertions
FailNow fails test
func (*Assertions) False ¶
func (as *Assertions) False(give bool, fmtAndArgs ...any) *Assertions
False check, please see False()
func (*Assertions) Gt ¶
func (as *Assertions) Gt(give, min any, fmtAndArgs ...any) *Assertions
Gt asserts that the give(intX) should not be greater than min
func (*Assertions) Gte ¶ added in v0.6.7
func (as *Assertions) Gte(give, min any, fmtAndArgs ...any) *Assertions
Gte asserts that the give(intX) should not be greater than or equal to min
func (*Assertions) IsType ¶ added in v0.5.8
func (as *Assertions) IsType(wantType, give any, fmtAndArgs ...any) *Assertions
IsType type equals assert
func (*Assertions) Len ¶
func (as *Assertions) Len(give any, wantLn int, fmtAndArgs ...any) *Assertions
Len assert given length is equals to wantLn
func (*Assertions) LenGt ¶
func (as *Assertions) LenGt(give any, minLn int, fmtAndArgs ...any) *Assertions
LenGt assert given length is greater than to minLn
func (*Assertions) Lt ¶
func (as *Assertions) Lt(give, max any, fmtAndArgs ...any) *Assertions
Lt asserts that the give(intX) should not be less than max
func (*Assertions) Lte ¶ added in v0.6.7
func (as *Assertions) Lte(give, max any, fmtAndArgs ...any) *Assertions
Lte asserts that the give(intX) should not be less than or equal to max
func (*Assertions) Neq ¶
func (as *Assertions) Neq(want, give any, fmtAndArgs ...any) *Assertions
Neq asserts that the want should not be equal to the given. alias of NotEq()
func (*Assertions) Nil ¶
func (as *Assertions) Nil(give any, fmtAndArgs ...any) *Assertions
Nil asserts that the given is a nil value
func (*Assertions) NoErr ¶
func (as *Assertions) NoErr(err error, fmtAndArgs ...any) *Assertions
NoErr asserts that the given is a nil error
func (*Assertions) NoError ¶ added in v0.6.11
func (as *Assertions) NoError(err error, fmtAndArgs ...any) *Assertions
NoError asserts that the given is a nil error
func (*Assertions) NotContains ¶ added in v0.5.7
func (as *Assertions) NotContains(src, elem any, fmtAndArgs ...any) *Assertions
NotContains asserts that the given data(string,slice,map) should not contain element
func (*Assertions) NotEmpty ¶
func (as *Assertions) NotEmpty(give any, fmtAndArgs ...any) *Assertions
NotEmpty check, please see NotEmpty()
func (*Assertions) NotEq ¶
func (as *Assertions) NotEq(want, give any, fmtAndArgs ...any) *Assertions
NotEq asserts that the want should not be equal to the given
func (*Assertions) NotEqual ¶ added in v0.6.7
func (as *Assertions) NotEqual(want, give any, fmtAndArgs ...any) *Assertions
NotEqual asserts that the want should not be equal to the given
Alias of NotEq()
func (*Assertions) NotNil ¶
func (as *Assertions) NotNil(val any, fmtAndArgs ...any) *Assertions
NotNil asserts that the given is a not nil value
func (*Assertions) NotPanics ¶
func (as *Assertions) NotPanics(fn PanicRunFunc, fmtAndArgs ...any) *Assertions
NotPanics check, please see NotPanics()
func (*Assertions) Panics ¶
func (as *Assertions) Panics(fn PanicRunFunc, fmtAndArgs ...any) *Assertions
Panics check, please see Panics()
func (*Assertions) PanicsErrMsg ¶
func (as *Assertions) PanicsErrMsg(fn PanicRunFunc, errMsg string, fmtAndArgs ...any) *Assertions
PanicsErrMsg check, please see PanicsErrMsg()
func (*Assertions) PanicsMsg ¶
func (as *Assertions) PanicsMsg(fn PanicRunFunc, wantVal any, fmtAndArgs ...any) *Assertions
PanicsMsg check, please see PanicsMsg()
func (*Assertions) StrContains ¶
func (as *Assertions) StrContains(s, sub string, fmtAndArgs ...any) *Assertions
StrContains asserts that the given strings is contains sub-string
func (*Assertions) True ¶
func (as *Assertions) True(give bool, fmtAndArgs ...any) *Assertions
True check, please see True()
func (*Assertions) WithMsg ¶ added in v0.6.9
func (as *Assertions) WithMsg(msg string) *Assertions
WithMsg set with prefix message.