I want to make sure that the function will cause an error when it is received and an invalid value. For example, let it say that I have a pos function that returns only a positive number:
pos :: Int -> Int pos x | x >= 0 = x | otherwise = error "Invalid Input"
This is a simplified example, but I hope you get this idea.
I want to be able to write a test case that will expect errors and consider it a passing test. For example:
tests = [pos 1 == 1, assertError pos (-1), pos 2 == 2, assertError pos (-2)] runTests = all (== True) tests
[My decision]
Here's what I ended up based on @hammar's comment.
instance Eq ErrorCall where x == y = (show x) == (show y) assertException :: (Exception e, Eq e) => e -> IO a -> IO () assertException ex action = handleJust isWanted (const $ return ()) $ do action assertFailure $ "Expected exception: " ++ show ex where isWanted = guard . (== ex) assertError ex f = TestCase $ assertException (ErrorCall ex) $ evaluate f tests = TestList [ (pos 0) ~?= 0 , (pos 1) ~?= 1 , assertError "Invalid Input" (pos (-1)) ] main = runTestTT tests
Joe hillenbrand
source share