constraints

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package constraints holds the reusable matchers the assertions evaluate.

A constraint answers three questions -- does this value match, what should a failure say, and what is this constraint called -- which is the whole of Constraint. Writing a comparison here rather than inside an assertion is what lets more than one assertion share it.

ArraySubset matches a value that carries at least what a subset names. SeeInOrder matches a string that carries several values, each after the last.

There is no database constraint here. Asking a connection whether a row is there belongs to hesape/arandutest, where the connection already is.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArraySubset

type ArraySubset struct {
	// Subset is what the value has to contain.
	Subset any

	// Strict asks for identity rather than a relaxed comparison. It records the
	// caller's choice; Equals is what actually performs it.
	Strict bool

	// Equals is the comparison, and defaults to reflect.DeepEqual when nil.
	//
	// It is a field because Go's == panics on maps and slices, and because it
	// holds int(1) and float64(1) apart -- which is the same JSON number
	// written twice.
	Equals func(a, b any) bool
}

ArraySubset matches a value that contains everything the subset names, and may contain more.

The test is to overlay the subset onto the value and compare: nothing changed means everything the subset asked for was already there.

func NewArraySubset

func NewArraySubset(subset any, strict bool) *ArraySubset

NewArraySubset returns a constraint over the given subset. Set Equals on the result to choose the comparison.

func (*ArraySubset) FailureDescription

func (c *ArraySubset) FailureDescription(other any) string

FailureDescription names the subset that was not carried.

func (*ArraySubset) Matches

func (c *ArraySubset) Matches(other any) bool

Matches reports whether the value carries everything the subset names.

It overlays the subset onto the value and compares the two with Equals, or with reflect.DeepEqual when Equals is nil.

func (*ArraySubset) String

func (c *ArraySubset) String() string

String names the constraint and the subset it looks for.

type Constraint

type Constraint interface {
	// Matches reports whether the value satisfies the constraint.
	Matches(other any) bool

	// FailureDescription is what a failure should say about the value. It is
	// read only after Matches has answered false.
	FailureDescription(other any) string

	// String names the constraint.
	String() string
}

Constraint is a reusable matcher. It is what an assertion evaluates a value against, and what it reads its failure message from.

Implement it to write a comparison more than one assertion can share.

type SeeInOrder

type SeeInOrder struct {
	// contains filtered or unexported fields
}

SeeInOrder matches a string in which the given values appear, each one after the last.

It is not safe for concurrent use: Matches records which value failed, so a SeeInOrder is built for one assertion and used by it.

func NewSeeInOrder

func NewSeeInOrder(content string) *SeeInOrder

NewSeeInOrder returns a constraint over the given content.

func (*SeeInOrder) FailureDescription

func (c *SeeInOrder) FailureDescription(other any) string

FailureDescription names the content and the value that was not found where it had to be. It is meaningful only after SeeInOrder.Matches returned false.

func (*SeeInOrder) Matches

func (c *SeeInOrder) Matches(other any) bool

Matches reports whether other, which must be a []string, appears in the content in that order. Anything else does not match.

Both sides are entity-decoded first, so a page that writes an apostrophe as ' still matches the apostrophe a test wrote. An empty value is skipped rather than matched: it is at every position, so honouring it would let a stray "" pass an ordering that is wrong.

func (*SeeInOrder) String

func (c *SeeInOrder) String() string

String names the constraint.

Jump to

Keyboard shortcuts

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