测试中 Text.XML.Light.Element 的比较

Comparison for Text.XML.Light.Element in test

我正在尝试编写一个简单的 hunit-test 来验证如何从函数创建 Text.XML.Light.Element

函数为:

createElement :: String -> String -> XML.Element
createElement tg txt =
 blank_element{ elName = qualName tg
          , elContent = [ Text blank_cdata { cdData = txt } ]
          }

我的测试很简单:

import Test.Framework (defaultMain)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, assertEqual)
import Text.XML.Light as XML
import MyModule.MyCode (createElement)

testCreateElement :: Assertion
testCreateElement = do
    let tg = "item"
    let txt = "example of text"
    let expectedElement = XML.Element {
                           elName = XML.QName{ qName = tg, qURI = Nothing, qPrefix = Nothing }
                           , elAttribs = [] :: [ Attr ]
                           , elContent = [ fromString txt ] :: [ Content ]
                           , elLine = Nothing
                           }
    assertEqual "create an element" expectedElement (createElement tg txt)

-- utils

fromString :: String -> Content
fromString txt = Text blank_cdata { cdData = txt }

我得到的是这个错误:

No instance for (Eq Element) arising from a use of ‘assertEqual’
    In a stmt of a 'do' block:
      assertEqual "create an element" expectedElement (xmlElement tg txt)
    In the expression:....

我查看了 Text.XML.Light 的源代码,似乎没有为 Element 定义的 Eq 实例。 我怎样才能使测试工作?

您可以通过编写自己的相等谓词来避免孤立实例:

equalElement :: XML.Element -> XML.Element -> Bool
equalElement = ...

然后通过 assertBool 而不是 assertEqual 使用它:

assertBool "create an element" $ equalElement expectedElement (createElement tg txt)