git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1473 from jkr/mediabag-test-2
2b7e7acJohn MacFarlane
pandoc.cabal+2−2
@@ -386,7 +386,6 @@ Test-Suite test-pandoc
386 syb >= 0.1 && < 0.5,
386 syb >= 0.1 && < 0.5,
387 pandoc,
387 pandoc,
388 pandoc-types >= 1.12.3.3 && < 1.13,
388 pandoc-types >= 1.12.3.3 && < 1.13,
389− base64-bytestring >= 0.1 && < 1.1,
390 bytestring >= 0.9 && < 0.11,
389 bytestring >= 0.9 && < 0.11,
391 text >= 0.11 && < 1.2,
390 text >= 0.11 && < 1.2,
392 directory >= 1 && < 1.3,
391 directory >= 1 && < 1.3,
@@ -400,7 +399,8 @@ Test-Suite test-pandoc
400 QuickCheck >= 2.4 && < 2.8,
399 QuickCheck >= 2.4 && < 2.8,
401 HUnit >= 1.2 && < 1.3,
400 HUnit >= 1.2 && < 1.3,
402 containers >= 0.1 && < 0.6,
401 containers >= 0.1 && < 0.6,
403− ansi-terminal >= 0.5 && < 0.7
402+ ansi-terminal >= 0.5 && < 0.7,
403+ zip-archive >= 0.2.3.2 && < 0.3
404 Other-Modules: Tests.Old
404 Other-Modules: Tests.Old
405 Tests.Helpers
405 Tests.Helpers
406 Tests.Arbitrary
406 Tests.Arbitrary
tests/Tests/Readers/Docx.hs+40−19
@@ -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)
16
17
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
58
59
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)
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
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)
70
88
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)
94+ outcome)
74
95
96+testMediaBag :: String -> FilePath -> Test
97+testMediaBag name docxFile = buildTest $ testMediaBagIO name docxFile
75
98
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"
187 ]
210 ]
188 , testGroup "media"
211 , testGroup "media"
189− [ testCompareMedia
212+ [ testMediaBag
190 "image extraction"
213 "image extraction"
191 "docx.image.docx"
214 "docx.image.docx"
192− "media/image1.jpeg"
193− "docx.image1.jpeg"
194 ]
215 ]
195 , testGroup "metadata"
216 , testGroup "metadata"
196 [ testCompareWithOpts def{readerStandalone=True}
217 [ testCompareWithOpts def{readerStandalone=True}
tests/docx.image.docx

Binary file not shown.

tests/docx.image1.jpegdeleted

Binary file not shown.

tests/docx.image_no_embed.native+2−2
@@ -1,2 +1,2 @@
1−[Header 2 ("an-image",[],[]) [Str "An",Space,Str "image"]
1+[Para [Str "An",Space,Str "image:"]
2−,Para [Image [] ("media/image1.jpeg","")]]
2+,Para [Image [] ("media/image1.jpg","")]]