How to identify an FSCheck generator so that it can be detected
I am writing an FSCheck generator to generate strings with the following properties:
- They are not zero.
- Trimming them does not affect the length
- They do not contain spaces.
Here's my generator code:
namespace Example
open FsCheck.Arb
module public Generation =
let hasChars (s : string) =
(isNull s |> not)
&& s.Length > 0
let isTrimmed (s : string) =
s.Trim().Length = s.Length
let isContinuous (s : string) =
s
|> Seq.exists ((=) ' ')
|> not
[<AbstractClass; Sealed>]
type public Generators = class end
type public ContinuousString = ContinuousString of string with
member x.Get = match x with ContinuousString r -> r
override x.ToString() = x.Get
type public Generators with
static member ContinuousString() =
Default.String()
|> filter hasChars
|> filter isTrimmed
|> filter isContinuous
|> convert ContinuousString string
And here is a test designed to test the generation:
[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
When I run this test I get:
System.Exception: No instances found on type Example.Generation+ContinuousString. Check that the type is public and has public static members with the right signature.
As far as I can tell from the FSCheck source code, the item I define should be found by the discovery filter and this method seems to be similar to the built-in one, for example NonEmptyString
.
What did I miss? Thank!
source to share
You are passing the wrong type to FsCheck. You must pass your class to it Generators
, not your ContinuousString
DU. Ie, this:
[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
should have been:
[<Property(Arbitrary=[| typeof<Generators> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
The FsCheck error message also tried to tell about this:
Make sure the type is public and has public static members with the correct signature.
The type that you have created, corresponds to what he is looking for Generators
.
source to share