I'm trying to write a unit test for the simple function that takes a list and just returns it,
func :: [a] -> [a]
func x = x
using the test code to test that it works as expected when given an empty list
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func []
main :: IO Counts
main = runTestTT $ TestList [emptyListTest]
However, I get the error
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 []
Other tests with non-empty lists work fine, and the function works fine when testing manually by calling func []
in ghci
.
I've also noticed that if I create a dummy type, and make a list taking elements of that type (if that's the correct way of saying it), then passing that to the test seems to work, and the test passes
data Dummy = Dummy
deriving(Eq, Show)
emptyList :: [Dummy]
emptyList = []
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func emptyList
Why is this? Is there a way to test functions with an empty list without going down the dummy type route?
Well, the error tells you exactly what is wrong. Read it.
The type variable `a0' is ambiguous
So, type your variable! GHC can't possibly know what type to use to test unless you do.
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func ([] :: [Int])
You may have to enable an extension to do it inline.