Haskell:单元测试中的多个断言?

Haskell: Multiple Assertions in Unit Testing?

我查找了一个类似的主题并从这里找到了这段代码:

引用:
假设我们有模块 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]

-- 结束报价 --
是否可以在其中有多个断言,比方说,testSafeHeadForEmptyList?我正在尝试对类似案例的测试样本进行分类,但只是不同的变体。我想避免下面这样的事情(假设每个 assert equal 都有不同的变化,但是都应该 return nothing,例如):

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 中可能吗?

如果你仔细看这里:Test - 你会看到 TestCaseTestListTest 类型的数据构造函数 - 所以你应该能够做这样的事情:

testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList = TestList $
    [ TestCase $ assertEqual "Should return Nothing for empty list"
                             Nothing (safeHead ([]::[Int]))
    , TestCase $ assertEqual "Should return Nothing for empty list"
                             Nothing (safeHead ([]::[Int]))
    ...
    , TestCase $ assertEqual "Should return Nothing for empty list"
                             Nothing (safeHead ([]::[Int]))
    ]

甚至 fmap TestCase-构造器,如果你愿意的话:

testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList = TestList $ fmap TestCase
    [ assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    , assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    ...
    , assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    ]

当然 TestLabel 也是一个构造函数,所以你可以在上面贴一个标签:

testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList = 
  TestLabel "safe head for empty list" $ TestList $ fmap TestCase
    [ assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    , assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    ...
    , assertEqual "Should return Nothing for empty list"
                  Nothing (safeHead ([]::[Int]))
    ]

(当然你也可以使用 (~:)