Skip to content

Commit

Permalink
Merge pull request #39 from purescript/arb-instances
Browse files Browse the repository at this point in the history
Add Arb/Coarb instances for all data dependencies
  • Loading branch information
garyb committed Aug 17, 2015
2 parents 1539b1d + 396663b commit 59fa5e0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Test/QuickCheck/Arbitrary.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import Prelude

import Data.Char (toCharCode, fromCharCode)
import Data.Either (Either(..))
import Data.Foldable (foldl)
import Data.Identity (Identity(..))
import Data.Int (fromNumber, toNumber)
import Data.Lazy (Lazy(), defer, force)
import Data.List (List(..))
import Data.Maybe (Maybe(..))
import Data.String (charCodeAt, fromCharArray, split)
import Data.Tuple (Tuple(..))
import Data.Int (fromNumber, toNumber)
import Data.Foldable (foldl)
import Test.QuickCheck.Gen

-- | The `Arbitrary` class represents those types whose values can be
Expand Down Expand Up @@ -115,3 +118,21 @@ instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
instance coarbEither :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Either a b) where
coarbitrary (Left a) = coarbitrary a
coarbitrary (Right b) = coarbitrary b

instance arbitraryList :: (Arbitrary a) => Arbitrary (List a) where
arbitrary = sized \n -> chooseInt zero n >>= flip listOf arbitrary

instance coarbList :: (Coarbitrary a) => Coarbitrary (List a) where
coarbitrary = foldl (\f x -> f <<< coarbitrary x) id

instance arbitraryIdentity :: (Arbitrary a) => Arbitrary (Identity a) where
arbitrary = Identity <$> arbitrary

instance coarbIdentity :: (Coarbitrary a) => Coarbitrary (Identity a) where
coarbitrary (Identity a) = coarbitrary a

instance arbitraryLazy :: (Arbitrary a) => Arbitrary (Lazy a) where
arbitrary = arbitrary >>= pure <<< defer <<< const

instance coarbLazy :: (Coarbitrary a) => Coarbitrary (Lazy a) where
coarbitrary a = coarbitrary (force a)

0 comments on commit 59fa5e0

Please sign in to comment.