我正在尝试编写一个简单的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
个实例。
我该如何进行测试?
答案 0 :(得分:0)
您可以通过编写自己的等式谓词来避免孤立实例:
equalElement :: XML.Element -> XML.Element -> Bool
equalElement = ...
然后通过assertBool
代替assertEqual
:
assertBool "create an element" $ equalElement expectedElement (createElement tg txt)