简体   繁体   English

如何将QuickCheck参数约束到非空字符串列表?

[英]How can I constrain a QuickCheck parameter to a list of non-empty Strings?

I have a property that takes a list of Strings: 我有一个属性,其中包含一个字符串列表:

myProp :: [String] -> Bool

I need to constrain the inputs that QuickCheck generates so that only non-empty strings are in the list. 我需要约束QuickCheck生成的输入,以便列表中只有非空字符串。

How can I do this? 我怎样才能做到这一点?

You use forAll together with listOf (which generates lists) and listOf1 (which generates non-empty lists). 您可以使用forAll连同listOf (生成列表)和listOf1 (生成非空列表)。

Examples 例子

quickCheck $ forAll (listOf $ listOf1 arbitrary) $ myProp
-- more verbose alternative to make things clear
nonEmptyString :: Gen String
nonEmptyString = listOf1 arbitrary

quickCheck $ forAll (listOf nonEmptyString) $ myProp
import Test.QuickCheck.Modifiers (NonEmptyList (..))

myProp :: [NonEmptyList Char] -> Bool
myProp xs0 =
  let xs = map getNonEmpty xs0
  in ...

Or, from first principles (no library functions): 或者,从第一原则(没有库函数):

quickCheck $ \ h t -> let {s :: String ; s = h : t } in length s > 0

here s runs through all non-empty values. 这里s遍历所有非空值。

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

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