我正在尝试使用FsCheck
库。我可以做一些测试,但前提条件似乎不起作用。下面的代码提供了一个示例:
let sortEven (xs: int list) =
xs
|> List.filter (fun x -> x % 2 = 0)
|> List.sort
let lengthGT0 (xs: int list) = (List.length xs) > 0
[<Property>]
let checkSortEven (xs: int list) =
lengthGT0 xs ==>
((xs |> sortEven |> List.sum) = (xs |> List.rev |> sortEven |> List.max))
使用空列表调用函数checkSortEven
作为其参数会生成异常,因为List.rev
执行相同的操作。因此,在上面的代码中,我尝试使用==>
运算符的前置条件来阻止FsCheck尝试空列表。但是,测试的第一个列表是空的还是收缩列表为空checkSortEven
被称为异常生成:
Falsifiable, after 1 test (0 shrinks) (StdGen (843297067,296354622)):
Original:
[]
with exception:
System.ArgumentException: The input sequence was empty.
Falsifiable, after 1 test (1 shrink) (StdGen (1296284131,296354622)):
Original:
[-1]
Shrunk:
[]
with exception:
System.ArgumentException: The input sequence was empty.
Falsifiable, after 1 test (2 shrinks) (StdGen (1865393009,296354622)):
Original:
[-1; 1]
Shrunk:
[]
with exception:
System.ArgumentException: The input sequence was empty.
在Why is my precondition being ignored on my Property-based test?上有关于SO的相关问题,但似乎是一个不同的案例(嵌套函数)。
我也尝试Check.QuickThrowOnFailure
代替Check.Quick
,结果基本相同。
关于为什么先决条件在这个例子中不起作用的任何想法?