1 -- | Minimal XML functionality needed to parse each document's
9 import Data.Either.Utils ( maybeToEither )
10 import Test.Tasty ( TestTree, testGroup )
11 import Test.Tasty.HUnit ( (@?=), Assertion, testCase )
12 import Text.Read ( readMaybe )
13 import Text.XML.HXT.Core (
23 -- | A tiny parser written in HXT to extract the "XML_File_ID" element
24 -- from a document. If we fail to parse an XML_File_ID, we return
25 -- the reason wrapped in a 'Left' constructor. The reason should be
28 -- 1. No XML_File_ID elements were found.
30 -- 2. An XML_File_ID element was found, but it could not be read
33 -- We use an Either rather than a Maybe because we do expect some
34 -- non-integer XML_File_IDs. In the examples, you will see
35 -- NHL_DepthChart_XML.XML with an XML_File_ID of "49618.61" and
36 -- CFL_Boxscore_XML1.xml with an XML_File_ID of "R28916". According
37 -- to Brijesh Patel of TSN, these are special category files and not
38 -- part of the usual feed.
40 -- We want to report them differently, "just in case."
42 parse_xmlfid :: String -- ^ The XML Document
43 -> Either String Integer
46 [] -> Left "No XML_File_ID elements found."
49 parse :: String -> [String]
53 /> hasName "XML_File_ID"
57 read_either_integer :: String -> Either String Integer
58 read_either_integer s =
59 let msg = "Could not parse XML_File_ID" ++ s ++ " as an integer."
61 maybeToEither msg (readMaybe s)
64 parse_results = map read_either_integer elements
75 xml_file_id_tests :: TestTree
77 testCase "XML_File_ID is parsed correctly" $ do
78 let xmlfids = ["19908216", "19908216", "19908245", "19908246", "19908247"]
81 check :: String -> Assertion
83 xml <- readFile ("test/xml/" ++ xmlfid ++ ".xml")
84 let actual = parse_xmlfid xml
85 -- The maybeToEither should always succeed here, so the error
86 -- message goes unused.
87 let expected = maybeToEither "derp" (readMaybe xmlfid)