test(pandoc): fix an occasionally erroneously failing test

This commit is contained in:
Steffen Jost 2022-12-13 10:51:10 +01:00
parent 8ebae1bee7
commit 10b443f188
2 changed files with 34 additions and 21 deletions

View File

@ -119,8 +119,8 @@ appMeta f (P.Pandoc m bs) = P.Pandoc (f m) bs
applyMetas :: (P.HasMeta p, Foldable t, P.ToMetaValue a) => t (Text, Maybe a) -> p -> p applyMetas :: (P.HasMeta p, Foldable t, P.ToMetaValue a) => t (Text, Maybe a) -> p -> p
applyMetas metas doc = Fold.foldr act doc metas applyMetas metas doc = Fold.foldr act doc metas
where where
act (_, Nothing) acc = acc act (k, Just v) acc | notNull k = P.setMeta k v acc
act (k, Just v ) acc = P.setMeta k v acc act _ acc = acc
-- | Add meta to pandoc. Existing variables will be overwritten. -- | Add meta to pandoc. Existing variables will be overwritten.

View File

@ -24,13 +24,15 @@ newtype ArbitraryMeta = ArbitraryMeta { unArbitraryMeta :: Meta }
newtype ArbitraryPandoc = ArbitraryPandoc { unArbitraryPandoc :: Pandoc } newtype ArbitraryPandoc = ArbitraryPandoc { unArbitraryPandoc :: Pandoc }
deriving newtype (Eq, Ord, Show, Read, Typeable) deriving newtype (Eq, Ord, Show, Read, Typeable)
newtype ArbitraryMapText a = ArbitraryMapText { unArbitraryMapText :: [(Text, a)] }
instance Arbitrary ArbitraryMeta where instance Arbitrary ArbitraryMeta where
arbitrary = do arbitrary = do
(x1 :: Inlines) <- arbitrary (x1 :: Inlines) <- arbitrary
(x2 :: [Inlines]) <- filter (not . Fold.null) <$> arbitrary (x2 :: [Inlines]) <- filter (not . Fold.null) <$> arbitrary
(x3 :: Inlines) <- arbitrary (x3 :: Inlines) <- arbitrary
(x4 :: [(Text, Text)]) <- filter (not . T.null . fst) <$> arbitrary (x4 :: [(Text, Text)]) <- unArbitraryMapText <$> arbitrary
(x5 :: [(Text, Bool)]) <- filter (not . T.null . fst) <$> arbitrary (x5 :: [(Text, Bool)]) <- unArbitraryMapText <$> arbitrary
return $ ArbitraryMeta return $ ArbitraryMeta
$ setMeta "title" x1 $ setMeta "title" x1
$ setMeta "author" x2 $ setMeta "author" x2
@ -44,6 +46,15 @@ instance Arbitrary ArbitraryPandoc where
meta <- arbitrary meta <- arbitrary
ArbitraryPandoc . Pandoc (unArbitraryMeta meta) <$> arbitrary ArbitraryPandoc . Pandoc (unArbitraryMeta meta) <$> arbitrary
nonEmptyString :: Gen Text
nonEmptyString = T.pack <$> listOf1 arbitrary
instance Arbitrary a => Arbitrary (ArbitraryMapText a) where
arbitrary =
let genKV = (,) <$> nonEmptyString <*> arbitrary
in ArbitraryMapText <$> listOf1 genKV
-- For Lens Check _Meta required: -- For Lens Check _Meta required:
--instance CoArbitrary Inline --instance CoArbitrary Inline
@ -59,13 +70,15 @@ spec = do
describe "applyMetas" $ do describe "applyMetas" $ do
it "should actually set values" $ do it "should actually set values" $ do
(metaList, apd) <- generate arbitrary (metaList0, apd) <- generate arbitrary
let let
metaList1 :: [(Text, Maybe MetaValue)] = second (Just . MetaString) <$> unArbitraryMapText metaList0
pd = unArbitraryPandoc apd pd = unArbitraryPandoc apd
(Pandoc metaNew _) = applyMetas (second (Just . MetaString) <$> metaList) pd (Pandoc metaNew _) = applyMetas metaList1 pd
mlKeys = Set.fromList $ fst <$> metaList metaMap1 = Map.fromList metaList1 -- remove duplicate keys
metaList' = [(k,t) | (k, MetaString t) <- mlist metaNew, Set.member k mlKeys] keys1 = Map.keysSet metaMap1
metaList' `shouldMatchList` metaList metaList' = [(k, Just t) | (k, t) <- mlist metaNew, k `Set.member` keys1]
metaList' `shouldMatchList` Map.toAscList metaMap1
it "should preserve untouched settings" $ do it "should preserve untouched settings" $ do
(metaList, apd) <- generate arbitrary (metaList, apd) <- generate arbitrary