找到快速检查失败的值

Xod*_*rap 14 haskell quickcheck

当一个值未通过QuickCheck测试时,我想用它进行调试.有什么方法可以做我喜欢的事情:

let failValue = quickCheck' myTest
in someStuff failValue
Run Code Online (Sandbox Code Playgroud)

如果我的数据read能够,那么我可能会破解某种方式从IO中获取它,但事实并非如此.

ham*_*mar 9

我在QuickCheck API中找不到任何方法来以一种很好的方式执行此操作,但这是我使用monadic QuickCheck API一起攻击的东西.它拦截并记录你的属性输入IORef,并假设如果失败,最后一个是罪魁祸首并将其返回到Just.如果测试通过,结果是Nothing.这可能会有所改进,但对于简单的单参数属性,它应该可以完成这项工作.

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"
Run Code Online (Sandbox Code Playgroud)