expect source pure
Package expect provides testing support for packages and realms.
Package expect provides testing support for packages and realms.
The opinionated approach taken on this package for testing is to use function chaining and semanthics to hopefully make unit and file testing fun. Focus is not on speed as there are other packages that would run tests faster like the official `uassert` or `urequire` packages.
Values can be asserted using the `Value()` function, for example:
Example
1func TestFoo(t *testing.T) {
2 got := 42
3 expect.Value(t, got).ToEqual(42)
4 expect.Value(t, got).Not().ToEqual(0)
5
6 expect.Value(t, "foo").ToEqual("foo")
7 expect.Value(t, 42).AsInt().Not().ToBeGreaterThan(50)
8 expect.Value(t, "TRUE").AsBoolean().ToBeTruthy()
9}
Functions can also be used to assert returned values, errors or panics.
Package supports four type of functions:
- func()
- func() any
- func() error
- func() (any, error)
Functions can be asserted using the `Func()` function, for example:
Example
1func TestFoo(t *testing.T) {
2 expect.Func(t, func() {
3 panic("Boom!")
4 }).ToPanic().WithMessage("Boom!")
5
6 wantErr := errors.New("Boom!")
7 expect.Func(t, func() error {
8 return wantErr
9 }).ToFail().WithMessage("Boom!")
10
11 expect.Func(t, func() error {
12 return wantErr
13 }).ToFail().WithError(wantErr)
14}
1
1
var ErrIncompatibleType
ErrIncompatibleType indicates that a value can't be casted to a different type.
11
func NewBooleanChecker
NewBooleanChecker creates a new checker of boolean values
func NewContext
NewContext creates a new testing context.
func NewErrorChecker
NewErrorChecker creates a new checker of errors.
func NewFloatChecker
NewFloatChecker creates a new checker of float64 values.
func Func
Func creates a new checker for functions.
func NewIntChecker
NewIntChecker creates a new checker of int64 values.
func NewMessageChecker
NewMessageChecker creates a new checker for text messages.
func NewStringChecker
NewStringChecker creates a new checker of string values.
func MockTestingT
MockTestingT creates a new testing mock that writes testing output to a string builder.
func NewUintChecker
NewUintChecker creates a new checker of uint64 values.
func Value
Value creates a new checker values of different types.
17
type AnyErrorFn
funcAnyErrorFn defines a type for generic functions that return a value and an error.
type AnyFn
funcAnyFn defines a type for generic functions that returns a value.
type BooleanChecker
structBooleanChecker asserts boolean values.
Methods on BooleanChecker
func Not
method on BooleanCheckerNot negates the next called expectation.
func ToBeFalsy
method on BooleanCheckerToBeFalsy asserts that current value is falsy.
func ToBeTruthy
method on BooleanCheckerToBeTruthy asserts that current value is truthy.
func ToEqual
method on BooleanCheckerToEqual asserts that current value is equal to an expected value.
type Context
structContext preserves the current testing context.
Methods on Context
func CheckExpectation
method on ContextCheckExpectation checks an assert expectation and calls a callback on fail. It returns true when the asserted expectation fails. Callback is called when a negated assertion succeeds or when non negated assertion fails.
func Fail
method on ContextFail makes the current test fail with a custom message.
func IsNegated
method on ContextIsNegated checks if current context negates current assert expectations.
func Prefix
method on ContextPrefix returns context's error prefix.
func T
method on ContextT returns context's testing T instance.
type ErrorChecker
structErrorChecker asserts error values.
Methods on ErrorChecker
func Not
method on ErrorCheckerNot negates the next called expectation.
func WithError
method on ErrorCheckerWithError asserts that current error message is the same as an expected error.
func WithMessage
method on ErrorCheckerWithMessage asserts that current error contains an expected message.
type ErrorFn
funcErrorFn defines a type for generic functions that return an error.
type FloatChecker
structFloatChecker asserts float64 values.
Methods on FloatChecker
func Not
method on FloatCheckerNot negates the next called expectation.
func ToBeGreaterOrEqualThan
method on FloatCheckerToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.
func ToBeGreaterThan
method on FloatCheckerToBeGreaterThan asserts that current value is greater than an expected value.
func ToBeLowerOrEqualThan
method on FloatCheckerToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.
func ToBeLowerThan
method on FloatCheckerToBeLowerThan asserts that current value is lower than an expected value.
func ToEqual
method on FloatCheckerToEqual asserts that current value is equal to an expected value.
type Fn
funcFn defines a type for generic functions.
type FuncChecker
structFuncChecker asserts function panics, errors and returned value.
Methods on FuncChecker
func Not
method on FuncCheckerNot negates the next called expectation.
func ToCrossPanic
method on FuncCheckerToCrossPanic return an message checker to assert if current function panicked when crossing. This assertion is handled only when making a crossing call to another realm, when asserting within the same realm use `ToPanic()`.
func ToFail
method on FuncCheckerToFail return an error checker to assert if current function returns an error.
func ToPanic
method on FuncCheckerToPanic return an message checker to assert if current function panicked. This assertion is handled within the same realm, to assert panics when crossing to another realm use the `ToAbort()` assertion.
Example usage:
func ToReturn
method on FuncCheckerToReturn asserts that current function returned a value equal to an expected value.
func WithFailPrefix
method on FuncCheckerWithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.
type IntChecker
structIntChecker asserts int64 values.
Methods on IntChecker
func Not
method on IntCheckerNot negates the next called expectation.
func ToBeGreaterOrEqualThan
method on IntCheckerToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.
func ToBeGreaterThan
method on IntCheckerToBeGreaterThan asserts that current value is greater than an expected value.
func ToBeLowerOrEqualThan
method on IntCheckerToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.
func ToBeLowerThan
method on IntCheckerToBeLowerThan asserts that current value is lower than an expected value.
func ToEqual
method on IntCheckerToEqual asserts that current value is equal to an expected value.
type MessageChecker
structMessageChecker asserts text messages.
Methods on MessageChecker
func Not
method on MessageCheckerNot negates the next called expectation.
func WithMessage
method on MessageCheckerWithMessage asserts that a message is equal to an expected message.
type MessageType
identMessageType defines a type for message checker errors.
type StringChecker
structStringChecker asserts string values.
Methods on StringChecker
func Not
method on StringCheckerNot negates the next called expectation.
func ToBeEmpty
method on StringCheckerToBeEmpty asserts that current value is an empty string.
func ToEqual
method on StringCheckerToEqual asserts that current value is equal to an expected value.
func ToHaveLength
method on StringCheckerToHaveLength asserts that current value has an expected length.
type Stringer
interfaceStringer defines an interface for values that has a String method.
type TestingT
interfaceTestingT defines a minimal interface for `testing.T` instances.
type UintChecker
structUintChecker asserts uint64 values.
Methods on UintChecker
func Not
method on UintCheckerNot negates the next called expectation.
func ToBeGreaterOrEqualThan
method on UintCheckerToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.
func ToBeGreaterThan
method on UintCheckerToBeGreaterThan asserts that current value is greater than an expected value.
func ToBeLowerOrEqualThan
method on UintCheckerToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.
func ToBeLowerThan
method on UintCheckerToBeLowerThan asserts that current value is lower than an expected value.
func ToEqual
method on UintCheckerToEqual asserts that current value is equal to an expected value.
type ValueChecker
structValueChecker asserts values of different types.
Methods on ValueChecker
func AsBoolean
method on ValueCheckerAsBoolean returns a checker to assert current value as a boolean.
func AsFloat
method on ValueCheckerAsFloat returns a checker to assert current value as a float64.
func AsInt
method on ValueCheckerAsInt returns a checker to assert current value as a int64.
func AsString
method on ValueCheckerAsString returns a checker to assert current value as a string.
func AsUint
method on ValueCheckerAsUint returns a checker to assert current value as a uint64.
func Not
method on ValueCheckerNot negates the next called expectation.
func ToBeNil
method on ValueCheckerToBeNil asserts that current value is nil.
func ToContainErrorString
method on ValueCheckerToContainErrorString asserts that current error value contains an error string.
func ToEqual
method on ValueCheckerToEqual asserts that current value is equal to an expected value.
func WithFailPrefix
method on ValueCheckerWithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.
4
- errors stdlib
- gno.land/p/nt/ufmt/v0 package
- strconv stdlib
- strings stdlib