Yoann
Yoann

Reputation: 576

shrinker does not respect condition

I am trying to implement a generator and a shrinker with fscheck that would produce two strings not empty and different to one another. In order to do so, I use the following code :

let nonWhiteSpaceString s =
    not (String.IsNullOrWhiteSpace s) && not (String.exists ((=) '\000') s)

type DifferentNonWhiteSpace = | DifferentNonWhiteSpace of NonWhiteSpaceString*NonWhiteSpaceString

type DifferentNonWhiteSpaceGen = 
    static member DifferentNonWhiteSpace() =

        let generator =
            generate<String>
            |> Gen.two
            |> Gen.filter (fun (a,b) -> nonWhiteSpaceString a && nonWhiteSpaceString b && a<>b )
            |> Gen.map (fun (a,b) -> DifferentNonWhiteSpace((NonWhiteSpaceString a),NonWhiteSpaceString(b)))
        let shrinker (DifferentNonWhiteSpace(a,b)) = 

            let seqA = a.Get.ToCharArray() 
                        |> shrink 
                        |> Seq.map String 
            b.Get.ToCharArray() 
            |> shrink 
            |> Seq.map  String  
            |> Seq.map2 (fun x y -> if x <> y then Some(DifferentNonWhiteSpace(NonWhiteSpaceString x,NonWhiteSpaceString y)) else  None) seqA
            |> Seq.choose id

        fromGenShrink (generator,shrinker)

I use it like in tests as follows:

testProperty "calculate Xml against different Prefix should produce youpi" <| fun (DifferentNonWhiteSpace(prefix1,prefix2)) -> 
  let xml1 = createXml <| changePrefix prefix1
  let xml2 = createXml <| changePrefix prefix2
  let actual = calculate xml1 xml2
  Expect.equal actual Youpi "return youpi"

Generator seems ok, but shrinker does not what I expected it to do :

Failed after 1 test. Parameters: DifferentNonWhiteSpace (NonWhiteSpaceString "K",NonWhiteSpaceString "▲") Shrunk 3 times to: DifferentNonWhiteSpace (NonWhiteSpaceString "a",NonWhiteSpaceString "a") Result: Exception Expecto.AssertException: return Bouh.

I should not have the same value in the shrunk data :

DifferentNonWhiteSpace (NonWhiteSpaceString "a",NonWhiteSpaceString "a")

Could someone point me out what I am doing wrong?

Thanks

[edit] Actually, I also have issues with the generator.

Failed after 11 tests. Parameters:
        DifferentNonWhiteSpace (NonWhiteSpaceString "v",NonWhiteSpaceString "v")
Result:
        Exception
  Expecto.AssertException: return Bouh.

it is weird...

Upvotes: 1

Views: 96

Answers (1)

Yoann
Yoann

Reputation: 576

Thx to Kurt Schelfthout comment , I have forgotten to use

testPropertyWithConfig config ....

instead of

testProperty ....

in order to use my custom arbitraries :

let config = 
        {   
            FsCheckConfig.defaultConfig with 
                arbitrary= 
                    [
                        typeof<DifferentNonWhiteSpaceGen>
                    ]
        }

Upvotes: 1

Related Questions