Generating values of provided types
See original GitHub issueIn F#, I can use the JSON type provider to define a type from a JSON document:
open FSharp.Data
type Foo = JsonProvider<"""
{
"foo": 42,
"bar": "baz"
}
""">
Obviously, this is only a simple example. In reality, the JSON document could be much more complex.
It’d be nice to be able to ask FsCheck to generate values of such types:
open FsCheck
let ``An intance of Foo can't be automatically generated`` () =
Arb.from<Foo.Root>
|> Prop.forAll <| ignore
|> Check.QuickThrowOnFailure
Unfortunately, this doesn’t work:
System.Exception :
The type FSharp.Data.Runtime.BaseTypes.IJsonDocument is not handled automatically
by FsCheck. Consider using another type or writing and registering a generator for it.
Clearly, it’s possible to overcome this problem by explicitly defining a generator:
let ``An instance can be created`` () =
gen {
let! foo = Arb.generate<int>
let! bar = Arb.generate<string>
return Foo.Root (foo, bar) }
|> Arb.fromGen
|> Prop.forAll <| ignore
|> Check.QuickThrowOnFailure
This works for this toy example, but if the JSON document is big and complicated, it’d require considerable maintenance.
It’d be nice if this ‘just worked’. The same with the XML provider, for that matter.
Can anything be done about this?
I’ll be happy to submit a pull request if this is something that seems possible and desirable, but I thought I’d ask here before starting investigation.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:8 (8 by maintainers)
Top GitHub Comments
Nice, thanks. I’m continuing the discussion there because here it would be completely off topic
@moodmosaic in fact your Fare may be enhanced to provide
Arbitrary
instances of regex based strings. I’m already using it in AntaniXml, although at the moment the generated regex strings have their own random seed.