@@ -12,66 +12,52 @@ Tests for the word docx reader.
1212-}
1313module Tests.Readers.Docx (tests ) where
1414
15+ import Data.Algorithm.Diff (getDiff )
1516import Codec.Archive.Zip
1617import qualified Data.ByteString as BS
1718import qualified Data.ByteString.Lazy as B
18- import qualified Data.Map as M
1919import qualified Data.Text as T
2020import Data.Maybe
2121import System.IO.Unsafe
2222import Test.Tasty
23+ import Test.Tasty.Golden.Advanced
2324import Test.Tasty.HUnit
2425import Tests.Helpers
2526import Text.Pandoc
2627import qualified Text.Pandoc.Class as P
2728import qualified Text.Pandoc.MediaBag as MB
2829import Text.Pandoc.UTF8 as UTF8
2930
30- -- We define a wrapper around pandoc that doesn't normalize in the
31- -- tests. Since we do our own normalization, we want to make sure
32- -- we're doing it right.
33-
34- newtype NoNormPandoc = NoNormPandoc { unNoNorm :: Pandoc }
35- deriving Show
36-
37- noNorm :: Pandoc -> NoNormPandoc
38- noNorm = NoNormPandoc
39-
4031defopts :: ReaderOptions
4132defopts = def{ readerExtensions = getDefaultExtensions " docx" }
4233
43- instance ToString NoNormPandoc where
44- toString d = T. unpack $ purely (writeNative def{ writerTemplate = s }) $ toPandoc d
45- where s = case d of
46- NoNormPandoc (Pandoc (Meta m) _)
47- | M. null m -> Nothing
48- | otherwise -> Just mempty -- need this to get meta output
49-
50- instance ToPandoc NoNormPandoc where
51- toPandoc = unNoNorm
34+ testCompare :: String -> FilePath -> FilePath -> TestTree
35+ testCompare = testCompareWithOpts defopts
5236
53- compareOutput :: ReaderOptions
54- -> FilePath
55- -> FilePath
56- -> IO (NoNormPandoc , NoNormPandoc )
57- compareOutput opts docxFile nativeFile = do
58- df <- B. readFile docxFile
59- nf <- UTF8. toText <$> BS. readFile nativeFile
60- p <- runIOorExplode $ readDocx opts df
61- df' <- runIOorExplode $ readNative def nf
62- return (noNorm p, noNorm df')
6337
64- testCompareWithOptsIO :: ReaderOptions -> String -> FilePath -> FilePath -> IO TestTree
65- testCompareWithOptsIO opts name docxFile nativeFile = do
66- (dp, np) <- compareOutput opts docxFile nativeFile
67- return $ test id name (dp, np)
38+ nativeDiff :: FilePath -> Pandoc -> Pandoc -> IO (Maybe String )
39+ nativeDiff normPath expectedNative actualNative
40+ | expectedNative == actualNative = return Nothing
41+ | otherwise = Just <$> do
42+ expected <- T. unpack <$> runIOorExplode (writeNative def expectedNative)
43+ actual <- T. unpack <$> runIOorExplode (writeNative def actualNative)
44+ let dash = replicate 72 ' -'
45+ let diff = getDiff (lines actual) (lines expected)
46+ return $ ' \n ' : dash ++
47+ " \n --- " ++ normPath ++
48+ " \n +++ " ++ " test" ++ " \n " ++
49+ showDiff (1 ,1 ) diff ++ dash
6850
6951testCompareWithOpts :: ReaderOptions -> String -> FilePath -> FilePath -> TestTree
70- testCompareWithOpts opts name docxFile nativeFile =
71- unsafePerformIO $ testCompareWithOptsIO opts name docxFile nativeFile
72-
73- testCompare :: String -> FilePath -> FilePath -> TestTree
74- testCompare = testCompareWithOpts defopts
52+ testCompareWithOpts opts testName docxFP nativeFP =
53+ goldenTest
54+ testName
55+ (do nf <- UTF8. toText <$> BS. readFile nativeFP
56+ runIOorExplode (readNative def nf))
57+ (do df <- B. readFile docxFP
58+ runIOorExplode (readDocx opts df))
59+ (nativeDiff nativeFP)
60+ (\ a -> runIOorExplode (writeNative def a) >>= BS. writeFile nativeFP . UTF8. fromText)
7561
7662testForWarningsWithOptsIO :: ReaderOptions -> String -> FilePath -> [String ] -> IO TestTree
7763testForWarningsWithOptsIO opts name docxFile expected = do
0 commit comments