Search code examples
f#fscheckproperty-based-testing

How to generate tuples by FsCheck


This is a json generation :

let strGen = Arb.Default.String()
                |> Arb.toGen
strGen
    |> Gen.arrayOf
    |> Gen.map (String.concat "\", \"")
    |> Gen.map (fun strs -> "[\"" + strs + "\"]")

How can I have the string that the json have been created from in my test body to assert the final result.


Solution

  • Can't seem to be able to put the code in comments, so here's a cleaned up version:

    let isDigitOrWord i =
            i |> String.isNullOrEmpty 
            |> not && Regex.IsMatch(i,"^[a-zA-Z0-9 ]*$")
    
    let strGen = Arb.Default.String() |> Arb.toGen
    
    Gen.arrayOf strGen 
    |> Gen.map (fun array ->
        let array = array |>  Array.filter isDigitOrWord
        let json =
            array
            |> String.concat "\", \"" 
            |> fun strs -> if strs|> String.isEmpty then strs else "\"" + strs + "\""
            |> fun strs -> "[" + strs + "]"
        array,json)