@@ -5,14 +5,15 @@ import Text.Pandoc.Readers.Native
55import Text.Pandoc.Definition
66import Tests.Helpers
77import Test.Framework
8−import qualified Data.ByteString as BS
8+import Test.HUnit (assertBool)
9+import Test.Framework.Providers.HUnit
910import qualified Data.ByteString.Lazy as B
10−import qualified Data.ByteString.Char8 as B8
11−import qualified Data.ByteString.Base64 as B64
1211import Text.Pandoc.Readers.Docx
1312import Text.Pandoc.Writers.Native (writeNative)
1413import qualified Data.Map as M
15−import Text.Pandoc.MediaBag (lookupMedia)
14+import Text.Pandoc.MediaBag (MediaBag, lookupMedia, mediaDirectory)
15+import Codec.Archive.Zip
16+import System.FilePath (combine)
1617
1718-- We define a wrapper around pandoc that doesn't normalize in the
1819-- tests. Since we do our own normalization, we want to make sure
@@ -56,22 +57,44 @@ testCompareWithOpts opts name docxFile nativeFile =
5657testCompare :: String -> FilePath -> FilePath -> Test
5758testCompare = testCompareWithOpts def
5859
59−testCompareMediaIO :: String -> FilePath -> FilePath -> FilePath -> IO Test
60−testCompareMediaIO name docxFile mediaPath mediaFile = do
60+getMedia :: FilePath -> FilePath -> IO (Maybe B.ByteString)
61+getMedia archivePath mediaPath = do
62+ zf <- B.readFile archivePath >>= return . toArchive
63+ return $ findEntryByPath (combine "word" mediaPath) zf >>= (Just . fromEntry)
64+
65+compareMediaPathIO :: FilePath -> MediaBag -> FilePath -> IO Bool
66+compareMediaPathIO mediaPath mediaBag docxPath = do
67+ docxMedia <- getMedia docxPath mediaPath
68+ let mbBS = case lookupMedia mediaPath mediaBag of
69+ Just (_, bs) -> bs
70+ Nothing -> error ("couldn't find " ++
71+ mediaPath ++
72+ " in media bag")
73+ docxBS = case docxMedia of
74+ Just bs -> bs
75+ Nothing -> error ("couldn't find " ++
76+ mediaPath ++
77+ " in media bag")
78+ return $ mbBS == docxBS
79+
80+compareMediaBagIO :: FilePath -> IO Bool
81+compareMediaBagIO docxFile = do
6182 df <- B.readFile docxFile
62− mf <- B.readFile mediaFile
6383 let (_, mb) = readDocx def df
64− dBytes = case lookupMedia mediaPath mb of
65− Just (_,bs) -> bs
66− Nothing -> error "Media file not found"
67− d64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks dBytes
68− m64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks mf
69− return $ test id name (d64, m64)
84+ bools <- mapM
85+ (\(fp, _, _) -> compareMediaPathIO fp mb docxFile)
86+ (mediaDirectory mb)
87+ return $ and bools
7088
71−testCompareMedia :: String -> FilePath -> FilePath -> FilePath -> Test
72−testCompareMedia name docxFile mediaPath mediaFile =
73− buildTest $ testCompareMediaIO name docxFile mediaPath mediaFile
89+testMediaBagIO :: String -> FilePath -> IO Test
90+testMediaBagIO name docxFile = do
91+ outcome <- compareMediaBagIO docxFile
92+ return $ testCase name (assertBool
93+ ("Media didn't match media bag in file " ++ docxFile)
94+ outcome)
7495
96+testMediaBag :: String -> FilePath -> Test
97+testMediaBag name docxFile = buildTest $ testMediaBagIO name docxFile
7598
7699tests :: [Test]
77100tests = [ testGroup "inlines"
@@ -186,11 +209,9 @@ tests = [ testGroup "inlines"
186209 "docx.track_changes_deletion_all.native"
187210 ]
188211 , testGroup "media"
189− [ testCompareMedia
212+ [ testMediaBag
190213 "image extraction"
191214 "docx.image.docx"
192− "media/image1.jpeg"
193− "docx.image1.jpeg"
194215 ]
195216 , testGroup "metadata"
196217 [ testCompareWithOpts def{readerStandalone=True}