@@ -2,191 +2,13 @@
22
33module Main where
44
5−import Test.Framework (defaultMain, testGroup, Test )
6−import Test.Framework.Providers.HUnit
7−import Test.HUnit ( assertBool )
5+import Test.Framework
86
9−import System.IO ( openTempFile, stderr )
10−import System.Process ( runProcess, waitForProcess )
11−import System.FilePath ( (</>), (<.>) )
12−import System.Directory
13−import System.Exit
14−import Data.Algorithm.Diff
15−import Text.Pandoc.Shared ( substitute, normalize, defaultWriterOptions )
16−import Text.Pandoc.Writers.Native ( writeNative )
17−import Text.Pandoc.Highlighting ( languages )
18−import Prelude hiding ( readFile )
19−import qualified Data.ByteString.Lazy as B
20−import Data.ByteString.Lazy.UTF8 (toString)
21−
22−readFileUTF8 :: FilePath -> IO String
23−readFileUTF8 f = B.readFile f >>= return . toString
24−
25−pandocPath :: FilePath
26−pandocPath = ".." </> "dist" </> "build" </> "pandoc" </> "pandoc"
27−
28−data TestResult = TestPassed
29− | TestError ExitCode
30− | TestFailed String FilePath [(DI, String)]
31− deriving (Eq)
32−
33−instance Show TestResult where
34− show TestPassed = "PASSED"
35− show (TestError ec) = "ERROR " ++ show ec
36− show (TestFailed cmd file d) = "\n--- " ++ file ++
37− "\n+++ " ++ cmd ++ "\n" ++ showDiff d
38−
39−showDiff :: [(DI, String)] -> String
40−showDiff [] = ""
41−showDiff ((F, ln) : ds) = "+ " ++ ln ++ "\n" ++ showDiff ds
42−showDiff ((S, ln) : ds) = "- " ++ ln ++ "\n" ++ showDiff ds
43−showDiff ((B, _ ) : ds) = showDiff ds
7+import qualified Old
448
459tests :: [Test]
46−tests = [ testGroup "markdown" [ testGroup "writer" (writerTests "markdown" ++ lhsWriterTests "markdown")
47− , testGroup "reader" [ test "basic" ["-r", "markdown", "-w", "native", "-s", "-S"]
48− "testsuite.txt" "testsuite.native"
49− , test "tables" ["-r", "markdown", "-w", "native", "--columns=80"]
50− "tables.txt" "tables.native"
51− , test "more" ["-r", "markdown", "-w", "native", "-S"]
52− "markdown-reader-more.txt" "markdown-reader-more.native"
53− , lhsReaderTest "markdown+lhs"
54− ]
55− , testGroup "citations" markdownCitationTests
56− ]
57− , testGroup "rst" [ testGroup "writer" (writerTests "rst" ++ lhsWriterTests "rst")
58− , testGroup "reader" [ test "basic" ["-r", "rst", "-w", "native", "-s", "-S", "--columns=80"]
59− "rst-reader.rst" "rst-reader.native"
60− , test "tables" ["-r", "rst", "-w", "native", "--columns=80"]
61− "tables.rst" "tables-rstsubset.native"
62− , lhsReaderTest "rst+lhs"
63− ]
64− ]
65− , testGroup "latex" [ testGroup "writer" (writerTests "latex" ++ lhsWriterTests "latex")
66− , testGroup "reader" [ test "basic" ["-r", "latex", "-w", "native", "-s", "-R"]
67− "latex-reader.latex" "latex-reader.native"
68− , lhsReaderTest "latex+lhs"
69− ]
70− , latexCitationTests "biblatex"
71− , latexCitationTests "natbib"
72− ]
73− , testGroup "html" [ testGroup "writer" (writerTests "html" ++ lhsWriterTests "html")
74− , test "reader" ["-r", "html", "-w", "native", "-s"]
75− "html-reader.html" "html-reader.native"
76− ]
77− , testGroup "s5" [ s5WriterTest "basic" ["-s"] "s5"
78− , s5WriterTest "fancy" ["-s","-m","-i"] "s5"
79− , s5WriterTest "fragment" [] "html"
80− , s5WriterTest "inserts" ["-s", "-H", "insert",
81− "-B", "insert", "-A", "insert", "-c", "main.css"] "html"
82− ]
83− , testGroup "textile" [ testGroup "writer" $ writerTests "textile"
84− , test "reader" ["-r", "textile", "-w", "native", "-s"]
85− "textile-reader.textile" "textile-reader.native"
86− ]
87− , testGroup "native" [ testGroup "writer" $ writerTests "native"
88− , test "reader" ["-r", "native", "-w", "native", "-s"]
89− "testsuite.native" "testsuite.native"
90− ]
91− , testGroup "other writers" $ map (\f -> testGroup f $ writerTests f)
92− [ "docbook", "opendocument" , "context" , "texinfo"
93− , "man" , "plain" , "mediawiki", "rtf", "org"
94− ]
10+tests = [ testGroup "Old" Old.tests
9511 ]
9612
9713main :: IO ()
9814main = defaultMain tests
99−
100−-- makes sure file is fully closed after reading
101−readFile' :: FilePath -> IO String
102−readFile' f = do s <- readFileUTF8 f
103− return $! (length s `seq` s)
104−
105−lhsWriterTests :: String -> [Test]
106−lhsWriterTests format
107− = [ t "lhs to normal" format
108− , t "lhs to lhs" (format ++ "+lhs")
109− ]
110− where
111− t n f = test n ["--columns=78", "-r", "native", "-s", "-w", f] "lhs-test.native" ("lhs-test" <.> ext f)
112− ext f = if null languages && format == "html"
113− then "nohl" <.> f
114− else f
115−
116−lhsReaderTest :: String -> Test
117−lhsReaderTest format =
118− testWithNormalize normalizer "lhs" ["-r", format, "-w", "native"] ("lhs-test" <.> format) "lhs-test.native"
119− where normalizer = writeNative defaultWriterOptions . normalize . read
120−
121−latexCitationTests :: String -> Test
122−latexCitationTests n
123− = testGroup (n ++ " citations")
124− [ t ("latex reader (" ++ n ++ " citations)") (["-r", "latex", "-w", "markdown", "-s", "--no-wrap"] ++ o)
125− f "markdown-citations.txt"
126− , t ("latex writer (" ++ n ++ " citations)") (["-r", "markdown", "-w", "latex", "-s", "--no-wrap"] ++ o)
127− "markdown-citations.txt" f
128− ]
129− where
130− o = ["--bibliography", "biblio.bib", "--csl", "chicago-author-date.csl", "--no-citeproc", "--" ++ n]
131− f = n ++ "-citations.latex"
132− normalizer = substitute "\160" " " . substitute "\8211" "-"
133− t = testWithNormalize normalizer
134−
135−writerTests :: String -> [Test]
136−writerTests format
137− = [ test "basic" (opts ++ ["-s"]) "testsuite.native" ("writer" <.> format)
138− , test "tables" opts "tables.native" ("tables" <.> format)
139− ]
140− where
141− opts = ["-r", "native", "-w", format, "--columns=78"]
142−
143−s5WriterTest :: String -> [String] -> String -> Test
144−s5WriterTest modifier opts format
145− = test (format ++ " writer (" ++ modifier ++ ")") (["-r", "native", "-w", format] ++ opts)
146− "s5.native" ("s5." ++ modifier <.> "html")
147−
148−markdownCitationTests :: [Test]
149−markdownCitationTests
150− = map styleToTest ["chicago-author-date","ieee","mhra"]
151− ++ [test "no-citeproc" wopts "markdown-citations.txt" "markdown-citations.txt"]
152− where
153− ropts = ["-r", "markdown", "-w", "markdown", "--bibliography", "biblio.bib", "--no-wrap"]
154− wopts = ropts ++ ["--no-citeproc"]
155− styleToTest style = test style (ropts ++ ["--csl", style ++ ".csl"])
156− "markdown-citations.txt" ("markdown-citations." ++ style ++ ".txt")
157−
158−-- | Run a test without normalize function, return True if test passed.
159−test :: String -- ^ Title of test
160− -> [String] -- ^ Options to pass to pandoc
161− -> String -- ^ Input filepath
162− -> FilePath -- ^ Norm (for test results) filepath
163− -> Test
164−test = testWithNormalize id
165−
166−-- | Run a test with normalize function, return True if test passed.
167−testWithNormalize :: (String -> String) -- ^ Normalize function for output
168− -> String -- ^ Title of test
169− -> [String] -- ^ Options to pass to pandoc
170− -> String -- ^ Input filepath
171− -> FilePath -- ^ Norm (for test results) filepath
172− -> Test
173−testWithNormalize normalizer testname opts inp norm = testCase testname $ do
174− (outputPath, hOut) <- openTempFile "" "pandoc-test"
175− let inpPath = inp
176− let normPath = norm
177− let options = ["--data-dir", ".."] ++ [inpPath] ++ opts
178− let cmd = pandocPath ++ " " ++ unwords options
179− ph <- runProcess pandocPath options Nothing
180− (Just [("LANG","en_US.UTF-8"),("HOME", "./")]) Nothing (Just hOut) (Just stderr)
181− ec <- waitForProcess ph
182− result <- if ec == ExitSuccess
183− then do
184− -- filter \r so the tests will work on Windows machines
185− outputContents <- readFile' outputPath >>= return . filter (/='\r') . normalizer
186− normContents <- readFile' normPath >>= return . filter (/='\r') . normalizer
187− if outputContents == normContents
188− then return TestPassed
189− else return $ TestFailed cmd normPath $ getDiff (lines outputContents) (lines normContents)
190− else return $ TestError ec
191− removeFile outputPath
192− assertBool (show result) (result == TestPassed)