簡體   English   中英

如何使用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來執行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM