我有一个简单的数据结构可以在 smallcheck 中进行测试。
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
import Test.Tasty
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import GHC.Generics
data T1 = T1 { p1 :: Int,
p2 :: Char,
p3 :: [Int]
} deriving (Eq, Show, Generic)
instance Monad m => Serial m T1
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Tests" [scProps]
scProps = testGroup "(checked by SmallCheck)"
[ testProperty "Test1" prop_test1
]
prop_test1 x = x == x
where types = (x :: T1)
在运行测试时,是否有任何通用的解决方案来为(单个)测试设置
Depth
参数,或者更好的是,一个细粒度的解决方案来为单个字段设置 Depth
参数,例如将 p3
的深度限制为 2 以避免搜索空间的组合爆炸?提前谢谢了!
朱尔斯
信息:
一个有点相关的问题是 here 。
编辑:
我采用了 Roman Cheplyaka 接受的答案中给出的解决方案,并在一个最小的工作示例中实现了它们(谢谢,Roman):
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
import Test.Tasty
import Test.Tasty.Options
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import Data.Functor
-- =============================================================================
main :: IO ()
main = defaultMain tests
-- =============================================================================
-- the data structure to be tested
data T1 = T1 { p1 :: Int,
p2 :: Char,
p3 :: Int,
p5 :: [Int]
} deriving (Show, Eq)
-- =============================================================================
-- some test-properties
prop_test1 x y = x == y
where types = (x :: T1, y :: T1)
prop_test2 x = x == x
where types = (x :: T1)
-- =============================================================================
-- how to change the depth of the search spaces?
{-| I Possibility: Command Line |-}
-- foo@bar$ runhaskell Test.hs --smallcheck-depth 2
-- -----------------------------------------------------------------------------
{-| II Possibility: |-}
-- normal:
-- tests :: TestTree
-- tests = testGroup "Tests" [scProps]
-- custom:
tests :: TestTree
tests = localOption d $ testGroup "Tests" [scProps]
where d = 3 :: SmallCheckDepth
scProps = testGroup "(checked by SmallCheck)"
[ testProperty "Test1" prop_test1,
testProperty "Test2" prop_test2
]
-- -----------------------------------------------------------------------------
{-| III Possibility: Adjust Depth when making the type instance of Serial |-}
-- normal:
-- instance (Monad m) => Serial m T1 where
-- series = T1 <$> series <~> series <~> series <~> series
-- custom:
instance (Monad m) => Serial m T1 where
series = localDepth (const 4) $ T1 <$> (localDepth (const 2) series) <~> series <~> series <~> (decDepth series)
-- (a few more examples):
-- instance (Monad m) => Serial m T1 where
-- series = decDepth $ T1 <$> series <~> series <~> series <~> (decDepth series )
-- instance (Monad m) => Serial m T1 where
-- series = localDepth (const 3) $ T1 <$> series <~> series <~> series <~> series
-- instance (Monad m) => Serial m T1 where
-- series = localDepth (const 4) $ T1 <$> series <~> series <~> series <~> (decDepth series)
最佳答案
我将首先列出 $$$ 和 smallcheck 提供的开箱即用的控制方式:
--smallcheck-depth
选项来控制 «common» 深度 adjustOption
来调整任何子树的深度,相对于在命令行(或父树)上指定的内容(或父树) localDepth
来调整每个特定字段所需的深度,相对于整个结构的深度。通常它是整体深度 - 1(这是 decDepth
在标准定义中所做的),但您可以发明自己的规则。 因此,这种控制是可能的,尽管是间接的。通常这就是你想要的——另见 this answer 。
如果你真的想在运行时控制单个字段的深度......是的,这是可能的,虽然它有点复杂,因为它不是一个预期的用例。 (不过,这真的很酷!) Here's a working example.
如果这个功能对你很重要,也许在 github 上打开一个问题并解释你为什么需要它。 (但首先,请务必阅读我对上面链接的 ocharles 的回复。您很少需要在运行时调整深度。)