assert

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package assert provides assertion helpers for testing, offering both functional and fluent assertion styles. When an assertion fails, the test will continue running. For assertions that should stop the test on failure, use the `require` package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Panic

func Panic(t internal.TestingT, fn func(), expr string, msg ...string)

Panic asserts that `fn` panics and the panic message matches `expr`. It reports an error if `fn` does not panic or if the recovered message does not satisfy `expr`.

func ToJsonString

func ToJsonString(v any) string

ToJsonString converts the given value to a JSON string.

func ToPrettyString

func ToPrettyString(v any) string

ToPrettyString converts the given value to a pretty string.

Types

type Assertion

type Assertion struct {
	AssertionBase[*Assertion]
	// contains filtered or unexported fields
}

Assertion wraps a test context and a value for fluent assertions.

func That

func That(t internal.TestingT, v any) *Assertion

That creates an Assertion for the given value v and test context t.

func (*Assertion) Contains

func (a *Assertion) Contains(expect any, msg ...string) *Assertion

Contains asserts that the wrapped value v has a method named 'Contains' that returns true when passed expect. It reports an error if the method does not exist or returns false.

func (*Assertion) Equal

func (a *Assertion) Equal(expect any, msg ...string) *Assertion

Equal asserts that the wrapped value v is `reflect.DeepEqual` to expect. It reports an error if the values are not deeply equal.

func (*Assertion) False

func (a *Assertion) False(msg ...string) *Assertion

False asserts that got is false. It reports an error if the value is true.

func (*Assertion) Has

func (a *Assertion) Has(expect any, msg ...string) *Assertion

Has asserts that the wrapped value v has a method named 'Has' that returns true when passed expect. It reports an error if the method does not exist or returns false.

func (*Assertion) Implements

func (a *Assertion) Implements(expect any, msg ...string) *Assertion

Implements asserts that the type of the wrapped value v implements the interface type of expect. The expect parameter must be an interface or pointer to interface. It reports an error if v does not implement the interface.

func (*Assertion) Nil

func (a *Assertion) Nil(msg ...string) *Assertion

Nil asserts that got is nil. It reports an error if the value is not nil.

func (*Assertion) NotEqual

func (a *Assertion) NotEqual(expect any, msg ...string) *Assertion

NotEqual asserts that the wrapped value v is not deeply equal to expect. It reports an error if the values are deeply equal.

func (*Assertion) NotNil

func (a *Assertion) NotNil(msg ...string) *Assertion

NotNil asserts that got is not nil. It reports an error if the value is nil.

func (*Assertion) NotSame

func (a *Assertion) NotSame(expect any, msg ...string) *Assertion

NotSame asserts that the wrapped value v and expect are not the same (using Go !=). It reports an error if v == expect.

func (*Assertion) Same

func (a *Assertion) Same(expect any, msg ...string) *Assertion

Same asserts that the wrapped value v and expect are the same (using Go ==). It reports an error if v != expect.

func (*Assertion) True

func (a *Assertion) True(msg ...string) *Assertion

True asserts that got is true. It reports an error if the value is false.

func (*Assertion) TypeOf

func (a *Assertion) TypeOf(expect any, msg ...string) *Assertion

TypeOf asserts that the type of the wrapped value v is assignable to the type of expect. It supports pointer to interface types. It reports an error if the types are not assignable.

type AssertionBase

type AssertionBase[T any] struct {
	// contains filtered or unexported fields
}

AssertionBase provides common functionality for `Assertion` and `Require`.

func (*AssertionBase[T]) Require

func (c *AssertionBase[T]) Require() T

Require is intended for internal use by the `require` package only. IMPORTANT: Do not call it directly!

type ErrorAssertion

type ErrorAssertion struct {
	AssertionBase[*ErrorAssertion]
	// contains filtered or unexported fields
}

ErrorAssertion provides assertion methods for values of type error. It is used to perform validations on error values in test cases.

func ThatError

func ThatError(t internal.TestingT, v error) *ErrorAssertion

ThatError returns a new ErrorAssertion for the given error value.

func (*ErrorAssertion) Is

func (a *ErrorAssertion) Is(target error, msg ...string) *ErrorAssertion

Is reports a test failure if the error is not the same as the given error.

func (*ErrorAssertion) Matches

func (a *ErrorAssertion) Matches(expr string, msg ...string) *ErrorAssertion

Matches reports a test failure if the error string does not match the given expression. It expects a non-nil error and uses the provided expression (typically a regex) to validate the error message content. Optional custom failure messages can be provided.

func (*ErrorAssertion) Nil

func (a *ErrorAssertion) Nil(msg ...string) *ErrorAssertion

Nil reports a test failure if the error is not nil.

func (*ErrorAssertion) NotIs

func (a *ErrorAssertion) NotIs(target error, msg ...string) *ErrorAssertion

NotIs reports a test failure if the error is the same as the given error.

func (*ErrorAssertion) NotNil

func (a *ErrorAssertion) NotNil(msg ...string) *ErrorAssertion

NotNil reports a test failure if the error is nil.

type MapAssertion

type MapAssertion[K, V comparable] struct {
	AssertionBase[*MapAssertion[K, V]]
	// contains filtered or unexported fields
}

MapAssertion encapsulates a map value and a test handler for making assertions on the map.

func ThatMap

func ThatMap[K, V comparable](t internal.TestingT, v map[K]V) *MapAssertion[K, V]

ThatMap returns a MapAssertion for the given testing object and map value.

func (*MapAssertion[K, V]) ContainsKey

func (a *MapAssertion[K, V]) ContainsKey(key K, msg ...string) *MapAssertion[K, V]

ContainsKey asserts that the map contains the expected key.

func (*MapAssertion[K, V]) ContainsKeyValue

func (a *MapAssertion[K, V]) ContainsKeyValue(key K, value V, msg ...string) *MapAssertion[K, V]

ContainsKeyValue asserts that the map contains the expected key-value pair.

func (*MapAssertion[K, V]) ContainsKeys

func (a *MapAssertion[K, V]) ContainsKeys(keys []K, msg ...string) *MapAssertion[K, V]

ContainsKeys asserts that the map contains all the expected keys.

func (*MapAssertion[K, V]) ContainsValue

func (a *MapAssertion[K, V]) ContainsValue(value V, msg ...string) *MapAssertion[K, V]

ContainsValue asserts that the map contains the expected value.

func (*MapAssertion[K, V]) ContainsValues

func (a *MapAssertion[K, V]) ContainsValues(values []V, msg ...string) *MapAssertion[K, V]

ContainsValues asserts that the map contains all the expected values.

func (*MapAssertion[K, V]) Empty

func (a *MapAssertion[K, V]) Empty(msg ...string) *MapAssertion[K, V]

Empty asserts that the map is empty.

func (*MapAssertion[K, V]) Equal

func (a *MapAssertion[K, V]) Equal(expect map[K]V, msg ...string) *MapAssertion[K, V]

Equal asserts that the map is equal to the expected map.

func (*MapAssertion[K, V]) HasSameKeys

func (a *MapAssertion[K, V]) HasSameKeys(expect map[K]V, msg ...string) *MapAssertion[K, V]

HasSameKeys asserts that the map has the same keys as the expected map.

func (*MapAssertion[K, V]) HasSameValues

func (a *MapAssertion[K, V]) HasSameValues(expect map[K]V, msg ...string) *MapAssertion[K, V]

HasSameValues asserts that the map has the same values as the expected map.

func (*MapAssertion[K, V]) Length

func (a *MapAssertion[K, V]) Length(length int, msg ...string) *MapAssertion[K, V]

Length asserts that the map has the expected length.

func (*MapAssertion[K, V]) Nil

func (a *MapAssertion[K, V]) Nil(msg ...string) *MapAssertion[K, V]

Nil asserts that the map is nil.

func (*MapAssertion[K, V]) NotContainsKey

func (a *MapAssertion[K, V]) NotContainsKey(key K, msg ...string) *MapAssertion[K, V]

NotContainsKey asserts that the map does not contain the expected key.

func (*MapAssertion[K, V]) NotContainsKeys

func (a *MapAssertion[K, V]) NotContainsKeys(keys []K, msg ...string) *MapAssertion[K, V]

NotContainsKeys asserts that the map does not contain any of the expected keys.

func (*MapAssertion[K, V]) NotContainsValue

func (a *MapAssertion[K, V]) NotContainsValue(value V, msg ...string) *MapAssertion[K, V]

NotContainsValue asserts that the map does not contain the expected value.

func (*MapAssertion[K, V]) NotContainsValues

func (a *MapAssertion[K, V]) NotContainsValues(values []V, msg ...string) *MapAssertion[K, V]

NotContainsValues asserts that the map does not contain any of the expected values.

func (*MapAssertion[K, V]) NotEmpty

func (a *MapAssertion[K, V]) NotEmpty(msg ...string) *MapAssertion[K, V]

NotEmpty asserts that the map is not empty.

func (*MapAssertion[K, V]) NotEqual

func (a *MapAssertion[K, V]) NotEqual(expect map[K]V, msg ...string) *MapAssertion[K, V]

NotEqual asserts that the map is not equal to the expected map.

func (*MapAssertion[K, V]) NotNil

func (a *MapAssertion[K, V]) NotNil(msg ...string) *MapAssertion[K, V]

NotNil asserts that the map is not nil.

func (*MapAssertion[K, V]) SubsetOf

func (a *MapAssertion[K, V]) SubsetOf(expect map[K]V, msg ...string) *MapAssertion[K, V]

SubsetOf asserts that the map is a subset of the expected map.

func (*MapAssertion[K, V]) SupersetOf

func (a *MapAssertion[K, V]) SupersetOf(expect map[K]V, msg ...string) *MapAssertion[K, V]

SupersetOf asserts that the map is a superset of the expected map.

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

type NumberAssertion

type NumberAssertion[T Number] struct {
	AssertionBase[*NumberAssertion[T]]
	// contains filtered or unexported fields
}

NumberAssertion encapsulates a number value and a test handler for making assertions on the number.

func ThatNumber

func ThatNumber[T Number](t internal.TestingT, v T) *NumberAssertion[T]

ThatNumber returns a NumberAssertion for the given testing object and number value.

func (*NumberAssertion[T]) Between

func (a *NumberAssertion[T]) Between(lower, upper T, msg ...string) *NumberAssertion[T]

Between asserts that the number value is between the lower and upper bounds.

func (*NumberAssertion[T]) Equal

func (a *NumberAssertion[T]) Equal(expect T, msg ...string) *NumberAssertion[T]

Equal asserts that the number value is equal to the expected value.

func (*NumberAssertion[T]) GreaterOrEqual

func (a *NumberAssertion[T]) GreaterOrEqual(expect T, msg ...string) *NumberAssertion[T]

GreaterOrEqual asserts that the number value is greater than or equal to the expected value.

func (*NumberAssertion[T]) GreaterThan

func (a *NumberAssertion[T]) GreaterThan(expect T, msg ...string) *NumberAssertion[T]

GreaterThan asserts that the number value is greater than the expected value.

func (*NumberAssertion[T]) InDelta

func (a *NumberAssertion[T]) InDelta(expect T, delta T, msg ...string) *NumberAssertion[T]

InDelta asserts that the number value is within the delta range of the expected value.

func (*NumberAssertion[T]) IsFinite

func (a *NumberAssertion[T]) IsFinite(msg ...string) *NumberAssertion[T]

IsFinite asserts that the number value is finite.

func (*NumberAssertion[T]) IsInf

func (a *NumberAssertion[T]) IsInf(sign int, msg ...string) *NumberAssertion[T]

IsInf asserts that the number value is infinite.

func (*NumberAssertion[T]) IsNaN

func (a *NumberAssertion[T]) IsNaN(msg ...string) *NumberAssertion[T]

IsNaN asserts that the number value is NaN (Not a Number).

func (*NumberAssertion[T]) LessOrEqual

func (a *NumberAssertion[T]) LessOrEqual(expect T, msg ...string) *NumberAssertion[T]

LessOrEqual asserts that the number value is less than or equal to the expected value.

func (*NumberAssertion[T]) LessThan

func (a *NumberAssertion[T]) LessThan(expect T, msg ...string) *NumberAssertion[T]

LessThan asserts that the number value is less than the expected value.

func (*NumberAssertion[T]) Negative

func (a *NumberAssertion[T]) Negative(msg ...string) *NumberAssertion[T]

Negative asserts that the number value is negative.

func (*NumberAssertion[T]) NotBetween

func (a *NumberAssertion[T]) NotBetween(lower, upper T, msg ...string) *NumberAssertion[T]

NotBetween asserts that the number value is not between the lower and upper bounds.

func (*NumberAssertion[T]) NotEqual

func (a *NumberAssertion[T]) NotEqual(expect T, msg ...string) *NumberAssertion[T]

NotEqual asserts that the number value is not equal to the expected value.

func (*NumberAssertion[T]) NotNegative

func (a *NumberAssertion[T]) NotNegative(msg ...string) *NumberAssertion[T]

NotNegative asserts that the number value is non-negative.

func (*NumberAssertion[T]) NotPositive

func (a *NumberAssertion[T]) NotPositive(msg ...string) *NumberAssertion[T]

NotPositive asserts that the number value is non-positive.

func (*NumberAssertion[T]) NotZero

func (a *NumberAssertion[T]) NotZero(msg ...string) *NumberAssertion[T]

NotZero asserts that the number value is not zero.

func (*NumberAssertion[T]) Positive

func (a *NumberAssertion[T]) Positive(msg ...string) *NumberAssertion[T]

Positive asserts that the number value is positive.

func (*NumberAssertion[T]) Zero

func (a *NumberAssertion[T]) Zero(msg ...string) *NumberAssertion[T]

Zero asserts that the number value is zero.

type SliceAssertion

type SliceAssertion[T comparable] struct {
	AssertionBase[*SliceAssertion[T]]
	// contains filtered or unexported fields
}

SliceAssertion encapsulates a slice value and a test handler for making assertions on the slice.

func ThatSlice

func ThatSlice[T comparable](t internal.TestingT, v []T) *SliceAssertion[T]

ThatSlice returns a SliceAssertion for the given testing object and slice value.

func (*SliceAssertion[T]) AllMatches

func (a *SliceAssertion[T]) AllMatches(fn func(T) bool, msg ...string) *SliceAssertion[T]

AllMatches asserts that all elements in the slice satisfy the given condition.

func (*SliceAssertion[T]) AllUnique

func (a *SliceAssertion[T]) AllUnique(msg ...string) *SliceAssertion[T]

AllUnique asserts that all elements in the slice are unique.

func (*SliceAssertion[T]) AnyMatches

func (a *SliceAssertion[T]) AnyMatches(fn func(T) bool, msg ...string) *SliceAssertion[T]

AnyMatches asserts that at least one element in the slice satisfies the given condition.

func (*SliceAssertion[T]) Contains

func (a *SliceAssertion[T]) Contains(element T, msg ...string) *SliceAssertion[T]

Contains asserts that the slice contains the expected element.

func (*SliceAssertion[T]) ContainsSlice

func (a *SliceAssertion[T]) ContainsSlice(sub []T, msg ...string) *SliceAssertion[T]

ContainsSlice asserts that the slice contains the expected sub-slice.

func (*SliceAssertion[T]) Empty

func (a *SliceAssertion[T]) Empty(msg ...string) *SliceAssertion[T]

Empty asserts that the slice is empty.

func (*SliceAssertion[T]) Equal

func (a *SliceAssertion[T]) Equal(expect []T, msg ...string) *SliceAssertion[T]

Equal asserts that the slice is equal to the expected slice.

func (*SliceAssertion[T]) HasPrefix

func (a *SliceAssertion[T]) HasPrefix(prefix []T, msg ...string) *SliceAssertion[T]

HasPrefix asserts that the slice starts with the specified prefix.

func (*SliceAssertion[T]) HasSuffix

func (a *SliceAssertion[T]) HasSuffix(suffix []T, msg ...string) *SliceAssertion[T]

HasSuffix asserts that the slice ends with the specified suffix.

func (*SliceAssertion[T]) Length

func (a *SliceAssertion[T]) Length(length int, msg ...string) *SliceAssertion[T]

Length asserts that the slice has the expected length.

func (*SliceAssertion[T]) Nil

func (a *SliceAssertion[T]) Nil(msg ...string) *SliceAssertion[T]

Nil asserts that the slice is nil.

func (*SliceAssertion[T]) NoneMatches

func (a *SliceAssertion[T]) NoneMatches(fn func(T) bool, msg ...string) *SliceAssertion[T]

NoneMatches asserts that no element in the slice satisfies the given condition.

func (*SliceAssertion[T]) NotContains

func (a *SliceAssertion[T]) NotContains(element T, msg ...string) *SliceAssertion[T]

NotContains asserts that the slice does not contain the expected element.

func (*SliceAssertion[T]) NotContainsSlice

func (a *SliceAssertion[T]) NotContainsSlice(sub []T, msg ...string) *SliceAssertion[T]

NotContainsSlice asserts that the slice does not contain the expected sub-slice.

func (*SliceAssertion[T]) NotEmpty

func (a *SliceAssertion[T]) NotEmpty(msg ...string) *SliceAssertion[T]

NotEmpty asserts that the slice is not empty.

func (*SliceAssertion[T]) NotEqual

func (a *SliceAssertion[T]) NotEqual(expect []T, msg ...string) *SliceAssertion[T]

NotEqual asserts that the slice is not equal to the expected slice.

func (*SliceAssertion[T]) NotNil

func (a *SliceAssertion[T]) NotNil(msg ...string) *SliceAssertion[T]

NotNil asserts that the slice is not nil.

type StringAssertion

type StringAssertion struct {
	AssertionBase[*StringAssertion]
	// contains filtered or unexported fields
}

StringAssertion encapsulates a string value and a test handler for making assertions on the string.

func ThatString

func ThatString(t internal.TestingT, v string) *StringAssertion

ThatString returns a StringAssertion for the given testing object and string value.

func (*StringAssertion) Blank

func (a *StringAssertion) Blank(msg ...string) *StringAssertion

Blank reports a test failure if the actual string is not blank (i.e., contains non-whitespace characters).

func (*StringAssertion) Contains

func (a *StringAssertion) Contains(substr string, msg ...string) *StringAssertion

Contains fails the test if the actual string does not contain the specified substring.

func (*StringAssertion) Equal

func (a *StringAssertion) Equal(expect string, msg ...string) *StringAssertion

Equal reports a test failure if the actual string is not equal to the expected string.

func (*StringAssertion) EqualFold

func (a *StringAssertion) EqualFold(expect string, msg ...string) *StringAssertion

EqualFold reports a test failure if the actual string and the given string are not equal under Unicode case-folding.

func (*StringAssertion) HasPrefix

func (a *StringAssertion) HasPrefix(prefix string, msg ...string) *StringAssertion

HasPrefix fails the test if the actual string does not start with the specified prefix.

func (*StringAssertion) HasSuffix

func (a *StringAssertion) HasSuffix(suffix string, msg ...string) *StringAssertion

HasSuffix fails the test if the actual string does not end with the specified suffix.

func (*StringAssertion) IsAlpha

func (a *StringAssertion) IsAlpha(msg ...string) *StringAssertion

IsAlpha reports a test failure if the actual string contains any non-alphabetic characters.

func (*StringAssertion) IsAlphaNumeric

func (a *StringAssertion) IsAlphaNumeric(msg ...string) *StringAssertion

IsAlphaNumeric reports a test failure if the actual string contains any non-alphanumeric characters.

func (*StringAssertion) IsBase64

func (a *StringAssertion) IsBase64(msg ...string) *StringAssertion

IsBase64 reports a test failure if the actual string is not a valid Base64 encoded string.

func (*StringAssertion) IsEmail

func (a *StringAssertion) IsEmail(msg ...string) *StringAssertion

IsEmail reports a test failure if the actual string is not a valid email address.

func (*StringAssertion) IsHex

func (a *StringAssertion) IsHex(msg ...string) *StringAssertion

IsHex reports a test failure if the actual string is not a valid hexadecimal number.

func (*StringAssertion) IsIPv4

func (a *StringAssertion) IsIPv4(msg ...string) *StringAssertion

IsIPv4 reports a test failure if the actual string is not a valid IPv4 address.

func (*StringAssertion) IsLowerCase

func (a *StringAssertion) IsLowerCase(msg ...string) *StringAssertion

IsLowerCase reports a test failure if the actual string contains any uppercase characters.

func (*StringAssertion) IsNumeric

func (a *StringAssertion) IsNumeric(msg ...string) *StringAssertion

IsNumeric reports a test failure if the actual string contains any non-numeric characters.

func (*StringAssertion) IsURL

func (a *StringAssertion) IsURL(msg ...string) *StringAssertion

IsURL reports a test failure if the actual string is not a valid URL.

func (*StringAssertion) IsUpperCase

func (a *StringAssertion) IsUpperCase(msg ...string) *StringAssertion

IsUpperCase reports a test failure if the actual string contains any lowercase characters.

func (*StringAssertion) JSONEqual

func (a *StringAssertion) JSONEqual(expect string, msg ...string) *StringAssertion

JSONEqual unmarshals both the actual and expected JSON strings into generic interfaces, then reports a test failure if their resulting structures are not deeply equal. If either string is invalid JSON, the test will fail with the unmarshal error.

func (*StringAssertion) Length

func (a *StringAssertion) Length(length int, msg ...string) *StringAssertion

Length reports a test failure if the actual string's length is not equal to the expected length.

func (*StringAssertion) Matches

func (a *StringAssertion) Matches(pattern string, msg ...string) *StringAssertion

Matches reports a test failure if the actual string does not match the given regular expression.

func (*StringAssertion) NotBlank

func (a *StringAssertion) NotBlank(msg ...string) *StringAssertion

NotBlank reports a test failure if the actual string is blank (i.e., empty or contains only whitespace characters).

func (*StringAssertion) NotEqual

func (a *StringAssertion) NotEqual(expect string, msg ...string) *StringAssertion

NotEqual reports a test failure if the actual string is equal to the given string.

Jump to

Keyboard shortcuts

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