@@ -0,0 +1,115 @@
1+{-# OPTIONS_GHC -Wall #-}
2+-- RunTests.hs - run test suite for pandoc
3+-- This script is designed to be run from the tests directory.
4+-- It assumes the pandoc executable is in dist/build/pandoc.
5+
6+module Main where
7+import System.Exit
8+import System.IO.UTF8
9+import System.IO ( openTempFile, stderr )
10+import Prelude hiding ( putStrLn, putStr, readFile )
11+import System.Process ( runProcess, waitForProcess )
12+import System.FilePath ( (</>), (<.>) )
13+import System.Directory
14+import System.Exit
15+import Text.Printf
16+import Diff
17+
18+pandocPath :: FilePath
19+pandocPath = ".." </> "dist" </> "build" </> "pandoc" </> "pandoc"
20+
21+data TestResult = TestPassed
22+ | TestError ExitCode
23+ | TestFailed [(DI, String)]
24+ deriving (Eq)
25+
26+instance Show TestResult where
27+ show TestPassed = "PASSED"
28+ show (TestError ec) = "ERROR " ++ show ec
29+ show (TestFailed d) = "FAILED\n" ++ showDiff d
30+
31+showDiff :: [(DI, String)] -> String
32+showDiff [] = ""
33+showDiff ((F, ln) : ds) = "|TEST| " ++ ln ++ "\n" ++ showDiff ds
34+showDiff ((S, ln) : ds) = "|NORM| " ++ ln ++ "\n" ++ showDiff ds
35+showDiff ((B, _ ) : ds) = showDiff ds
36+
37+writerFormats :: [String]
38+writerFormats = [ "native"
39+ , "html"
40+ , "docbook"
41+ , "opendocument"
42+ , "latex"
43+ , "context"
44+ , "texinfo"
45+ , "man"
46+ , "markdown"
47+ , "rst"
48+ , "mediawiki"
49+ , "rtf"
50+ ]
51+
52+main :: IO ()
53+main = do
54+ r1s <- mapM runWriterTest writerFormats
55+ r2 <- runS5WriterTest "basic" ["-s"] "s5"
56+ r3 <- runS5WriterTest "fancy" ["-s","-m","-i"] "s5"
57+ r4 <- runS5WriterTest "fragment" [] "html"
58+ r5 <- runS5WriterTest "inserts" ["-s", "-H", "insert",
59+ "-B", "insert", "-A", "insert", "-c", "main.css"] "html"
60+ r6 <- runTest "markdown reader" ["-r", "markdown", "-w", "native", "-s", "-S"]
61+ "testsuite.txt" "testsuite.native"
62+ r7 <- runTest "markdown reader (tables)" ["-r", "markdown", "-w", "native"]
63+ "tables.txt" "tables.native"
64+ r8 <- runTest "rst reader" ["-r", "rst", "-w", "native", "-s", "-S"]
65+ "rst-reader.rst" "rst-reader.native"
66+ r9 <- runTest "html reader" ["-r", "html", "-w", "native", "-s"]
67+ "html-reader.html" "html-reader.native"
68+ r10 <- runTest "latex reader" ["-r", "latex", "-w", "native", "-s", "-R"]
69+ "latex-reader.latex" "latex-reader.native"
70+ r11 <- runTest "native reader" ["-r", "native", "-w", "native", "-s"]
71+ "testsuite.native" "testsuite.native"
72+ let results = r1s ++ [r2, r3, r4, r5, r6, r7, r8, r9, r10, r11]
73+ if all id results
74+ then do
75+ putStrLn "\nAll tests passed."
76+ exitWith ExitSuccess
77+ else do
78+ let failures = length $ filter not results
79+ putStrLn $ "\n" ++ show failures ++ " tests failed."
80+ exitWith (ExitFailure failures)
81+
82+runWriterTest :: String -> IO Bool
83+runWriterTest format = do
84+ r1 <- runTest (format ++ " writer") ["-r", "native", "-s", "-w", format] "testsuite.native" ("writer" <.> format)
85+ r2 <- runTest (format ++ " writer (tables)") ["-r", "native", "-w", format] "tables.native" ("tables" <.> format)
86+ return (r1 && r2)
87+
88+runS5WriterTest :: String -> [String] -> String -> IO Bool
89+runS5WriterTest modifier opts format = runTest (format ++ " writer (" ++ modifier ++ ")")
90+ (["-r", "native", "-w", format] ++ opts) "s5.native" ("s5." ++ modifier <.> "html")
91+
92+-- | Run a test, return True if test passed.
93+runTest :: String -- ^ Title of test
94+ -> [String] -- ^ Options to pass to pandoc
95+ -> String -- ^ Input filepath
96+ -> FilePath -- ^ Norm (for test results) filepath
97+ -> IO Bool
98+runTest testname opts inp norm = do
99+ (outputPath, hOut) <- openTempFile "" "pandoc-test"
100+ let inpPath = inp
101+ let normPath = norm
102+ -- Note: COLUMNS must be set for markdown table reader
103+ ph <- runProcess pandocPath (opts ++ [inpPath]) Nothing (Just [("COLUMNS", "80")]) Nothing (Just hOut) (Just stderr)
104+ ec <- waitForProcess ph
105+ result <- if ec == ExitSuccess
106+ then do
107+ outputContents <- readFile outputPath
108+ normContents <- readFile normPath
109+ if outputContents == normContents
110+ then return TestPassed
111+ else return $ TestFailed $ getDiff (lines outputContents) (lines normContents)
112+ else return $ TestError ec
113+ removeFile outputPath
114+ putStrLn $ printf "%-28s ---> %s" testname (show result)
115+ return (result == TestPassed)