In FsCheck, how to generate a test record with non-negative fields?

前端 未结 2 748
予麋鹿
予麋鹿 2020-12-20 22:20

In F#, I have a record with a few fields:

    type myRecord = { a:float; b:float; c:float }

I am using FsCheck to test some properties whic

2条回答
  •  再見小時候
    2020-12-20 22:59

    Try this:

    type Generators = 
        static member arbMyRecord =
            fun (a,b,c) -> { myRecord.a = a; b = b; c = c }
             (Arb.generate |> Gen.suchThat ((<) 0.) |> Gen.three)
            |> Arb.fromGen
    
    Arb.register() |> ignore
    Check.Quick verify_this_property
    

    The is an infix map, useful for applicative style. This is an equivalent generator:

    type Generators = 
        static member arbMyRecord =
            Arb.generate 
            |> Gen.suchThat ((<) 0.) 
            |> Gen.three
            |> Gen.map (fun (a,b,c) -> { myRecord.a = a; b = b; c = c })
            |> Arb.fromGen
    

    If you don't want to globally register your generator, you can use forAll:

    Check.Quick (forAll Generators.arbMyRecord verify_this_property)
    

    Shrinking left as an exercise ;)

提交回复
热议问题