檢查檔案中的所有屬性

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.