aboutsummaryrefslogtreecommitdiff
path: root/test/Tests/Readers/EPUB.hs
blob: 13e23991154c051cd8e664452728767f3b0b3849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{- |
   Module      : Tests.Readers.EPUB
   Copyright   : © 2006-2021 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.eu>
   Stability   : alpha
   Portability : portable

Tests for the EPUB mediabag.
-}
module Tests.Readers.EPUB (tests) where

import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit
import qualified Text.Pandoc.Class as P
import Text.Pandoc.MediaBag (MediaBag, mediaDirectory)
import Text.Pandoc.Options
import Text.Pandoc.Readers.EPUB

getMediaBag :: FilePath -> IO MediaBag
getMediaBag fp = do
  bs <- BL.readFile fp
  P.runIOorExplode $ do
    readEPUB def bs
    P.getMediaBag

testMediaBag :: FilePath -> [(String, String, Int)] -> IO ()
testMediaBag fp bag = do
  actBag <- mediaDirectory <$> getMediaBag fp
  assertBool (show "MediaBag did not match:\nExpected: "
             ++ show bag
             ++ "\nActual: "
             ++ show actBag)
             (actBag == packBag bag)
  where
    packBag = map $ \(x, y, z) -> (x, T.pack y, z)

featuresBag :: [(String, String, Int)]
featuresBag = [("img/check.gif","image/gif",1340)
              ,("img/check.jpg","image/jpeg",2661)
              ,("img/check.png","image/png",2815)
              ,("img/multiscripts_and_greek_alphabet.png","image/png",10060)
              ]

-- with additional meta tag for cover in EPUB2 format
epub3CoverBag :: [(String, String, Int)]
epub3CoverBag = [("wasteland-cover.jpg","image/jpeg",103477)]

epub3NoCoverBag :: [(String, String, Int)]
epub3NoCoverBag = [("img/check.gif","image/gif",1340)
                  ,("img/check.jpg","image/jpeg",2661)
                  ,("img/check.png","image/png",2815)
                  ]

-- content.opf uses the word `picture` to refer to the cover as much as validly possible
-- to check if references are resolved correctly
epub2PictureBag :: [(String, String, Int)]
epub2PictureBag = [("image/image.jpg","image/jpeg",9713)]

-- content.opf contains the word `cover` as much as possible, to check if possible multiple matches cause errors
epub2CoverBag :: [(String, String, Int)]
epub2CoverBag = [("image/cover.jpg","image/jpeg",9713)]

epub2NoCoverBag :: [(String, String, Int)]
epub2NoCoverBag = []

tests :: [TestTree]
tests =
  [ testGroup "EPUB Mediabag"
    [ testCase "features bag"
      (testMediaBag "epub/img.epub" featuresBag),
      testCase "EPUB3 cover bag"
      (testMediaBag "epub/wasteland.epub" epub3CoverBag),
      testCase "EPUB3 no cover bag"
      (testMediaBag "epub/img_no_cover.epub" epub3NoCoverBag),
      testCase "EPUB2 picture bag"
      (testMediaBag "epub/epub2_picture.epub" epub2PictureBag),
      testCase "EPUB2 cover bag"
      (testMediaBag "epub/epub2_cover.epub" epub2CoverBag),
      testCase "EPUB2 no cover bag"
      (testMediaBag "epub/epub2_no_cover.epub" epub2NoCoverBag)
    ]
  ]