Non ho trovato nulla nell'API QuickCheck per farlo in un modo carino, ma ecco qualcosa che ho hackerato usando l'API di QuickCheck monadica. Intercetta e registra gli input sulla proprietà in un IORef
e presuppone che, in caso di errore, l'ultimo sia stato il colpevole e lo restituisca in un Just
. Se il test è stato superato, il risultato è Nothing
. Questo può probabilmente essere raffinato un po 'ma per semplici proprietà a un argomento dovrebbe fare il lavoro.
import Control.Monad
import Data.IORef
import Test.QuickCheck
import Test.QuickCheck.Monadic
prop_failIfZero :: Int -> Bool
prop_failIfZero n = n /= 0
quickCheck' :: (Arbitrary a, Show a) => (a -> Bool) -> IO (Maybe a)
quickCheck' prop = do input <- newIORef Nothing
result <- quickCheckWithResult args (logInput input prop)
case result of
Failure {} -> readIORef input
_ -> return Nothing
where
logInput input prop x = monadicIO $ do run $ writeIORef input (Just x)
assert (prop x)
args = stdArgs { chatty = False }
main = do failed <- quickCheck' prop_failIfZero
case failed of
Just x -> putStrLn $ "The input that failed was: " ++ show x
Nothing -> putStrLn "The test passed"
molto intelligente, grazie – Xodarap
Questo piccolo trucco appena fatto la mia esperienza di debug Haskell molto meglio. Grazie –