git.iko.soy/typeable/pandoc
on master help sign in
Consolidated some common functions in Tests.Helper.
a435422John MacFarlane
test/Tests/Command.hs+1−39
@@ -1,10 +1,10 @@
1module Tests.Command (findPandoc, runTest, tests)
1module Tests.Command (findPandoc, runTest, tests)
2where
2where
3
3
4+import Tests.Helpers
4import Test.Framework
5import Test.Framework
5import Test.Framework.Providers.HUnit
6import Test.Framework.Providers.HUnit
6import Test.HUnit ( assertBool )
7import Test.HUnit ( assertBool )
7−import System.Environment.Executable (getExecutablePath)
8import System.FilePath ( (</>), takeDirectory, splitDirectories,
8import System.FilePath ( (</>), takeDirectory, splitDirectories,
9 joinPath )
9 joinPath )
10import System.Process
10import System.Process
@@ -14,47 +14,9 @@ import Text.Pandoc
14import Data.Algorithm.Diff
14import Data.Algorithm.Diff
15import Prelude hiding ( readFile )
15import Prelude hiding ( readFile )
16import qualified Text.Pandoc.UTF8 as UTF8
16import qualified Text.Pandoc.UTF8 as UTF8
17−import Text.Printf
18import Data.List (isSuffixOf)
17import Data.List (isSuffixOf)
19import Text.Pandoc.Shared (trimr)
18import Text.Pandoc.Shared (trimr)
20
19
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−
58-- | Run a test with normalize function, return True if test passed.
20-- | Run a test with normalize function, return True if test passed.
59runTest :: String -- ^ Title of test
21runTest :: String -- ^ Title of test
60 -> String -- ^ Shell command
22 -> String -- ^ Shell command
test/Tests/Helpers.hs+46
@@ -2,6 +2,9 @@
2-- Utility functions for the test suite.
2-- Utility functions for the test suite.
3
3
4module Tests.Helpers ( test
4module Tests.Helpers ( test
5+ , TestResult(..)
6+ , showDiff
7+ , findPandoc
5 , (=?>)
8 , (=?>)
6 , purely
9 , purely
7 , property
10 , property
@@ -20,9 +23,14 @@ import Test.HUnit (assertBool)
20import Text.Pandoc.Shared (trimr)
23import Text.Pandoc.Shared (trimr)
21import Text.Pandoc.Options
24import Text.Pandoc.Options
22import Text.Pandoc.Writers.Native (writeNative)
25import Text.Pandoc.Writers.Native (writeNative)
26+import Text.Printf
27+import System.Environment.Executable (getExecutablePath)
23import qualified Test.QuickCheck.Property as QP
28import qualified Test.QuickCheck.Property as QP
24import Data.Algorithm.Diff
29import Data.Algorithm.Diff
25import qualified Data.Map as M
30import qualified Data.Map as M
31+import System.Exit
32+import System.Directory
33+import System.FilePath
26
34
27test :: (ToString a, ToString b, ToString c)
35test :: (ToString a, ToString b, ToString c)
28 => (a -> b) -- ^ function to test
36 => (a -> b) -- ^ function to test
@@ -43,6 +51,44 @@ test fn name (input, expected) =
43 dashes "" = replicate 72 '-'
51 dashes "" = replicate 72 '-'
44 dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
52 dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
45
53
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+
46vividize :: Diff String -> String
92vividize :: Diff String -> String
47vividize (Both s _) = " " ++ s
93vividize (Both s _) = " " ++ s
48vividize (First s) = "- " ++ s
94vividize (First s) = "- " ++ s
test/Tests/Old.hs+5−42
@@ -1,46 +1,17 @@
1module Tests.Old (tests) where
1module Tests.Old (tests) where
2
2
3+import Tests.Helpers hiding (test)
3import Test.Framework (testGroup, Test )
4import Test.Framework (testGroup, Test )
4import Test.Framework.Providers.HUnit
5import Test.Framework.Providers.HUnit
5import Test.HUnit ( assertBool )
6import Test.HUnit ( assertBool )
6−import System.Environment.Executable (getExecutablePath)
7import System.IO ( openTempFile, stderr )
7import System.IO ( openTempFile, stderr )
8import System.Process ( runProcess, waitForProcess )
8import System.Process ( runProcess, waitForProcess )
9−import System.FilePath ( (</>), (<.>), takeDirectory, splitDirectories,
9+import System.FilePath ( (</>), (<.>), splitDirectories, joinPath )
10− joinPath )
11import System.Directory
10import System.Directory
12import System.Exit
11import System.Exit
13import Data.Algorithm.Diff
12import Data.Algorithm.Diff
14import Prelude hiding ( readFile )
13import Prelude hiding ( readFile )
15−import qualified Data.ByteString.Lazy as B
14+import qualified Text.Pandoc.UTF8 as UTF8
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
44
15
45tests :: [Test]
16tests :: [Test]
46tests = [ testGroup "markdown"
17tests = [ testGroup "markdown"
@@ -175,7 +146,7 @@ tests = [ testGroup "markdown"
175
146
176-- makes sure file is fully closed after reading
147-- makes sure file is fully closed after reading
177readFile' :: FilePath -> IO String
148readFile' :: FilePath -> IO String
178−readFile' f = do s <- readFileUTF8 f
149+readFile' f = do s <- UTF8.readFile f
179 return $! (length s `seq` s)
150 return $! (length s `seq` s)
180
151
181lhsWriterTests :: String -> [Test]
152lhsWriterTests :: String -> [Test]
@@ -242,15 +213,7 @@ testWithNormalize normalizer testname opts inp norm = testCase testname $ do
242 -- find pandoc executable relative to test-pandoc
213 -- find pandoc executable relative to test-pandoc
243 -- First, try in same directory (e.g. if both in ~/.cabal/bin)
214 -- First, try in same directory (e.g. if both in ~/.cabal/bin)
244 -- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
215 -- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
245− pandocPath <- do
216+ pandocPath <- findPandoc
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"
254 (outputPath, hOut) <- openTempFile "" "pandoc-test"
217 (outputPath, hOut) <- openTempFile "" "pandoc-test"
255 let inpPath = inp
218 let inpPath = inp
256 let normPath = norm
219 let normPath = norm