assert

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package assert provides some tool functions for use with the Go testing.

inspired the package: github.com/stretchr/testify/assert

Index

Constants

This section is empty.

Variables

View Source
var (
	// ShowFullPath on show error trace
	ShowFullPath = true
	// EnableColor on show error trace
	EnableColor = true
)

Functions

func Contains

func Contains(t TestingT, src, elem any, fmtAndArgs ...any) bool

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 ContainsKey

func ContainsKey(t TestingT, mp, key any, fmtAndArgs ...any) bool

ContainsKey asserts that the given map is contains key

func ContainsKeys added in v0.5.8

func ContainsKeys(t TestingT, mp any, keys any, fmtAndArgs ...any) bool

ContainsKeys asserts that the map is contains all given keys

Usage:

ContainsKeys(t, map[string]any{...}, []string{"key1", "key2"})

func DisableColor

func DisableColor()

DisableColor render

func Empty

func Empty(t TestingT, give any, fmtAndArgs ...any) bool

Empty asserts that the give should be empty

func Eq

func Eq(t TestingT, want, give any, fmtAndArgs ...any) bool

Eq asserts that the want should equal to the given

func Err

func Err(t TestingT, err error, fmtAndArgs ...any) bool

Err asserts that the given is a not nil error

func ErrMsg

func ErrMsg(t TestingT, err error, wantMsg string, fmtAndArgs ...any) bool

ErrMsg asserts that the given is a not nil error and error message equals wantMsg

func ErrSubMsg

func ErrSubMsg(t TestingT, err error, subMsg string, fmtAndArgs ...any) bool

ErrSubMsg asserts that the given is a not nil error and the error message contains subMsg

func Fail

func Fail(t TestingT, failMsg string, fmtAndArgs ...any) bool

Fail reports a failure through

func FailNow

func FailNow(t TestingT, failMsg string, fmtAndArgs ...any) bool

FailNow fails test

func False

func False(t TestingT, give bool, fmtAndArgs ...any) bool

False asserts that the given is a bool false

func Gt

func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool

func HideFullPath

func HideFullPath()

HideFullPath render

func IsType added in v0.5.8

func IsType(t TestingT, wantType, give any, fmtAndArgs ...any) bool

IsType assert data type equals

Usage:

assert.IsType(t, 0, val) // assert type is int

func Len

func Len(t TestingT, give any, wantLn int, fmtAndArgs ...any) bool

func LenGt

func LenGt(t TestingT, give any, minLn int, fmtAndArgs ...any) bool

func Lt

func Lt(t TestingT, give, max int, fmtAndArgs ...any) bool

func Neq

func Neq(t TestingT, want, give any, fmtAndArgs ...any) bool

Neq asserts that the want should not be equal to the given. alias of NotEq()

func Nil

func Nil(t TestingT, give any, fmtAndArgs ...any) bool

Nil asserts that the given is a nil value

func NoErr

func NoErr(t TestingT, err error, fmtAndArgs ...any) bool

NoErr asserts that the given is a nil error

func NotContains added in v0.5.7

func NotContains(t TestingT, src, elem any, fmtAndArgs ...any) bool

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 NotEmpty

func NotEmpty(t TestingT, give any, fmtAndArgs ...any) bool

NotEmpty asserts that the give should not be empty

func NotEq

func NotEq(t TestingT, want, give any, fmtAndArgs ...any) bool

NotEq asserts that the want should not be equal to the given

func NotNil

func NotNil(t TestingT, give any, fmtAndArgs ...any) bool

NotNil asserts that the given is a not nil value

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

func NotSame(t TestingT, want, actual any, fmtAndArgs ...any) bool

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 interface{}, fmtAndArgs ...any) bool

PanicsMsg should panic and with a value

func Same added in v0.5.8

func Same(t TestingT, wanted, actual any, fmtAndArgs ...any) bool

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

func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool

StrContains asserts that the given strings is contains sub-string

func True

func True(t TestingT, give bool, fmtAndArgs ...any) bool

True asserts that the given is a bool true

Types

type Assertions

type Assertions struct {
	// 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

func (*Assertions) ContainsKey

func (as *Assertions) ContainsKey(mp, key any, fmtAndArgs ...any) *Assertions

func (*Assertions) Empty

func (as *Assertions) Empty(give any, fmtAndArgs ...any) *Assertions

func (*Assertions) Eq

func (as *Assertions) Eq(want, give any, fmtAndArgs ...any) *Assertions

func (*Assertions) Err

func (as *Assertions) Err(err error, fmtAndArgs ...any) *Assertions

Err asserts that the given is a not nil error

func (*Assertions) ErrMsg

func (as *Assertions) ErrMsg(err error, errMsg string, fmtAndArgs ...any) *Assertions

func (*Assertions) ErrSubMsg

func (as *Assertions) ErrSubMsg(err error, subMsg string, fmtAndArgs ...any) *Assertions

func (*Assertions) Fail

func (as *Assertions) Fail(failMsg string, fmtAndArgs ...any) *Assertions

func (*Assertions) FailNow

func (as *Assertions) FailNow(failMsg string, fmtAndArgs ...any) *Assertions

func (*Assertions) False

func (as *Assertions) False(give bool, fmtAndArgs ...any) *Assertions

func (*Assertions) Gt

func (as *Assertions) Gt(give, min int, fmtAndArgs ...any) *Assertions

func (Assertions) IsFail

func (as Assertions) IsFail() bool

IsFail for last check

func (Assertions) IsOk

func (as Assertions) IsOk() bool

IsOk for last check

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

func (*Assertions) LenGt

func (as *Assertions) LenGt(give any, minLn int, fmtAndArgs ...any) *Assertions

func (*Assertions) Lt

func (as *Assertions) Lt(give, max int, fmtAndArgs ...any) *Assertions

func (*Assertions) Neq

func (as *Assertions) Neq(want, give any, fmtAndArgs ...any) *Assertions

func (*Assertions) Nil

func (as *Assertions) Nil(give any, fmtAndArgs ...any) *Assertions

func (*Assertions) NoErr

func (as *Assertions) NoErr(err error, fmtAndArgs ...any) *Assertions

NoErr 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

func (*Assertions) NotEmpty

func (as *Assertions) NotEmpty(give any, fmtAndArgs ...any) *Assertions

func (*Assertions) NotEq

func (as *Assertions) NotEq(want, give any, fmtAndArgs ...any) *Assertions

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

func (*Assertions) Panics

func (as *Assertions) Panics(fn PanicRunFunc, fmtAndArgs ...any) *Assertions

func (*Assertions) PanicsErrMsg

func (as *Assertions) PanicsErrMsg(fn PanicRunFunc, errMsg string, fmtAndArgs ...any) *Assertions

func (*Assertions) PanicsMsg

func (as *Assertions) PanicsMsg(fn PanicRunFunc, wantVal interface{}, fmtAndArgs ...any) *Assertions

func (*Assertions) StrContains

func (as *Assertions) StrContains(s, sub string, fmtAndArgs ...any) *Assertions

func (*Assertions) True

func (as *Assertions) True(give bool, fmtAndArgs ...any) *Assertions

type PanicRunFunc

type PanicRunFunc func()

PanicRunFunc define

type TestingT

type TestingT interface {
	Helper()
	Name() string
	Error(args ...any)
}

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

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