0

我如何测试Control.Monad.Except(两个保护结果)如下函数:

foo :: Double -> Double -> Except String Double
foo x y
  | x < -10.0 = throwError "Invalid parameter"
  | otherwise = pure $ x + y

使用hunit

4

1 回答 1

2

编写一些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
    ]
于 2017-01-14T23:08:34.807 回答