FsCheck generators by choosing from feature pools

Is there a way to generate a string in FsCheck by selecting only one item from each list of strings and then concatenating the result?

I was just completely stuck and didn't seem to understand this. I looked through the docs and in the github repository for something similar. And I did most of my reading in FsCheck from FSharpForFunAndProfit .

This is what I would think:

let rand = System.Random() let randInt max = rand.Next(0, max) let selectLetter (string: string) = let whichLettersIndex = String.length string |> randInt string.Substring(whichLettersIndex, 1) let generateOddlySpelledWord listOfStrings = List.map selectLetter listOfStrings |> String.concat "" let usingGenerateOddlySpelledWord = generateOddlySpelledWord ["zZ"; "oO0Ò"; "eEê"] 

This should generate something like "Z0ê" or "zTE".

+6
source share
1 answer

Does it do what you want?

 open FsCheck let createGenerators (l : string seq) = l |> Seq.map Gen.elements |> Seq.toList type OddlySpelledWords = static member String() = ["zZ"; "oO0Ò"; "eEê"] |> createGenerators |> Gen.sequence |> Gen.map (List.map string >> String.concat "") |> Arb.fromGen 

Ad-hoc test:

 open FsCheck.Xunit [<Property(Arbitrary = [| typeof<OddlySpelledWords> |])>] let test (s : string) = printfn "%s" s 

Output (Truncated):

  z0ê ZÒe ZOe zoê ZÒe zoê Z0e zoê z0ê ZOe zÒê z0E zoe 

Explanation

The createGenerators function is of type seq string -> Gen<char> list , and it creates Gen from each row using Gen.elements , because the string is also char seq ; Gen.elements creates a Gen that selects one of these char values ​​from each row.

Then it uses Gen.sequence to convert the Gen<char> list to Gen <char list> , and then displays from there.


By the way, you can also enable createGenerators :

 type OddlySpelledWords = static member String() = ["zZ"; "oO0Ò"; "eEê"] |> List.map Gen.elements |> Gen.sequence |> Gen.map (List.map string >> String.concat "") |> Arb.fromGen 
+6
source

Source: https://habr.com/ru/post/983193/


All Articles