如何在 doctest 中使用 QuickCheck 的多行输入?
How can I use multi-line input with QuickCheck in doctest?
从 Doctest's readme 开始,可以将 doctest 与 QuickCheck 一起使用,如下所示:
-- |
-- prop> sort xs == (sort . sort) (xs :: [Int])
我想用多行来描述这个属性,大概像
-- |
-- prop> sort xs ==
-- (sort . sort) (xs :: [Int])
Doctest本身支持多行输入(同样来自readme)
-- |
-- >>> :{
-- let
-- x = 1
-- y = 2
-- in x + y + multiline
-- :}
-- 6
我尝试了几种我想出的类似语法,例如
-- |
-- prop> :{ sort xs ==
-- (sort . sort) (xs :: [Int])
-- }:
没有任何成功。 (在上面的例子中,错误信息是parse error on input '{'
。)
如何在 doctest 中使用 Quickcheck 的多行输入?
截至 2017 年 9 月,doctest does not allow multi-line properties。但是,您可以像往常一样在 doctest 中使用 quickCheck
:
-- >>> import Test.QuickCheck
-- >>> import Data.List (sort)
-- >>> :{
-- quickCheck $ \xs ->
-- sort xs ==
-- (sort . sort) (xs :: [Int])
-- :}
-- +++ OK, passed 100 tests.
这很冗长,但可以让您编写任意长的检查。请注意,您始终可以创建一个 prop_*
函数并在您的 doctest 中使用它。
从 Doctest's readme 开始,可以将 doctest 与 QuickCheck 一起使用,如下所示:
-- |
-- prop> sort xs == (sort . sort) (xs :: [Int])
我想用多行来描述这个属性,大概像
-- |
-- prop> sort xs ==
-- (sort . sort) (xs :: [Int])
Doctest本身支持多行输入(同样来自readme)
-- |
-- >>> :{
-- let
-- x = 1
-- y = 2
-- in x + y + multiline
-- :}
-- 6
我尝试了几种我想出的类似语法,例如
-- |
-- prop> :{ sort xs ==
-- (sort . sort) (xs :: [Int])
-- }:
没有任何成功。 (在上面的例子中,错误信息是parse error on input '{'
。)
如何在 doctest 中使用 Quickcheck 的多行输入?
截至 2017 年 9 月,doctest does not allow multi-line properties。但是,您可以像往常一样在 doctest 中使用 quickCheck
:
-- >>> import Test.QuickCheck
-- >>> import Data.List (sort)
-- >>> :{
-- quickCheck $ \xs ->
-- sort xs ==
-- (sort . sort) (xs :: [Int])
-- :}
-- +++ OK, passed 100 tests.
这很冗长,但可以让您编写任意长的检查。请注意,您始终可以创建一个 prop_*
函数并在您的 doctest 中使用它。