/
forall_no_shrink.go
63 lines (57 loc) · 2.15 KB
/
forall_no_shrink.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
package prop
import (
"fmt"
"reflect"
"github.com/leanovate/gopter"
)
/*
ForAllNoShrink creates a property that requires the check condition to be true for all values.
As the name suggests the generated values will not be shrunk if the condition falsiies.
"condition" has to be a function with the same number of parameters as the provided
generators "gens". The function may return a simple bool (true means that the
condition has passed), a string (empty string means that condition has passed),
a *PropResult, or one of former combined with an error.
*/
func ForAllNoShrink(condition interface{}, gens ...gopter.Gen) gopter.Prop {
callCheck, err := checkConditionFunc(condition, len(gens))
if err != nil {
return ErrorProp(err)
}
return gopter.SaveProp(func(genParams *gopter.GenParameters) *gopter.PropResult {
genResults := make([]*gopter.GenResult, len(gens))
values := make([]reflect.Value, len(gens))
valuesFormated := make([]string, len(gens))
var ok bool
for i, gen := range gens {
result := gen(genParams)
genResults[i] = result
values[i], ok = result.RetrieveAsValue()
if !ok {
return &gopter.PropResult{
Status: gopter.PropUndecided,
}
}
valuesFormated[i] = fmt.Sprintf("%+v", values[i].Interface())
}
result := callCheck(values)
for i, genResult := range genResults {
result = result.AddArgs(gopter.NewPropArg(genResult, 0, values[i].Interface(), valuesFormated[i], values[i].Interface(), valuesFormated[i]))
}
return result
})
}
// ForAllNoShrink1 creates a property that requires the check condition to be true for all values
// As the name suggests the generated values will not be shrunk if the condition falsiies
func ForAllNoShrink1(gen gopter.Gen, check func(interface{}) (interface{}, error)) gopter.Prop {
return gopter.SaveProp(func(genParams *gopter.GenParameters) *gopter.PropResult {
genResult := gen(genParams)
value, ok := genResult.Retrieve()
if !ok {
return &gopter.PropResult{
Status: gopter.PropUndecided,
}
}
valueFormated := fmt.Sprintf("%+v", value)
return convertResult(check(value)).AddArgs(gopter.NewPropArg(genResult, 0, value, valueFormated, value, valueFormated))
})
}