Why is my precondition being ignored on my Property-based test?

佐手、 提交于 2019-12-13 01:13:54

问题


Why is my precondition being ignored on my Property-based test?

The precondition for my test is the following:

fun rowCount -> rowCount >= 0

Thus, my actual test is:

[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| 
            fun rowCount -> rowCount >= 0 ==>  
                            fun rowCount -> rowCount |> createGrid
                                                     |> Map.toList
                                                     |> List.length = rowCount * rowCount

However, my test continues to fail:

Result Message: System.Exception : Falsifiable, after 3 tests (1 shrink) (StdGen (985619705,296133555)): Original: 1 -1 Shrunk: 0 -1

Domain:

let createGrid rowCount = 

    [for x in 0..rowCount-1 do
        for y in 0..rowCount-1 do
            yield { X=x; Y=y; State=Dead } 
    ]|> List.map (fun c -> (c.X, c.Y), { X=c.X; Y=c.Y; State=Dead })
     |> Map.ofList

[UPDATE]

I've also tried:

let precondition rowCount =
    rowCount >= 0

let ``some property`` rowCount = 

    precondition rowCount ==> rowCount |> createGrid 
                                       |> Map.toList
                                       |> List.length = rowCount * rowCount
[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| ``some property``

However, I receive the following error:

Type mismatch. Expecting a Property -> 'a but given a int -> Map<(int * int),Cell> The type 'Property' does not match the type 'int'


回答1:


As @FyodorSoikin points out in his comment, you have two nested functions that each take a rowCount.

The second rowCount value shadows the first one, but the ==> precondition function only works on the first rowCount value. Thus, the rowCount value actually used for testing is still unbounded.

Make the test simpler, and it'll work:

open Xunit
open FsCheck

[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| fun rowCount ->
        rowCount >= 0 ==>  
        (rowCount
            |> createGrid
            |> Map.toList
            |> List.length = rowCount * rowCount)


来源:https://stackoverflow.com/questions/36145001/why-is-my-precondition-being-ignored-on-my-property-based-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!