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