1 -- | Minimal XML functionality needed to parse each document's
10 import Data.Either.Utils ( maybeToEither )
11 import Test.Tasty ( TestTree, testGroup )
12 import Test.Tasty.HUnit ( (@?=), Assertion, testCase )
13 import Text.Read ( readMaybe )
14 import Text.XML.HXT.Core (
24 -- | A tiny parser written in HXT to extract the \"XML_File_ID\"
25 -- element from a document. If we fail to parse an XML_File_ID, we
26 -- return the reason wrapped in a 'Left' constructor. The reason
27 -- should be one of two things:
29 -- 1. No XML_File_ID elements were found.
31 -- 2. An XML_File_ID element was found, but it could not be read
34 -- We use an Either rather than a Maybe because we do expect some
35 -- non-integer XML_File_IDs. In the examples, you will see
36 -- NHL_DepthChart_XML.XML with an XML_File_ID of \"49618.61\" and
37 -- CFL_Boxscore_XML1.xml with an XML_File_ID of
38 -- \"R28916\". According to Brijesh Patel of TSN, these are special
39 -- category files and not part of the usual feed.
41 -- We want to report them differently, \"just in case.\"
43 parse_xmlfid :: String -- ^ The XML Document
44 -> Either String Integer
47 [] -> Left "No XML_File_ID elements found."
50 parse :: String -> [String]
54 /> hasName "XML_File_ID"
58 read_either_integer :: String -> Either String Integer
59 read_either_integer s =
60 let msg = "Could not parse XML_File_ID " ++ s ++ " as an integer."
62 maybeToEither msg (readMaybe s)
65 parse_results = map read_either_integer elements
72 -- | A list of all tests for this module.
81 -- | Ensure that we parse the correct XML_File_ID out of some known
84 xml_file_id_tests :: TestTree
86 testCase "XML_File_ID is parsed correctly" $ do
87 let xmlfids = ["19908216", "19908216", "19908245", "19908246", "19908247"]
90 check :: String -> Assertion
92 xml <- readFile ("test/xml/" ++ xmlfid ++ ".xml")
93 let actual = parse_xmlfid xml
94 -- The maybeToEither should always succeed here, so the error
95 -- message goes unused.
96 let expected = maybeToEither "derp" (readMaybe xmlfid)