Docx tests: rewrite mediabag tests.

This will allow us to test the whole mediabag (making sure, for example,
that images are added with the correct keys) instead of just individual
extracted images. We compare each entry in the media bag to an image
extracted on the fly from the docx. As a result, we only need one file
to test with.

The image in the current tests was also replaced with a smaller one.
This commit is contained in:
Jesse Rosenthal 2014-07-31 12:10:33 -04:00
parent 6dd2418476
commit ed71e9b31d
6 changed files with 45 additions and 23 deletions

View file

@ -386,7 +386,6 @@ Test-Suite test-pandoc
syb >= 0.1 && < 0.5, syb >= 0.1 && < 0.5,
pandoc, pandoc,
pandoc-types >= 1.12.3.3 && < 1.13, pandoc-types >= 1.12.3.3 && < 1.13,
base64-bytestring >= 0.1 && < 1.1,
bytestring >= 0.9 && < 0.11, bytestring >= 0.9 && < 0.11,
text >= 0.11 && < 1.2, text >= 0.11 && < 1.2,
directory >= 1 && < 1.3, directory >= 1 && < 1.3,
@ -400,7 +399,8 @@ Test-Suite test-pandoc
QuickCheck >= 2.4 && < 2.8, QuickCheck >= 2.4 && < 2.8,
HUnit >= 1.2 && < 1.3, HUnit >= 1.2 && < 1.3,
containers >= 0.1 && < 0.6, containers >= 0.1 && < 0.6,
ansi-terminal >= 0.5 && < 0.7 ansi-terminal >= 0.5 && < 0.7,
zip-archive >= 0.2.3.2 && < 0.3
Other-Modules: Tests.Old Other-Modules: Tests.Old
Tests.Helpers Tests.Helpers
Tests.Arbitrary Tests.Arbitrary

View file

@ -5,14 +5,15 @@ import Text.Pandoc.Readers.Native
import Text.Pandoc.Definition import Text.Pandoc.Definition
import Tests.Helpers import Tests.Helpers
import Test.Framework import Test.Framework
import qualified Data.ByteString as BS import Test.HUnit (assertBool)
import Test.Framework.Providers.HUnit
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Base64 as B64
import Text.Pandoc.Readers.Docx import Text.Pandoc.Readers.Docx
import Text.Pandoc.Writers.Native (writeNative) import Text.Pandoc.Writers.Native (writeNative)
import qualified Data.Map as M import qualified Data.Map as M
import Text.Pandoc.MediaBag (lookupMedia) import Text.Pandoc.MediaBag (MediaBag, lookupMedia, mediaDirectory)
import Codec.Archive.Zip
import System.FilePath (combine)
-- We define a wrapper around pandoc that doesn't normalize in the -- We define a wrapper around pandoc that doesn't normalize in the
-- tests. Since we do our own normalization, we want to make sure -- tests. Since we do our own normalization, we want to make sure
@ -56,22 +57,44 @@ testCompareWithOpts opts name docxFile nativeFile =
testCompare :: String -> FilePath -> FilePath -> Test testCompare :: String -> FilePath -> FilePath -> Test
testCompare = testCompareWithOpts def testCompare = testCompareWithOpts def
testCompareMediaIO :: String -> FilePath -> FilePath -> FilePath -> IO Test getMedia :: FilePath -> FilePath -> IO (Maybe B.ByteString)
testCompareMediaIO name docxFile mediaPath mediaFile = do getMedia archivePath mediaPath = do
zf <- B.readFile archivePath >>= return . toArchive
return $ findEntryByPath (combine "word" mediaPath) zf >>= (Just . fromEntry)
compareMediaPathIO :: FilePath -> MediaBag -> FilePath -> IO Bool
compareMediaPathIO mediaPath mediaBag docxPath = do
docxMedia <- getMedia docxPath mediaPath
let mbBS = case lookupMedia mediaPath mediaBag of
Just (_, bs) -> bs
Nothing -> error ("couldn't find " ++
mediaPath ++
" in media bag")
docxBS = case docxMedia of
Just bs -> bs
Nothing -> error ("couldn't find " ++
mediaPath ++
" in media bag")
return $ mbBS == docxBS
compareMediaBagIO :: FilePath -> IO Bool
compareMediaBagIO docxFile = do
df <- B.readFile docxFile df <- B.readFile docxFile
mf <- B.readFile mediaFile
let (_, mb) = readDocx def df let (_, mb) = readDocx def df
dBytes = case lookupMedia mediaPath mb of bools <- mapM
Just (_,bs) -> bs (\(fp, _, _) -> compareMediaPathIO fp mb docxFile)
Nothing -> error "Media file not found" (mediaDirectory mb)
d64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks dBytes return $ and bools
m64 = B8.unpack $ B64.encode $ BS.concat $ B.toChunks mf
return $ test id name (d64, m64)
testCompareMedia :: String -> FilePath -> FilePath -> FilePath -> Test testMediaBagIO :: String -> FilePath -> IO Test
testCompareMedia name docxFile mediaPath mediaFile = testMediaBagIO name docxFile = do
buildTest $ testCompareMediaIO name docxFile mediaPath mediaFile outcome <- compareMediaBagIO docxFile
return $ testCase name (assertBool
("Media didn't match media bag in file " ++ docxFile)
outcome)
testMediaBag :: String -> FilePath -> Test
testMediaBag name docxFile = buildTest $ testMediaBagIO name docxFile
tests :: [Test] tests :: [Test]
tests = [ testGroup "inlines" tests = [ testGroup "inlines"
@ -186,11 +209,9 @@ tests = [ testGroup "inlines"
"docx.track_changes_deletion_all.native" "docx.track_changes_deletion_all.native"
] ]
, testGroup "media" , testGroup "media"
[ testCompareMedia [ testMediaBag
"image extraction" "image extraction"
"docx.image.docx" "docx.image.docx"
"media/image1.jpeg"
"docx.image1.jpeg"
] ]
, testGroup "metadata" , testGroup "metadata"
[ testCompareWithOpts def{readerStandalone=True} [ testCompareWithOpts def{readerStandalone=True}

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

View file

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