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?
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?
You use forAll
together with listOf
(which generates lists) and listOf1
(which generates non-empty lists).
quickCheck $ forAll (listOf $ listOf1 arbitrary) $ myProp
-- more verbose alternative to make things clear
nonEmptyString :: Gen String
nonEmptyString = listOf1 arbitrary
quickCheck $ forAll (listOf nonEmptyString) $ myProp
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.
import Test.QuickCheck.Modifiers (NonEmptyList (..))
myProp :: [NonEmptyList Char] -> Bool
myProp xs0 =
let xs = map getNonEmpty xs0
in ...
© 2022 - 2024 — McMap. All rights reserved.