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

二次信任 提交于 2021-02-04 15:49:16

问题


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.

How can I do this?


回答1:


You use forAll together with listOf (which generates lists) and listOf1 (which generates non-empty lists).

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



回答2:


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

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



回答3:


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.



来源:https://stackoverflow.com/questions/28329303/how-can-i-constrain-a-quickcheck-parameter-to-a-list-of-non-empty-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!