1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
module Text.XML.Expat.UnitTests where
import Text.XML.Expat.Tree hiding (parse)
import qualified Text.XML.Expat.Tree as Tree
import Text.XML.Expat.SAX (SAXEvent(..))
import qualified Text.XML.Expat.SAX as SAX
import Text.XML.Expat.Cursor
import Text.XML.Expat.Format
import Text.XML.Expat.Extended (LDocument)
import qualified Text.XML.Expat.Extended as Extended
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy.Char8 as LC
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T
import Foreign
import Foreign.C
import Data.ByteString.Internal (c2w, w2c)
import Data.Char
import Data.Maybe
import Data.Monoid
import Data.IORef
import Control.Applicative
import Control.Arrow (first)
import Control.Exception as E
import Control.Monad
import Control.DeepSeq
import Test.HUnit hiding (Node)
import System.IO
import Test.Framework.Providers.HUnit (hUnitTestToTests)
toByteStringL :: String -> L.ByteString
toByteStringL = L.pack . map c2w
fromByteStringL :: L.ByteString -> String
fromByteStringL = map w2c . L.unpack
toByteString :: String -> B.ByteString
toByteString = B.pack . map c2w
fromByteString :: B.ByteString -> String
fromByteString = map w2c . B.unpack
testDoc :: (Show tag, Show text) =>
(ParseOptions tag text
-> bs
-> Either XMLParseError (Node tag text))
-> (Node tag text -> L.ByteString)
-> (String -> bs)
-> String
-> Int
-> String
-> IO ()
testDoc parseFn fmt toBS descr0 idx xml = do
let eTree = parseFn popts (toBS xml)
descr = descr0++" #"++show idx
case eTree of
Right tree -> do
let out = fromByteStringL $ fmt tree
assertEqual descr xml out
Left error -> do
hPutStrLn stderr $ "parse failed: "++show error
assertFailure descr
where
popts = defaultParseOptions { overrideEncoding = Just UTF8 }
eitherify f mEnc bs = do
case f mEnc bs of
(_, Just err) -> Left err
(doc, Nothing) -> Right doc
test_error1 :: IO ()
test_error1 = do
let eDoc = Tree.parse' defaultParseOptions (toByteString "<hello></goodbye>") :: Either XMLParseError (UNode String)
assertEqual "error1" (Left $ XMLParseError "mismatched tag" (XMLParseLocation 1 9 9 0)) eDoc
test_error2 :: IO ()
test_error2 = do
assertEqual "error2" (
Element {eName = "hello", eAttributes = [], eChildren = []},
Just (XMLParseError "mismatched tag" (XMLParseLocation 1 9 9 0))
) (Tree.parse defaultParseOptions
(toByteStringL "<hello></goodbye>") :: (UNode String, Maybe XMLParseError))
test_error3 :: IO ()
test_error3 =
assertEqual "error3" (
Element {eName = "open", eAttributes = [], eChildren = [
Element {eName = "test1", eAttributes = [], eChildren = [Text "Hello"]},
Element {eName = "hello", eAttributes = [], eChildren = []}
]},
Just (XMLParseError "mismatched tag" (XMLParseLocation 1 35 35 0))
) $ Tree.parse defaultParseOptions
(toByteStringL "<open><test1>Hello</test1><hello></goodbye>")
test_error4 :: IO ()
test_error4 = do
let eDoc = Tree.parse' defaultParseOptions (toByteString "!") :: Either XMLParseError (UNode String)
assertEqual "error1" (Left $ XMLParseError "not well-formed (invalid token)"
(XMLParseLocation 1 0 0 0)) eDoc
test_entities1 = do
assertEqual "parse error" merr Nothing
assertEqual "entity substitution" (Text "foo") c
where
xml = "<root>&entity;</root>"
popts = defaultParseOptions { entityDecoder = Just entityLookup }
(tree,merr) = Tree.parse popts $ toByteStringL xml
c = current $ fromJust $ firstChild $ fromTree tree
entityLookup b = if b == "entity"
then Just "foo"
else Nothing
test_entities2 = do
assertEqual "wrong answer" (Element "html" [] [Text "\228"], Nothing) pr
where
pr :: (UNode String, Maybe XMLParseError)
pr = Tree.parse opt $ LC.pack "<html>ä</html>"
where
opt = defaultParseOptions
{ entityDecoder = Just ed }
ed "auml" = Just "\228"
ed _ = Nothing
test_textContent = do
let tree = Element "cheese" [("type", "edam")]
[Text "You don't actually ",
Element "sub" [] [Text "have any "],
Text "cheese at all",
Text ", do you?"]
assertEqual "textContent" "You don't actually have any cheese at all, do you?" (textContent tree)
testXMLFile :: IO String
testXMLFile = do
s <- map w2c . B.unpack <$> B.readFile "test.xml"
-- Remove trailing newline
return (reverse . dropWhile (== '\n') . reverse $ s)
test_indent = do
let tests = [
("#1",
toByteString "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test/>",
toByteString "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test/>"),
("#2",
toByteString "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test>With some text in it</test>",
toByteString "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test>With some text in it</test>"),
("#3",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test><ignorance/><freedom/><war/></test>",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test>\n <ignorance/>\n <freedom/>\n <war/>\n</test>"),
("#4",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test><ignorance>strength</ignorance><freedom>Slavery</freedom><war>Peace</war></test>",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test>\n <ignorance>strength</ignorance>\n <freedom>Slavery</freedom>\n <war>Peace</war>\n</test>"),
("#5",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test>Extra here<ministries><mini name=\"minitrue\">Ministry of Truth</mini>In between"++
"<mini name=\"minilove\">Ministry of Love</mini>\n And some more"++
"<mini name=\"miniplenty\">Ministry of Plenty</mini>"++
"<mini name=\"minipax\">Ministry of Peace<at-war-with>Eurasia</at-war-with></mini></ministries>"++
"<wisdom><ignorance>strength</ignorance></wisdom></test>",
toByteString $ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test>Extra here\n <ministries>\n <mini name=\"minitrue\">Ministry of Truth</mini>In between"++
"\n <mini name=\"minilove\">Ministry of Love</mini>And some more"++
"\n <mini name=\"miniplenty\">Ministry of Plenty</mini>"++
"\n <mini name=\"minipax\">Ministry of Peace\n <at-war-with>Eurasia</at-war-with>\n </mini>\n </ministries>"++
"\n <wisdom>\n <ignorance>strength</ignorance>\n </wisdom>\n</test>")
]
forM_ tests $ \(name, inp, outSB) -> do
let eree = Tree.parse' defaultParseOptions inp :: Either XMLParseError (UNode String)
case eree of
Left err -> assertFailure $ show err
Right tree -> do
let outIS = format' (indent 2 tree)
assertEqual name outSB outIS
test_setAttribute :: IO ()
test_setAttribute = do
assertEqual "#1" [("abc", "def")] $ getAttributes $
setAttribute "abc" "def"
(Element "test" [] [])
assertEqual "#2" [("abc", "def")] $ getAttributes $
setAttribute "abc" "def"
(Element "test" [("abc", "xyzzy")] [])
assertEqual "#2" [("abc", "def"), ("abc", "xyzzy")] $ getAttributes $
setAttribute "abc" "def"
(Element "test" [("abc", "zapf"), ("abc", "xyzzy")] [])
assertEqual "#3" [("zanzi", "zapf"), ("bar", "xyzzy"), ("abc", "def")] $ getAttributes $
setAttribute "abc" "def"
(Element "test" [("zanzi", "zapf"), ("bar", "xyzzy")] [])
assertEqual "#4" [("zanzi", "zapf"), ("bar", "xyzzy")] $ getAttributes $
deleteAttribute "abc"
(Element "test" [("zanzi", "zapf"), ("bar", "xyzzy"), ("abc", "def")] [])
assertEqual "#5" [("zanzi", "zapf"), ("abc", "def")] $ getAttributes $
deleteAttribute "bar"
(Element "test" [("zanzi", "zapf"), ("bar", "xyzzy"), ("abc", "def")] [])
assertEqual "#6" [("zanzi", "zapf"), ("bar", "xyzzy"), ("abc", "def")] $ getAttributes $
deleteAttribute "bumpf"
(Element "test" [("zanzi", "zapf"), ("bar", "xyzzy"), ("abc", "def")] [])
simpleDocs = [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<test xmlns:abc=\"http://blacksapphire.com/abc\"><abc:test1 type=\"expression\">Cat & mouse</abc:test1><test2 type=\"communication\" language=\"Rhyming slang\">Dog & bone</test2></test>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"++
"<second><test><test1 type=\"expression\">Cat & mouse</test1><test2 type=\"communication\" language=\"Rhyming slang\">Dog & bone</test2></test><test>Rose & Crown</test></second>",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test>Cat & mouse</test>"
]
data ParseFormatTest = ParseFormatTest {
}
test_xmlDecl1 :: IO ()
test_xmlDecl1 = do
assertEqual "plain" [XMLDeclaration "1.0" Nothing Nothing,StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\"?><hello/>") :: [SAXEvent String String])
assertEqual "withEnc" [XMLDeclaration "1.0" (Just "UTF-8") Nothing,StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello/>") :: [SAXEvent String String])
assertEqual "SA0" [XMLDeclaration "1.0" Nothing (Just False),StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\" standalone=\"no\"?><hello/>") :: [SAXEvent String String])
assertEqual "SA1" [XMLDeclaration "1.0" Nothing (Just True),StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\" standalone=\"yes\"?><hello/>") :: [SAXEvent String String])
assertEqual "SA0enc" [XMLDeclaration "1.0" (Just "UTF-8") (Just False),StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><hello/>") :: [SAXEvent String String])
assertEqual "SA1enc" [XMLDeclaration "1.0" (Just "UTF-8") (Just True),StartElement "hello" [],EndElement "hello"]
(SAX.parse defaultParseOptions (LC.pack "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><hello/>") :: [SAXEvent String String])
normalizeSAXText :: Monoid t => [SAXEvent s t] -> [SAXEvent s t]
normalizeSAXText (CharacterData a:CharacterData b:xs) = normalizeSAXText (CharacterData (a `mappend` b):xs)
normalizeSAXText (x:xs) = x:normalizeSAXText xs
normalizeSAXText [] = []
test_various :: IO ()
test_various =
assertEqual "var1" [
StartElement "test" [],
StartElement "sample" [("id","5")],
CharacterData "This \"text with quotations\" should be escaped.",
EndElement "sample",
StartElement "mytest" [],
CharacterData "//",
StartCData,
CharacterData "This \"text with quotations\" should not be escaped.//",
EndCData,
EndElement "mytest",
ProcessingInstruction "php" "somecode(); ",
Comment " this is a comment ",
EndElement "test"
]
(normalizeSAXText $ SAX.parse defaultParseOptions (LC.pack variousText) :: [SAXEvent String String])
variousText =
"<test>"++
"<sample id=\"5\">This \"text with quotations\" should be escaped.</sample>"++
"<mytest>"++
"//<![CDATA["++
"This \"text with quotations\" should not be escaped."++
"//]]>"++
"</mytest>"++
"<?php somecode(); ?>"++
"<!-- this is a comment -->"++
"</test>"
quotationOut =
"<test><sample id=\"5\">This "text with quotations" should be escaped.</sample>"++
"<mytest>//<![CDATA[This \"text with quotations\" should not be escaped.//]]>"++
"</mytest><?php somecode(); ?><!-- this is a comment --></test>"
test_quotation =
assertEqual "quotation"
(quotationOut, Nothing)
$ first (C.unpack . mconcat . LC.toChunks . formatDocument)
$ (Extended.parse defaultParseOptions (LC.pack variousText) :: (LDocument String String, Maybe XMLParseError))
tests = hUnitTestToTests $
TestList [
t' ("String",
Tree.parse' :: ParseOptions String String
-> B.ByteString
-> Either XMLParseError (Node String String),
format),
t' ("ByteString",
Tree.parse' :: ParseOptions B.ByteString B.ByteString
-> B.ByteString
-> Either XMLParseError (Node B.ByteString B.ByteString),
format),
t' ("Text",
Tree.parse' :: ParseOptions T.Text T.Text
-> B.ByteString
-> Either XMLParseError (Node T.Text T.Text),
format),
t ("String/Lazy",
eitherify $ Tree.parse :: ParseOptions String String
-> L.ByteString
-> Either XMLParseError (Node String String),
format),
t ("ByteString/Lazy",
eitherify $ Tree.parse :: ParseOptions B.ByteString B.ByteString
-> L.ByteString
-> Either XMLParseError (Node B.ByteString B.ByteString),
format),
t ("Text/Lazy",
eitherify $ Tree.parse :: ParseOptions T.Text T.Text
-> L.ByteString
-> Either XMLParseError (Node T.Text T.Text),
format),
TestLabel "error1" $ TestCase $ test_error1,
TestLabel "error2" $ TestCase $ test_error2,
TestLabel "error3" $ TestCase $ test_error3,
TestLabel "error4" $ TestCase $ test_error4,
TestLabel "entities1" $ TestCase $ test_entities1,
TestLabel "entities2" $ TestCase $ test_entities2,
TestLabel "textContent" $ TestCase $ test_textContent,
TestLabel "indent" $ TestCase $ test_indent,
TestLabel "setAttribute" $ TestCase $ test_setAttribute,
TestLabel "xmlDecl1" $ TestCase $ test_xmlDecl1,
TestLabel "various" $ TestCase $ test_various,
TestLabel "quotation" $ TestCase $ test_quotation
]
where
t (descr, parse, fmt) = TestLabel descr $ TestCase $ do
f <- testXMLFile
let docs = f:simpleDocs
forM_ (zip [1..] docs) $ \(idx, doc) ->
testDoc parse fmt toByteStringL descr idx doc
t' (descr, parse, fmt) = TestLabel descr $ TestCase $ do
f <- testXMLFile
let docs = f:simpleDocs
forM_ (zip [1..] docs) $ \(idx, doc) ->
testDoc parse fmt toByteString descr idx doc
|