Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

expect source pure

Package expect provides testing support for packages and realms.

Overview

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}

Constants 1

Variables 1

var ErrIncompatibleType

1var ErrIncompatibleType = errors.New("incompatible type")
source

ErrIncompatibleType indicates that a value can't be casted to a different type.

Functions 11

func NewBooleanChecker

1func NewBooleanChecker(ctx Context, value bool) BooleanChecker
source

NewBooleanChecker creates a new checker of boolean values

func NewContext

1func NewContext(t TestingT) Context
source

NewContext creates a new testing context.

func NewErrorChecker

1func NewErrorChecker(ctx Context, err error) ErrorChecker
source

NewErrorChecker creates a new checker of errors.

func NewFloatChecker

1func NewFloatChecker(ctx Context, value float64) FloatChecker
source

NewFloatChecker creates a new checker of float64 values.

func Func

1func Func(t TestingT, fn any) FuncChecker
source

Func creates a new checker for functions.

func NewIntChecker

1func NewIntChecker(ctx Context, value int64) IntChecker
source

NewIntChecker creates a new checker of int64 values.

func NewMessageChecker

1func NewMessageChecker(ctx Context, msg string, t MessageType) MessageChecker
source

NewMessageChecker creates a new checker for text messages.

func NewStringChecker

1func NewStringChecker(ctx Context, value string) StringChecker
source

NewStringChecker creates a new checker of string values.

func MockTestingT

1func MockTestingT(output *strings.Builder) TestingT
source

MockTestingT creates a new testing mock that writes testing output to a string builder.

func NewUintChecker

1func NewUintChecker(ctx Context, value uint64) UintChecker
source

NewUintChecker creates a new checker of uint64 values.

func Value

1func Value(t TestingT, value any) ValueChecker
source

Value creates a new checker values of different types.

Types 17

type AnyErrorFn

func
1type AnyErrorFn = func() (any, error)
source

AnyErrorFn defines a type for generic functions that return a value and an error.

type AnyFn

func
1type AnyFn = func() any
source

AnyFn defines a type for generic functions that returns a value.

type BooleanChecker

struct
1type BooleanChecker struct {
2	ctx   Context
3	value bool
4}
source

BooleanChecker asserts boolean values.

Methods on BooleanChecker

func Not

method on BooleanChecker
1func (c BooleanChecker) Not() BooleanChecker
source

Not negates the next called expectation.

func ToBeFalsy

method on BooleanChecker
1func (c BooleanChecker) ToBeFalsy()
source

ToBeFalsy asserts that current value is falsy.

func ToBeTruthy

method on BooleanChecker
1func (c BooleanChecker) ToBeTruthy()
source

ToBeTruthy asserts that current value is truthy.

func ToEqual

method on BooleanChecker
1func (c BooleanChecker) ToEqual(v bool)
source

ToEqual asserts that current value is equal to an expected value.

type Context

struct
1type Context struct {
2	t       TestingT
3	negated bool
4	prefix  string
5}
source

Context preserves the current testing context.

Methods on Context

func CheckExpectation

method on Context
1func (c Context) CheckExpectation(success bool, cb func(Context) string) bool
source

CheckExpectation 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 Context
1func (c Context) Fail(msg string, args ...any)
source

Fail makes the current test fail with a custom message.

func IsNegated

method on Context
1func (c Context) IsNegated() bool
source

IsNegated checks if current context negates current assert expectations.

func Prefix

method on Context
1func (c Context) Prefix() string
source

Prefix returns context's error prefix.

func T

method on Context
1func (c Context) T() TestingT
source

T returns context's testing T instance.

type ErrorChecker

struct
1type ErrorChecker struct {
2	ctx Context
3	err error
4}
source

ErrorChecker asserts error values.

Methods on ErrorChecker

func Not

method on ErrorChecker
1func (c ErrorChecker) Not() ErrorChecker
source

Not negates the next called expectation.

func WithError

method on ErrorChecker
1func (c ErrorChecker) WithError(err error)
source

WithError asserts that current error message is the same as an expected error.

func WithMessage

method on ErrorChecker
1func (c ErrorChecker) WithMessage(msg string)
source

WithMessage asserts that current error contains an expected message.

type ErrorFn

func
1type ErrorFn = func() error
source

ErrorFn defines a type for generic functions that return an error.

type FloatChecker

struct
1type FloatChecker struct {
2	ctx   Context
3	value float64
4}
source

FloatChecker asserts float64 values.

Methods on FloatChecker

func Not

method on FloatChecker
1func (c FloatChecker) Not() FloatChecker
source

Not negates the next called expectation.

func ToBeGreaterOrEqualThan

method on FloatChecker
1func (c FloatChecker) ToBeGreaterOrEqualThan(value float64)
source

ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.

func ToBeGreaterThan

method on FloatChecker
1func (c FloatChecker) ToBeGreaterThan(value float64)
source

ToBeGreaterThan asserts that current value is greater than an expected value.

func ToBeLowerOrEqualThan

method on FloatChecker
1func (c FloatChecker) ToBeLowerOrEqualThan(value float64)
source

ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.

func ToBeLowerThan

method on FloatChecker
1func (c FloatChecker) ToBeLowerThan(value float64)
source

ToBeLowerThan asserts that current value is lower than an expected value.

func ToEqual

method on FloatChecker
1func (c FloatChecker) ToEqual(value float64)
source

ToEqual asserts that current value is equal to an expected value.

type Fn

func
1type Fn = func()
source

Fn defines a type for generic functions.

type FuncChecker

struct
1type FuncChecker struct {
2	ctx Context
3	fn  any
4}
source

FuncChecker asserts function panics, errors and returned value.

Methods on FuncChecker

func Not

method on FuncChecker
1func (c FuncChecker) Not() FuncChecker
source

Not negates the next called expectation.

func ToCrossPanic

method on FuncChecker
1func (c FuncChecker) ToCrossPanic() MessageChecker
source

ToCrossPanic 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 FuncChecker
1func (c FuncChecker) ToFail() ErrorChecker
source

ToFail return an error checker to assert if current function returns an error.

func ToPanic

method on FuncChecker
1func (c FuncChecker) ToPanic() MessageChecker
source

ToPanic 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:

Example
1func TestFoo(t *testing.T) {
2  expect.Func(t, func() {
3    Foo(cross)
4  }).Not().ToCrossPanic()
5}

func ToReturn

method on FuncChecker
1func (c FuncChecker) ToReturn(value any)
source

ToReturn asserts that current function returned a value equal to an expected value.

func WithFailPrefix

method on FuncChecker
1func (c FuncChecker) WithFailPrefix(prefix string) FuncChecker
source

WithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.

type IntChecker

struct
1type IntChecker struct {
2	ctx   Context
3	value int64
4}
source

IntChecker asserts int64 values.

Methods on IntChecker

func Not

method on IntChecker
1func (c IntChecker) Not() IntChecker
source

Not negates the next called expectation.

func ToBeGreaterOrEqualThan

method on IntChecker
1func (c IntChecker) ToBeGreaterOrEqualThan(value int64)
source

ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.

func ToBeGreaterThan

method on IntChecker
1func (c IntChecker) ToBeGreaterThan(value int64)
source

ToBeGreaterThan asserts that current value is greater than an expected value.

func ToBeLowerOrEqualThan

method on IntChecker
1func (c IntChecker) ToBeLowerOrEqualThan(value int64)
source

ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.

func ToBeLowerThan

method on IntChecker
1func (c IntChecker) ToBeLowerThan(value int64)
source

ToBeLowerThan asserts that current value is lower than an expected value.

func ToEqual

method on IntChecker
1func (c IntChecker) ToEqual(value int64)
source

ToEqual asserts that current value is equal to an expected value.

type MessageChecker

struct
1type MessageChecker struct {
2	ctx     Context
3	msg     string
4	msgType MessageType
5}
source

MessageChecker asserts text messages.

Methods on MessageChecker

func Not

method on MessageChecker
1func (c MessageChecker) Not() MessageChecker
source

Not negates the next called expectation.

func WithMessage

method on MessageChecker
1func (c MessageChecker) WithMessage(msg string)
source

WithMessage asserts that a message is equal to an expected message.

type MessageType

ident
1type MessageType string
source

MessageType defines a type for message checker errors.

type StringChecker

struct
1type StringChecker struct {
2	ctx   Context
3	value string
4}
source

StringChecker asserts string values.

Methods on StringChecker

func Not

method on StringChecker
1func (c StringChecker) Not() StringChecker
source

Not negates the next called expectation.

func ToBeEmpty

method on StringChecker
1func (c StringChecker) ToBeEmpty()
source

ToBeEmpty asserts that current value is an empty string.

func ToEqual

method on StringChecker
1func (c StringChecker) ToEqual(v string)
source

ToEqual asserts that current value is equal to an expected value.

func ToHaveLength

method on StringChecker
1func (c StringChecker) ToHaveLength(length int)
source

ToHaveLength asserts that current value has an expected length.

type Stringer

interface
1type Stringer interface {
2	String() string
3}
source

Stringer defines an interface for values that has a String method.

type TestingT

interface
1type TestingT interface {
2	Helper()
3	Fatal(args ...any)
4	Fatalf(format string, args ...any)
5}
source

TestingT defines a minimal interface for `testing.T` instances.

type UintChecker

struct
1type UintChecker struct {
2	ctx   Context
3	value uint64
4}
source

UintChecker asserts uint64 values.

Methods on UintChecker

func Not

method on UintChecker
1func (c UintChecker) Not() UintChecker
source

Not negates the next called expectation.

func ToBeGreaterOrEqualThan

method on UintChecker
1func (c UintChecker) ToBeGreaterOrEqualThan(value uint64)
source

ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.

func ToBeGreaterThan

method on UintChecker
1func (c UintChecker) ToBeGreaterThan(value uint64)
source

ToBeGreaterThan asserts that current value is greater than an expected value.

func ToBeLowerOrEqualThan

method on UintChecker
1func (c UintChecker) ToBeLowerOrEqualThan(value uint64)
source

ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.

func ToBeLowerThan

method on UintChecker
1func (c UintChecker) ToBeLowerThan(value uint64)
source

ToBeLowerThan asserts that current value is lower than an expected value.

func ToEqual

method on UintChecker
1func (c UintChecker) ToEqual(value uint64)
source

ToEqual asserts that current value is equal to an expected value.

type ValueChecker

struct
1type ValueChecker struct {
2	ctx   Context
3	value any
4}
source

ValueChecker asserts values of different types.

Methods on ValueChecker

func AsBoolean

method on ValueChecker
1func (c ValueChecker) AsBoolean() BooleanChecker
source

AsBoolean returns a checker to assert current value as a boolean.

func AsFloat

method on ValueChecker
1func (c ValueChecker) AsFloat() FloatChecker
source

AsFloat returns a checker to assert current value as a float64.

func AsInt

method on ValueChecker
1func (c ValueChecker) AsInt() IntChecker
source

AsInt returns a checker to assert current value as a int64.

func AsString

method on ValueChecker
1func (c ValueChecker) AsString() StringChecker
source

AsString returns a checker to assert current value as a string.

func AsUint

method on ValueChecker
1func (c ValueChecker) AsUint() UintChecker
source

AsUint returns a checker to assert current value as a uint64.

func Not

method on ValueChecker
1func (c ValueChecker) Not() ValueChecker
source

Not negates the next called expectation.

func ToBeNil

method on ValueChecker
1func (c ValueChecker) ToBeNil()
source

ToBeNil asserts that current value is nil.

func ToContainErrorString

method on ValueChecker
1func (c ValueChecker) ToContainErrorString(msg string)
source

ToContainErrorString asserts that current error value contains an error string.

func ToEqual

method on ValueChecker
1func (c ValueChecker) ToEqual(value any)
source

ToEqual asserts that current value is equal to an expected value.

func WithFailPrefix

method on ValueChecker
1func (c ValueChecker) WithFailPrefix(prefix string) ValueChecker
source

WithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.

Imports 4

Source Files 57