如何用 Hunit 测试导入 Control.Monad.Except?
how to test import Control.Monad.Except with Hunit?
我如何测试 Control.Monad.Except
(两个守卫结果)这样的函数:
foo :: Double -> Double -> Except String Double
foo x y
| x < -10.0 = throwError "Invalid parameter"
| otherwise = pure $ x + y
使用 hunit
?
编写一些使用 runExcept
to execute an Except
action and use ~?=
检查其结果的函数非常简单。
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
]
我如何测试 Control.Monad.Except
(两个守卫结果)这样的函数:
foo :: Double -> Double -> Except String Double
foo x y
| x < -10.0 = throwError "Invalid parameter"
| otherwise = pure $ x + y
使用 hunit
?
编写一些使用 runExcept
to execute an Except
action and use ~?=
检查其结果的函数非常简单。
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
]