Search code examples
haskellquickcheck

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.

How can I do this?


Solution

  • 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