问题
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