How do I write a FsCheck generator for an interface in C#
Asked Answered
A

1

5

Suppose I have an interface IPerson with 2 read properties age (int) and name (string).

I also have a class Person implementing IPerson.

How do I write a FsCheck generator for generating instances of IPerson type?

Alleman answered 4/2, 2016 at 14:11 Comment(3)
Define "generator for IPerson"Noumenon
I mean generator in the FsCheck meaning: fscheck.github.io/FsCheck/TestData.htmlAlleman
Don't quite understand why this downvoted or being closed as unclear. It's a reasonably clear question unless you don't know what FsCheck is, but in that case just move along, nothing to see here.Complacence
C
9

Something like the below should work:

Gen<IPerson> gen = from age in Arb.Default.Int32().Generator
                   from name in Arb.Default.String().Generator
                   select new Person(age, name) as IPerson;
Complacence answered 5/2, 2016 at 15:50 Comment(2)
It works, indeed. But how can you use the "from" clause on Arb.Default.Int32().Generator, as IGen<int> does not implement IEnumerable<int>. What 's the magic?Alleman
LINQ syntax works on any type that implements certain methods (Select, SelectMany, GroupBy etc), not just IEnumerable. Gen implements some of those.Complacence

© 2022 - 2024 — McMap. All rights reserved.