haskelloption-typehunit

HUnit does not allow to compile test cases if `Nothing == Nothing` condition is present in the test


I have encountered a weird HUnit behavior. It does not allow to compile test cases if Nothing == Nothing condition is present in the test. Here is my code that reproduces this behavior:

module TestTest where 

import Control.Exception
import Control.Monad
import Test.HUnit
import Test.AssertError

testTests = test [ 
    "test A01"  ~: "x == x" ~: True ~=? Nothing == Nothing,
    "test _"    ~: "empty test" ~: True ~=? True
    ]

runTests :: IO Counts
runTests = do
    runTestTT testTests

Attempt to load the file with this contents in ghci returns following error:

[2 of 2] Compiling TestTest         ( Test/TestTest.hs, interpreted )

Test/TestTest.hs:9:49:
    No instance for (Eq a0) arising from a use of ‘==’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Eq Counts -- Defined in ‘Test.HUnit.Base’
      instance Eq Node -- Defined in ‘Test.HUnit.Base’
      instance Eq State -- Defined in ‘Test.HUnit.Base’
      ...plus 53 others
    In the second argument of ‘(~=?)’, namely ‘Nothing == Nothing’
    In the second argument of ‘(~:)’, namely
      ‘True ~=? Nothing == Nothing’
    In the second argument of ‘(~:)’, namely
      ‘"x == x" ~: True ~=? Nothing == Nothing’
Failed, modules loaded: Test.AssertError.

Note that condition Just 2 == Just 2 in the same test case works fine. If I type Nothing == Nothing in ghci, it returns True as expected.

Any ideas why HUnit might behave this way? Is this a bug or expected behavior?


Solution

  • The problem is that you specify two Nothings, and none of these thus hint what the type of a will be. Of course you can reason that for Nothing it does not matter. But Haskell does not reason that way: it is interested in "to what (==) function should I point?".

    You can resolve the problem by making the type explicit. For instance:

    testTests = test [ 
      "test A01"  ~: "x == x" ~: True ~=? (Nothing :: Maybe Int) == Nothing,
      "test _"    ~: "empty test" ~: True ~=? True
      ]