[英]how to test import Control.Monad.Except with Hunit?
How can I test Control.Monad.Except
(both guard results) a function like: 我如何测试
Control.Monad.Except
(两个保护结果)的功能如下:
foo :: Double -> Double -> Except String Double
foo x y
| x < -10.0 = throwError "Invalid parameter"
| otherwise = pure $ x + y
using hunit
? 使用
hunit
吗?
It's pretty straightforward to write some functions which use runExcept
to execute an Except
action and use ~?=
to check its results. 编写一些使用
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
Example usage: 用法示例:
testFoo = TestList [
foo -11 2 `shouldThrow` "Invalid parameter",
foo 3 1 `shouldReturn` 4
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.