Force FsCheck to generate NonEmptyString to distinguish between join fields of type string

I am trying to perform the following behavior with FsCheck: I would like to create a generator that will generate an instance of type MyUnion , with each string field not equal to null / empty.

 type MyNestedUnion = | X of string | Y of int * string type MyUnion = | A of int * int * string * string | B of MyNestedUnion 

My "real" type is much larger / deeper than MyUnion , and FsCheck is able to generate an instance without any problems, but the string fields in cases of merging are sometimes empty. (For example, it can generate B (Y (123, "")) )

Perhaps there is some obvious way to combine FsCheck NonEmptyString and its support to generate arbitrary connection types that I am missing?

Any tips / pointers in the right direction are greatly appreciated.

Thanks!

+7
f # testing fscheck
source share
1 answer

This is contrary to property-based testing based on properties (in which you explicitly prohibit the creation of reliable test cases), but you can connect a non-empty string generator that will be used for all rows:

 type Alt = static member NonEmptyString () : Arbitrary<string> = Arb.Default.NonEmptyString() |> Arb.convert (fun (nes : NonEmptyString) -> nes.Get) NonEmptyString.NonEmptyString Arb.register<Alt>() let g = Arb.generate<MyUnion> Gen.sample 1 10 g 

Note that you need to reregister the default generator after the test, since the mappings are global.

Another solution would be to use a default generator and then filter values ​​that contain invalid strings (i.e. using ==> ), but you may find that this is not possible for particularly deep nested types.

+5
source share

All Articles