Skip to content

Commit

Permalink
Catch exceptions in callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nick8325 committed Apr 26, 2011
1 parent 3294864 commit b8570ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Test/QuickCheck/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,21 @@ localMinFound st res =

callbackPostTest :: State -> P.Result -> IO ()
callbackPostTest st res =
sequence_ [ f st res | PostTest _ f <- callbacks res ]
sequence_ [ safely st (f st res) | PostTest _ f <- callbacks res ]

callbackPostFinalFailure :: State -> P.Result -> IO ()
callbackPostFinalFailure st res =
sequence_ [ f st res | PostFinalFailure _ f <- callbacks res ]
sequence_ [ safely st (f st res) | PostFinalFailure _ f <- callbacks res ]

safely :: State -> IO () -> IO ()
safely st x = do
r <- tryEvaluateIO x
case r of
Left e ->
putLine (terminal st)
("*** Exception in callback: " ++ show e)
Right x ->
return x

--------------------------------------------------------------------------
-- the end.
2 changes: 1 addition & 1 deletion examples/Weird.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Test.QuickCheck
import Test.QuickCheck.Property
import Test.QuickCheck.Function

prop = callback (PostTest (\_ _ -> putStrLn "\n\n\napa\n\n\n")) f
prop = callback (PostTest Counterexample (\_ _ -> putStrLn "\n\n\napa\n\n\n")) f
where f :: Int -> Bool
f _ = undefined

Expand Down

0 comments on commit b8570ad

Please sign in to comment.