on master help sign in
Add tests for nodeChildrenBy and nodeAttributeBy
c30c21dNeil Mitchell
src/Main.hs+10
@@ -33,6 +33,16 @@ main = do
33 let Right d = documentParse r
33 let Right d = documentParse r
34 when (r /= documentRender d) $ fail "Different after rerendering"
34 when (r /= documentRender d) $ fail "Different after rerendering"
35
35
36+ let Right doc = fmap documentNode $ documentParse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
37+ map nodeName (nodeChildren doc) === ["test","test","b","test","test"]
38+ length (nodeChildrenBy doc "test") === 4
39+ length (nodeChildrenBy doc "b") === 1
40+ length (nodeChildrenBy doc "extra") === 0
41+ nodeAttributes (head $ nodeChildren doc) === [Attribute "id" "1", Attribute "extra" "2"]
42+ map (`nodeAttributeBy` "id") (nodeChildrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
43+
44+
45+a === b = if a == b then putStrLn "success" else fail "mismatch"
36
46
37rerender :: Document -> BS.ByteString
47rerender :: Document -> BS.ByteString
38rerender = contents . documentNode
48rerender = contents . documentNode
src/Text/XML/Hexml.hs+1−1
@@ -58,7 +58,7 @@ data Document = Document BS.ByteString (ForeignPtr CDocument)
58
58
59data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
59data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
60
60
61−data Attribute = Attribute BS.ByteString BS.ByteString deriving Show
61+data Attribute = Attribute BS.ByteString BS.ByteString deriving (Show, Eq, Ord)
62
62
63instance Show Document where
63instance Show Document where
64 show d = "Document " ++ show (BS.unpack $ nodeOuter $ documentNode d)
64 show d = "Document " ++ show (BS.unpack $ nodeOuter $ documentNode d)