property

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorPredicate      = fmt.Errorf("predicate configuration error") // Property predicate is invalid
	ErrorInputs         = fmt.Errorf("inputs generator error")        // Property input generator is invalid
	ErrorPropertyConfig = fmt.Errorf("property configuration error")  // Property returned an error
)

Functions

func Predicate

func Predicate(definition any) predicate

Predicate creates new predicate used by Property (see Define). The definition parameter must be a function, that can have arbitrary number of input parameters and a single output parameter of error type.

Types

type Details

type Details struct {
	NumberOfShrinks uint
	FailureReason   error
	FailureInput    arbitrary.Arbitraries
}

type InputsGenerator

type InputsGenerator func(targets []reflect.Type, bias constraints.Bias, random arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error)

InputsGenerator generates inputs for Property that are passed to the property's predicate (see Define). Unlike arbitrary.Generator, which generates a single instance of arbitrary.Arbitrary, this generator produces the same number of arbitraries as the number of passed targets. The bias and random parameters have the same role as in arbitrary.Generator.

func Inputs

func Inputs(generators ...arbitrary.Generator) InputsGenerator

Inputs returns InputsGenerator used by Property for generating property inputs (see Define). The variadic generators parameter are generators used for each input of property's predicate. Number of generators must match the number of targets. Error is returned if:

  • Number of generators doesn't match number of targets passed to [Inputs Generator]
  • Any of generators retuns an error while generating arbitrary

func (InputsGenerator) Filter

func (generator InputsGenerator) Filter(predicate any) InputsGenerator

Filter returns a generator that generates values only if the predicate is satisfied. The predicate is a function that takes one input (of any type) and returns a bool. The input parameters of the predicate must match the input parameters of the property, and the output parameter must be a bool. An error is returned if the predicate is invalid or if the generation of any values fails.

NOTE: The returned generator will retry generation until the predicate is satisfied, which can affect the speed of the generator.

func (InputsGenerator) Log

func (generator InputsGenerator) Log() InputsGenerator

Log returns a generator that prints generated arbitrary values and their types to standard output.

func (InputsGenerator) NoShrink

func (generator InputsGenerator) NoShrink() InputsGenerator

NoShrink returns a generator that generates arbitraries without shrinking capabilites (without shrinker).

type Property

type Property func(r arbitrary.Random, bias constraints.Bias) (Details, error)

Property is a function that takes arbitrary.Random and constraints.Bias parameters as inputs and returns Details and an error as output parameters. See Define for usage.

func Define

func Define(generator InputsGenerator, predicate predicate) Property

Define creates a new property by specifying an input generator and a predicate. The generator is specified using Inputs, and the predicate is specified using Predicate. The following example demonstrates how to define a property:

// The number of generators used for inputs must match the number of predicate input
// parameters. The generator at index i must be able to generate a value for the predicate's
// parameter at index i. In this example, two generator.Int() generators are used
// because the predicate's input parameters x and y are of type int.
property.Define(
	property.Inputs(
		generator.Int(),
		generator.Int(),
	),
	property.Predicate(func(x int, y int) error {
		// ... rest of the predicate logic
		return nil
	}),
)

Property returned by Define will generate random inputs for predicte returned by Predicate. If predicate returns an error for a generated input it will try to minimize inputs using shrinking. An error is returned when:

  • generator returns an error
  • predicate returns an error
  • shrinking process returns an error

Jump to

Keyboard shortcuts

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