assert

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package assert parses and evaluates `# @assert` expressions: `<selector> <op> <value>`, `<selector> exists` / `not exists`, a predicate such as `<selector> isInteger` or `not isEmpty`, `<selector> length <op> <n>` and `<selector> matchesSchema <file>`.

Index

Constants

This section is empty.

Variables

View Source
var Operators = []string{"==", "!=", "<=", ">=", "<", ">", "contains", "matches", "startsWith", "endsWith", "matchesSchema", "exists", "not exists"}

Operators lists the supported comparison operators.

View Source
var Predicates = []string{"isString", "isNumber", "isInteger", "isBoolean", "isArray", "isObject", "isNull", "isEmpty"}

Predicates take no value: `body.$.id isInteger`, `body.$.items not isEmpty`. Each also has a `not` form.

Functions

func ParseNumber

func ParseNumber(s string) (*big.Rat, bool)

ParseNumber reads a decimal number exactly, so large integers such as 9007199254740993 keep their value instead of rounding through float64, and values beyond float64's range (1e1000) still compare as numbers. Words, Inf, NaN, fractions like 1/2 and numbers with more than 4096 digits or an exponent beyond ±4096 are not numbers here and compare as text.

func Redact

func Redact(expr, mask string) string

Redact keeps an assertion's selector and operator and replaces its value with mask. The selector is read the way Parse reads it, so a filter with spaces stays whole. What Parse cannot read is masked after its first two words, so a secret never shows because the expression around it was malformed.

func Schemas

func Schemas(read func(path string) ([]byte, error)) func(path string) (*jsonschema.Resolved, error)

Schemas returns a Schema loader that reads each file with read, then parses and resolves it once: every later assertion naming the same file, and every retry of the request, gets the resolved schema (or the same error) without reading it again. It is not safe for concurrent use.

Types

type Expr

type Expr struct {
	Selector string
	// Op is the operator, a predicate (`isString`, `not isEmpty`) or
	// `matchesSchema`, whose Value is the schema file.
	Op    string
	Value string // raw right-hand side; may contain {{placeholders}}
	// Length compares the selected value's length (characters, elements
	// or keys) rather than the value: `body.$.items length == 3`.
	Length bool
}

Expr is a parsed assertion.

func Parse

func Parse(expr string) (Expr, error)

Parse splits an expression into selector, operator and value.

func (Expr) Display

func (e Expr) Display(expected string) string

Display is how an expression reads in results: the selector, the operator and the rendered value.

func (Expr) Unary

func (e Expr) Unary() bool

Unary reports an operator that takes no value.

type Options

type Options struct {
	// Schema returns the JSON Schema named by `matchesSchema`, read from
	// a file resolved and confined the way the caller's files are; build
	// it with Schemas. Nil refuses the operator.
	Schema func(path string) (*jsonschema.Resolved, error)
}

Options carries what evaluation may need beyond the response.

type Result

type Result struct {
	Expr     string `json:"expr"`
	Pass     bool   `json:"pass"`
	Actual   string `json:"actual,omitempty"`
	Expected string `json:"expected,omitempty"`
	Error    string `json:"error,omitempty"`
}

Result is the outcome of evaluating one assertion.

func Eval

func Eval(e Expr, expected string, resp *selector.Response) Result

Eval evaluates a parsed expression against a response. expected is the right-hand side after template rendering.

func EvalWith

func EvalWith(e Expr, expected string, resp *selector.Response, opts Options) Result

EvalWith is Eval with what matchesSchema needs.

Jump to

Keyboard shortcuts

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