我正在为我的本地函数式编程组写一篇关于Haskell的简介。作为一个基础,我使用美味测试框架,我想测试索引函数(!!)。
MinimalExample.hs
module MinimalExample where
myIndex :: Int -> [a] -> a
myIndex _ [] = error "index too large"
myIndex 0 (x:_) = x
myIndex n (_:xs) = myIndex (n-1) xs MinimalTests.hs
module MinimalTests where
import Test.Tasty
import Test.Tasty.SmallCheck as SC
import Test.SmallCheck.Series
import MinimalExample
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Tests" [scProps]
scProps :: TestTree
scProps = testGroup "(checked by SmallCheck)"
[ SC.testProperty "(!!) == myIndex" $ \lst n ->
lst !! n == myIndex (n::Int) (lst::[Int])
]测试不应在“太大的索引”上失败,因为错误/异常是相同的。
对于负输入,测试应该失败--可以通过添加NonNegative作为输入的约束,或者在myIndex-function中添加相应的子句来解决这个问题。
发布于 2015-02-05 14:47:59
您可以使用勺子包,或者编写一个类似的函数,如果要测试异常是否相同,则返回异常。
https://stackoverflow.com/questions/22158159
复制相似问题