检查文件中的所有属性

quickCheckAll 是一个模板 Haskell 帮助程序,它查找当前文件中名称以 prop_ 开头并测试它们的所有定义。

{-# LANGUAGE TemplateHaskell #-}

import Test.QuickCheck (quickCheckAll)
import Data.List (sort)

idempotent::Eq a => (a -> a) -> a -> Bool
idempotent f x = f (f x) == f x

prop_sortIdempotent = idempotent sort

-- does not begin with prop_, will not be picked up by the test runner
sortDoesNotChangeLength xs = length (sort xs) == length xs

return []
main = $quickCheckAll

请注意,return [] 行是必需的。它以文本方式在模板 Haskell 可见的上方生成定义。

$ runhaskell QuickCheckAllExample.hs
=== prop_sortIdempotent from tree.hs:7 ===
+++ OK, passed 100 tests.