我正在学习 Haskell,并成为一名优秀的开发人员,边走边写单元测试。我实现了各种排序算法和相应的测试。但是,我觉得单独的测试是多余的,因为输入和输出没有变化,只有用于对输入进行排序的算法是变化的。有没有办法在其他各种单元测试框架中尽可能地创建数据驱动测试或数据表?
module RecursionSpec (main, spec) where
import Test.Hspec
import Recursion
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
let input = [3, 1, 5, 2, 4]
output = [1, 2, 3, 4, 5]
describe "bubblesort" $ do
it ("sorts " ++ show input) $ do
bubblesort input `shouldBe` output
describe "mergesort" $ do
it ("sorts " ++ show input) $ do
mergesort input `shouldBe` output
describe "quicksort" $ do
it ("sorts " ++ show input) $ do
quicksort input `shouldBe` output
此外,我收到以下警告,我想了解并消除这些警告。
warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Show a0)
arising from a use of ‘show’ at test/RecursionSpec.hs:14:21-30
(Eq a0)
arising from a use of ‘shouldBe’ at test/RecursionSpec.hs:15:7-40
(Ord a0)
arising from a use of ‘bubblesort’ at test/RecursionSpec.hs:15:7-22
(Num a0)
arising from the literal ‘1’ at test/RecursionSpec.hs:12:17
(Num a0)
arising from the literal ‘3’ at test/RecursionSpec.hs:11:16
• In the second argument of ‘(++)’, namely ‘show input’
In the first argument of ‘it’, namely ‘("sorts " ++ show input)’
In the expression: it ("sorts " ++ show input)