How can I use multi-line input with QuickCheck in doctest?
Asked Answered
F

1

11

From Doctest's readme, one can use doctest with QuickCheck, like this:

-- |
-- prop> sort xs == (sort . sort) (xs :: [Int])

I would like to describe this property using multiple lines, probably like

-- |
-- prop> sort xs ==
--            (sort . sort) (xs :: [Int])

Doctest itself supports multi-line input (again from readme)

-- |
-- >>> :{
--  let
--    x = 1
--    y = 2
--  in x + y + multiline
-- :}
-- 6

and I tried several similar syntaxes I came up with, such as

-- |
-- prop> :{ sort xs ==
--           (sort . sort) (xs :: [Int])
-- }:

without any success. (In the example above, the error message is parse error on input '{'.)

How can I use multi-line input with Quickcheck in doctest?

Fruitful answered 15/6, 2015 at 3:20 Comment(2)
I believe that just like ghci, the multi-line syntax requires that the open and closing symbols be on their own line. Your example won't work because :{ is not on its own line.Peptic
@Peptic I believe you are right about that case. I can't avoid parse error and have prop> recognize multiple lines as the input.Fruitful
F
2

As of September 2017, doctest does not allow multi-line properties. However, you can use quickCheck as usual in a doctest:

-- >>> import Test.QuickCheck
-- >>> import Data.List (sort)
-- >>> :{
--  quickCheck $ \xs -> 
--      sort xs ==
--            (sort . sort) (xs :: [Int])
-- :}
-- +++ OK, passed 100 tests.

That's verbose, but will enable you to write arbitrary long checks. Note that you can always create a prop_* function and use that in your doctest.

Felloe answered 25/9, 2017 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.