question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating values of provided types

See original GitHub issue

In 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
giacomociticommented, Sep 8, 2016

Nice, thanks. I’m continuing the discussion there because here it would be completely off topic

1reaction
giacomociticommented, Sep 7, 2016

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - How to generate value from type?
I can accept that as an answer if there's no way to generate values from types. – Raphael Rafatpanah. Mar 23, 2022 at...
Read more >
Documentation - Creating Types from Types
In this section we'll cover ways to express a new type in terms of an existing type or value. Generics - Types which...
Read more >
Documentation - Generics
We've now added a type variable Type to the identity function. This Type allows us to capture the type the user provides (e.g....
Read more >
An Overview of Identifiers in Hibernate/JPA
If we're using the default generation type, the persistence provider will determine values based on the type of the primary key attribute.
Read more >
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found