3

同様に、quickcheck が反例をサポートする方法:

property \x ->
  counterexample ("Foo failed with: " ++ ...) $
    foo x

しかし、それが動作する方法でshouldBe、例えば

failDetails (" details: " ++ baz a) $
  a `shouldBe` 2

そして、次の行に沿って何かを印刷したいと思います:

expected: 2
 but got: 3
 details: ...
4

2 に答える 2

5

はい、それは可能であるようです:

import Control.Exception
import Test.HUnit.Lang (HUnitFailure(..))

failDetails details assert = do
  assert `catch` \(HUnitFailure loc msg) -> do
    throw $ HUnitFailure loc $ msg ++ "\n" ++ details

によってスローされた例外をキャッチしshouldBe、メッセージを修正して再スローします。

次のように使用することもできます。

1 `shouldBe` 2
  $> failDetails "foobar"

定義すると:

($>) = flip ($)
infixl 0 $>
{-# INLINE ($>) #-}
于 2016-09-25T12:33:02.757 に答える