-
Notifications
You must be signed in to change notification settings - Fork 2
/
inputs-generator.go
105 lines (93 loc) · 4.24 KB
/
inputs-generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package property
import (
"fmt"
"reflect"
"github.com/steffnova/go-check/arbitrary"
"github.com/steffnova/go-check/constraints"
"github.com/steffnova/go-check/shrinker"
)
// 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].
type InputsGenerator func(targets []reflect.Type, bias constraints.Bias, random arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error)
// 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 (generator InputsGenerator) Filter(predicate any) InputsGenerator {
return func(targets []reflect.Type, b constraints.Bias, r arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error) {
predicateType := reflect.TypeOf(predicate)
if predicateType.NumIn() != len(targets) {
return nil, nil, inputMissmatchError(targets, predicateType)
}
for index := range targets {
if predicateType.In(index) != targets[index] {
return nil, nil, inputMissmatchError(targets, predicateType)
}
}
for {
arbs, shrinker, err := generator(targets, b, r)
if err != nil {
return nil, nil, err
}
predicateVal := reflect.ValueOf(predicate)
out := predicateVal.Call(arbitrary.Arbitraries(arbs).Values())
if out[0].Bool() {
return arbs, shrinker.Filter(predicate), nil
}
}
}
}
// Log returns a generator that prints generated arbitrary values and their types to standard output.
func (generator InputsGenerator) Log() InputsGenerator {
return func(t []reflect.Type, b constraints.Bias, r arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error) {
arbs, shrinker, err := generator(t, b, r)
if err != nil {
return nil, nil, err
}
for _, val := range arbs.Values() {
fmt.Printf("<%s> %#v\n", val.Type().String(), val.Interface())
}
return arbs, shrinker.Log(0), nil
}
}
// NoShrink returns a generator that generates arbitraries without shrinking capabilites (without shrinker).
func (generator InputsGenerator) NoShrink() InputsGenerator {
return func(t []reflect.Type, b constraints.Bias, r arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error) {
arbs, shrinker, err := generator(t, b, r)
if err != nil {
return nil, nil, err
}
for index := range arbs {
arbs[index].Shrinker = nil
}
return arbs, shrinker, nil
}
}
// 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 Inputs(generators ...arbitrary.Generator) InputsGenerator {
return func(targets []reflect.Type, b constraints.Bias, r arbitrary.Random) (arbitrary.Arbitraries, inputShrinker, error) {
if len(targets) != len(generators) {
return nil, nil, fmt.Errorf("%w: Number of generators (%d) must match number of targets (%d)", ErrorInputs, len(generators), len(targets))
}
arbs := make(arbitrary.Arbitraries, len(targets))
for index := range targets {
var err error
arbs[index], err = generators[index](targets[index], b, r)
if err != nil {
return nil, nil, fmt.Errorf("generator with index %d failed to generate arbitrary for target %s: %s. %w", index, targets[index], err, ErrorInputs)
}
}
arb := arbitrary.Arbitrary{Elements: arbs}
arb.Shrinker = shrinker.CollectionElements(arb)
return arbs, shrinkers(arb), nil
}
}