assert

package
v0.10.9 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package assert provides an incredibly simple assertion framework, that relies on generics and simplicity. All assertions are "fatal" and cause the test to abort at the failure line (rather than continue on error).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](t testing.TB, slice []T, item T)

Contains asserts that the item is in the slice provided. Empty or nil slices always cause failure.

func Equal

func Equal[T comparable](t testing.TB, valOne, valTwo T)

Equal causes a test to fail if the two (comparable) values are not equal. Be aware that two different pointers and objects passed as interfaces that are implemented by pointer receivers are comparable as equal and will fail this assertion even if their *values* are equal.

func EqualItems added in v0.9.0

func EqualItems[T comparable](t testing.TB, one, two []T)

EqualItems compares the values in two slices and creates an error if all items are not equal.

func Error

func Error(t testing.TB, err error)

Error fails the test if the error is nil.

func ErrorIs

func ErrorIs(t testing.TB, err, target error)

ErrorIs is an assertion form of errors.Is, and fails the test if the error (or its wrapped values) are not equal to the target error.

func Failing

func Failing[T testing.TB](t T, test func(T))

Failing asserts that the specified test fails. This was required for validating the behavior of the assertion, and may be useful in your own testing.

func MaxRuntime added in v0.8.2

func MaxRuntime(t testing.TB, dur time.Duration, op func())

MaxRuntime runs an operation and asserts that the operations runtime was less than the provided duration.

func MinRuntime added in v0.8.2

func MinRuntime(t testing.TB, dur time.Duration, op func())

MinRuntime runs an operation and asserts that the operations runtime was greater than the provided duration.

func Nil added in v0.10.4

func Nil(t testing.TB, val any)

Nil causes a test to fail if the value is not nil. This operation uses reflection, (unlike many in this package,) and correctly handles nil values assigned to interfaces (e.g. that they are nil.)

func NilPtr added in v0.10.4

func NilPtr[T any](t testing.TB, val *T)

NilPtr asserts that the pointer value is nil. Use Nil (which uses reflection) for these pointer values as well maps, channels, slices, and interfaces.

func NotContains

func NotContains[T comparable](t testing.TB, slice []T, item T)

Contains asserts that the item is *not* in the slice provided. If the input slice is empty, this assertion will never error.

func NotEqual

func NotEqual[T comparable](t testing.TB, valOne, valTwo T)

NotEqual causes a test to fail if two (comparable) values are not equal. Be aware that pointers to objects (including objects passed as interfaces implemented by pointers) will pass this test, even if their values are equal.

func NotEqualItems added in v0.9.0

func NotEqualItems[T comparable](t testing.TB, one, two []T)

EqualItems compares the values in two slices and creates a failure if all items are not equal

func NotError

func NotError(t testing.TB, err error)

NotError fails the test if the error is non-nil.

func NotErrorIs added in v0.8.0

func NotErrorIs(t testing.TB, err, target error)

NotErrorIs is an assertion form of !errors.Is, and fails the test if the error (or its wrapped values) are equal to the target error.

func NotNil added in v0.10.4

func NotNil(t testing.TB, val any)

NotNil causes a test to fail if the value is nil. This operation uses reflection, (unlike many in this package,) and correctly handles nil values assigned to interfaces (e.g. that they are nil.)

func NotNilPtr added in v0.10.4

func NotNilPtr[T any](t testing.TB, val *T)

NotNilPtr asserts that the pointer value is not equal to nil. Use Nil (which uses reflection) for these pointer values as well maps, channels, slices, and interfaces.

func NotPanic

func NotPanic(t testing.TB, fn func())

NotPanic asserts that the function does not panic.

func NotSubstring

func NotSubstring(t testing.TB, str, substr string)

NotSubstring asserts that the substring is not present in the outer string.

func NotType added in v0.9.2

func NotType[T any](t testing.TB, obj any)

NotType fails the test when the type of the specifier matches the type of the object.

func NotZero

func NotZero[T comparable](t testing.TB, val T)

NotZero fails a test if the value is the zero for its type.

func Panic

func Panic(t testing.TB, fn func())

Panic asserts that the function raises a panic.

func PanicValue

func PanicValue[T comparable](t testing.TB, fn func(), value T)

PanicValue asserts that the function raises a panic and that the value, as returned by recover() is equal to the value provided.

func Runtime added in v0.10.0

func Runtime(t testing.TB, min, max time.Duration, op func())

Runtime asserts that the function will execute for less than the absolute difference of the two durations provided. The absolute difference between the durations is use to max

func Substring

func Substring(t testing.TB, str, substr string)

Substring asserts that the substring is present in the string.

func True

func True(t testing.TB, cond bool)

True causes a test to fail if the condition is false.

func Type added in v0.9.2

func Type[T any](t testing.TB, obj any)

Type fails the test if the type of the object doesn't match the specifier type provided.

func Zero

func Zero[T comparable](t testing.TB, val T)

Zero fails a test if the value is not the zero-value for its type.

Types

This section is empty.

Directories

Path Synopsis
GENERATED FILE FROM ASSERTION PACKAGE
GENERATED FILE FROM ASSERTION PACKAGE

Jump to

Keyboard shortcuts

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