1

我查找了一个类似的主题,并从这里找到了这段代码: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 中可行吗?

4

1 回答 1

2

如果您在这里仔细看:Test-您会看到TestCase并且TestList是该Test类型的数据构造函数-因此您应该能够执行以下操作:

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-constructor:

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]))
    ]

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

于 2021-04-14T04:47:48.953 回答