[英]Why does this QuickCheck test with a polymorphic signature pass when it should fail?
[英]QuickCheck: why isn't there a function to pass a test and what to use instead?
为什么没有类似hedgehog
success
的QuickCheck
功能? 我特别想知道如何翻译如下属性:
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.