我查找了一个类似的主题,并从这里找到了这段代码:https ://stackoverflow.com/a/21419654/14386048
引用:
假设我们有模块SafePrelude.hs
:
module SafePrelude where
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
我们可以TestSafePrelude.hs
如下进行测试:
module TestSafePrelude where
import Test.HUnit
import SafePrelude
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
testSafeHeadForNonEmptyList :: Test
testSafeHeadForNonEmptyList =
TestCase $ assertEqual "Should return (Just head) for non empty list" (Just 1)
(safeHead ([1]::[Int]))
main :: IO Counts
main = runTestTT $ TestList [testSafeHeadForEmptyList, testSafeHeadForNonEmptyList]
-- End Quote --
是否有可能在里面有多个断言,比如说,testSafeHeadForEmptyList
?我正在尝试对类似案例的测试样本进行分类,但只是不同的变体。我想避免下面这样的事情(假设每个 assert equal 都有不同的变化,但都应该什么都不返回,例如):
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
testSafeHeadForEmptyList2 :: Test
testSafeHeadForEmptyList2 =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
...
testSafeHeadForEmptyList99 :: Test
testSafeHeadForEmptyList99 =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
这在 Haskell 中可行吗?