1

我正在尝试编写一个简单hunit-test的方法来验证 aText.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看起来没有EqElement. 我怎样才能使测试工作?

4

1 回答 1

0

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

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

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

assertBool "create an element" $ equalElement expectedElement (createElement tg txt)
于 2016-04-07T09:30:29.050 回答