Documentation
¶
Index ¶
- func NotPanic(t testing.TB, fn func(), msgAndArgs ...interface{})
- func Panic(t testing.TB, fn func(), msgAndArgs ...interface{})
- type Assertion
- func (a *Assertion[T]) BeEmpty(t testing.TB)
- func (a *Assertion[T]) BeEqual(t testing.TB, expected T, msgAndArgs ...interface{})
- func (a *Assertion[T]) BeFalse(t testing.TB)
- func (a *Assertion[T]) BeGreaterOrEqualThan(t testing.TB, expected T, msgAndArgs ...interface{})
- func (a *Assertion[T]) BeGreaterThan(t testing.TB, expected T, msgAndArgs ...interface{})
- func (a *Assertion[T]) BeLessThan(t testing.TB, expected T, msgAndArgs ...interface{})
- func (a *Assertion[T]) BeNil(t testing.TB)
- func (a *Assertion[T]) BeNotEmpty(t testing.TB)
- func (a *Assertion[T]) BeNotNil(t testing.TB)
- func (a *Assertion[T]) BeTrue(t testing.TB)
- func (a *Assertion[T]) Contain(t testing.TB, expected any, msgAndArgs ...interface{})
- func (a *Assertion[T]) ContainFunc(t testing.TB, expected func(TItem any) bool, msgAndArgs ...interface{})
- func (a *Assertion[T]) NotContain(t testing.TB, expected any, msgAndArgs ...interface{})
- type ContainResult
- type Ordered
- type SimilarItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NotPanic ¶
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.
Example:
should.NotPanic(t, func() {
result := add(1, 2)
_ = result
})
should.NotPanic(t, func() {
user.Save()
}, "Save operation should not panic")
The function parameter must not be nil.
func Panic ¶
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.
Example:
should.Panic(t, func() {
panic("expected panic")
})
should.Panic(t, func() {
divide(1, 0)
}, "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.
func Ensure ¶
Ensure creates a new assertion for the given value.
This is the entry point for all assertions in the Should library. It returns an Assertion object that provides fluent assertion methods.
Example:
should.Ensure(42).BeEqual(t, 42)
should.Ensure("hello").BeNotEmpty(t)
should.Ensure([]int{1, 2, 3}).Contain(t, 2)
func (*Assertion[T]) BeEmpty ¶
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.Ensure("").BeEmpty(t)
should.Ensure([]int{}).BeEmpty(t)
should.Ensure(map[string]int{}).BeEmpty(t)
Only works with strings, slices, arrays, maps, channels, or pointers.
func (*Assertion[T]) BeEqual ¶
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.Ensure("hello").BeEqual(t, "hello")
should.Ensure(42).BeEqual(t, 42)
should.Ensure(user).BeEqual(t, expectedUser)
Works with any comparable types. Uses deep comparison for complex objects.
func (*Assertion[T]) BeFalse ¶
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.Ensure(false).BeFalse(t) should.Ensure(user.IsDeleted).BeFalse(t)
If the input is not a boolean, the test fails immediately.
func (*Assertion[T]) BeGreaterOrEqualThan ¶
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.
Example:
should.Ensure(10).BeGreaterOrEqualThan(t, 10) should.Ensure(user.Score).BeGreaterOrEqualThan(t, 0, "Score cannot be negative") should.Ensure(3.14).BeGreaterOrEqualThan(t, 3.14)
Only works with numeric types. Both values must be numeric.
func (*Assertion[T]) BeGreaterThan ¶
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.
Example:
should.Ensure(10).BeGreaterThan(t, 5) should.Ensure(user.Age).BeGreaterThan(t, 18, "User must be adult") should.Ensure(3.14).BeGreaterThan(t, 2.71)
Only works with numeric types. Both values must be numeric.
func (*Assertion[T]) BeLessThan ¶
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.
Example:
should.Ensure(5).BeLessThan(t, 10) should.Ensure(user.Age).BeLessThan(t, 65, "User must be under retirement age") should.Ensure(2.71).BeLessThan(t, 3.14)
Only works with numeric types. Both values must be numeric.
func (*Assertion[T]) BeNil ¶
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.Ensure(ptr).BeNil(t) var slice []int should.Ensure(slice).BeNil(t)
Only works with nillable types (pointers, interfaces, channels, functions, slices, maps).
func (*Assertion[T]) BeNotEmpty ¶
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.Ensure("hello").BeNotEmpty(t)
should.Ensure([]int{1, 2, 3}).BeNotEmpty(t)
should.Ensure(&user).BeNotEmpty(t)
Only works with strings, slices, arrays, maps, channels, or pointers.
func (*Assertion[T]) BeNotNil ¶
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.Ensure(user).BeNotNil(t)
should.Ensure(make([]int, 0)).BeNotNil(t)
Only works with nillable types (pointers, interfaces, channels, functions, slices, maps).
func (*Assertion[T]) BeTrue ¶
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.Ensure(true).BeTrue(t) should.Ensure(user.IsActive).BeTrue(t)
If the input is not a boolean, the test fails immediately.
func (*Assertion[T]) Contain ¶
Contain reports a test failure if the slice or array does not contain the expected value.
If the value is a []string and the expected is a string, it provides more detailed output to help identify similar or near-matching elements. For []int, it shows insertion context to help understand where the missing value would fit.
Example:
should.Ensure(users).Contain(t, "user3")
should.Ensure([]int{1, 2, 3}).Contain(t, 2)
should.Ensure([]string{"apple", "banana"}).Contain(t, "apple")
If the input is not a slice or array, the test fails immediately.
func (*Assertion[T]) ContainFunc ¶
func (a *Assertion[T]) ContainFunc(t testing.TB, expected func(TItem any) bool, msgAndArgs ...interface{})
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.Ensure(users).ContainFunc(t, func(item any) bool {
user := item.(User)
return user.Age > 18
})
should.Ensure(numbers).ContainFunc(t, func(item any) bool {
return item.(int) % 2 == 0
})
If the input is not a slice or array, the test fails immediately.
func (*Assertion[T]) NotContain ¶
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.Ensure(users).NotContain(t, "bannedUser")
should.Ensure([]int{1, 2, 3}).NotContain(t, 4)
should.Ensure([]string{"apple", "banana"}).NotContain(t, "orange")
If the input is not a slice or array, the test fails immediately.
type ContainResult ¶
type ContainResult struct {
Found bool
Exact bool
Similar []SimilarItem
Context []interface{}
MaxShow int
Total int
}
ContainResult result of the contains search