|
| 1 | +import System.Directory |
| 2 | +import System.FilePath |
| 3 | +import Text.XML.Light |
| 4 | +import System.IO |
| 5 | +import Text.TeXMath |
| 6 | +import Text.TeXMath.Macros |
| 7 | +import System.Exit |
| 8 | +import Control.Applicative |
| 9 | +import GHC.IO.Encoding (setLocaleEncoding) |
| 10 | + |
| 11 | +data Status = Pass |
| 12 | + | Fail FilePath FilePath String |
| 13 | + | Error FilePath FilePath String |
| 14 | + deriving (Eq, Show) |
| 15 | + |
| 16 | +main :: IO () |
| 17 | +main = do |
| 18 | + setLocaleEncoding utf8 |
| 19 | + setCurrentDirectory "tests" |
| 20 | + texs <- filter (\x -> takeExtension x == ".tex") <$> getDirectoryContents "." |
| 21 | + statuses <- concat <$> mapM runTeXTests texs |
| 22 | + let passes = length $ filter (== Pass) statuses |
| 23 | + let failures = length statuses - passes |
| 24 | + putStrLn $ show passes ++ " tests passed, " ++ show failures ++ " failed." |
| 25 | + if all (== Pass) statuses |
| 26 | + then do |
| 27 | + exitWith ExitSuccess |
| 28 | + else do |
| 29 | + mapM_ printStatus statuses |
| 30 | + exitWith $ ExitFailure failures |
| 31 | + |
| 32 | +printStatus :: Status -> IO () |
| 33 | +printStatus Pass = return () |
| 34 | +printStatus (Fail inp out actual) = do |
| 35 | + putStrLn $ "FAILED: " ++ inp ++ " ==> " ++ out |
| 36 | + putStrLn "------ input ---------" |
| 37 | + readFile inp >>= putStr |
| 38 | + putStrLn "------ expected ------" |
| 39 | + readFile out >>= putStr |
| 40 | + putStrLn "------ actual --------" |
| 41 | + putStr actual |
| 42 | + putStrLn "----------------------" |
| 43 | +printStatus (Error inp out msg) = |
| 44 | + putStrLn $ "ERROR: " ++ inp ++ " ==> " ++ out ++ "\n" ++ msg |
| 45 | + |
| 46 | +runTeXTests :: FilePath -> IO [Status] |
| 47 | +runTeXTests path = do |
| 48 | + xhtmlStatus <- runTest |
| 49 | + (fmap (ppTopElement . inHtml) . texMathToMathML DisplayBlock . parseMacros) |
| 50 | + path (replaceExtension path "xhtml") |
| 51 | + ommlStatus <- runTest |
| 52 | + (fmap ppTopElement . texMathToOMML DisplayBlock . parseMacros) |
| 53 | + path (replaceExtension path "omml") |
| 54 | + return [xhtmlStatus, ommlStatus] |
| 55 | + |
| 56 | +parseMacros :: String -> String |
| 57 | +parseMacros inp = |
| 58 | + let (ms, rest) = parseMacroDefinitions inp |
| 59 | + in applyMacros ms rest |
| 60 | + |
| 61 | +inHtml :: Element -> Element |
| 62 | +inHtml x = |
| 63 | + add_attr (Attr (unqual "xmlns") "http://www.w3.org/1999/xhtml") $ |
| 64 | + unode "html" |
| 65 | + [ unode "head" $ |
| 66 | + add_attr (Attr (unqual "content") "application/xhtml+xml; charset=UTF-8") |
| 67 | + $ add_attr (Attr (unqual "http-equiv") "Content-Type") |
| 68 | + $ unode "meta" () |
| 69 | + , unode "body" x ] |
| 70 | + |
| 71 | +runTest :: (String -> Either String String) |
| 72 | + -> FilePath |
| 73 | + -> FilePath |
| 74 | + -> IO Status |
| 75 | +runTest fn input output = do |
| 76 | + inp_t <- readFile input |
| 77 | + out_t <- readFile output |
| 78 | + case fn inp_t of |
| 79 | + Left msg -> return $ Error input output msg |
| 80 | + Right r -> |
| 81 | + if r == out_t |
| 82 | + then return Pass |
| 83 | + else return $ Fail input output r |
0 commit comments