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

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

为什么没有类似于 hedgehogsuccessQuickCheck 功能?特别是我想知道如何翻译 属性 如下所示:

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

QuickCheck 中,如果我使用 =/=,那么我将被迫 return 类型 Property,并且 [=] 似乎没有常量函数34=]一个路过属性.

所以我要么求助于 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 class 中的 property 函数和各种 Testable 实例来创建具有您需要的任何特征的 Property

例如,property ()总是成功。再举一个例子,property (b :: Bool) 成功当且仅当 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]