Skip to content

Commit

Permalink
Document Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmutant committed Jan 8, 2023
1 parent e62b94b commit c558d3e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type generatorImpl[V any] interface {
value(t *T) V
}

// Generator describes a generator of values of type V.
type Generator[V any] struct {
impl generatorImpl[V]
strOnce sync.Once
Expand All @@ -37,6 +38,7 @@ func (g *Generator[V]) String() string {
return g.str
}

// Draw produces a value from the generator.
func (g *Generator[V]) Draw(t *T, label string) V {
if t.tbLog && t.tb != nil {
t.tb.Helper()
Expand Down Expand Up @@ -74,6 +76,8 @@ func (g *Generator[V]) value(t *T) V {
return v
}

// Example produces an example value from the generator. If seed is provided, value is produced deterministically
// based on seed. Example should only be used for examples; always use *Generator.Draw in property-based tests.
func (g *Generator[V]) Example(seed ...int) V {
s := baseSeed()
if len(seed) > 0 {
Expand All @@ -86,10 +90,12 @@ func (g *Generator[V]) Example(seed ...int) V {
return v
}

// Filter creates a generator producing only values from g for which fn returns true.
func (g *Generator[V]) Filter(fn func(V) bool) *Generator[V] {
return filter(g, fn)
}

// AsAny creates a generator producing values from g converted to any.
func (g *Generator[V]) AsAny() *Generator[any] {
return asAny(g)
}
Expand Down

0 comments on commit c558d3e

Please sign in to comment.