2011-01-22 05:50:18 +01:00
|
|
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, TemplateHaskell #-}
|
2011-01-13 20:11:55 +01:00
|
|
|
-- Utility functions for the test suite.
|
|
|
|
|
2011-01-22 21:18:59 +01:00
|
|
|
module Tests.Helpers ( lit
|
2011-01-22 22:54:12 +01:00
|
|
|
, file
|
2011-01-22 21:18:59 +01:00
|
|
|
, test
|
|
|
|
, (=?>)
|
|
|
|
, property
|
|
|
|
, ToString(..)
|
|
|
|
, ToPandoc(..)
|
|
|
|
)
|
|
|
|
where
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
import Text.Pandoc.Definition
|
|
|
|
import Text.Pandoc.Builder (Inlines, Blocks, doc, plain)
|
2011-01-12 14:16:35 +01:00
|
|
|
import Test.Framework
|
|
|
|
import Test.Framework.Providers.HUnit
|
2011-01-22 21:18:59 +01:00
|
|
|
import Test.Framework.Providers.QuickCheck2
|
|
|
|
import Test.HUnit (assertBool)
|
2011-01-22 05:50:18 +01:00
|
|
|
import Text.Pandoc.Shared (normalize, defaultWriterOptions,
|
|
|
|
WriterOptions(..), removeTrailingSpace)
|
|
|
|
import Text.Pandoc.Writers.Native (writeNative)
|
|
|
|
import Language.Haskell.TH.Quote
|
2011-01-22 22:54:12 +01:00
|
|
|
import Language.Haskell.TH.Syntax (Q, runIO)
|
2011-01-22 21:18:59 +01:00
|
|
|
import qualified Test.QuickCheck.Property as QP
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
lit :: QuasiQuoter
|
|
|
|
lit = QuasiQuoter ((\a -> let b = rnl a in [|b|]) . filter (/= '\r')) $
|
|
|
|
error "Cannot use lit as a pattern"
|
|
|
|
where rnl ('\n':xs) = xs
|
|
|
|
rnl xs = xs
|
2011-01-21 19:23:41 +01:00
|
|
|
|
2011-01-22 22:54:12 +01:00
|
|
|
file :: QuasiQuoter
|
|
|
|
file = quoteFile lit
|
|
|
|
|
|
|
|
-- adapted from TH 2.5 code
|
|
|
|
quoteFile :: QuasiQuoter -> QuasiQuoter
|
|
|
|
quoteFile (QuasiQuoter { quoteExp = qe, quotePat = qp }) =
|
|
|
|
QuasiQuoter { quoteExp = get qe, quotePat = get qp }
|
|
|
|
where
|
|
|
|
get :: (String -> Q a) -> String -> Q a
|
|
|
|
get old_quoter file_name = do { file_cts <- runIO (readFile file_name)
|
|
|
|
; old_quoter file_cts }
|
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
test :: (ToString a, ToString b, ToString c)
|
|
|
|
=> (a -> b) -- ^ function to test
|
|
|
|
-> String -- ^ name of test case
|
|
|
|
-> (a, c) -- ^ (input, expected value)
|
|
|
|
-> Test
|
|
|
|
test fn name (input, expected) =
|
|
|
|
testCase name $ assertBool msg (actual' == expected')
|
|
|
|
where msg = dashes "input" ++ input' ++
|
|
|
|
dashes "expected" ++ expected' ++
|
|
|
|
dashes "got" ++ actual' ++
|
|
|
|
dashes ""
|
|
|
|
input' = toString input
|
|
|
|
actual' = toString $ fn input
|
|
|
|
expected' = toString expected
|
|
|
|
dashes "" = '\n' : replicate 72 '-'
|
|
|
|
dashes x = '\n' : replicate (72 - length x - 5) '-' ++ " " ++
|
|
|
|
x ++ " ---\n"
|
2011-01-21 19:23:41 +01:00
|
|
|
|
2011-01-22 21:18:59 +01:00
|
|
|
property :: QP.Testable a => TestName -> a -> Test
|
|
|
|
property = testProperty
|
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
infix 6 =?>
|
|
|
|
(=?>) :: a -> b -> (a,b)
|
|
|
|
x =?> y = (x, y)
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
class ToString a where
|
|
|
|
toString :: a -> String
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
instance ToString Pandoc where
|
|
|
|
toString d = writeNative defaultWriterOptions{ writerStandalone = s }
|
|
|
|
$ toPandoc d
|
|
|
|
where s = case d of
|
|
|
|
(Pandoc (Meta [] [] []) _) -> False
|
|
|
|
_ -> True
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
instance ToString Blocks where
|
|
|
|
toString = writeNative defaultWriterOptions . toPandoc
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
instance ToString Inlines where
|
|
|
|
toString = removeTrailingSpace . writeNative defaultWriterOptions .
|
|
|
|
toPandoc
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
instance ToString String where
|
|
|
|
toString = id
|
2011-01-19 08:34:34 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
class ToPandoc a where
|
|
|
|
toPandoc :: a -> Pandoc
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-22 05:50:18 +01:00
|
|
|
instance ToPandoc Pandoc where
|
|
|
|
toPandoc = normalize
|
|
|
|
|
|
|
|
instance ToPandoc Blocks where
|
|
|
|
toPandoc = normalize . doc
|
|
|
|
|
|
|
|
instance ToPandoc Inlines where
|
|
|
|
toPandoc = normalize . doc . plain
|