Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for using flatmap (as in #79) #80

Merged
merged 2 commits into from
Apr 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions example_flatmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gopter_test

import (
"reflect"

"github.com/leanovate/gopter"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
)

func Example_flatmap() {

type IntPair struct {
Fst int
Snd int
}

// Generate a pair of integers, such that the first
// is in the range of 10-20 and the second in the
// in the range of 2k-50, depending on the value of
// the first.
genIntPair := func() gopter.Gen {
return gen.IntRange(10, 20).FlatMap(func(v interface{}) gopter.Gen {
k := v.(int)
return gen.IntRange(2*k, 50).Map(func(m int) IntPair {
return IntPair{Fst: k, Snd: m}
})
},
reflect.TypeOf(int(0)))
}

parameters := gopter.DefaultTestParameters()
parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
properties := gopter.NewProperties(parameters)

properties.Property("Generate a dependent pair of integers", prop.ForAll(
func(p IntPair) bool {
a := p.Fst
b := p.Snd
return a*2 <= b
},
genIntPair(),
))

// When using testing.T you might just use: properties.TestingRun(t)
properties.Run(gopter.ConsoleReporter(false))

// Output:
// + Generate a dependent pair of integers: OK, passed 100 tests.
}