assert

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 16 Imported by: 0

README

Assert Utils

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

Install

go get github.com/gookit/goutil/sysutil

GoDocs

Please see Go docs

Function API

func Contains(t TestingT, src, elem any, 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 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 int, 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 int, 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 NotContains(t TestingT, src, elem 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 interface{}, fmtAndArgs ...any) bool
func Same(t TestingT, wanted, actual any, fmtAndArgs ...any) bool
func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool
func True(t TestingT, give bool, fmtAndArgs ...any) bool
type Assertions struct{ ... }
    func New(t TestingT) *Assertions

Code Check & Testing

gofmt -w -l ./
golint ./...
go test ./...

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 Equal added in v0.6.0

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

Equal asserts that the want should equal to the given.

alias of Eq()

func Err

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

Err asserts that the given is a not nil error

func ErrIs added in v0.5.10

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

ErrIs asserts that the given error is equals wantErr

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 Error added in v0.6.0

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

Error asserts that the given is a not nil error. alias of Error()

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

Gt asserts that the give(intX) should not be greater than max

func HideFullPath

func HideFullPath()

HideFullPath render

func IsKind added in v0.5.9

func IsKind(t TestingT, wantKind reflect.Kind, give any, fmtAndArgs ...any) bool

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

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

Len assert given length is equals to wantLn

func LenGt

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

LenGt assert given length is greater than to minLn

func Lt

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

Lt asserts that the give(intX) should not be less than max

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 NoError added in v0.6.0

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

NoError asserts that the given is a nil error. alias of NoError()

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 NotEqual added in v0.6.0

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

NotEqual asserts that the want should not be equal to the given.

alias of NotEq()

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 any, 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

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

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) 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) 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

func (*Assertions) Gt

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

Gt asserts that the give(intX) should not be greater than max

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

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 int, fmtAndArgs ...any) *Assertions

Lt asserts that the give(intX) should not be less than 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

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

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

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) 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 any, fmtAndArgs ...any) *Assertions

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

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