我如何测试Control.Monad.Except
(两个保护结果)如下函数:
foo :: Double -> Double -> Except String Double
foo x y
| x < -10.0 = throwError "Invalid parameter"
| otherwise = pure $ x + y
使用hunit
?
编写一些runExcept
用于执行Except
操作并用于~?=
检查其结果的函数非常简单。
shouldThrow :: Eq e => Except e a -> e -> Test
m `shouldThrow` e = runExcept m ~?= Left e
shouldReturn :: Eq a => Except e a -> a -> Test
m `shouldReturn` x = runExcept m ~?= Right x
示例用法:
testFoo = TestList [
foo -11 2 `shouldThrow` "Invalid parameter",
foo 3 1 `shouldReturn` 4
]