forked from leanovate/gopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
205 lines (190 loc) · 6.59 KB
/
gen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gopter
import (
"fmt"
"reflect"
)
// Gen generator of arbitrary values.
// Usually properties are checked by verifing a condition holds true for
// arbitrary input parameters generated by a Gen.
//
// IMPORTANT: Even though a generator is supposed to generate random values, it
// should do this in a reproducable way. Therefore a generator has to create the
// same result for the same GenParameters, i.e. ensure that you just use the
// RNG provided by GenParameters and no external one.
// If you just plug generators together you do not have to worry about this.
type Gen func(*GenParameters) *GenResult
// Sample generate a sample value.
// Depending on the state of the RNG the generate might fail to provide a sample
func (g Gen) Sample() (interface{}, bool) {
return g(DefaultGenParameters()).Retrieve()
}
// WithLabel adds a label to a generated value.
// Labels are usually used for reporting for the arguments of a property check.
func (g Gen) WithLabel(label string) Gen {
return func(genParams *GenParameters) *GenResult {
result := g(genParams)
result.Labels = append(result.Labels, label)
return result
}
}
// SuchThat creates a derived generator by adding a sieve.
// f: has to be a function with one parameter (matching the generated value) returning a bool.
// All generated values are expected to satisfy
// f(value) == true.
// Use this care, if the sieve to to fine the generator will have many misses which results
// in an undecided property.
func (g Gen) SuchThat(f interface{}) Gen {
checkVal := reflect.ValueOf(f)
checkType := checkVal.Type()
if checkVal.Kind() != reflect.Func {
panic(fmt.Sprintf("Param of SuchThat has to be a func, but is %v", checkType.Kind()))
}
if checkType.NumIn() != 1 {
panic(fmt.Sprintf("Param of SuchThat has to be a func with one param, but is %v", checkType.NumIn()))
} else {
genResultType := g(DefaultGenParameters()).ResultType
if !genResultType.AssignableTo(checkType.In(0)) {
panic(fmt.Sprintf("Param of SuchThat has to be a func with one param assignable to %v, but is %v", genResultType, checkType.In(0)))
}
}
if checkType.NumOut() != 1 {
panic(fmt.Sprintf("Param of SuchThat has to be a func with one return value, but is %v", checkType.NumOut()))
} else if checkType.Out(0).Kind() != reflect.Bool {
panic(fmt.Sprintf("Param of SuchThat has to be a func with one return value of bool, but is %v", checkType.Out(0).Kind()))
}
sieve := func(v interface{}) bool {
return checkVal.Call([]reflect.Value{reflect.ValueOf(v)})[0].Bool()
}
return func(genParams *GenParameters) *GenResult {
result := g(genParams)
prevSieve := result.Sieve
if prevSieve == nil {
result.Sieve = sieve
} else {
result.Sieve = func(value interface{}) bool {
return prevSieve(value) && sieve(value)
}
}
return result
}
}
// WithShrinker creates a derived generator with a specific shrinker
func (g Gen) WithShrinker(shrinker Shrinker) Gen {
return func(genParams *GenParameters) *GenResult {
result := g(genParams)
if shrinker == nil {
result.Shrinker = NoShrinker
} else {
result.Shrinker = shrinker
}
return result
}
}
// Map creates a derived generators by mapping all generatored values with a given function.
// f: has to be a function with one parameter (matching the generated value) and a single return.
// Note: The derived generator will not have a sieve or shrinker.
func (g Gen) Map(f interface{}) Gen {
mapperVal := reflect.ValueOf(f)
mapperType := mapperVal.Type()
if mapperVal.Kind() != reflect.Func {
panic(fmt.Sprintf("Param of Map has to be a func, but is %v", mapperType.Kind()))
}
if mapperType.NumIn() != 1 {
panic(fmt.Sprintf("Param of Map has to be a func with one param, but is %v", mapperType.NumIn()))
} else {
genResultType := g(DefaultGenParameters()).ResultType
if !genResultType.AssignableTo(mapperType.In(0)) {
panic(fmt.Sprintf("Param of Map has to be a func with one param assignable to %v, but is %v", genResultType, mapperType.In(0)))
}
}
if mapperType.NumOut() != 1 {
panic(fmt.Sprintf("Param of Map has to be a func with one return value, but is %v", mapperType.NumOut()))
}
return func(genParams *GenParameters) *GenResult {
result := g(genParams)
value, ok := result.RetrieveAsValue()
if ok {
mapped := mapperVal.Call([]reflect.Value{value})[0]
return &GenResult{
Shrinker: NoShrinker,
result: mapped.Interface(),
Labels: result.Labels,
ResultType: mapperType.Out(0),
}
}
return &GenResult{
Shrinker: NoShrinker,
result: nil,
Labels: result.Labels,
ResultType: mapperType.Out(0),
}
}
}
// FlatMap creates a derived generator by passing a generated value to a function which itself
// creates a generator.
func (g Gen) FlatMap(f func(interface{}) Gen, resultType reflect.Type) Gen {
return func(genParams *GenParameters) *GenResult {
result := g(genParams)
value, ok := result.Retrieve()
if ok {
return f(value)(genParams)
}
return &GenResult{
Shrinker: NoShrinker,
result: nil,
Labels: result.Labels,
ResultType: resultType,
}
}
}
// MapResult creates a derived generator by mapping the GenResult directly.
// Contrary to `Map` and `FlatMap` this also allow the conversion of
// shrinkers and sieves, but implementation is more cumbersome.
func (g Gen) MapResult(f func(*GenResult) *GenResult) Gen {
return func(genParams *GenParameters) *GenResult {
return f(g(genParams))
}
}
// CombineGens creates a generators from a list of generators.
// The result type will be a []interface{} containing the generated values of each generators in
// the list.
// Note: The combined generator will not have a sieve or shrinker.
func CombineGens(gens ...Gen) Gen {
return func(genParams *GenParameters) *GenResult {
labels := []string{}
values := make([]interface{}, len(gens))
shrinkers := make([]Shrinker, len(gens))
sieves := make([]func(v interface{}) bool, len(gens))
var ok bool
for i, gen := range gens {
result := gen(genParams)
labels = append(labels, result.Labels...)
shrinkers[i] = result.Shrinker
sieves[i] = result.Sieve
values[i], ok = result.Retrieve()
if !ok {
return &GenResult{
Shrinker: NoShrinker,
result: nil,
Labels: result.Labels,
ResultType: reflect.TypeOf(values),
}
}
}
return &GenResult{
Shrinker: CombineShrinker(shrinkers...),
result: values,
Labels: labels,
ResultType: reflect.TypeOf(values),
Sieve: func(v interface{}) bool {
values := v.([]interface{})
for i, value := range values {
if sieves[i] != nil && !sieves[i](value) {
return false
}
}
return true
},
}
}
}