1

I new to property-based and unit testing, and in my project I want to to use this technique, but unfortunately it is easy to say... I watched a talk about FsCheck.XUnit library, but the guy was testing numeric function (modulus)... And I want to test functions that use strings, lists and arrays.. Maybe you guys could give a hint or a source where I can look through? P.S. every where I was looking there were only numerical example, which looked like pretty easy to test.

There are some functions I wish to test:

let wordSplit (text:string) = 
  text.Split([|' ';'\n';'\r';'\t';'!';',';'.';'?';';';':'; '/'
  ;'\\';'-';'+'; '*'; '#';'(';')';'^';'"';'\'';'`'; '@';'~';'|'|]
  ,StringSplitOptions.RemoveEmptyEntries)
  |> Array.toList 

let rec matchTails (tail1 : string list) (tail2 : string list) = 
    match tail1, tail2 with
        | h1::t1 , h2::t2 -> 
            if (h1=h2) then 
                matchTails t1 t2
            else
                false
        | [], _ -> false
        | _, []  -> true

let rec phraseProcessor (textH: string) (textT: string list) (phrases: string list list) (n:int) = 
    match phrases with 
    |[] -> n
    | h :: t ->
        match h with
        |x when x.Head = textH && (matchTails (textT) (x.Tail)) ->
            phraseProcessor (textH) (textT) (t) (n+1)
        | _ -> 
            phraseProcessor (textH) (textT) (t) (n)


let rec wordChanger (phrases : string list list) (text:string list) (n:int)= 
    match text with
    | [] -> n
    | h :: t ->
        wordChanger phrases t (phraseProcessor (h) (t) (phrases) (n))
alex_z
  • 329
  • 4
  • 12

1 Answers1

1

What problem did you have with non-integer ?

You can check https://fsharpforfunandprofit.com/posts/property-based-testing/ he is giving example of string and custom type...

And of course you can generate random strings too!

let stringGenerator = Arb.generate<string>

// generate 3 strings with a maximum size of 1
Gen.sample 1 3 stringGenerator 
// result: [""; "!"; "I"]

// generate 3 strings with a maximum size of 10
Gen.sample 10 3 stringGenerator 
// result: [""; "eiX$a^"; "U%0Ika&r"]

The best thing is that the generator will work with your own user-defined types too!

type Color = Red | Green of int | Blue of bool

let colorGenerator = Arb.generate<Color>

// generate 10 colors with a maximum size of 50
Gen.sample 50 10 colorGenerator 

// result: [Green -47; Red; Red; Red; Blue true; 
// Green 2; Blue false; Red; Blue true; Green -12]

https://fsharpforfunandprofit.com/posts/property-based-testing-2/

If you wanna generate complex type: How does one generate a "complex" object in FsCheck?

rad
  • 1,787
  • 18
  • 30