git.iko.soy/typeable/pandoc
on master help sign in
Consolidated some common functions in Tests.Helper.
commita435422d0fb08832ecd8951bb42684aed27a7cb7 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 4 Feb 2017, 21:54 (10 years ago) parentce9ec67
3 files changed+52−81
test/Tests/Command.hs+1−39 test/Tests/Helpers.hs+46 test/Tests/Old.hs+5−42
test/Tests/Command.hs+1−39
@@ -1,10 +1,10 @@
11module Tests.Command (findPandoc, runTest, tests)
22where
33
4+import Tests.Helpers
45import Test.Framework
56import Test.Framework.Providers.HUnit
67import Test.HUnit ( assertBool )
7−import System.Environment.Executable (getExecutablePath)
88import System.FilePath ( (</>), takeDirectory, splitDirectories,
99 joinPath )
1010import System.Process
@@ -14,47 +14,9 @@ import Text.Pandoc
1414import Data.Algorithm.Diff
1515import Prelude hiding ( readFile )
1616import qualified Text.Pandoc.UTF8 as UTF8
17−import Text.Printf
1817import Data.List (isSuffixOf)
1918import Text.Pandoc.Shared (trimr)
2019
21−data TestResult = TestPassed
22− | TestError ExitCode
23− | TestFailed String FilePath [Diff String]
24− deriving (Eq)
25−
26−instance Show TestResult where
27− show TestPassed = "PASSED"
28− show (TestError ec) = "ERROR " ++ show ec
29− show (TestFailed cmd file d) = '\n' : dash ++
30− "\n--- " ++ file ++
31− "\n+++ " ++ cmd ++ "\n" ++ showDiff (1,1) d ++
32− dash
33− where dash = replicate 72 '-'
34−
35−showDiff :: (Int,Int) -> [Diff String] -> String
36−showDiff _ [] = ""
37−showDiff (l,r) (First ln : ds) =
38− printf "+%4d " l ++ ln ++ "\n" ++ showDiff (l+1,r) ds
39−showDiff (l,r) (Second ln : ds) =
40− printf "-%4d " r ++ ln ++ "\n" ++ showDiff (l,r+1) ds
41−showDiff (l,r) (Both _ _ : ds) =
42− showDiff (l+1,r+1) ds
43−
44−-- | Find pandoc executable relative to test-pandoc
45−-- First, try in same directory (e.g. if both in ~/.cabal/bin)
46−-- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
47−findPandoc :: IO FilePath
48−findPandoc = do
49− testExePath <- getExecutablePath
50− let testExeDir = takeDirectory testExePath
51− found <- doesFileExist (testExeDir </> "pandoc")
52− return $ if found
53− then testExeDir </> "pandoc"
54− else case splitDirectories testExeDir of
55− [] -> error "test-pandoc: empty testExeDir"
56− xs -> joinPath (init xs) </> "pandoc" </> "pandoc"
57−
5820-- | Run a test with normalize function, return True if test passed.
5921runTest :: String -- ^ Title of test
6022 -> String -- ^ Shell command
test/Tests/Helpers.hs+46
@@ -2,6 +2,9 @@
22-- Utility functions for the test suite.
33
44module Tests.Helpers ( test
5+ , TestResult(..)
6+ , showDiff
7+ , findPandoc
58 , (=?>)
69 , purely
710 , property
@@ -20,9 +23,14 @@ import Test.HUnit (assertBool)
2023import Text.Pandoc.Shared (trimr)
2124import Text.Pandoc.Options
2225import Text.Pandoc.Writers.Native (writeNative)
26+import Text.Printf
27+import System.Environment.Executable (getExecutablePath)
2328import qualified Test.QuickCheck.Property as QP
2429import Data.Algorithm.Diff
2530import qualified Data.Map as M
31+import System.Exit
32+import System.Directory
33+import System.FilePath
2634
2735test :: (ToString a, ToString b, ToString c)
2836 => (a -> b) -- ^ function to test
@@ -43,6 +51,44 @@ test fn name (input, expected) =
4351 dashes "" = replicate 72 '-'
4452 dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
4553
54+data TestResult = TestPassed
55+ | TestError ExitCode
56+ | TestFailed String FilePath [Diff String]
57+ deriving (Eq)
58+
59+instance Show TestResult where
60+ show TestPassed = "PASSED"
61+ show (TestError ec) = "ERROR " ++ show ec
62+ show (TestFailed cmd file d) = '\n' : dash ++
63+ "\n--- " ++ file ++
64+ "\n+++ " ++ cmd ++ "\n" ++ showDiff (1,1) d ++
65+ dash
66+ where dash = replicate 72 '-'
67+
68+showDiff :: (Int,Int) -> [Diff String] -> String
69+showDiff _ [] = ""
70+showDiff (l,r) (First ln : ds) =
71+ printf "+%4d " l ++ ln ++ "\n" ++ showDiff (l+1,r) ds
72+showDiff (l,r) (Second ln : ds) =
73+ printf "-%4d " r ++ ln ++ "\n" ++ showDiff (l,r+1) ds
74+showDiff (l,r) (Both _ _ : ds) =
75+ showDiff (l+1,r+1) ds
76+
77+-- | Find pandoc executable relative to test-pandoc
78+-- First, try in same directory (e.g. if both in ~/.cabal/bin)
79+-- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
80+findPandoc :: IO FilePath
81+findPandoc = do
82+ testExePath <- getExecutablePath
83+ let testExeDir = takeDirectory testExePath
84+ found <- doesFileExist (testExeDir </> "pandoc")
85+ return $ if found
86+ then testExeDir </> "pandoc"
87+ else case splitDirectories testExeDir of
88+ [] -> error "test-pandoc: empty testExeDir"
89+ xs -> joinPath (init xs) </> "pandoc" </> "pandoc"
90+
91+
4692vividize :: Diff String -> String
4793vividize (Both s _) = " " ++ s
4894vividize (First s) = "- " ++ s
test/Tests/Old.hs+5−42
@@ -1,46 +1,17 @@
11module Tests.Old (tests) where
22
3+import Tests.Helpers hiding (test)
34import Test.Framework (testGroup, Test )
45import Test.Framework.Providers.HUnit
56import Test.HUnit ( assertBool )
6−import System.Environment.Executable (getExecutablePath)
77import System.IO ( openTempFile, stderr )
88import System.Process ( runProcess, waitForProcess )
9−import System.FilePath ( (</>), (<.>), takeDirectory, splitDirectories,
10− joinPath )
9+import System.FilePath ( (</>), (<.>), splitDirectories, joinPath )
1110import System.Directory
1211import System.Exit
1312import Data.Algorithm.Diff
1413import Prelude hiding ( readFile )
15−import qualified Data.ByteString.Lazy as B
16−import Text.Pandoc.UTF8 (toStringLazy)
17−import Text.Printf
18−
19−readFileUTF8 :: FilePath -> IO String
20−readFileUTF8 f = B.readFile f >>= return . toStringLazy
21−
22−data TestResult = TestPassed
23− | TestError ExitCode
24− | TestFailed String FilePath [Diff String]
25− deriving (Eq)
26−
27−instance Show TestResult where
28− show TestPassed = "PASSED"
29− show (TestError ec) = "ERROR " ++ show ec
30− show (TestFailed cmd file d) = '\n' : dash ++
31− "\n--- " ++ file ++
32− "\n+++ " ++ cmd ++ "\n" ++ showDiff (1,1) d ++
33− dash
34− where dash = replicate 72 '-'
35−
36−showDiff :: (Int,Int) -> [Diff String] -> String
37−showDiff _ [] = ""
38−showDiff (l,r) (First ln : ds) =
39− printf "+%4d " l ++ ln ++ "\n" ++ showDiff (l+1,r) ds
40−showDiff (l,r) (Second ln : ds) =
41− printf "-%4d " r ++ ln ++ "\n" ++ showDiff (l,r+1) ds
42−showDiff (l,r) (Both _ _ : ds) =
43− showDiff (l+1,r+1) ds
14+import qualified Text.Pandoc.UTF8 as UTF8
4415
4516tests :: [Test]
4617tests = [ testGroup "markdown"
@@ -175,7 +146,7 @@ tests = [ testGroup "markdown"
175146
176147-- makes sure file is fully closed after reading
177148readFile' :: FilePath -> IO String
178−readFile' f = do s <- readFileUTF8 f
149+readFile' f = do s <- UTF8.readFile f
179150 return $! (length s `seq` s)
180151
181152lhsWriterTests :: String -> [Test]
@@ -242,15 +213,7 @@ testWithNormalize normalizer testname opts inp norm = testCase testname $ do
242213 -- find pandoc executable relative to test-pandoc
243214 -- First, try in same directory (e.g. if both in ~/.cabal/bin)
244215 -- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
245− pandocPath <- do
246− testExePath <- getExecutablePath
247− let testExeDir = takeDirectory testExePath
248− found <- doesFileExist (testExeDir </> "pandoc")
249− return $ if found
250− then testExeDir </> "pandoc"
251− else case splitDirectories testExeDir of
252− [] -> error "test-pandoc: empty testExeDir"
253− xs -> joinPath (init xs) </> "pandoc" </> "pandoc"
216+ pandocPath <- findPandoc
254217 (outputPath, hOut) <- openTempFile "" "pandoc-test"
255218 let inpPath = inp
256219 let normPath = norm