git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1473 from jkr/mediabag-test-2

Docx tests: rewrite mediabag tests.

commit2b7e7acc4b1ebff2ed4e3ef109686a845a4b3aa5 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 31 Jul 2014, 15:28 (12 years ago) parent0565a8196ad375
5 files changed+44−23
pandoc.cabal+2−2 tests/Tests/Readers/Docx.hs+40−19 tests/docx.image.docx tests/docx.image1.jpeg tests/docx.image_no_embed.native+2−2
pandoc.cabal+2−2
@@ -386,7 +386,6 @@ Test-Suite test-pandoc
386386 syb >= 0.1 && < 0.5,
387387 pandoc,
388388 pandoc-types >= 1.12.3.3 && < 1.13,
389− base64-bytestring >= 0.1 && < 1.1,
390389 bytestring >= 0.9 && < 0.11,
391390 text >= 0.11 && < 1.2,
392391 directory >= 1 && < 1.3,
@@ -400,7 +399,8 @@ Test-Suite test-pandoc
400399 QuickCheck >= 2.4 && < 2.8,
401400 HUnit >= 1.2 && < 1.3,
402401 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
404404 Other-Modules: Tests.Old
405405 Tests.Helpers
406406 Tests.Arbitrary
tests/Tests/Readers/Docx.hs+40−19
@@ -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}
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"]
2−,Para [Image [] ("media/image1.jpeg","")]]
1+[Para [Str "An",Space,Str "image:"]
2+,Para [Image [] ("media/image1.jpg","")]]