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

时间:2015-05-24 12:42:04

标签: haskell hunit

我正在尝试编写一个简单的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的源代码,看起来没有为Eq定义Element个实例。 我该如何进行测试?

1 个答案:

答案 0 :(得分:0)

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

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

然后通过assertBool代替assertEqual

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