繁体   English   中英

QuickCheck:为什么没有通过测试的函数以及使用什么来代替?

[英]QuickCheck: why isn't there a function to pass a test and what to use instead?

为什么没有类似hedgehog successQuickCheck功能? 我特别想知道如何翻译如下属性:

prop_specialPair :: Property
prop_specialPair = property $ do
  (_, xs) <- forAll specialPair
  case xs of
    x:_ -> x /== 3
    _   -> success

QuickCheck如果我使用=/=然后我被迫返回Property类型的东西,并且似乎没有常量函数来返回传递的属性。

所以我要么不得不求助于Bool类型:

prop_specialPair :: SpecialPair -> Bool
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x == 3
    _   -> True

或者使用非常笨拙的编码来获取success ,例如:

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> True === True

有没有更好的方法在QuickCheck表达上述属性?

您可以使用Testable类中的property函数和各种Testable实例来创建具有您需要的任何特征的Property

例如, property ()总是成功。 再举一个例子, property (b :: Bool) iff b == True成功。

所以你可以这样做,例如:

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> property ()

或者,您可以使用Testable Result实例使其更加明确,并且值succeeded :: Result

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> property succeeded

您还可以将含义与(==>)

prop_specialPair :: Property
prop_specialPair =
  forAll specialPair $ \(_, xs) ->
    not (null xs) ==> take 1 xs =/= [3]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM