2011-01-21 20:50:18 -08:00
|
|
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, TemplateHaskell #-}
|
2011-01-13 11:11:55 -08:00
|
|
|
-- Utility functions for the test suite.
|
|
|
|
|
2011-01-22 12:18:59 -08:00
|
|
|
module Tests.Helpers ( lit
|
2011-01-22 13:54:12 -08:00
|
|
|
, file
|
2011-01-22 12:18:59 -08:00
|
|
|
, test
|
|
|
|
, (=?>)
|
|
|
|
, property
|
|
|
|
, ToString(..)
|
|
|
|
, ToPandoc(..)
|
|
|
|
)
|
|
|
|
where
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-21 20:50:18 -08: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 12:18:59 -08:00
|
|
|
import Test.Framework.Providers.QuickCheck2
|
|
|
|
import Test.HUnit (assertBool)
|
2012-09-29 17:09:34 -04:00
|
|
|
import Text.Pandoc.Shared (normalize, trimr)
|
2012-07-26 22:59:56 -07:00
|
|
|
import Text.Pandoc.Options
|
2011-01-21 20:50:18 -08:00
|
|
|
import Text.Pandoc.Writers.Native (writeNative)
|
2011-01-26 09:09:32 -08:00
|
|
|
import Language.Haskell.TH.Quote (QuasiQuoter(..))
|
2011-01-22 13:54:12 -08:00
|
|
|
import Language.Haskell.TH.Syntax (Q, runIO)
|
2011-01-22 12:18:59 -08:00
|
|
|
import qualified Test.QuickCheck.Property as QP
|
2011-01-22 14:58:32 -08:00
|
|
|
import System.Console.ANSI
|
|
|
|
import Data.Algorithm.Diff
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
lit :: QuasiQuoter
|
2011-01-26 09:09:32 -08:00
|
|
|
lit = QuasiQuoter {
|
|
|
|
quoteExp = (\a -> let b = rnl a in [|b|]) . filter (/= '\r')
|
|
|
|
, quotePat = error "Cannot use lit as a pattern"
|
|
|
|
}
|
2011-01-21 20:50:18 -08:00
|
|
|
where rnl ('\n':xs) = xs
|
|
|
|
rnl xs = xs
|
2011-01-21 10:23:41 -08:00
|
|
|
|
2011-01-22 13:54:12 -08: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-21 20:50:18 -08: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')
|
2011-01-22 14:58:32 -08:00
|
|
|
where msg = nl ++ dashes "input" ++ nl ++ input' ++ nl ++
|
|
|
|
dashes "expected" ++ nl ++ expected'' ++
|
|
|
|
dashes "got" ++ nl ++ actual'' ++
|
2011-01-21 20:50:18 -08:00
|
|
|
dashes ""
|
2011-01-22 14:58:32 -08:00
|
|
|
nl = "\n"
|
2011-01-21 20:50:18 -08:00
|
|
|
input' = toString input
|
|
|
|
actual' = toString $ fn input
|
|
|
|
expected' = toString expected
|
2011-01-22 14:58:32 -08:00
|
|
|
diff = getDiff (lines expected') (lines actual')
|
|
|
|
expected'' = unlines $ map vividize $ filter (\(d,_) -> d /= S) diff
|
|
|
|
actual'' = unlines $ map vividize $ filter (\(d,_) -> d /= F) diff
|
|
|
|
dashes "" = replicate 72 '-'
|
|
|
|
dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
|
|
|
|
|
|
|
|
vividize :: (DI,String) -> String
|
|
|
|
vividize (B,s) = s
|
2012-02-10 21:40:55 -08:00
|
|
|
vividize (F,s) = s
|
|
|
|
vividize (S,s) = setSGRCode [SetColor Background Dull Red
|
|
|
|
, SetColor Foreground Vivid White] ++ s
|
|
|
|
++ setSGRCode [Reset]
|
2011-01-21 10:23:41 -08:00
|
|
|
|
2011-01-22 12:18:59 -08:00
|
|
|
property :: QP.Testable a => TestName -> a -> Test
|
|
|
|
property = testProperty
|
|
|
|
|
2012-02-05 13:23:06 -08:00
|
|
|
infix 5 =?>
|
2011-01-21 20:50:18 -08:00
|
|
|
(=?>) :: a -> b -> (a,b)
|
|
|
|
x =?> y = (x, y)
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
class ToString a where
|
|
|
|
toString :: a -> String
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
instance ToString Pandoc where
|
2012-07-26 22:59:56 -07:00
|
|
|
toString d = writeNative def{ writerStandalone = s } $ toPandoc d
|
2011-01-21 20:50:18 -08:00
|
|
|
where s = case d of
|
|
|
|
(Pandoc (Meta [] [] []) _) -> False
|
|
|
|
_ -> True
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
instance ToString Blocks where
|
2012-07-26 22:59:56 -07:00
|
|
|
toString = writeNative def . toPandoc
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
instance ToString Inlines where
|
2012-09-29 17:09:34 -04:00
|
|
|
toString = trimr . writeNative def . toPandoc
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
instance ToString String where
|
|
|
|
toString = id
|
2011-01-18 23:34:34 -08:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
class ToPandoc a where
|
|
|
|
toPandoc :: a -> Pandoc
|
2011-01-12 14:16:35 +01:00
|
|
|
|
2011-01-21 20:50:18 -08:00
|
|
|
instance ToPandoc Pandoc where
|
|
|
|
toPandoc = normalize
|
|
|
|
|
|
|
|
instance ToPandoc Blocks where
|
|
|
|
toPandoc = normalize . doc
|
|
|
|
|
|
|
|
instance ToPandoc Inlines where
|
|
|
|
toPandoc = normalize . doc . plain
|