リストを受け取ってそれを返す単純な関数の単体テストを作成しようとしています。
func :: [a] -> [a]
func x = x
テスト コードを使用して、空のリストが与えられたときに期待どおりに動作することをテストする
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func []
main :: IO Counts
main = runTestTT $ TestList [emptyListTest]
ただし、エラーが発生します
No instance for (Show a0) arising from a use of `assertEqual'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 28 others
In the expression: assertEqual "for (func [])," []
In the second argument of `($)', namely
`assertEqual "for (func [])," [] $ func []'
In the expression:
TestCase $ assertEqual "for (func [])," [] $ func []
空でないリストを使用した他のテストは正常に機能し、 を呼び出して手動でテストすると、関数は正常に機能func []
しghci
ます。
また、ダミーの型を作成し、その型の要素を取得するリストを作成すると (それが正しい言い方である場合)、それをテストに渡すとうまくいくように見え、テストに合格することにも気付きました。
data Dummy = Dummy
deriving(Eq, Show)
emptyList :: [Dummy]
emptyList = []
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func emptyList
どうしてこれなの?ダミータイプのルートをたどらずに空のリストで関数をテストする方法はありますか?