assert

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

README

Assert

Description

This package is heavily inspired from stretchr/testify, and you can regard it as a fork of the upstream repository.

This package extracts all assertions and republish them with new ones including:

  • (*Assertion).ErrorRegexp

The Assert package servers as a supplement of Golang's testing for convenient assertions. And thus I don't want to implement anything like suite or mock.

  • suite can be simply implemented leveraging Golang's Subtests.
  • mock is not a good practice as it's hard to sync logics between the mock and the real object.

Usage

go get github.com/tisonkun/assert

The bundle itself is licensed under the Apache License.

Copyright 2022 tison wander4096@gmail.com.

You can see all transitive licenses and notices under the LICENSES folder.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallerInfo

func CallerInfo() []string

CallerInfo returns an array of strings containing the file and line number of each stack frame leading from the current test to the assert call that failed.

func ObjectsAreEqual

func ObjectsAreEqual(expected, actual any) bool

ObjectsAreEqual determines if two objects are considered equal.

This function does no assertion of any kind.

func ObjectsAreEqualValues

func ObjectsAreEqualValues(expected, actual any) bool

ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.

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

func (a *Assertions) Condition(comp Comparison, msgAndArgs ...any) bool

Condition uses a Comparison to assert a complex condition.

func (*Assertions) Contains

func (a *Assertions) Contains(s, contains any, msgAndArgs ...any) bool

Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.

func (*Assertions) DirExists

func (a *Assertions) DirExists(path string, msgAndArgs ...any) bool

DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists.

func (*Assertions) ElementsMatch

func (a *Assertions) ElementsMatch(listA, listB any, msgAndArgs ...any) (ok bool)

ElementsMatch asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.

func (*Assertions) Empty

func (a *Assertions) Empty(object any, msgAndArgs ...any) bool

Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

func (*Assertions) Equal

func (a *Assertions) Equal(expected, actual any, msgAndArgs ...any) bool

Equal asserts that two objects are equal. Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses). Function equality cannot be determined and will always fail.

func (*Assertions) EqualError

func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...any) bool

EqualError asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.

func (*Assertions) EqualValues

func (a *Assertions) EqualValues(expected, actual any, msgAndArgs ...any) bool

EqualValues asserts that two objects are equal or convertable to the same types and equal.

func (*Assertions) Error

func (a *Assertions) Error(err error, msgAndArgs ...any) bool

Error asserts that a function returned an error (i.e. not `nil`).

func (*Assertions) ErrorAs

func (a *Assertions) ErrorAs(err error, target any, msgAndArgs ...any) bool

ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. This is a wrapper for errors.As.

func (*Assertions) ErrorContains

func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs ...any) bool

ErrorContains asserts that a function returned an error (i.e. not `nil`) and that the error contains the specified substring.

func (*Assertions) ErrorIs

func (a *Assertions) ErrorIs(err, target error, msgAndArgs ...any) bool

ErrorIs asserts that at least one of the errors in err's chain matches target. This is a wrapper for errors.Is.

func (*Assertions) ErrorRegexp

func (a *Assertions) ErrorRegexp(theError error, rx any, msgAndArgs ...any) bool

ErrorRegexp asserts that a function returned an error (i.e. not `nil`) and that the error is matched by a specified regexp.

func (*Assertions) Eventually

func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any) bool

Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick.

func (*Assertions) Exactly

func (a *Assertions) Exactly(expected, actual any, msgAndArgs ...any) bool

Exactly asserts that two objects are equal in value and type.

func (*Assertions) Fail

func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any) bool

Fail reports a failure through

func (*Assertions) FailNow

func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) bool

FailNow fails test

func (*Assertions) False

func (a *Assertions) False(value bool, msgAndArgs ...any) bool

False asserts that the specified value is false.

func (*Assertions) FileExists

func (a *Assertions) FileExists(path string, msgAndArgs ...any) bool

FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file.

func (*Assertions) Greater

func (a *Assertions) Greater(e1 any, e2 any, msgAndArgs ...any) bool

Greater asserts that the first element is greater than the second

func (*Assertions) GreaterOrEqual

func (a *Assertions) GreaterOrEqual(e1 any, e2 any, msgAndArgs ...any) bool

GreaterOrEqual asserts that the first element is greater than or equal to the second

func (*Assertions) Implements

func (a *Assertions) Implements(interfaceObject any, object any, msgAndArgs ...any) bool

Implements asserts that an object is implemented by the specified interface.

func (*Assertions) InDelta

func (a *Assertions) InDelta(expected, actual any, delta float64, msgAndArgs ...any) bool

InDelta asserts that the two numerals are within delta of each other.

func (*Assertions) InDeltaMapValues

func (a *Assertions) InDeltaMapValues(expected, actual any, delta float64, msgAndArgs ...any) bool

InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

func (*Assertions) InDeltaSlice

func (a *Assertions) InDeltaSlice(expected, actual any, delta float64, msgAndArgs ...any) bool

InDeltaSlice is the same as InDelta, except it compares two slices.

func (*Assertions) InEpsilon

func (a *Assertions) InEpsilon(expected, actual any, epsilon float64, msgAndArgs ...any) bool

InEpsilon asserts that expected and actual have a relative error less than epsilon

func (*Assertions) InEpsilonSlice

func (a *Assertions) InEpsilonSlice(expected, actual any, epsilon float64, msgAndArgs ...any) bool

InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.

func (*Assertions) IsDecreasing

func (a *Assertions) IsDecreasing(object any, msgAndArgs ...any) bool

IsDecreasing asserts that the collection is decreasing

func (*Assertions) IsIncreasing

func (a *Assertions) IsIncreasing(object any, msgAndArgs ...any) bool

IsIncreasing asserts that the collection is increasing

func (*Assertions) IsNonDecreasing

func (a *Assertions) IsNonDecreasing(object any, msgAndArgs ...any) bool

IsNonDecreasing asserts that the collection is not decreasing

func (*Assertions) IsNonIncreasing

func (a *Assertions) IsNonIncreasing(object any, msgAndArgs ...any) bool

IsNonIncreasing asserts that the collection is not increasing

func (*Assertions) IsType

func (a *Assertions) IsType(expectedType any, object any, msgAndArgs ...any) bool

IsType asserts that the specified objects are of the same type.

func (*Assertions) JSONEq

func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...any) bool

JSONEq asserts that two JSON strings are equivalent.

func (*Assertions) Len

func (a *Assertions) Len(object any, length int, msgAndArgs ...any) bool

Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.

func (*Assertions) Less

func (a *Assertions) Less(e1 any, e2 any, msgAndArgs ...any) bool

Less asserts that the first element is less than the second

func (*Assertions) LessOrEqual

func (a *Assertions) LessOrEqual(e1 any, e2 any, msgAndArgs ...any) bool

LessOrEqual asserts that the first element is less than or equal to the second

func (*Assertions) Negative

func (a *Assertions) Negative(e any, msgAndArgs ...any) bool

Negative asserts that the specified element is negative

func (*Assertions) Never

func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any) bool

Never asserts that the given condition doesn't satisfy in waitFor time, periodically checking the target function each tick.

func (*Assertions) Nil

func (a *Assertions) Nil(object any, msgAndArgs ...any) bool

Nil asserts that the specified object is nil.

func (*Assertions) NoDirExists

func (a *Assertions) NoDirExists(path string, msgAndArgs ...any) bool

NoDirExists checks whether a directory does not exist in the given path. It fails if the path points to an existing _directory_ only.

func (*Assertions) NoError

func (a *Assertions) NoError(err error, msgAndArgs ...any) bool

NoError asserts that a function returned no error (i.e. `nil`).

func (*Assertions) NoFileExists

func (a *Assertions) NoFileExists(path string, msgAndArgs ...any) bool

NoFileExists checks whether a file does not exist in a given path. It fails if the path points to an existing _file_ only.

func (*Assertions) NotContains

func (a *Assertions) NotContains(s, contains any, msgAndArgs ...any) bool

NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.

func (*Assertions) NotEmpty

func (a *Assertions) NotEmpty(object any, msgAndArgs ...any) bool

NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.

func (*Assertions) NotEqual

func (a *Assertions) NotEqual(expected, actual any, msgAndArgs ...any) bool

NotEqual asserts that the specified values are NOT equal. Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func (*Assertions) NotEqualValues

func (a *Assertions) NotEqualValues(expected, actual any, msgAndArgs ...any) bool

NotEqualValues asserts that two objects are not equal even when converted to the same type

func (*Assertions) NotErrorIs

func (a *Assertions) NotErrorIs(err, target error, msgAndArgs ...any) bool

NotErrorIs asserts that at none of the errors in err's chain matches target. This is a wrapper for errors.Is.

func (*Assertions) NotNil

func (a *Assertions) NotNil(object any, msgAndArgs ...any) bool

NotNil asserts that the specified object is not nil.

func (*Assertions) NotPanics

func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs ...any) bool

NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.

func (*Assertions) NotRegexp

func (a *Assertions) NotRegexp(rx any, str any, msgAndArgs ...any) bool

NotRegexp asserts that a specified regexp does not match a string.

func (*Assertions) NotSame

func (a *Assertions) NotSame(expected, actual any, msgAndArgs ...any) bool

NotSame asserts that two pointers do not reference the same object. Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func (*Assertions) NotSubset

func (a *Assertions) NotSubset(list, subset any, msgAndArgs ...any) (ok bool)

NotSubset asserts that the specified list(array, slice...) contains not all elements given in the specified subset(array, slice...).

func (*Assertions) NotZero

func (a *Assertions) NotZero(i any, msgAndArgs ...any) bool

NotZero asserts that i is not the zero value for its type.

func (*Assertions) Panics

func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...any) bool

Panics asserts that the code inside the specified PanicTestFunc panics.

func (*Assertions) PanicsWithError

func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs ...any) bool

PanicsWithError asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison.

func (*Assertions) PanicsWithValue

func (a *Assertions) PanicsWithValue(expected any, f PanicTestFunc, msgAndArgs ...any) bool

PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value equals the expected panic value.

func (*Assertions) Positive

func (a *Assertions) Positive(e any, msgAndArgs ...any) bool

Positive asserts that the specified element is positive

func (*Assertions) Regexp

func (a *Assertions) Regexp(rx any, str any, msgAndArgs ...any) bool

Regexp asserts that a specified regexp matches a string.

func (*Assertions) Same

func (a *Assertions) Same(expected, actual any, msgAndArgs ...any) bool

Same asserts that two pointers reference the same object. Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func (*Assertions) Subset

func (a *Assertions) Subset(list, subset any, msgAndArgs ...any) (ok bool)

Subset asserts that the specified list(array, slice, map...) contains all elements given in the specified subset(array, slice, map...).

func (*Assertions) True

func (a *Assertions) True(value bool, msgAndArgs ...any) bool

True asserts that the specified value is true.

func (*Assertions) WithOnFailure

func (a *Assertions) WithOnFailure(f func(TestingT)) *Assertions

WithOnFailure returns a new Assertions with customized behaviour on failure.

func (*Assertions) WithinDuration

func (a *Assertions) WithinDuration(expected, actual time.Time, delta time.Duration, msgAndArgs ...any) bool

WithinDuration asserts that the two times are within duration delta of each other.

func (*Assertions) WithinTimeRange added in v1.2.1

func (a *Assertions) WithinTimeRange(actual, start, end time.Time, msgAndArgs ...any) bool

WithinTimeRange asserts that a time is within a time range (inclusive).

func (*Assertions) YAMLEq

func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...any) bool

YAMLEq asserts that two YAML strings are equivalent.

func (*Assertions) Zero

func (a *Assertions) Zero(i any, msgAndArgs ...any) bool

Zero asserts that i is the zero value for its type.

type CompareType

type CompareType int

type Comparison

type Comparison func() (success bool)

Comparison is a custom function that returns true on success and false on failure

type PanicTestFunc

type PanicTestFunc func()

PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics methods, and represents a simple func that takes no arguments, and returns nothing.

type TestingT

type TestingT interface {
	Errorf(format string, args ...any)
	FailNow()
}

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