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
|
package gopter
import "reflect"
// GenResult contains the result of a generator.
type GenResult struct {
Labels []string
Shrinker Shrinker
ResultType reflect.Type
Result interface{}
Sieve func(interface{}) bool
}
// NewGenResult creates a new generator result from for a concrete value and
// shrinker.
// Note: The concrete value "result" not be nil
func NewGenResult(result interface{}, shrinker Shrinker) *GenResult {
return &GenResult{
Shrinker: shrinker,
ResultType: reflect.TypeOf(result),
Result: result,
}
}
// NewEmptyResult creates an empty generator result.
// Unless the sieve does not explicitly allow it, empty (i.e. nil-valued)
// results are considered invalid.
func NewEmptyResult(resultType reflect.Type) *GenResult {
return &GenResult{
ResultType: resultType,
Shrinker: NoShrinker,
}
}
// Retrieve gets the concrete generator result.
// If the result is invalid or does not pass the sieve there is no concrete
// value and the property using the generator should be undecided.
func (r *GenResult) Retrieve() (interface{}, bool) {
if (r.Sieve == nil && r.Result != nil) || (r.Sieve != nil && r.Sieve(r.Result)) {
return r.Result, true
}
return nil, false
}
// RetrieveAsValue get the concrete generator result as reflect value.
// If the result is invalid or does not pass the sieve there is no concrete
// value and the property using the generator should be undecided.
func (r *GenResult) RetrieveAsValue() (reflect.Value, bool) {
if r.Result != nil && (r.Sieve == nil || r.Sieve(r.Result)) {
return reflect.ValueOf(r.Result), true
} else if r.Result == nil && r.Sieve != nil && r.Sieve(r.Result) {
return reflect.Zero(r.ResultType), true
}
return reflect.Zero(r.ResultType), false
}
|