如果测试中存在“Nothing == Nothing”条件,则HUnit不允许编译测试用例

时间:2017-07-22 13:01:15

标签: haskell maybe hunit

我遇到了一种奇怪的HUnit行为。如果测试中存在Nothing == Nothing条件,则不允许编译测试用例。这是我的代码重现了这种行为:

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

尝试在ghci中使用此内容加载文件时返回以下错误:

[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.

请注意,同一测试用例中的条件Just 2 == Just 2正常工作。如果我在Nothing == Nothing中输入ghci,则会按预期返回True

HUnit可能以这种方式行事的任何想法?这是一个错误还是预期的行为?

1 个答案:

答案 0 :(得分:3)

问题是你指定了两个a,因此这些都没有暗示Nothing的类型。当然,你可以推断(==)并不重要。但是Haskell没有这样的理由:它对“我应该指出什么testTests = test [ "test A01" ~: "x == x" ~: True ~=? (Nothing :: Maybe Int) == Nothing, "test _" ~: "empty test" ~: True ~=? True ]函数感兴趣?”。

您可以通过使类型显式来解决问题。例如:

{{1}}