![](/img/trans.png)
[英]Is it possible to use HUnit with test-framework in a monad other than IO?
[英]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
來執行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
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.