diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-12-29 08:37:19 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-12-29 08:37:19 -0800 |
commit | 4962220315d5e2c429ab63f558381528e808eafd (patch) | |
tree | b9aa106be12d34f353e85ada351cebd24b94dd2f /test | |
parent | 37778077debda3b1af80be92728c3a675f8ed384 (diff) | |
parent | 76442a791c4db9df43792dbd3733272607d4586e (diff) | |
download | pandoc-4962220315d5e2c429ab63f558381528e808eafd.tar.gz |
Merge branch 'master' of github.com:jgm/pandoc
Diffstat (limited to 'test')
-rw-r--r-- | test/Tests/Writers/Powerpoint.hs | 104 |
1 files changed, 86 insertions, 18 deletions
diff --git a/test/Tests/Writers/Powerpoint.hs b/test/Tests/Writers/Powerpoint.hs index 46ebd77bd..39fd1bab5 100644 --- a/test/Tests/Writers/Powerpoint.hs +++ b/test/Tests/Writers/Powerpoint.hs @@ -5,27 +5,35 @@ module Tests.Writers.Powerpoint (tests) where import Control.Exception (throwIO) import Text.Pandoc import Text.Pandoc.Builder +import Text.Pandoc.Arbitrary () +import Text.Pandoc.Walk import Test.Tasty import Test.Tasty.HUnit +import Test.Tasty.QuickCheck import Codec.Archive.Zip -import Data.List (isPrefixOf, isSuffixOf) +import Text.XML.Light +import Data.List (isPrefixOf, isSuffixOf, sort) +import Data.Maybe (mapMaybe) + +getPptxArchive :: WriterOptions -> Pandoc -> IO Archive +getPptxArchive opts pd = do + mbs <- runIO $ + do setUserDataDir $ Just "../data" + writePowerpoint opts pd + case mbs of + Left e -> throwIO e + Right bs -> return $ toArchive bs ----- Number of Slides ----------- numberOfSlides :: WriterOptions -> Pandoc -> IO Int numberOfSlides opts pd = do - mbs <- runIO $ - do setUserDataDir $ Just "../data" - writePowerpoint opts pd - case mbs of - Left e -> throwIO e - Right bs -> do - let archive = toArchive bs - return $ - length $ - filter (isSuffixOf ".xml") $ - filter (isPrefixOf "ppt/slides/slide") $ - filesInArchive archive + archive <- getPptxArchive opts pd + return $ + length $ + filter (isSuffixOf ".xml") $ + filter (isPrefixOf "ppt/slides/slide") $ + filesInArchive archive testNumberOfSlides :: TestName -> Int -> WriterOptions -> Pandoc -> TestTree testNumberOfSlides name n opts pd = @@ -52,12 +60,12 @@ numSlideTests = testGroup "Number of slides in output" def (doc $ header 1 "Header" <> header 2 "subeader" <> para "foo") , testNumberOfSlides - "With h1 slide (using default slide-level)" 2 - def + "With h1 slide (using slide-level 3)" 2 + def {writerSlideLevel= Just 3} (doc $ header 1 "Header" <> para "foo") , testNumberOfSlides - "With h2 slide (using default slide-level)" 2 - def + "With h2 slide (using slide-level 3)" 3 + def {writerSlideLevel= Just 3} (doc $ header 1 "Header" <> header 2 "subeader" <> para "foo") , testNumberOfSlides "With image slide, no header" 3 @@ -94,8 +102,68 @@ numSlideTests = testGroup "Number of slides in output" def (doc $ para "first slide" <> horizontalRule <> para "last slide") + , testNumberOfSlides + "with notes slide" 2 + def + (doc $ + para $ text "Foo" <> note (para "note text")) ] +----- Content Types ----------- + + +contentTypesFileExists :: WriterOptions -> Pandoc -> TestTree +contentTypesFileExists opts pd = + testCase "Existence of [Content_Types].xml file" $ + do archive <- getPptxArchive opts pd + assertBool "Missing [Content_Types].xml file" $ + "[Content_Types].xml" `elem` (filesInArchive archive) + + + +-- We want an "Override" entry for each xml file under ppt/. +prop_ContentOverrides :: Pandoc -> IO Bool +prop_ContentOverrides pd = do + -- remove Math to avoid warnings + let go :: Inline -> Inline + go (Math _ _) = Str "Math" + go i = i + pd' = walk go pd + archive <- getPptxArchive def pd' + let xmlFiles = filter ("[Content_Types].xml" /=) $ + filter (isSuffixOf ".xml") $ + filesInArchive archive + contentTypes <- case findEntryByPath "[Content_Types].xml" archive of + Just ent -> return $ fromEntry ent + Nothing -> throwIO $ + PandocSomeError "Missing [Content_Types].xml file" + typesElem <- case parseXMLDoc contentTypes of + Just element -> return $ element + Nothing -> throwIO $ + PandocSomeError "[Content_Types].xml cannot be parsed" + let ns = findAttr (QName "xmlns" Nothing Nothing) typesElem + overrides = findChildren (QName "Override" ns Nothing) typesElem + partNames = mapMaybe (findAttr (QName "PartName" Nothing Nothing)) overrides + -- files in content_types are absolute + absXmlFiles = map (\fp -> case fp of + ('/':_) -> fp + _ -> '/': fp + ) + xmlFiles + return $ sort absXmlFiles == sort partNames + +contentOverridesTests :: TestTree +contentOverridesTests = localOption (QuickCheckTests 20) $ + testProperty "Content Overrides for each XML file" $ + \x -> ioProperty $ prop_ContentOverrides (x :: Pandoc) + +contentTypeTests :: TestTree +contentTypeTests = testGroup "[Content_Types].xml file" + [ contentTypesFileExists def (doc $ para "foo") + , contentOverridesTests + ] tests :: [TestTree] -tests = [numSlideTests] +tests = [ numSlideTests + , contentTypeTests + ] |