github.com/leanovate/gopter@v0.2.9/prop/forall_no_shrink.go (about)

     1  package prop
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/leanovate/gopter"
     8  )
     9  
    10  /*
    11  ForAllNoShrink creates a property that requires the check condition to be true for all values.
    12  As the name suggests the generated values will not be shrunk if the condition falsiies.
    13  
    14  "condition" has to be a function with the same number of parameters as the provided
    15  generators "gens". The function may return a simple bool (true means that the
    16  condition has passed), a string (empty string means that condition has passed),
    17  a *PropResult, or one of former combined with an error.
    18  */
    19  func ForAllNoShrink(condition interface{}, gens ...gopter.Gen) gopter.Prop {
    20  	callCheck, err := checkConditionFunc(condition, len(gens))
    21  	if err != nil {
    22  		return ErrorProp(err)
    23  	}
    24  
    25  	return gopter.SaveProp(func(genParams *gopter.GenParameters) *gopter.PropResult {
    26  		genResults := make([]*gopter.GenResult, len(gens))
    27  		values := make([]reflect.Value, len(gens))
    28  		valuesFormated := make([]string, len(gens))
    29  		var ok bool
    30  		for i, gen := range gens {
    31  			result := gen(genParams)
    32  			genResults[i] = result
    33  			values[i], ok = result.RetrieveAsValue()
    34  			if !ok {
    35  				return &gopter.PropResult{
    36  					Status: gopter.PropUndecided,
    37  				}
    38  			}
    39  			valuesFormated[i] = fmt.Sprintf("%+v", values[i].Interface())
    40  		}
    41  		result := callCheck(values)
    42  		for i, genResult := range genResults {
    43  			result = result.AddArgs(gopter.NewPropArg(genResult, 0, values[i].Interface(), valuesFormated[i], values[i].Interface(), valuesFormated[i]))
    44  		}
    45  		return result
    46  	})
    47  }
    48  
    49  // ForAllNoShrink1 creates a property that requires the check condition to be true for all values
    50  // As the name suggests the generated values will not be shrunk if the condition falsiies
    51  func ForAllNoShrink1(gen gopter.Gen, check func(interface{}) (interface{}, error)) gopter.Prop {
    52  	return gopter.SaveProp(func(genParams *gopter.GenParameters) *gopter.PropResult {
    53  		genResult := gen(genParams)
    54  		value, ok := genResult.Retrieve()
    55  		if !ok {
    56  			return &gopter.PropResult{
    57  				Status: gopter.PropUndecided,
    58  			}
    59  		}
    60  		valueFormated := fmt.Sprintf("%+v", value)
    61  		return convertResult(check(value)).AddArgs(gopter.NewPropArg(genResult, 0, value, valueFormated, value, valueFormated))
    62  	})
    63  }