Arturo Hernandez
Arturo Hernandez

Reputation: 2859

How do I make use of FsCheck custom generators from C#?

I have the following code:

var gen = from x in Arb.Generate<int>()
from int y in Gen.Choose(5, 10)
where x > 5
select new tuple { Fst = x, Snd = y };

And I can run

Prop.ForAll<tuple>(c =>
      Console.WriteLine($"{c.Fst}, {c.Snd}")
).Check(Configuration.Default);

I see all the ways to construct generators and define properties.

But I just don't find quickly enough how to use them together.

Upvotes: 4

Views: 2263

Answers (1)

EddPorter
EddPorter

Reputation: 188

You need to register the custom generator with FsCheck. See FSCheck docs.

In short, create a class to keep your custom generators. Have a public static method returning an Arbitrary<T> where T is the type you are generating.

In your example, you would need to wrap your generator in a call to Arb.From(...).

public class MyGenerators {
    public static Arbitrary<tuple> Tuple() {
        return Arb.From(from x in Arb.Generate<int>()
                        from int y in Gen.Choose(5, 10)
                        where x > 5
                        select new tuple { Fst = x, Snd = y });
    }
}

Finally, call Arb.Register<MyGenerators>() before running your test.

Upvotes: 6

Related Questions