@@ -5,14 +5,15 @@ import Text.Pandoc.Readers.Native
5import Text.Pandoc.Definition
5import Text.Pandoc.Definition
6import Tests.Helpers
6import Tests.Helpers
7import Test.Framework
7import Test.Framework
8−import qualified Data.ByteString as BS
8+import Test.HUnit (assertBool)
9+import Test.Framework.Providers.HUnit
9import qualified Data.ByteString.Lazy as B
10import qualified Data.ByteString.Lazy as B
10−import qualified Data.ByteString.Char8 as B8
11−import qualified Data.ByteString.Base64 as B64
12import Text.Pandoc.Readers.Docx
11import Text.Pandoc.Readers.Docx
13import Text.Pandoc.Writers.Native (writeNative)
12import Text.Pandoc.Writers.Native (writeNative)
14import qualified Data.Map as M
13import 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)
17-- We define a wrapper around pandoc that doesn't normalize in the
18-- We define a wrapper around pandoc that doesn't normalize in the
18-- tests. Since we do our own normalization, we want to make sure
19-- tests. Since we do our own normalization, we want to make sure
@@ -56,22 +57,44 @@ testCompareWithOpts opts name docxFile nativeFile =
56testCompare :: String -> FilePath -> FilePath -> Test
57testCompare :: String -> FilePath -> FilePath -> Test
57testCompare = testCompareWithOpts def
58testCompare = testCompareWithOpts def
59−testCompareMediaIO :: String -> FilePath -> FilePath -> FilePath -> IO Test
60+getMedia :: FilePath -> FilePath -> IO (Maybe B.ByteString)
60−testCompareMediaIO name docxFile mediaPath mediaFile = do
61+getMedia archivePath mediaPath = do
62+ zf <- B.readFile archivePath >>= return . toArchive
63+ return $ findEntryByPath (combine "word" mediaPath) zf >>= (Just . fromEntry)
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
70+ Nothing -> error ("couldn't find " ++
73+ docxBS = case docxMedia of
75+ Nothing -> error ("couldn't find " ++
78+ return $ mbBS == docxBS
80+compareMediaBagIO :: FilePath -> IO Bool
81+compareMediaBagIO docxFile = do
61 df <- B.readFile docxFile
82 df <- B.readFile docxFile
62− mf <- B.readFile mediaFile
63 let (_, mb) = readDocx def df
83 let (_, mb) = readDocx def df
64− dBytes = case lookupMedia mediaPath mb of
84+ bools <- mapM
65− Just (_,bs) -> bs
85+ (\(fp, _, _) -> compareMediaPathIO fp mb docxFile)
66− Nothing -> error "Media file not found"
86+ (mediaDirectory mb)
67− d64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks dBytes
87+ return $ and bools
68− m64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks mf
69− return $ test id name (d64, m64)
71−testCompareMedia :: String -> FilePath -> FilePath -> FilePath -> Test
89+testMediaBagIO :: String -> FilePath -> IO Test
72−testCompareMedia name docxFile mediaPath mediaFile =
90+testMediaBagIO name docxFile = do
73− buildTest $ testCompareMediaIO name docxFile mediaPath mediaFile
91+ outcome <- compareMediaBagIO docxFile
92+ return $ testCase name (assertBool
93+ ("Media didn't match media bag in file " ++ docxFile)
96+testMediaBag :: String -> FilePath -> Test
97+testMediaBag name docxFile = buildTest $ testMediaBagIO name docxFile
76tests :: [Test]
99tests :: [Test]
77tests = [ testGroup "inlines"
100tests = [ testGroup "inlines"
@@ -186,11 +209,9 @@ tests = [ testGroup "inlines"
186 "docx.track_changes_deletion_all.native"
209 "docx.track_changes_deletion_all.native"
188 , testGroup "media"
211 , testGroup "media"
189− [ testCompareMedia
212+ [ testMediaBag
190 "image extraction"
213 "image extraction"
191 "docx.image.docx"
214 "docx.image.docx"
195 , testGroup "metadata"
216 , testGroup "metadata"
196 [ testCompareWithOpts def{readerStandalone=True}
217 [ testCompareWithOpts def{readerStandalone=True}