简体   繁体   English

是否可以在IO以外的monad中使用带有测试框架的HUnit?

[英]Is it possible to use HUnit with test-framework in a monad other than IO?

I currently have the following test code: 我目前有以下测试代码:

testUpdate :: Test
testUpdate = testCase "update does change artist" $ do
  (created, Just revised, parents) <- mbTest $ do
    Just editor <- fmap entityRef <$> findEditorByName "acid2"

    created <- create editor startWith
    let artistId = coreMbid created

    newRev <- update editor (coreRevision created) expected

    editId <- openEdit
    includeRevision editId newRev
    apply editId

    found <- findLatest artistId
    parents <- revisionParents newRev

    return (created, found, parents)

  coreData revised @?= expected

  assertBool "The old revision is a direct parent of the new revision" $
    parents == [coreRevision created]

  where
    startWith = ...
    expected = ...

This kinda works, but it's messy. 这种方式有效,但它很混乱。 I'd much rather be able to write something without having to return the various things under test, and instead have the assertions where they make sense. 我宁愿能够写一些东西而不必返回被测试的各种东西,而是在它们有意义的地方有断言。

I see there is the Assertable class, but it seems like I'd probably have to end up reinventing a bunch of stuff. 我看到有Assertable类,但似乎我可能不得不最终重新发明一堆东西。

Why not just have your monad return an IO computation of type IO a , which is a pure value? 为什么不让你的monad返回类型IO a的IO计算,这是一个值? Since as in your comment, the case where the monad is an instance of MonadIO is trivial, assume that the monad allows pure computation: 因为在你的评论中,monad是MonadIO实例的情况很简单,假设monad允许纯计算:

newtype M a = M {runM :: ....}
instance Monad M where
  ...

makeTest :: M Assertion
makeTest = do
    created <- ..
    found   <- ..
    parents <- ..
    let test1 = coreData revised @?= expected
    ...
    let test2 = assertBool "The old revision..." $
                   parents == [coreRevision create]

    return $ test1 >> test2

testUpdate :: Test
testUpdate = testCase "update does change artist" $ runM makeTest

A bonus is that you could return a collection of tests by one monadic computation, just as you would in the list monad. 奖励是您可以通过一个monadic计算返回一组测试,就像在monad列表中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM