2008-08-09 18:51:08 +02:00
|
|
|
{-# OPTIONS_GHC -Wall #-}
|
|
|
|
-- RunTests.hs - run test suite for pandoc
|
|
|
|
-- This script is designed to be run from the tests directory.
|
|
|
|
-- It assumes the pandoc executable is in dist/build/pandoc.
|
2009-02-14 17:10:19 +01:00
|
|
|
--
|
|
|
|
-- runhaskell -i.. RunTests.hs [lhs]
|
|
|
|
--
|
|
|
|
-- If the lhs argument is provided, tests for lhs support will be
|
|
|
|
-- run. These presuppose that pandoc has been compiled with the
|
|
|
|
-- -fhighlighting flag, so these tests are not run by default.
|
2009-12-31 02:08:30 +01:00
|
|
|
--
|
|
|
|
-- This program assumes that the Diff package has been installed:
|
|
|
|
-- cabal install Diff
|
2008-08-09 18:51:08 +02:00
|
|
|
|
|
|
|
module Main where
|
|
|
|
import System.IO.UTF8
|
2009-12-31 02:08:30 +01:00
|
|
|
import System.IO ( openTempFile, stderr, stdout, hFlush )
|
2008-08-09 18:51:08 +02:00
|
|
|
import Prelude hiding ( putStrLn, putStr, readFile )
|
|
|
|
import System.Process ( runProcess, waitForProcess )
|
|
|
|
import System.FilePath ( (</>), (<.>) )
|
|
|
|
import System.Directory
|
2009-02-14 17:10:19 +01:00
|
|
|
import System.Environment
|
2008-08-09 18:51:08 +02:00
|
|
|
import System.Exit
|
|
|
|
import Text.Printf
|
2009-12-31 02:08:30 +01:00
|
|
|
import Data.Algorithm.Diff
|
2008-08-09 18:51:08 +02:00
|
|
|
|
|
|
|
pandocPath :: FilePath
|
|
|
|
pandocPath = ".." </> "dist" </> "build" </> "pandoc" </> "pandoc"
|
|
|
|
|
|
|
|
data TestResult = TestPassed
|
|
|
|
| TestError ExitCode
|
|
|
|
| TestFailed [(DI, String)]
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
instance Show TestResult where
|
|
|
|
show TestPassed = "PASSED"
|
|
|
|
show (TestError ec) = "ERROR " ++ show ec
|
|
|
|
show (TestFailed d) = "FAILED\n" ++ showDiff d
|
|
|
|
|
|
|
|
showDiff :: [(DI, String)] -> String
|
|
|
|
showDiff [] = ""
|
|
|
|
showDiff ((F, ln) : ds) = "|TEST| " ++ ln ++ "\n" ++ showDiff ds
|
|
|
|
showDiff ((S, ln) : ds) = "|NORM| " ++ ln ++ "\n" ++ showDiff ds
|
|
|
|
showDiff ((B, _ ) : ds) = showDiff ds
|
|
|
|
|
|
|
|
writerFormats :: [String]
|
|
|
|
writerFormats = [ "native"
|
|
|
|
, "html"
|
|
|
|
, "docbook"
|
|
|
|
, "opendocument"
|
|
|
|
, "latex"
|
|
|
|
, "context"
|
|
|
|
, "texinfo"
|
|
|
|
, "man"
|
|
|
|
, "markdown"
|
|
|
|
, "rst"
|
|
|
|
, "mediawiki"
|
|
|
|
, "rtf"
|
|
|
|
]
|
|
|
|
|
2009-02-07 20:20:49 +01:00
|
|
|
lhsWriterFormats :: [String]
|
|
|
|
lhsWriterFormats = [ "markdown"
|
|
|
|
, "markdown+lhs"
|
|
|
|
, "rst"
|
|
|
|
, "rst+lhs"
|
|
|
|
, "latex"
|
|
|
|
, "latex+lhs"
|
|
|
|
, "html"
|
|
|
|
, "html+lhs"
|
|
|
|
]
|
|
|
|
|
2009-02-14 05:08:06 +01:00
|
|
|
lhsReaderFormats :: [String]
|
|
|
|
lhsReaderFormats = [ "markdown+lhs"
|
|
|
|
, "rst+lhs"
|
|
|
|
, "latex+lhs"
|
|
|
|
]
|
|
|
|
|
2008-08-09 18:51:08 +02:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2009-02-14 17:10:19 +01:00
|
|
|
args <- getArgs
|
|
|
|
let runLhsTests = "lhs" `elem` args
|
2008-08-09 18:51:08 +02:00
|
|
|
r1s <- mapM runWriterTest writerFormats
|
|
|
|
r2 <- runS5WriterTest "basic" ["-s"] "s5"
|
|
|
|
r3 <- runS5WriterTest "fancy" ["-s","-m","-i"] "s5"
|
|
|
|
r4 <- runS5WriterTest "fragment" [] "html"
|
|
|
|
r5 <- runS5WriterTest "inserts" ["-s", "-H", "insert",
|
|
|
|
"-B", "insert", "-A", "insert", "-c", "main.css"] "html"
|
|
|
|
r6 <- runTest "markdown reader" ["-r", "markdown", "-w", "native", "-s", "-S"]
|
|
|
|
"testsuite.txt" "testsuite.native"
|
|
|
|
r7 <- runTest "markdown reader (tables)" ["-r", "markdown", "-w", "native"]
|
|
|
|
"tables.txt" "tables.native"
|
2008-08-11 01:26:32 +02:00
|
|
|
r7a <- runTest "markdown reader (more)" ["-r", "markdown", "-w", "native"]
|
|
|
|
"markdown-reader-more.txt" "markdown-reader-more.native"
|
2008-08-09 18:51:08 +02:00
|
|
|
r8 <- runTest "rst reader" ["-r", "rst", "-w", "native", "-s", "-S"]
|
|
|
|
"rst-reader.rst" "rst-reader.native"
|
2010-02-20 09:30:34 +01:00
|
|
|
r8a <- runTest "rst reader (tables)" ["-r", "rst", "-w", "native"]
|
|
|
|
"tables.rst" "tables-rstsubset.native"
|
2008-08-09 18:51:08 +02:00
|
|
|
r9 <- runTest "html reader" ["-r", "html", "-w", "native", "-s"]
|
|
|
|
"html-reader.html" "html-reader.native"
|
|
|
|
r10 <- runTest "latex reader" ["-r", "latex", "-w", "native", "-s", "-R"]
|
|
|
|
"latex-reader.latex" "latex-reader.native"
|
|
|
|
r11 <- runTest "native reader" ["-r", "native", "-w", "native", "-s"]
|
|
|
|
"testsuite.native" "testsuite.native"
|
2009-02-14 17:10:19 +01:00
|
|
|
r12s <- if runLhsTests
|
|
|
|
then mapM runLhsWriterTest lhsWriterFormats
|
2009-02-14 17:25:39 +01:00
|
|
|
else putStrLn "Skipping lhs writer tests because they presuppose highlighting support" >> return []
|
2009-02-14 17:10:19 +01:00
|
|
|
r13s <- if runLhsTests
|
|
|
|
then mapM runLhsReaderTest lhsReaderFormats
|
2009-02-14 17:25:39 +01:00
|
|
|
else putStrLn "Skipping lhs reader tests because they presuppose highlighting support" >> return []
|
2010-02-27 04:57:03 +01:00
|
|
|
let results = r1s ++
|
|
|
|
[ r2, r3, r4, r5 -- S5
|
|
|
|
, r6, r7, r7a -- markdown reader
|
|
|
|
, r8, r8a -- rst
|
|
|
|
, r9 -- html
|
|
|
|
, r10 -- latex
|
|
|
|
, r11 -- native
|
|
|
|
] ++ r12s ++ r13s
|
2008-08-09 18:51:08 +02:00
|
|
|
if all id results
|
|
|
|
then do
|
|
|
|
putStrLn "\nAll tests passed."
|
|
|
|
exitWith ExitSuccess
|
|
|
|
else do
|
|
|
|
let failures = length $ filter not results
|
|
|
|
putStrLn $ "\n" ++ show failures ++ " tests failed."
|
|
|
|
exitWith (ExitFailure failures)
|
|
|
|
|
2008-08-23 20:55:08 +02:00
|
|
|
-- makes sure file is fully closed after reading
|
|
|
|
readFile' :: FilePath -> IO String
|
|
|
|
readFile' f = do s <- readFile f
|
|
|
|
return $! (length s `seq` s)
|
|
|
|
|
2009-02-07 20:20:49 +01:00
|
|
|
runLhsWriterTest :: String -> IO Bool
|
|
|
|
runLhsWriterTest format =
|
|
|
|
runTest ("(lhs) " ++ format ++ " writer") ["-r", "native", "-s", "-w", format] "lhs-test.native" ("lhs-test" <.> format)
|
|
|
|
|
2009-02-14 05:08:06 +01:00
|
|
|
runLhsReaderTest :: String -> IO Bool
|
|
|
|
runLhsReaderTest format =
|
|
|
|
runTest ("(lhs) " ++ format ++ " reader") ["-r", format, "-w", "html+lhs"] ("lhs-test" <.> format) "lhs-test.fragment.html+lhs"
|
|
|
|
|
2008-08-09 18:51:08 +02:00
|
|
|
runWriterTest :: String -> IO Bool
|
|
|
|
runWriterTest format = do
|
|
|
|
r1 <- runTest (format ++ " writer") ["-r", "native", "-s", "-w", format] "testsuite.native" ("writer" <.> format)
|
|
|
|
r2 <- runTest (format ++ " writer (tables)") ["-r", "native", "-w", format] "tables.native" ("tables" <.> format)
|
|
|
|
return (r1 && r2)
|
|
|
|
|
|
|
|
runS5WriterTest :: String -> [String] -> String -> IO Bool
|
|
|
|
runS5WriterTest modifier opts format = runTest (format ++ " writer (" ++ modifier ++ ")")
|
|
|
|
(["-r", "native", "-w", format] ++ opts) "s5.native" ("s5." ++ modifier <.> "html")
|
|
|
|
|
|
|
|
-- | Run a test, return True if test passed.
|
|
|
|
runTest :: String -- ^ Title of test
|
|
|
|
-> [String] -- ^ Options to pass to pandoc
|
|
|
|
-> String -- ^ Input filepath
|
|
|
|
-> FilePath -- ^ Norm (for test results) filepath
|
|
|
|
-> IO Bool
|
|
|
|
runTest testname opts inp norm = do
|
2009-12-31 02:08:30 +01:00
|
|
|
putStr $ printf "%-28s ---> " testname
|
2008-08-09 18:51:08 +02:00
|
|
|
(outputPath, hOut) <- openTempFile "" "pandoc-test"
|
|
|
|
let inpPath = inp
|
|
|
|
let normPath = norm
|
2009-12-31 02:08:30 +01:00
|
|
|
hFlush stdout
|
2008-08-09 18:51:08 +02:00
|
|
|
-- Note: COLUMNS must be set for markdown table reader
|
2010-02-21 17:47:33 +01:00
|
|
|
-- and we need LANG set for ghc 6.12
|
|
|
|
ph <- runProcess pandocPath (opts ++ [inpPath] ++ ["--data-dir", ".."]) Nothing (Just [("COLUMNS", "80"),("LANG","en_US.UTF-8")]) Nothing (Just hOut) (Just stderr)
|
2008-08-09 18:51:08 +02:00
|
|
|
ec <- waitForProcess ph
|
|
|
|
result <- if ec == ExitSuccess
|
|
|
|
then do
|
2008-08-14 21:54:52 +02:00
|
|
|
-- filter \r so the tests will work on Windows machines
|
2008-08-23 20:55:08 +02:00
|
|
|
outputContents <- readFile' outputPath >>= return . filter (/='\r')
|
|
|
|
normContents <- readFile' normPath >>= return . filter (/='\r')
|
2008-08-09 18:51:08 +02:00
|
|
|
if outputContents == normContents
|
|
|
|
then return TestPassed
|
|
|
|
else return $ TestFailed $ getDiff (lines outputContents) (lines normContents)
|
|
|
|
else return $ TestError ec
|
|
|
|
removeFile outputPath
|
2009-12-31 02:08:30 +01:00
|
|
|
putStrLn (show result)
|
2008-08-09 18:51:08 +02:00
|
|
|
return (result == TestPassed)
|