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

时间:2015-02-04 19:01:35

标签: haskell quickcheck

我有一个带有字符串列表的属性:

myProp :: [String] -> Bool

我需要限制QuickCheck生成的输入,以便列表中只包含非空字符串。

我该怎么做?

3 个答案:

答案 0 :(得分:5)

您将forAlllistOf(生成列表)和listOf1(生成非空列表)一起使用。

实施例

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

quickCheck $ forAll (listOf nonEmptyString) $ myProp

答案 1 :(得分:3)

import Test.QuickCheck.Modifiers (NonEmptyList (..))

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

答案 2 :(得分:2)

或者,从第一原则(没有库函数):

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

此处s遍历所有非空值。

相关问题