Skip to content

Commit 59fa5e0

Browse files
committed
Merge pull request #39 from purescript/arb-instances
Add Arb/Coarb instances for all data dependencies
2 parents 1539b1d + 396663b commit 59fa5e0

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Test/QuickCheck/Arbitrary.purs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import Prelude
44

55
import Data.Char (toCharCode, fromCharCode)
66
import Data.Either (Either(..))
7+
import Data.Foldable (foldl)
8+
import Data.Identity (Identity(..))
9+
import Data.Int (fromNumber, toNumber)
10+
import Data.Lazy (Lazy(), defer, force)
11+
import Data.List (List(..))
712
import Data.Maybe (Maybe(..))
813
import Data.String (charCodeAt, fromCharArray, split)
914
import Data.Tuple (Tuple(..))
10-
import Data.Int (fromNumber, toNumber)
11-
import Data.Foldable (foldl)
1215
import Test.QuickCheck.Gen
1316

1417
-- | The `Arbitrary` class represents those types whose values can be
@@ -115,3 +118,21 @@ instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
115118
instance coarbEither :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Either a b) where
116119
coarbitrary (Left a) = coarbitrary a
117120
coarbitrary (Right b) = coarbitrary b
121+
122+
instance arbitraryList :: (Arbitrary a) => Arbitrary (List a) where
123+
arbitrary = sized \n -> chooseInt zero n >>= flip listOf arbitrary
124+
125+
instance coarbList :: (Coarbitrary a) => Coarbitrary (List a) where
126+
coarbitrary = foldl (\f x -> f <<< coarbitrary x) id
127+
128+
instance arbitraryIdentity :: (Arbitrary a) => Arbitrary (Identity a) where
129+
arbitrary = Identity <$> arbitrary
130+
131+
instance coarbIdentity :: (Coarbitrary a) => Coarbitrary (Identity a) where
132+
coarbitrary (Identity a) = coarbitrary a
133+
134+
instance arbitraryLazy :: (Arbitrary a) => Arbitrary (Lazy a) where
135+
arbitrary = arbitrary >>= pure <<< defer <<< const
136+
137+
instance coarbLazy :: (Coarbitrary a) => Coarbitrary (Lazy a) where
138+
coarbitrary a = coarbitrary (force a)

0 commit comments

Comments
 (0)