@@ -13,18 +13,22 @@
1313-- cabal install Diff
1414
1515module Main where
16−import System.IO ( openTempFile, stderr, stdout, hFlush )
16+
17+import Test.Framework (defaultMain, testGroup, Test )
18+import Test.Framework.Providers.HUnit
19+
20+import Test.HUnit hiding ( Test )
21+
22+import System.IO ( openTempFile, stderr )
1723import System.Process ( runProcess, waitForProcess )
1824import System.FilePath ( (</>), (<.>) )
1925import System.Directory
20−import System.Environment
2126import System.Exit
22−import Text.Printf
2327import Data.Algorithm.Diff
2428import Text.Pandoc.Shared ( substitute )
2529import Prelude hiding ( readFile )
2630import qualified Data.ByteString.Lazy as B
27−import Data.ByteString.Lazy.UTF8 (toString, fromString)
31+import Data.ByteString.Lazy.UTF8 (toString)
2832
2933readFileUTF8 :: FilePath -> IO String
3034readFileUTF8 f = B.readFile f >>= return . toString
@@ -48,24 +52,6 @@ showDiff ((F, ln) : ds) = "|TEST| " ++ ln ++ "\n" ++ showDiff ds
4852showDiff ((S, ln) : ds) = "|NORM| " ++ ln ++ "\n" ++ showDiff ds
4953showDiff ((B, _ ) : ds) = showDiff ds
5054
51−writerFormats :: [String]
52−writerFormats = [ "native"
53− , "html"
54− , "docbook"
55− , "opendocument"
56− , "latex"
57− , "context"
58− , "texinfo"
59− , "man"
60− , "plain"
61− , "markdown"
62− , "rst"
63− , "mediawiki"
64− , "textile"
65− , "rtf"
66− , "org"
67− ]
68−
6955lhsWriterFormats :: [String]
7056lhsWriterFormats = [ "markdown"
7157 , "markdown+lhs"
@@ -83,100 +69,106 @@ lhsReaderFormats = [ "markdown+lhs"
8369 , "latex+lhs"
8470 ]
8571
72+markdownCitationTest :: Test
73+markdownCitationTest
74+ = testGroup "citations" $ map styleToTest ["chicago-author-date","ieee","mhra"]
75+ ++ [runTest "no-citeproc" wopts "markdown-citations.txt" "markdown-citations.txt"]
76+ where
77+ ropts = ["-r", "markdown", "-w", "markdown", "--bibliography", "biblio.bib", "--no-wrap"]
78+ wopts = ropts ++ ["--no-citeproc"]
79+ styleToTest style = runTest style (ropts ++ ["--csl", style ++ ".csl"])
80+ "markdown-citations.txt" ("markdown-citations." ++ style ++ ".txt")
81+
82+
83+tests :: [Test]
84+tests = [ testGroup "markdown" [ runWriterTest "" "markdown"
85+ , runTest "reader" ["-r", "markdown", "-w", "native", "-s", "-S"]
86+ "testsuite.txt" "testsuite.native"
87+ , runTest "reader (tables)" ["-r", "markdown", "-w", "native"]
88+ "tables.txt" "tables.native"
89+ , runTest "reader (more)" ["-r", "markdown", "-w", "native", "-S"]
90+ "markdown-reader-more.txt" "markdown-reader-more.native"
91+ , markdownCitationTest
92+ ]
93+ , testGroup "rst" [ runWriterTest "" "rst"
94+ , runTest "reader" ["-r", "rst", "-w", "native", "-s", "-S"]
95+ "rst-reader.rst" "rst-reader.native"
96+ , runTest "reader (tables)" ["-r", "rst", "-w", "native"]
97+ "tables.rst" "tables-rstsubset.native"
98+ ]
99+ , testGroup "latex" [ runWriterTest "" "latex"
100+ , runTest "reader" ["-r", "latex", "-w", "native", "-s", "-R"]
101+ "latex-reader.latex" "latex-reader.native"
102+ , runLatexCitationTests "biblatex"
103+ , runLatexCitationTests "natbib"
104+ ]
105+ , testGroup "html" [ runWriterTest "" "html"
106+ , runTest "reader" ["-r", "html", "-w", "native", "-s"]
107+ "html-reader.html" "html-reader.native"
108+ ]
109+ , testGroup "s5" [ runS5WriterTest "basic" ["-s"] "s5"
110+ , runS5WriterTest "fancy" ["-s","-m","-i"] "s5"
111+ , runS5WriterTest "fragment" [] "html"
112+ , runS5WriterTest "inserts" ["-s", "-H", "insert",
113+ "-B", "insert", "-A", "insert", "-c", "main.css"] "html"
114+ ]
115+ , testGroup "textile" [ runWriterTest "" "textile"
116+ , runTest "reader" ["-r", "textile", "-w", "native", "-s"]
117+ "textile-reader.textile" "textile-reader.native"
118+ ]
119+ , testGroup "native" [ runWriterTest "" "native"
120+ , runTest "reader" ["-r", "native", "-w", "native", "-s"]
121+ "testsuite.native" "testsuite.native"
122+ ]
123+ , testGroup "other writers" $ map (\f -> runWriterTest f f) [ "docbook", "opendocument" , "context" , "texinfo"
124+ , "man" , "plain" , "mediawiki", "rtf", "org"
125+ ]
126+ , testGroup "lhs" [ testGroup "writer" $ map runLhsWriterTest lhsWriterFormats
127+ , testGroup "reader" $ map runLhsReaderTest lhsReaderFormats
128+ ]
129+ ]
130+
86131main :: IO ()
87−main = do
88− args <- getArgs
89− let runLhsTests = "lhs" `elem` args
90− r1s <- mapM runWriterTest writerFormats
91− r2 <- runS5WriterTest "basic" ["-s"] "s5"
92− r3 <- runS5WriterTest "fancy" ["-s","-m","-i"] "s5"
93− r4 <- runS5WriterTest "fragment" [] "html"
94− r5 <- runS5WriterTest "inserts" ["-s", "-H", "insert",
95− "-B", "insert", "-A", "insert", "-c", "main.css"] "html"
96− r6 <- runTest "markdown reader" ["-r", "markdown", "-w", "native", "-s", "-S"]
97− "testsuite.txt" "testsuite.native"
98− r7 <- runTest "markdown reader (tables)" ["-r", "markdown", "-w", "native"]
99− "tables.txt" "tables.native"
100− r7a <- runTest "markdown reader (more)" ["-r", "markdown", "-w", "native", "-S"]
101− "markdown-reader-more.txt" "markdown-reader-more.native"
102− r8 <- runTest "rst reader" ["-r", "rst", "-w", "native", "-s", "-S"]
103− "rst-reader.rst" "rst-reader.native"
104− r8a <- runTest "rst reader (tables)" ["-r", "rst", "-w", "native"]
105− "tables.rst" "tables-rstsubset.native"
106− r9 <- runTest "html reader" ["-r", "html", "-w", "native", "-s"]
107− "html-reader.html" "html-reader.native"
108− r10 <- runTest "latex reader" ["-r", "latex", "-w", "native", "-s", "-R"]
109− "latex-reader.latex" "latex-reader.native"
110− rTextile1 <- runTest "textile reader" ["-r", "textile", "-w", "native", "-s"]
111− "textile-reader.textile" "textile-reader.native"
112− r11 <- runTest "native reader" ["-r", "native", "-w", "native", "-s"]
113− "testsuite.native" "testsuite.native"
114− r14s <- mapM (\style -> runTest ("markdown reader (citations) (" ++ style ++ ")") ["-r", "markdown", "-w", "markdown", "--bibliography", "biblio.bib", "--csl", style ++ ".csl", "--no-wrap"] "markdown-citations.txt" ("markdown-citations." ++ style ++ ".txt")) ["chicago-author-date","ieee","mhra"]
115− let citopts = ["--bibliography", "biblio.bib", "--csl", "chicago-author-date.csl", "--no-citeproc"]
116− r15 <- runTest "markdown writer (citations)" (["-r", "markdown", "-w", "markdown", "--no-wrap"] ++ citopts)
117− "markdown-citations.txt" "markdown-citations.txt"
118− r16s <- runLatexCitationTests citopts "biblatex"
119− r17s <- runLatexCitationTests citopts "natbib"
120− r12s <- if runLhsTests
121− then mapM runLhsWriterTest lhsWriterFormats
122− else putStrLn "Skipping lhs writer tests because they presuppose highlighting support" >> return []
123− r13s <- if runLhsTests
124− then mapM runLhsReaderTest lhsReaderFormats
125− else putStrLn "Skipping lhs reader tests because they presuppose highlighting support" >> return []
126− let results = r1s ++
127−
128− [ r2, r3, r4, r5 -- S5
129− , r6, r7, r7a -- markdown reader
130− , r8, r8a -- rst
131− , r9 -- html
132− , r10 -- latex
133− , rTextile1 -- textile
134− , r11 -- native
135− , r15 -- markdown citations
136− ] ++ r12s ++ r13s ++ r14s ++ r16s ++ r17s
137− if all id results
138− then do
139− putStrLn "\nAll tests passed."
140− exitWith ExitSuccess
141− else do
142− let failures = length $ filter not results
143− putStrLn $ "\n" ++ show failures ++ " tests failed."
144− exitWith (ExitFailure failures)
132+main = defaultMain tests
145133
146134-- makes sure file is fully closed after reading
147135readFile' :: FilePath -> IO String
148136readFile' f = do s <- readFileUTF8 f
149137 return $! (length s `seq` s)
150138
151−runLhsWriterTest :: String -> IO Bool
139+runLhsWriterTest :: String -> Test
152140runLhsWriterTest format =
153− runTest ("(lhs) " ++ format ++ " writer") ["--columns=78", "-r", "native", "-s", "-w", format] "lhs-test.native" ("lhs-test" <.> format)
141+ runTest format ["--columns=78", "-r", "native", "-s", "-w", format] "lhs-test.native" ("lhs-test" <.> format)
154142
155−runLhsReaderTest :: String -> IO Bool
143+runLhsReaderTest :: String -> Test
156144runLhsReaderTest format =
157− runTest ("(lhs) " ++ format ++ " reader") ["-r", format, "-w", "html+lhs"] ("lhs-test" <.> format) "lhs-test.fragment.html+lhs"
158−
159−
160−runLatexCitationTests :: [String] -> String -> IO [Bool]
161−runLatexCitationTests o n
162− = sequence [ rt ("latex reader (" ++ n ++ " citations)") (["-r", "latex", "-w", "markdown", "-s", "--no-wrap"] ++ o')
163− f "markdown-citations.txt"
164− , rt ("latex writer (" ++ n ++ " citations)") (["-r", "markdown", "-w", "latex", "-s", "--no-wrap"] ++ o')
165− "markdown-citations.txt" f
166− ]
167− where
168− o' = o ++ ["--" ++ n]
169− f = n ++ "-citations.latex"
170− normalize = substitute "\160" " " . substitute "\8211" "-"
171− rt = runTestWithNormalize normalize
172−
173−runWriterTest :: String -> IO Bool
174−runWriterTest format = do
175− r1 <- runTest (format ++ " writer") ["-r", "native", "-s", "-w", format, "--columns=78"] "testsuite.native" ("writer" <.> format)
176− r2 <- runTest (format ++ " writer (tables)") ["-r", "native", "-w", format, "--columns=78"] "tables.native" ("tables" <.> format)
177− return (r1 && r2)
178−
179−runS5WriterTest :: String -> [String] -> String -> IO Bool
145+ runTest format ["-r", format, "-w", "html+lhs"] ("lhs-test" <.> format) "lhs-test.fragment.html+lhs"
146+
147+
148+runLatexCitationTests :: String -> Test
149+runLatexCitationTests n
150+ = testGroup (n ++ " citations")
151+ [ rt ("latex reader (" ++ n ++ " citations)") (["-r", "latex", "-w", "markdown", "-s", "--no-wrap"] ++ o)
152+ f "markdown-citations.txt"
153+ , rt ("latex writer (" ++ n ++ " citations)") (["-r", "markdown", "-w", "latex", "-s", "--no-wrap"] ++ o)
154+ "markdown-citations.txt" f
155+ ]
156+ where
157+ o = ["--bibliography", "biblio.bib", "--csl", "chicago-author-date.csl", "--no-citeproc", "--" ++ n]
158+ f = n ++ "-citations.latex"
159+ normalize = substitute "\160" " " . substitute "\8211" "-"
160+ rt = runTestWithNormalize normalize
161+
162+runWriterTest :: String -> String -> Test
163+runWriterTest prefix format
164+ = testGroup name [ runTest "basic" (opts ++ ["-s"]) "testsuite.native" ("writer" <.> format)
165+ , runTest "tables" opts "tables.native" ("tables" <.> format)
166+ ]
167+ where
168+ name = if (null prefix) then "writer" else prefix ++ " writer"
169+ opts = ["-r", "native", "-w", format, "--columns=78"]
170+
171+runS5WriterTest :: String -> [String] -> String -> Test
180172runS5WriterTest modifier opts format = runTest (format ++ " writer (" ++ modifier ++ ")")
181173 (["-r", "native", "-w", format] ++ opts) "s5.native" ("s5." ++ modifier <.> "html")
182174
@@ -186,7 +178,7 @@ runTest :: String -- ^ Title of test
186178 -> [String] -- ^ Options to pass to pandoc
187179 -> String -- ^ Input filepath
188180 -> FilePath -- ^ Norm (for test results) filepath
189− -> IO Bool
181+ -> Test
190182runTest = runTestWithNormalize id
191183
192184-- | Run a test with normalize function, return True if test passed.
@@ -195,13 +187,11 @@ runTestWithNormalize :: (String -> String) -- ^ Normalize function for output
195187 -> [String] -- ^ Options to pass to pandoc
196188 -> String -- ^ Input filepath
197189 -> FilePath -- ^ Norm (for test results) filepath
198− -> IO Bool
199−runTestWithNormalize normalize testname opts inp norm = do
200− putStr $ printf "%-28s ---> " testname
190+ -> Test
191+runTestWithNormalize normalize testname opts inp norm = testCase testname $ do
201192 (outputPath, hOut) <- openTempFile "" "pandoc-test"
202193 let inpPath = inp
203194 let normPath = norm
204− hFlush stdout
205195 ph <- runProcess pandocPath (["--columns=80"] ++ [inpPath] ++ ["--data-dir", ".."] ++ opts) Nothing
206196 (Just [("LANG","en_US.UTF-8"),("HOME", "./")]) Nothing (Just hOut) (Just stderr)
207197 ec <- waitForProcess ph
@@ -215,5 +205,4 @@ runTestWithNormalize normalize testname opts inp norm = do
215205 else return $ TestFailed $ getDiff (lines outputContents) (lines normContents)
216206 else return $ TestError ec
217207 removeFile outputPath
218− B.putStrLn (fromString $ show result)
219− return (result == TestPassed)
208+ assertBool (show result) (result == TestPassed)