gpt4 book ai didi

haskell - 增强测试.QuickCheck

转载 作者:行者123 更新时间:2023-12-05 01:08:13 26 4
gpt4 key购买 nike

我要延期 QuickCheck在测试失败时给我更好的信息(而不仅仅是种子)。例如,我希望能够按照以下方式创建内容:

eqTest :: Eq a => a -> a -> TestResult
eqTest x y = if x == y
then HappyResult
else SadResult $ show x <> " /= " <> show y

或(使用 Monoid 实例在 SadResult 上“停止”并在 HappyResult 上“继续”,类似于 (&&)TestResult 运算符)
listEqTest :: Eq a => [a] -> [a] -> TestResult
listEqTest [] [] = HappyResult
listEqTest [] ys = SadResult $ "Ran out of xs to compare to " <> show ys
listEqTest xs [] = SadResult $ "Ran out of ys to compare to " <> show xs
listEqTest (x:xs) (y:ys) = eqTest x y <> listEqTest xs ys

如何扩展 QuickCheck 功能?或者,是否有一个更可扩展的随机测试库?

谢谢!

最佳答案

通过阅读 QuickCheck 文档,您要查找的类型是 Result :

data Result
= MkResult
{ ok :: Maybe Bool -- ^ result of the test case; Nothing = discard
, expect :: Bool -- ^ indicates what the expected result of the property is
, reason :: String -- ^ a message indicating what went wrong
, interrupted :: Bool -- ^ indicates if the test case was cancelled by pressing ^C
, abort :: Bool -- ^ if True, the test should not be repeated
, stamp :: [(String,Int)] -- ^ the collected values for this test case
, callbacks :: [Callback] -- ^ the callbacks for this test case
}

感谢 instance Testable Result您可以将其用作 QuickCheck 测试的返回类型:
Prelude Test.QuickCheck Test.QuickCheck.Property> let test xs = if 13 `elem` xs then MkResult (Just False) True "Input contained bad number" False False [] [] else succeeded
Prelude Test.QuickCheck Test.QuickCheck.Property> quickCheck test
*** Failed! Input contained bad number (after 17 tests and 3 shrinks):
[13]

关于haskell - 增强测试.QuickCheck,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17392579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com