aboutsummaryrefslogtreecommitdiff
path: root/test/Tests/Helpers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'test/Tests/Helpers.hs')
-rw-r--r--test/Tests/Helpers.hs87
1 files changed, 46 insertions, 41 deletions
diff --git a/test/Tests/Helpers.hs b/test/Tests/Helpers.hs
index 64c2785ed..c8957a050 100644
--- a/test/Tests/Helpers.hs
+++ b/test/Tests/Helpers.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Tests.Helpers
Copyright : © 2006-2021 John MacFarlane
@@ -17,73 +18,77 @@ module Tests.Helpers ( test
, (=?>)
, purely
, ToString(..)
+ , ToText(..)
, ToPandoc(..)
)
where
import Data.Algorithm.Diff
import qualified Data.Map as M
-import Data.Text (Text, unpack)
+import qualified Data.Text as T
import System.Exit
import Test.Tasty
import Test.Tasty.HUnit
-import Text.Pandoc.Builder (Blocks, Inlines, doc, plain)
+import Text.Pandoc.Builder (Blocks, Inlines, doc, plain, nullMeta)
import Text.Pandoc.Class
import Text.Pandoc.Definition
import Text.Pandoc.Options
import Text.Pandoc.Shared (trimr)
import Text.Pandoc.Writers.Native (writeNative)
import Text.Printf
+import qualified GHC.Show
+import Prelude hiding (First)
-test :: (ToString a, ToString b, ToString c, HasCallStack)
+test :: (ToText a, ToText b, ToText c, HasCallStack)
=> (a -> b) -- ^ function to test
-> String -- ^ name of test case
-> (a, c) -- ^ (input, expected value)
-> TestTree
test fn name (input, expected) =
- testCase name' $ assertBool msg (actual' == expected')
- where msg = nl ++ dashes "input" ++ nl ++ input' ++ nl ++
- dashes "result" ++ nl ++
- unlines (map vividize diff) ++
+ testCase name' $ assertBool (T.unpack msg) (actual' == expected')
+ where msg = nl <> dashes "input" <> nl <> input' <> nl <>
+ dashes "result" <> nl <>
+ T.unlines (map vividize diff) <>
dashes ""
nl = "\n"
name' = if length name > 54
- then take 52 name ++ "..." -- avoid wide output
+ then Prelude.take 52 name ++ "..." -- avoid wide output
else name
- input' = toString input
- actual' = lines $ toString $ fn input
- expected' = lines $ toString expected
+ input' = toText input
+ actual' = T.lines $ toText $ fn input
+ expected' = T.lines $ toText expected
diff = getDiff expected' actual'
- dashes "" = replicate 72 '-'
- dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
+ dashes "" = T.replicate 72 "-"
+ dashes x = T.replicate (72 - T.length x - 5) "-" <>
+ " " <> x <> " ---"
data TestResult = TestPassed
| TestError ExitCode
- | TestFailed String FilePath [Diff String]
+ | TestFailed String FilePath [Diff Text]
deriving (Eq)
instance Show TestResult where
show TestPassed = "PASSED"
- show (TestError ec) = "ERROR " ++ show ec
- show (TestFailed cmd file d) = '\n' : dash ++
- "\n--- " ++ file ++
- "\n+++ " ++ cmd ++ "\n" ++ showDiff (1,1) d ++
- dash
+ show (TestError ec) = "ERROR " <> show ec
+ show (TestFailed cmd file d) = "\n" <> dash <>
+ "\n--- " <> file <>
+ "\n+++ " <> cmd <> "\n" <>
+ showDiff (1,1) d <> dash
where dash = replicate 72 '-'
-showDiff :: (Int,Int) -> [Diff String] -> String
+showDiff :: ToString a => (Int,Int) -> [Diff a] -> String
showDiff _ [] = ""
showDiff (l,r) (First ln : ds) =
- printf "+%4d " l ++ ln ++ "\n" ++ showDiff (l+1,r) ds
+ printf "+%4d " l <> toString ln <> "\n" <> showDiff (l+1,r) ds
showDiff (l,r) (Second ln : ds) =
- printf "-%4d " r ++ ln ++ "\n" ++ showDiff (l,r+1) ds
+ printf "-%4d " r <> toString ln <> "\n" <> showDiff (l,r+1) ds
showDiff (l,r) (Both _ _ : ds) =
showDiff (l+1,r+1) ds
-vividize :: Diff String -> String
-vividize (Both s _) = " " ++ s
-vividize (First s) = "- " ++ s
-vividize (Second s) = "+ " ++ s
+vividize :: Diff Text -> Text
+vividize (Both s _) = " " <> s
+vividize (First s) = "- " <> s
+vividize (Second s) = "+ " <> s
purely :: (b -> PandocPure a) -> b -> a
purely f = either (error . show) id . runPure . f
@@ -92,28 +97,28 @@ infix 5 =?>
(=?>) :: a -> b -> (a,b)
x =?> y = (x, y)
-class ToString a where
- toString :: a -> String
-
instance ToString Pandoc where
- toString d = unpack $
- purely (writeNative def{ writerTemplate = s }) $ toPandoc d
- where s = case d of
- (Pandoc (Meta m) _)
- | M.null m -> Nothing
- | otherwise -> Just mempty -- need this to get meta output
+ toString = T.unpack . toText
instance ToString Blocks where
- toString = unpack . purely (writeNative def) . toPandoc
+ toString = T.unpack . toText
instance ToString Inlines where
- toString = unpack . trimr . purely (writeNative def) . toPandoc
+ toString = T.unpack . toText
+
+instance ToText Pandoc where
+ toText d@(Pandoc (Meta m) _)
+ | M.null m
+ = purely (writeNative def) $ toPandoc d
+ toText d@(Pandoc m _)
+ = purely (writeNative def{ writerTemplate = Just mempty }) $ toPandoc d
+ -- need this to get meta output
-instance ToString String where
- toString = id
+instance ToText Blocks where
+ toText = purely (writeNative def) . toPandoc
-instance ToString Text where
- toString = unpack
+instance ToText Inlines where
+ toText = T.stripEnd . purely (writeNative def) . toPandoc
class ToPandoc a where
toPandoc :: a -> Pandoc