2

我正在为我当地的函数式编程小组写一篇关于 Haskell 的介绍。作为基础,我正在使用 Tasty-testing 框架,并且我想测试 indexing function (!!)

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 中添加相应的子句来解决。

4

1 回答 1

1

如果您想测试异常是否相同,您可以使用勺子包,或者编写一个类似的函数来返回异常。

于 2015-02-05T14:47:59.710 回答