git.iko.soy/typeable/pandoc
on master help sign in
Improved test framework.

Now there is a uniform interface for reader and writer tests. Also added a quasiquoter, for multiline strings.

commit15250859c355634670ce1ad532800bca591dc99b authorJohn MacFarlane <jgm@berkeley.edu> dateFri 21 Jan 2011, 20:50 (16 years ago) parentb3c1a89
4 files changed+120−48
pandoc.cabal+2−1 tests/Tests/Helpers.hs+57−22 tests/Tests/Readers/LaTeX.hs+18−13 tests/Tests/Writers/ConTeXt.hs+43−12
pandoc.cabal+2−1
@@ -341,7 +341,8 @@ Executable test-pandoc
341341 Extensions: CPP
342342 Build-Depends: base >= 4 && < 5, Diff, test-framework >= 0.3 && < 0.4,
343343 test-framework-hunit >= 0.2 && < 0.3,
344− HUnit >= 1.2 && < 1.3
344+ HUnit >= 1.2 && < 1.3,
345+ template-haskell == 2.4.*
345346 Other-Modules: Tests.Old
346347 Tests.Helpers
347348 Tests.Arbitrary
tests/Tests/Helpers.hs+57−22
@@ -1,39 +1,74 @@
1−{-# LANGUAGE TypeSynonymInstances #-}
1+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, TemplateHaskell #-}
22-- Utility functions for the test suite.
33
44module Tests.Helpers where
55
6−import Text.Pandoc
7−import Text.Pandoc.Builder
6+import Text.Pandoc.Definition
7+import Text.Pandoc.Builder (Inlines, Blocks, doc, plain)
88import Test.Framework
99import Test.Framework.Providers.HUnit
1010import Test.HUnit hiding (Test)
11+import Text.Pandoc.Shared (normalize, defaultWriterOptions,
12+ WriterOptions(..), removeTrailingSpace)
13+import Text.Pandoc.Writers.Native (writeNative)
14+import Language.Haskell.TH.Quote
1115
12−infix 8 -->
16+lit :: QuasiQuoter
17+lit = QuasiQuoter ((\a -> let b = rnl a in [|b|]) . filter (/= '\r')) $
18+ error "Cannot use lit as a pattern"
19+ where rnl ('\n':xs) = xs
20+ rnl xs = xs
1321
14−(-->) :: (Eq a, Show a, Show b) => (b, a) -> a -> Assertion
15−(b,a) --> e = assertEqual (show b) e a
22+test :: (ToString a, ToString b, ToString c)
23+ => (a -> b) -- ^ function to test
24+ -> String -- ^ name of test case
25+ -> (a, c) -- ^ (input, expected value)
26+ -> Test
27+test fn name (input, expected) =
28+ testCase name $ assertBool msg (actual' == expected')
29+ where msg = dashes "input" ++ input' ++
30+ dashes "expected" ++ expected' ++
31+ dashes "got" ++ actual' ++
32+ dashes ""
33+ input' = toString input
34+ actual' = toString $ fn input
35+ expected' = toString expected
36+ dashes "" = '\n' : replicate 72 '-'
37+ dashes x = '\n' : replicate (72 - length x - 5) '-' ++ " " ++
38+ x ++ " ---\n"
1639
17−-- In the first argument, the String is the input, and the Pandoc
18−-- the output, of a pandoc reader. The input is shown in case
19−-- the test fails.
20−class Expect a where
21− (=?>) :: (String, Pandoc) -> a -> Assertion
40+infix 6 =?>
41+(=?>) :: a -> b -> (a,b)
42+x =?> y = (x, y)
2243
23−infix 8 =?>
44+class ToString a where
45+ toString :: a -> String
2446
25−(=:) :: TestName -> Assertion -> Test
26−(=:) = testCase
47+instance ToString Pandoc where
48+ toString d = writeNative defaultWriterOptions{ writerStandalone = s }
49+ $ toPandoc d
50+ where s = case d of
51+ (Pandoc (Meta [] [] []) _) -> False
52+ _ -> True
2753
28−infix 6 =:
54+instance ToString Blocks where
55+ toString = writeNative defaultWriterOptions . toPandoc
2956
30−instance Expect Inlines where
31− (s, Pandoc _ [Para ils]) =?> e = assertEqual (show s) (toList e) ils
32− (s, g) =?> e = assertEqual (show s) (doc $ para e) g
57+instance ToString Inlines where
58+ toString = removeTrailingSpace . writeNative defaultWriterOptions .
59+ toPandoc
3360
34−instance Expect Blocks where
35− (s, Pandoc _ bls) =?> e = assertEqual (show s) (toList e) bls
61+instance ToString String where
62+ toString = id
3663
37−instance Expect Pandoc where
38− (s, g) =?> e = assertEqual (show s) e g
64+class ToPandoc a where
65+ toPandoc :: a -> Pandoc
3966
67+instance ToPandoc Pandoc where
68+ toPandoc = normalize
69+
70+instance ToPandoc Blocks where
71+ toPandoc = normalize . doc
72+
73+instance ToPandoc Inlines where
74+ toPandoc = normalize . doc . plain
tests/Tests/Readers/LaTeX.hs+18−13
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Tests.Readers.LaTeX (tests) where
23
34import Text.Pandoc.Definition
@@ -5,34 +6,38 @@ import Test.Framework
56import Tests.Helpers
67import Text.Pandoc.Builder
78import Text.Pandoc
8−import Text.Pandoc.Shared (normalize)
99
10−latex :: String -> (String, Pandoc)
11−latex s = (s, normalize . readLaTeX defaultParserState{stateSmart = True} $ s)
10+latex :: String -> Pandoc
11+latex = readLaTeX defaultParserState
12+
13+infix 5 =:
14+(=:) :: ToString c
15+ => String -> (String, c) -> Test
16+(=:) = test latex
1217
1318tests :: [Test]
1419tests = [ testGroup "basic"
1520 [ "simple" =:
16− latex "word" =?> str "word"
21+ "wo rd" =?> para "word"
1722 , "space" =:
18− latex "some text" =?> text "some text"
23+ "some text" =?> para ("some text")
1924 , "emphasized" =:
20− latex "\\emph{emphasized}" =?> (emph $ str "emphasized")
25+ "\\emph{emphasized}" =?> para (emph "emphasized")
2126 ]
2227
2328 , testGroup "headers"
2429 [ "level 1" =:
25− latex "\\section{header}" =?> header 1 (str "header")
30+ "\\section{header}" =?> header 1 "header"
2631 , "level 2" =:
27− latex "\\subsection{header}" =?> header 2 (str "header")
32+ "\\subsection{header}" =?> header 2 "header"
2833 , "level 3" =:
29− latex "\\subsubsection{header}" =?> header 3 (str "header")
34+ "\\subsubsection{header}" =?> header 3 "header"
3035 , "emph" =:
31− latex "\\section{text \\emph{emph}}" =?>
32− header 1 (str "text" +++ space +++ emph (str "emph"))
36+ "\\section{text \\emph{emph}}" =?>
37+ header 1 ("text" +++ space +++ emph "emph")
3338 , "link" =:
34− latex "\\section{text \\href{/url}{link}}" =?>
35− header 1 (str "text" +++ space +++ link "/url" "" (str "link"))
39+ "\\section{text \\href{/url}{link}}" =?>
40+ header 1 ("text" +++ space +++ link "/url" "" "link")
3641 ]
3742 ]
3843
tests/Tests/Writers/ConTeXt.hs+43−12
@@ -1,29 +1,60 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
22module Tests.Writers.ConTeXt (tests) where
33
44import Test.Framework
55import Text.Pandoc.Builder
66import Text.Pandoc
7−import Text.Pandoc.Shared (removeTrailingSpace)
87import Tests.Helpers
98
10−inlines :: Inlines -> (Inlines, String)
11−inlines ils = (ils, removeTrailingSpace .
12− writeConTeXt defaultWriterOptions . doc . plain $ ils)
9+context :: (ToString a, ToPandoc a) => a -> String
10+context = writeConTeXt defaultWriterOptions . toPandoc
1311
14−blocks :: Blocks -> (Blocks, String)
15−blocks bls = (bls, writeConTeXt defaultWriterOptions . doc $ bls)
12+{-
13+ "my test" =: X =?> Y
14+
15+is shorthand for
16+
17+ test context "my test" $ X =?> Y
18+
19+which is in turn shorthand for
20+
21+ test context "my test" (X,Y)
22+-}
23+
24+infix 5 =:
25+(=:) :: (ToString a, ToPandoc a)
26+ => String -> (a, String) -> Test
27+(=:) = test context
1628
1729tests :: [Test]
1830tests = [ testGroup "inline code"
19− [ "with '}'" =:
20− inlines (code "}") --> "\\mono{\\letterclosebrace{x}}"
21− , "without '}'" =:
22− inlines (code "]") --> "\\type{]}"
31+ [ "with '}'" =: code "}" =?> "\\mono{\\letterclosebrace{}}"
32+ , "without '}'" =: code "]" =?> "\\type{]}"
2333 ]
2434 , testGroup "headers"
2535 [ "level 1" =:
26− blocks (header 1 "My header") --> "\\subject{My header}"
36+ header 1 "My header" =?> "\\subject{My header}"
37+ ]
38+ , testGroup "bullet lists"
39+ [ "nested" =:
40+ bulletList [plain (text "top")
41+ ,bulletList [plain (text "next")
42+ ,bulletList [plain (text "bot")]]]
43+ =?> [$lit|
44+\startitemize
45+\item
46+ top
47+\item
48+ \startitemize
49+ \item
50+ next
51+ \item
52+ \startitemize
53+ \item
54+ bot
55+ \stopitemize
56+ \stopitemize
57+\stopitemize|]
2758 ]
2859 ]
2960