fradrive/test/PandocSpec.hs

82 lines
2.8 KiB
Haskell

module PandocSpec where
import TestImport
import Utils.Print
import qualified Data.Set as Set
import qualified Data.Map.Lazy as Map
import qualified Data.Foldable as Fold
import Text.Pandoc
import Text.Pandoc.Builder
import Text.Pandoc.Arbitrary ()
-- Instance Arbitrary Meta is somewhat useless, as it always generates the same 3 keys.
newtype ArbitraryMeta = ArbitraryMeta { unArbitraryMeta :: Meta }
deriving newtype (Eq, Ord, Semigroup, Monoid, Show, Read)
instance Arbitrary ArbitraryMeta where
arbitrary
= do (x1 :: Inlines) <- arbitrary
(x2 :: [Inlines]) <- filter (not . Fold.null) <$> arbitrary
(x3 :: Inlines) <- arbitrary
(x4 :: [(Text, Text)]) <- arbitrary
(x5 :: [(Text, Bool)]) <- arbitrary
return $ ArbitraryMeta
$ setMeta "title" x1
$ setMeta "author" x2
$ setMeta "date" x3
$ applyMetas x4
$ applyMetas x5
nullMeta
-- For Lens Check _Meta required:
--instance CoArbitrary Inline
--instance CoArbitrary MetaValue
--instance CoArbitrary Meta
--instance Function Inline
--instance Function MetaValue
--instance Function Meta
spec :: Spec
spec = do
let mlist = Map.toList . unMeta
describe "applyMetas" $ do
it "should actually set values" $ do
(ml, pd) <- generate arbitrary
let
mlKeys = Set.fromList $ fst <$> ml
(Pandoc newMeta _) = applyMetas (fmap MetaString <$> ml) pd
ml' = [(k,t) | (k, MetaString t) <- mlist newMeta, Set.member k mlKeys]
ml `shouldMatchList` ml'
describe "addMeta" $ do
it "should possibly overwrite existing settings" $ do
(abMetaOverwrite, abMetaOriginal, blocks) <- generate arbitrary
let
metaOverwrite = unArbitraryMeta abMetaOverwrite
metaOriginal = unArbitraryMeta abMetaOriginal
pd = Pandoc metaOriginal blocks
(Pandoc newMeta _) = addMeta metaOverwrite pd
mlist newMeta `shouldContain` mlist metaOverwrite
it "should preserve untouched settings" $ do
(abMetaOverwrite, abMetaOriginal, blocks) <- generate arbitrary
let
metaOverwrite = unArbitraryMeta abMetaOverwrite
metaOriginal = unArbitraryMeta abMetaOriginal
pd = Pandoc metaOriginal blocks
(Pandoc newMeta _) = addMeta metaOverwrite pd
mlist newMeta `shouldContain` Map.toList (unMeta metaOriginal `Map.difference` unMeta metaOverwrite)
it "should preserve document blocks" $ do
(metaOverwrite, pd) <- generate arbitrary
let
(Pandoc _ oldBlocks) = pd
(Pandoc _ newBlocks) = addMeta (unArbitraryMeta metaOverwrite) pd
oldBlocks `shouldBe` newBlocks
-- describe "_Meta" . it "is a lens" . property $ isLens _Meta