action

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContextKeyUnmarshalActionValue is a context key for the unmarshal action value.
	ContextKeyUnmarshalActionValue contextKey = iota
)

Variables

View Source
var (
	// ErrUnmarshal	when unmarshalling the response
	ErrUnmarshal = errors.New("unmarshal error")

	// ErrActionNotApplied when action is not applied
	ErrActionNotApplied = errors.New("action not applied")

	// ErrAction when action is not applied
	ErrAction = errors.New("action")

	// ErrNotPresent is passed from tester when value is not present
	// This is used in some assertions such as AssertExists.
	ErrNotPresent = errors.New("not present")
)
View Source
var (
	ErrActionAssertBetween       = newErrAction("AssertBetween")
	ErrActionAssertEquals        = newErrAction("AssertEquals")
	ErrActionAssertExists        = newErrAction("AssertExists")
	ErrActionAssertIn            = newErrAction("AssertIn")
	ErrActionAssertKeys          = newErrAction("AssertKeys")
	ErrActionAssertLen           = newErrAction("AssertLen")
	ErrActionAssertGt            = newErrAction("AssertGt")
	ErrActionAssertGte           = newErrAction("AssertGte")
	ErrActionAssertLt            = newErrAction("AssertLt")
	ErrActionAssertLte           = newErrAction("AssertLte")
	ErrActionAssertAll           = newErrAction("AssertAll")
	ErrActionAssertAny           = newErrAction("AssertAny")
	ErrActionAssertRegexMatch    = newErrAction("ErrActionAssertRegexMatch")
	ErrActionAssertRegexSearch   = newErrAction("ErrActionAssertRegexSearch")
	ErrActionUnmarshal           = newErrAction("Unmarshal")
	ErrActionUnmarshalObjectKeys = newErrAction("UnmarshalObjectKeys")
	ErrActionAssertNot           = newErrAction("AssertNot")
	ErrActionAssertContains      = newErrAction("AssertContains")
	ErrActionAssertNil           = newErrAction("AssertNil")
	ErrActionPath                = newErrAction("Path")
)

Functions

This section is empty.

Types

type Action

type Action interface {
	Run(t require.TestingT, ctx context.Context, value any, raw json.RawMessage, err error) error
	Value(t require.TestingT) (any, bool)
	Supports(support Support) bool
	BaseError() error
}

Action is an action that can be performed on the response. It has currently 2 meanings: Assertion and Unmarshal. TODO: add path or id to run so we can print better error messages?

func AssertAll

func AssertAll(a ...Action) Action

AssertAll asserts that all actions are executed

func AssertAny

func AssertAny(a ...Action) Action

AssertAny asserts that any action succeeded

func AssertBetween added in v0.6.0

func AssertBetween[T comparable](min, max T) Action

AssertBetween asserts that given integer value is between the values (inclusive)

func AssertContains added in v0.6.0

func AssertContains(contains string) Action

AssertContains asserts json raw message contains the given string

func AssertEquals

func AssertEquals(value any) Action

AssertEquals asserts that given value is equal to the value in the response

func AssertExists

func AssertExists() Action

AssertExists asserts that the value exists in the response based on given argument

func AssertGt

func AssertGt[T comparable](value T) Action

AssertGt asserts that given value is greater than the value in the response

func AssertGte

func AssertGte[T comparable](value T) Action

AssertGte asserts that given value is greater than or equal to the value in the response

func AssertIn

func AssertIn[T any](values ...T) Action

AssertIn asserts that value is in the given list of values

func AssertJsonPath added in v0.6.0

func AssertJsonPath(path string, action Action) Action

AssertJsonPath matches the JSON sub path. This is only used in advanced scenarios.

func AssertKeys

func AssertKeys(keys ...string) Action

AssertKeys asserts that given map has the given keys

func AssertKeysIn added in v0.6.0

func AssertKeysIn(keys ...string) Action

AssertKeysIn asserts that given map has the given keys

func AssertLen

func AssertLen[T constraints.Integer](l T) Action

AssertLen asserts that given array/object has the given length

func AssertLt

func AssertLt[T comparable](value T) Action

AssertLt asserts that given value is less than the value in the response

func AssertLte

func AssertLte[T comparable](value T) Action

AssertLte asserts that given value is less than or equal to the value in the response

func AssertNil added in v0.6.0

func AssertNil() Action

AssertNil asserts that given value is nil

func AssertNot added in v0.6.0

func AssertNot(a Action) Action

AssertNot asserts that given action does not succeed, otherwise it fails This assertion does not need any special handling, it just relies on correct errors (ErrAction, ErrNotPresent)

func AssertRegexMatch added in v0.6.0

func AssertRegexMatch(pattern *regexp.Regexp) Action

AssertRegexMatch asserts that given value matches the regex in the response if count provided it will check for the number of matches, otherwise it will check if the value matches the regex

func AssertRegexSearch added in v0.6.0

func AssertRegexSearch(pattern *regexp.Regexp, count int) Action

AssertRegexSearch asserts that given regular expression is found given number of times

func AssertZero added in v0.6.0

func AssertZero() Action

AssertZero asserts that given value is zero value It checks json.RawMessage for zero values

func Print added in v0.6.0

func Print(target ...io.Writer) Action

Print is an action that prints the value and error to the given writer. By default, it prints to os.Stdout.

func Unmarshal

func Unmarshal(into any) Action

Unmarshal action unmarshal the given json response to the given value

func UnmarshalObjectKeys

func UnmarshalObjectKeys(keys KV) Action

UnmarshalObjectKeys action unmarshal the given json response object keys to the given values kv should be pairs of string key and addressable value

type KV

type KV map[string]any

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Number constraint for number types

type Support

type Support int

Support is a type that represents the support level of an action.

const (
	SupportHeader Support = iota
	SupportJson
	SupportStatus
)

Support levels for actions.

Jump to

Keyboard shortcuts

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