Documentation
¶
Overview ¶
Package assert provides the underlying implementation for the Should assertion library. It contains the core assertion logic, which is then exposed through the top-level `should` package. This package handles value comparisons, error formatting, and detailed difference reporting.
Index ¶
- func BeEmpty[T any](t testing.TB, actual T, config ...AssertionConfig)
- func BeEqual[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
- func BeFalse[T any](t testing.TB, actual T, config ...AssertionConfig)
- func BeGreaterOrEqualThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
- func BeGreaterThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
- func BeLessThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
- func BeNil[T any](t testing.TB, actual T, config ...AssertionConfig)
- func BeNotEmpty[T any](t testing.TB, actual T, config ...AssertionConfig)
- func BeNotNil[T any](t testing.TB, actual T, config ...AssertionConfig)
- func BeTrue[T any](t testing.TB, actual T, config ...AssertionConfig)
- func Contain[T any](t testing.TB, actual T, expected any, config ...AssertionConfig)
- func ContainFunc[T any](t testing.TB, actual T, expected func(TItem any) bool, ...)
- func NotContain[T any](t testing.TB, actual T, expected any, config ...AssertionConfig)
- func NotPanic(t testing.TB, fn func(), config ...AssertionConfig)
- func Panic(t testing.TB, fn func(), config ...AssertionConfig)
- type Assertion
- type AssertionConfig
- type ContainResult
- type Ordered
- type SimilarItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BeEmpty ¶
func BeEmpty[T any](t testing.TB, actual T, config ...AssertionConfig)
BeEmpty reports a test failure if the value is not empty.
This assertion works with strings, slices, arrays, maps, channels, and pointers. For strings, empty means zero length. For slices/arrays/maps/channels, empty means zero length. For pointers, empty means nil. Provides detailed error messages showing the type, length, and content of non-empty values.
Example:
should.BeEmpty(t, "")
should.BeEmpty(t, []int{}, should.AssertionConfig{Message: "List should be empty"})
should.BeEmpty(t, map[string]int{})
Only works with strings, slices, arrays, maps, channels, or pointers.
func BeEqual ¶
func BeEqual[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
BeEqual reports a test failure if the two values are not deeply equal.
This assertion uses Go's reflect.DeepEqual for comparison and provides detailed error messages showing exactly what differs between the values. For complex objects, it shows field-by-field differences to help identify the specific mismatches.
Example:
should.BeEqual(t, "hello", "hello")
should.BeEqual(t, 42, 42)
should.BeEqual(t, user, expectedUser, should.AssertionConfig{Message: "User objects should match"})
Works with any comparable types. Uses deep comparison for complex objects.
func BeFalse ¶
func BeFalse[T any](t testing.TB, actual T, config ...AssertionConfig)
BeFalse reports a test failure if the value is not false.
This assertion only works with boolean values and will fail immediately if the value is not a boolean type.
Example:
should.BeFalse(t, false)
should.BeFalse(t, user.IsDeleted, should.AssertionConfig{Message: "User should not be deleted"})
If the input is not a boolean, the test fails immediately.
func BeGreaterOrEqualThan ¶
func BeGreaterOrEqualThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
BeGreaterOrEqualThan reports a test failure if the value is not greater than or equal to the expected threshold.
This assertion works with all numeric types (int, float, etc.) and provides detailed error messages when the assertion fails. It supports optional custom error messages through AssertionConfig.
Example:
should.BeGreaterOrEqualThan(t, 10, 10)
should.BeGreaterOrEqualThan(t, user.Score, 0, should.AssertionConfig{Message: "Score cannot be negative"})
should.BeGreaterOrEqualThan(t, 3.14, 3.14)
Only works with numeric types. Both values must be numeric.
func BeGreaterThan ¶
func BeGreaterThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
BeGreaterThan reports a test failure if the value is not greater than the expected threshold.
This assertion works with all numeric types (int, float, etc.) and provides detailed error messages showing the actual value, threshold, difference, and helpful hints. It supports optional custom error messages through AssertionConfig.
Example:
should.BeGreaterThan(t, 10, 5)
should.BeGreaterThan(t, user.Age, 18, should.AssertionConfig{Message: "User must be adult"})
should.BeGreaterThan(t, 3.14, 2.71)
Only works with numeric types. Both values must be numeric.
func BeLessThan ¶
func BeLessThan[T any](t testing.TB, actual T, expected T, config ...AssertionConfig)
BeLessThan reports a test failure if the value is not less than the expected threshold.
This assertion works with all numeric types (int, float, etc.) and provides detailed error messages showing the actual value, threshold, difference, and helpful hints. It supports optional custom error messages through AssertionConfig.
Example:
should.BeLessThan(t, 5, 10)
should.BeLessThan(t, user.Age, 65, should.AssertionConfig{Message: "User must be under retirement age"})
should.BeLessThan(t, 2.71, 3.14)
Only works with numeric types. Both values must be numeric.
func BeNil ¶
func BeNil[T any](t testing.TB, actual T, config ...AssertionConfig)
BeNil reports a test failure if the value is not nil.
This assertion works with pointers, interfaces, channels, functions, slices, and maps. It uses Go's reflection to check if the value is nil.
Example:
var ptr *int
should.BeNil(t, ptr)
var slice []int
should.BeNil(t, slice, should.AssertionConfig{Message: "Slice should be nil"})
Only works with nillable types (pointers, interfaces, channels, functions, slices, maps).
func BeNotEmpty ¶
func BeNotEmpty[T any](t testing.TB, actual T, config ...AssertionConfig)
BeNotEmpty reports a test failure if the value is empty.
This assertion works with strings, slices, arrays, maps, channels, and pointers. For strings, non-empty means length > 0. For slices/arrays/maps/channels, non-empty means length > 0. For pointers, non-empty means not nil. Provides detailed error messages for empty values.
Example:
should.BeNotEmpty(t, "hello")
should.BeNotEmpty(t, []int{1, 2, 3}, should.AssertionConfig{Message: "List must have items"})
should.BeNotEmpty(t, &user)
Only works with strings, slices, arrays, maps, channels, or pointers.
func BeNotNil ¶
func BeNotNil[T any](t testing.TB, actual T, config ...AssertionConfig)
BeNotNil reports a test failure if the value is nil.
This assertion works with pointers, interfaces, channels, functions, slices, and maps. It uses Go's reflection to check if the value is not nil.
Example:
user := &User{Name: "John"}
should.BeNotNil(t, user, should.AssertionConfig{Message: "User must not be nil"})
should.BeNotNil(t, make([]int, 0))
Only works with nillable types (pointers, interfaces, channels, functions, slices, maps).
func BeTrue ¶
func BeTrue[T any](t testing.TB, actual T, config ...AssertionConfig)
BeTrue reports a test failure if the value is not true.
This assertion only works with boolean values and will fail immediately if the value is not a boolean type.
Example:
should.BeTrue(t, true)
should.BeTrue(t, user.IsActive, should.AssertionConfig{Message: "User must be active"})
If the input is not a boolean, the test fails immediately.
func Contain ¶
func Contain[T any](t testing.TB, actual T, expected any, config ...AssertionConfig)
Contain reports a test failure if the slice or array does not contain the expected value.
This assertion provides intelligent error messages based on the type of collection: - For []string: Shows similar elements and typo detection - For numeric slices ([]int, []float64, etc.): Shows insertion context and sorted position - For other types: Shows formatted collection with clear error messages Supports all slice and array types.
Example:
should.Contain(t, users, "user3")
should.Contain(t, []int{1, 2, 3}, 2)
should.Contain(t, []float64{1.1, 2.2}, 1.5, should.AssertionConfig{Message: "Expected value missing"})
should.Contain(t, []string{"apple", "banana"}, "apple")
If the input is not a slice or array, the test fails immediately.
func ContainFunc ¶
func ContainFunc[T any](t testing.TB, actual T, expected func(TItem any) bool, config ...AssertionConfig)
ContainFunc reports a test failure if no element in the slice or array matches the predicate function.
This assertion allows for custom matching logic by providing a predicate function that will be called for each element in the collection. The test passes if any element makes the predicate return true.
Example:
should.ContainFunc(t, users, func(item any) bool {
user := item.(User)
return user.Age > 18
})
should.ContainFunc(t, numbers, func(item any) bool {
return item.(int) % 2 == 0
}, should.AssertionConfig{Message: "No even numbers found"})
If the input is not a slice or array, the test fails immediately.
func NotContain ¶
func NotContain[T any](t testing.TB, actual T, expected any, config ...AssertionConfig)
NotContain reports a test failure if the slice or array contains the expected value.
This assertion works with slices and arrays of any type and provides detailed error messages showing where the unexpected element was found.
Example:
should.NotContain(t, users, "bannedUser")
should.NotContain(t, []int{1, 2, 3}, 4)
should.NotContain(t, []string{"apple", "banana"}, "orange", should.AssertionConfig{Message: "Should not have orange"})
If the input is not a slice or array, the test fails immediately.
func NotPanic ¶
func NotPanic(t testing.TB, fn func(), config ...AssertionConfig)
NotPanic reports a test failure if the given function panics.
This assertion executes the provided function and expects it to complete normally without panicking. If a panic occurs, it captures the panic value and includes it in the error message. Supports optional custom error messages through AssertionConfig.
Example:
should.NotPanic(t, func() {
result := add(1, 2)
_ = result
})
should.NotPanic(t, func() {
user.Save()
}, should.AssertionConfig{Message: "Save operation should not panic"})
The function parameter must not be nil.
func Panic ¶
func Panic(t testing.TB, fn func(), config ...AssertionConfig)
Panic reports a test failure if the given function does not panic.
This assertion executes the provided function and expects it to panic. It captures and recovers from the panic to prevent the test from crashing. Supports optional custom error messages through AssertionConfig.
Example:
should.Panic(t, func() {
panic("expected panic")
})
should.Panic(t, func() {
divide(1, 0)
}, should.AssertionConfig{Message: "Division by zero should panic"})
The function parameter must not be nil.
Types ¶
type Assertion ¶
type Assertion[T any] struct { // contains filtered or unexported fields }
Assertion is a struct that contains the value to be asserted.
type AssertionConfig ¶
type AssertionConfig struct {
Message string // Custom error message to display when assertion fails
}
AssertionConfig provides configuration options for assertions. It allows for custom error messages and future extensibility.
type ContainResult ¶
type ContainResult struct {
Found bool
Exact bool
Similar []SimilarItem
Context []interface{}
MaxShow int
Total int
}
ContainResult result of the contains search