Question

How do I define the Arbitrary instance (as stated here) when using doctest and quickcheck?

Doctest and Cabal are set up as described here with a separate directory for tests.

The doctest line looks like this:

-- prop> (\s -> (decode . encode $ s == s)) :: ByteString -> Bool
decode :: ByteString -> ByteString
encode :: ByteString -> ByteString

Where and how do I define the Arbitrary instance, so that doctest can find it? Note that I would want to define it in the test project.

Was it helpful?

Solution

Try

-- $setup
-- >>> import Control.Applicative
-- >>> import qualified Data.ByteString as ByteString
-- >>> import Test.QuickCheck
-- >>> instance Arbitrary ByteString where arbitrary = ByteString.pack <$> arbitrary
-- >>> instance CoArbitrary ByteString where coarbitrary = coarbitrary . ByteString.unpack

-- |
-- prop> \ s -> (decode . encode) s == s
decode:: ByteString -> ByteString
encode :: ByteString -> ByteString

Named chunks can be used for such definitions. However, each complete definition must be on one line, and doctest will report each use of >>> as a success or failure - so in this case, 6 attempts will be reported, even though only 1 of them is actually a test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top