Skip to content

Commit 59ada9a

Browse files
committed
Add expectFailure combinator
1 parent 40bd9e5 commit 59ada9a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Hedgehog/Extras/Test/Base.hs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module Hedgehog.Extras.Test.Base
6060

6161
, failWithCustom
6262
, failMessage
63+
, expectFailure
6364

6465
, assertByDeadlineM
6566
, assertByDeadlineIO
@@ -156,6 +157,14 @@ failWithCustom cs mdiff msg = liftTest $ mkTest (Left $ H.Failure (getCaller cs)
156157
failMessage :: MonadTest m => CallStack -> String -> m a
157158
failMessage cs = failWithCustom cs Nothing
158159

160+
-- | Invert the behavior of a property: success becomes failure and vice versa.
161+
expectFailure :: HasCallStack => H.TestT IO m -> H.PropertyT IO ()
162+
expectFailure prop = GHC.withFrozenCallStack $ do
163+
(res, _) <- H.evalIO $ H.runTestT prop
164+
case res of
165+
Left _ -> pure () -- Property failed so we succeed
166+
_ -> H.failWith Nothing "Expected the test to fail but it passed" -- Property passed but we expected a failure
167+
159168
-- | Create a workspace directory which will exist for at least the duration of
160169
-- the supplied block.
161170
--

0 commit comments

Comments
 (0)