Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
GenValidity
exists to make tests involving Validity
types easier and speed
up the generation of data for them.
Let's use the example from Data.Validity
again: A datatype that represents
primes.
To implement tests for this datatype, we would have to be able to generate
both primes and non-primes. We could do this with
(Prime $ arbitrary)
but this is tedious and inefficient.suchThat
isValid
The GenValid
type class allows you to specify how to (efficiently)
generate valid data of the given type to allow for easier and quicker testing.
Just instantiating GenUnchecked
already gives you access to a default instance
of GenValid
and GenInvalid
but writing custom implementations of these functions
may speed up the generation of data.
For example, to generate primes, we don't have to consider even numbers other than 2. A more efficient implementation could then look as follows:
instance GenUnchecked Prime where genUnchecked = Prime <$> arbitrary
instance GenValid Prime where genValid = Prime <$> (oneof [ pure 2 , ((\y -> 2 * abs y + 1) <$> arbitrary) `suchThat` isPrime) ])
Typical examples of tests involving validity could look as follows:
it "succeeds when given valid input" $ do forAll genValid $ \input -> myFunction input `shouldSatisfy` isRight
it "produces valid output when it succeeds" $ do forAll genUnchecked $ \input -> case myFunction input of Nothing -> return () -- Can happen Just output -> output `shouldSatisfy` isValid
- module Data.Validity
- class GenUnchecked a where
- class (Validity a, GenUnchecked a) => GenValid a where
- class (Validity a, GenUnchecked a) => GenInvalid a where
- upTo :: Int -> Gen Int
- genSplit :: Int -> Gen (Int, Int)
- genSplit3 :: Int -> Gen (Int, Int, Int)
- arbPartition :: Int -> Gen [Int]
- genListOf :: Gen a -> Gen [a]
Documentation
module Data.Validity
class GenUnchecked a where Source #
A class of types for which truly arbitrary values can be generated.
genUnchecked :: Gen a Source #
GenUnchecked Bool Source # | |
GenUnchecked Char Source # | |
GenUnchecked Double Source # | |
GenUnchecked Float Source # | |
GenUnchecked Int Source # | |
GenUnchecked Integer Source # | |
GenUnchecked Ordering Source # | |
GenUnchecked Word Source # | |
GenUnchecked () Source # | |
GenUnchecked a => GenUnchecked [a] Source # | |
GenUnchecked a => GenUnchecked (Maybe a) Source # | |
(GenUnchecked a, GenUnchecked b) => GenUnchecked (Either a b) Source # | |
(GenUnchecked a, GenUnchecked b) => GenUnchecked (a, b) Source # | |
(GenUnchecked a, GenUnchecked b, GenUnchecked c) => GenUnchecked (a, b, c) Source # | |
class (Validity a, GenUnchecked a) => GenValid a where Source #
A class of types for which valid values can be generated.
If you also write Arbitrary
instances for GenValid
types, it may be
best to simply write arbitrary = genValid
.
GenValid Bool Source # | |
GenValid Char Source # | |
GenValid Double Source # | |
GenValid Float Source # | |
GenValid Int Source # | |
GenValid Integer Source # | |
GenValid Ordering Source # | |
GenValid Word Source # | |
GenValid () Source # | |
GenValid a => GenValid [a] Source # | If we can generate values of a certain type, we can also generate lists of them. |
GenValid a => GenValid (Maybe a) Source # | |
(GenValid a, GenValid b) => GenValid (Either a b) Source # | |
(GenValid a, GenValid b) => GenValid (a, b) Source # | |
(GenValid a, GenValid b, GenValid c) => GenValid (a, b, c) Source # | |
class (Validity a, GenUnchecked a) => GenInvalid a where Source #
A class of types for which invalid values can be generated.
genInvalid :: Gen a Source #
GenInvalid Double Source # | Either |
GenInvalid Float Source # | Either |
GenInvalid a => GenInvalid [a] Source # | This instance ensures that the generated list contains at least one element
that satisfies |
GenInvalid a => GenInvalid (Maybe a) Source # | |
(GenInvalid a, GenInvalid b) => GenInvalid (Either a b) Source # | This instance ensures that the generated tupse contains at least one invalid element. The other element is unchecked. |
(GenInvalid a, GenInvalid b) => GenInvalid (a, b) Source # | |
(GenInvalid a, GenInvalid b, GenInvalid c) => GenInvalid (a, b, c) Source # | This instance ensures that the generated triple contains at least one invalid element. The other two are unchecked. |
genSplit :: Int -> Gen (Int, Int) Source #
'genSplit a' generates a tuple '(b, c)' such that 'b + c' equals a
.
genSplit3 :: Int -> Gen (Int, Int, Int) Source #
'genSplit a' generates a triple '(b, c, d)' such that 'b + c + d' equals a
.