I looked up a similar topic and found this snippet of code from here: https://stackoverflow.com/a/21419654/14386048
To quote:
Let's assume that we have module SafePrelude.hs
:
module SafePrelude where
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
we can put tests into TestSafePrelude.hs
as follows:
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 --
Is it possible to have multiple assertions within, let's say, testSafeHeadForEmptyList
? I'm trying to categorize my test samples of similar cases, but just different variations. I want to avoid something like this below (assume each assert equal has a different variation, but all should return nothing, for example):
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]))
Is this possible in Haskell?
Aucun commentaire:
Enregistrer un commentaire