assert

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package assert provides value assertions.

Index

Constants

This section is empty.

Variables

View Source
var AppendError = multierror.Append

AppendError is a helper function that will append more errors onto an Error in order to create a larger multi-error.

Functions

func RegisterCustomEqualer added in v0.7.0

func RegisterCustomEqualer(eq Equaler)

RegisterCustomEqualer appends eq as a custom equaler. Registered equaler will be used when all default equalers judge two values are not equal.

Types

type Assertion

type Assertion interface {
	Assert(v interface{}) error
}

Assertion implements value assertion.

func And added in v0.5.1

func And(assertions ...Assertion) Assertion

And returns a new assertion to ensure that value passes all assertions. If the assertions are empty, it returns an error.

func Build

func Build(ctx context.Context, expect any, fs ...BuildOpt) (Assertion, error)

Build builds an assertion from Go value. If the Assert method of built assertion isn't called, the context value should be canceled to avoid a goroutine leak.

func Contains

func Contains(assertion Assertion) Assertion

Contains returns an assertion to ensure a value contains the value.

func Equal

func Equal(expected interface{}, customEqs ...Equaler) Assertion

Equal returns an assertion to ensure a value equals the expected value.

func Greater added in v0.6.2

func Greater(expected interface{}) Assertion

Greater returns an assertion to ensure a value greater than the expected value.

func GreaterOrEqual added in v0.6.2

func GreaterOrEqual(expected interface{}) Assertion

GreaterOrEqual returns an assertion to ensure a value equal or greater than the expected value.

func Length added in v0.6.2

func Length(expected interface{}) Assertion

Length returns an assertion to ensure a value length is the expected value.

func Less added in v0.6.2

func Less(expected interface{}) Assertion

Less returns an assertion to ensure a value less than the expected value.

func LessOrEqual added in v0.6.2

func LessOrEqual(expected interface{}) Assertion

LessOrEqual returns an assertion to ensure a value equal or less than the expected value.

func MustBuild added in v0.16.0

func MustBuild(ctx context.Context, expect any, fs ...BuildOpt) Assertion

MustBuild builds an assertion from Go value. If the Assert method of built assertion isn't called, the context value should be canceled to avoid a goroutine leak. If it fails to build, creates an assertion function that returns the build error.

func NotContains

func NotContains(assertion Assertion) Assertion

NotContains returns an assertion to ensure a value doesn't contain the value.

func NotZero

func NotZero() Assertion

NotZero returns an assertion to ensure a value is not zero value.

func Or added in v0.5.0

func Or(assertions ...Assertion) Assertion

Or returns new assertion to ensure that value passes at least one of assertions. If the assertions are empty, it returns an error.

func Regexp added in v0.6.0

func Regexp(expr string) Assertion

Regexp returns an assertion to ensure a value matches the regular expression pattern.

type AssertionFunc

type AssertionFunc func(v interface{}) error

AssertionFunc is an adaptor to allow the use of ordinary functions as assertions.

func (AssertionFunc) Assert

func (f AssertionFunc) Assert(v interface{}) error

Assert asserts the v.

type BuildOpt added in v0.16.0

type BuildOpt func(*buildOpt)

BuildOpt represents an option for Build().

func FromTemplate added in v0.16.0

func FromTemplate(data any) BuildOpt

FromTemplate is a build option that executes templates before building assertions.

func WithEqualers added in v0.16.0

func WithEqualers(eqs ...Equaler) BuildOpt

WithEqualers is a build option that enables custom equalers.

type Equaler added in v0.7.0

type Equaler interface {
	// Equal checks two values are equal or not.
	// If the ok is true, the err should be used as result.
	Equal(expected, got interface{}) (ok bool, err error)
}

Equaler is the interface for custom equaler.

func EqualerFunc added in v0.7.0

func EqualerFunc(eq func(interface{}, interface{}) (bool, error)) Equaler

EqualerFunc is an adaptor to allow the use of ordinary functions as Equaler.

type Error

type Error = multierror.Error

Error is an error type to track multiple errors.

Jump to

Keyboard shortcuts

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