-
Notifications
You must be signed in to change notification settings - Fork 25
/
generator.go
99 lines (78 loc) · 2.02 KB
/
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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import "reflect"
type value interface{}
type generatorImpl interface {
String() string
type_() reflect.Type
value(t *T) value
}
type Generator struct {
impl generatorImpl
typ reflect.Type
str string
}
func newGenerator(impl generatorImpl) *Generator {
return &Generator{
impl: impl,
typ: impl.type_(),
str: impl.String(),
}
}
func (g *Generator) String() string {
return g.str
}
func (g *Generator) type_() reflect.Type {
return g.typ
}
func (g *Generator) Draw(t *T, label string) interface{} {
if t.tb != nil {
t.tb.Helper()
}
return t.draw(g, label)
}
func (g *Generator) value(t *T) value {
i := t.s.beginGroup(g.str, true)
v := g.impl.value(t)
u := reflect.TypeOf(v)
assertf(v != nil, "%v has generated a nil value", g)
assertf(u.AssignableTo(g.typ), "%v has generated a value of type %v which is not assignable to %v", g, u, g.typ)
t.s.endGroup(i, false)
return v
}
func (g *Generator) Example(seed ...uint64) interface{} {
s := baseSeed()
if len(seed) > 0 {
s = seed[0]
}
v, n, err := example(g, newT(nil, newRandomBitStream(s, false), false))
assertf(err == nil, "%v failed to generate an example in %v tries: %v", g, n, err)
return v
}
func (g *Generator) Filter(fn interface{}) *Generator {
return filter(g, fn)
}
func (g *Generator) Map(fn interface{}) *Generator {
return map_(g, fn)
}
func example(g *Generator, t *T) (value, int, error) {
for i := 1; ; i++ {
r, err := recoverValue(g, t)
if err == nil {
return r, i, nil
} else if i == exampleMaxTries {
if err != nil {
return nil, i, err
}
return nil, i, errCantGenDueToFilter
}
}
}
func recoverValue(g *Generator, t *T) (v value, err *testError) {
defer func() { err = panicToError(recover(), 3) }()
return g.value(t), nil
}