module Test.Framework.Runners.XML.JUnitWriter (
RunDescription(..),
serialize,
#ifdef TEST
morphFlatTestCase, morphNestedTestCase
#endif
) where
import Test.Framework.Core (TestName)
import Test.Framework.Runners.Core (RunTest(..), FinishedTest)
import Data.List ( intercalate )
import Data.Maybe ( fromMaybe )
import Text.XML.Light ( ppTopElement, unqual, unode
, Attr(..), Element(..) )
data RunDescription = RunDescription {
errors :: Int
, failedCount :: Int
, skipped :: Maybe Int
, hostname :: Maybe String
, suiteName :: String
, testCount :: Int
, time :: Double
, timeStamp :: Maybe String
, runId :: Maybe String
, package :: Maybe String
, tests :: [FinishedTest]
} deriving (Show)
serialize :: Bool -> RunDescription -> String
serialize nested = ppTopElement . toXml nested
toXml :: Bool -> RunDescription -> Element
toXml nested runDesc = unode "testsuite" (attrs, morph_cases (tests runDesc))
where
morph_cases | nested = map morphNestedTestCase
| otherwise = concatMap (morphFlatTestCase [])
attrs :: [Attr]
attrs = map (\(x,f)->Attr (unqual x) (f runDesc)) fields
fields = [ ("errors", show . errors)
, ("failures", show . failedCount)
, ("skipped", fromMaybe "" . fmap show . skipped)
, ("hostname", fromMaybe "" . hostname)
, ("name", id . suiteName)
, ("tests", show . testCount)
, ("time", show . time)
, ("timeStamp", fromMaybe "" . timeStamp)
, ("id", fromMaybe "" . runId)
, ("package", fromMaybe "" . package)
]
morphFlatTestCase :: [String] -> FinishedTest -> [Element]
morphFlatTestCase path (RunTestGroup gname testList)
= concatMap (morphFlatTestCase (gname:path)) testList
morphFlatTestCase path (RunTest tName _ res) = [morphOneTestCase cName tName res]
where cName | null path = "<none>"
| otherwise = intercalate "." (reverse path)
morphNestedTestCase :: FinishedTest -> Element
morphNestedTestCase (RunTestGroup gname testList) =
unode "testsuite" (attrs, map morphNestedTestCase testList)
where attrs = [ Attr (unqual "name") gname ]
morphNestedTestCase (RunTest tName _ res) = morphOneTestCase "" tName res
morphOneTestCase :: String -> TestName -> (String, Bool) -> Element
morphOneTestCase cName tName (tout, pass) = case pass of
True -> unode "testcase" caseAttrs
False -> unode "testcase" (caseAttrs, unode "failure" (failAttrs, tout))
where caseAttrs = [ Attr (unqual "name") tName
, Attr (unqual "classname") cName
, Attr (unqual "time") ""
]
failAttrs = [ Attr (unqual "message") ""
, Attr (unqual "type") ""
]