git.iko.soy/typeable/pandoc
on 1.15.0.5 help sign in
85 lines 2.5 kB history raw
1{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
2-- Utility functions for the test suite.
4module Tests.Helpers ( test
5 , (=?>)
6 , property
7 , ToString(..)
8 , ToPandoc(..)
9 )
10 where
12import Text.Pandoc.Definition
13import Text.Pandoc.Builder (Inlines, Blocks, doc, plain)
14import Test.Framework
15import Test.Framework.Providers.HUnit
16import Test.Framework.Providers.QuickCheck2
17import Test.HUnit (assertBool)
18import Text.Pandoc.Shared (normalize, trimr)
19import Text.Pandoc.Options
20import Text.Pandoc.Writers.Native (writeNative)
21import qualified Test.QuickCheck.Property as QP
22import Data.Algorithm.Diff
23import qualified Data.Map as M
25test :: (ToString a, ToString b, ToString c)
26 => (a -> b) -- ^ function to test
27 -> String -- ^ name of test case
28 -> (a, c) -- ^ (input, expected value)
29 -> Test
30test fn name (input, expected) =
31 testCase name $ assertBool msg (actual' == expected')
32 where msg = nl ++ dashes "input" ++ nl ++ input' ++ nl ++
33 dashes "result" ++ nl ++
34 unlines (map vividize diff) ++
35 dashes ""
36 nl = "\n"
37 input' = toString input
38 actual' = lines $ toString $ fn input
39 expected' = lines $ toString expected
40 diff = getDiff expected' actual'
41 dashes "" = replicate 72 '-'
42 dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
44vividize :: Diff String -> String
45vividize (Both s _) = " " ++ s
46vividize (First s) = "- " ++ s
47vividize (Second s) = "+ " ++ s
49property :: QP.Testable a => TestName -> a -> Test
50property = testProperty
52infix 5 =?>
53(=?>) :: a -> b -> (a,b)
54x =?> y = (x, y)
56class ToString a where
57 toString :: a -> String
59instance ToString Pandoc where
60 toString d = writeNative def{ writerStandalone = s } $ toPandoc d
61 where s = case d of
62 (Pandoc (Meta m) _)
63 | M.null m -> False
64 | otherwise -> True
66instance ToString Blocks where
67 toString = writeNative def . toPandoc
69instance ToString Inlines where
70 toString = trimr . writeNative def . toPandoc
72instance ToString String where
73 toString = id
75class ToPandoc a where
76 toPandoc :: a -> Pandoc
78instance ToPandoc Pandoc where
79 toPandoc = normalize
81instance ToPandoc Blocks where
82 toPandoc = normalize . doc
84instance ToPandoc Inlines where
85 toPandoc = normalize . doc . plain