Comments (4)
Scalacheck works well because of scala's for-comprehention notation, which is a very nice way to write these map/flatMap cascades.
Your example
val myGen = for {
n <- Gen.choose(10,20)
m <- Gen.choose(2*n, 500)
} yield (n,m)
actually expands to something like:
Gen.choose(10, 20).flatMap(n -> Gen.choose(2*n, 500).map(m -> (n,m))
I think you could write it like this in go as well, though you have to be very careful when using external variables in anonymous functions.
from gopter.
The problem here is that the final generator is not the result of a FlatMap. I.e. "n" and "m" are completely independent generators within the struct-generator
I thing the correct way would look something like this:
gen.IntRange(10, 20).FlatMap(func(v interface{}) gopter.Gen {
n := v.(int)
var gen_map = map[string]gopter.Gen{"Fst": gen.Const(n), "Snd": gen.IntRange(2*k, 50) }
return gen.Struct(
reflect.TypeOf(IntPair{}),
gen_map,
)
}
Hope this makes sense
from gopter.
Thanks, that works indeed. Here is the solution a bit reformatted:
genIntPair := func() gopter.Gen {
return gen.IntRange(10, 20).FlatMap(func(v interface{}) gopter.Gen {
k := v.(int)
n := gen.Const(k)
m := gen.IntRange(2*k, 50)
var gen_map = map[string]gopter.Gen{"Fst": n, "Snd": m}
return gen.Struct(
reflect.TypeOf(IntPair{}),
gen_map,
)
},
reflect.TypeOf(int(0)))
}
So the trick is that the first generated integer value must be re-introduced as generator by applying Const
, the trivial generator (akin to return
in a monadic setting).
If you have more dependencies some syntactic sugar would be nice, but this seems to be difficult in Go.
from gopter.
I like your FlatMap -> Map approach. This boils down to
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)))
}
This is still baroque, but way more to the point than the first version. I will update my example PR #80
from gopter.
Related Issues (20)
- Decoding complex inputs upon test failure HOT 1
- cut a new release HOT 1
- How to generate a slice with a range as length? HOT 1
- []Struct with SuchThat clause within a Struct triggers index out of bounds exception on master HOT 2
- properties.Run doesn't stop HOT 7
- Problem Shrinking *time.Time HOT 2
- Not able to generate slice of pointers HOT 1
- Best way to capture tests for regressions HOT 6
- GenCommand: Generating Commands Based On The Current State HOT 1
- Is it dangerous to share references between states? HOT 1
- Initial state of slices are not held for reporting HOT 7
- Generating unique values HOT 2
- Cannot reproduce tests with seed when restarting tests HOT 8
- Stateful testing: Clarification around InititalStateGen and PostConditions HOT 4
- gen.StructStrict, which errors if gens contains keys the target struct does not contain? HOT 3
- Tests failing on master branch HOT 7
- Getting values outside of range when using gen.IntRange (probably during shrink) HOT 7
- Check paniced when using OneGenOf HOT 1
- Support for generics?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gopter.