Files
pandoc/pandoc-lua-engine/test/Tests/Lua/Writer.hs
T
Albert Krewinkel a088cbf563 Lua: support extensions in custom writers
Custom writers can define the extensions that they support via the
global `writer_extensions`. The variable's value must be a table with
all supported extensions as keys, and their default status as values.

E.g., the below specifies that the writer support the extensions `smart`
and `sourcepos`, but only the `smart` extension is enabled by default:

    writer_extensions = {
      smart = true,
      sourcepos = false,
    }
2022-10-10 09:39:18 -07:00

82 lines
3.2 KiB
Haskell

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Tests.Lua.Writer
Copyright : © 2019-2022 Albert Krewinkel
License : GNU GPL, version 2 or above
Maintainer : Albert Krewinkel <albert@zeitkraut.de>
Tests for custom Lua writers.
-}
module Tests.Lua.Writer (tests) where
import Data.Default (Default (def))
import Text.Pandoc.Class (runIOorExplode, readFileStrict)
import Text.Pandoc.Extensions (Extension (..))
import Text.Pandoc.Format (ExtensionsDiff (..), FlavoredFormat (..),
applyExtensionsDiff)
import Text.Pandoc.Lua (writeCustom)
import Text.Pandoc.Options (WriterOptions (..))
import Text.Pandoc.Readers (readNative)
import Text.Pandoc.Writers (Writer (ByteStringWriter, TextWriter))
import Test.Tasty (TestTree)
import Test.Tasty.Golden (goldenVsString)
import Test.Tasty.HUnit (testCase, (@?=))
import qualified Data.ByteString.Lazy as BL
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.UTF8 as UTF8
tests :: [TestTree]
tests =
[ goldenVsString "default testsuite"
"writer.custom"
(runIOorExplode $ do
source <- UTF8.toText <$> readFileStrict "testsuite.native"
doc <- readNative def source
txt <- writeCustom "sample.lua" >>= \case
(TextWriter f, _) -> f def doc
_ -> error "Expected a text writer"
pure $ BL.fromStrict (UTF8.fromText txt))
, goldenVsString "tables testsuite"
"tables.custom"
(runIOorExplode $ do
source <- UTF8.toText <$> readFileStrict "tables.native"
doc <- readNative def source
txt <- writeCustom "sample.lua" >>= \case
(TextWriter f, _) -> f def doc
_ -> error "Expected a text writer"
pure $ BL.fromStrict (UTF8.fromText txt))
, goldenVsString "tables testsuite"
"bytestring.bin"
(runIOorExplode $ do
txt <- writeCustom "bytestring.lua" >>= \case
(ByteStringWriter f, _) -> f def mempty
_ -> error "Expected a bytestring writer"
pure txt)
, testCase "preset extensions" $ do
let ediff = ExtensionsDiff{extsToEnable = [], extsToDisable = []}
let format = FlavoredFormat "extensions.lua" ediff
result <- runIOorExplode $ writeCustom "extensions.lua" >>= \case
(TextWriter write, extsConf) -> do
exts <- applyExtensionsDiff extsConf format
write def{writerExtensions = exts} (B.doc mempty)
_ -> error "Expected a text writer"
result @?= "smart extension is enabled;\ncitations extension is disabled\n"
, testCase "modified extensions" $ do
let ediff = ExtensionsDiff
{ extsToEnable = [Ext_citations]
, extsToDisable = []
}
let format = FlavoredFormat "extensions.lua" ediff
result <- runIOorExplode $ writeCustom "extensions.lua" >>= \case
(TextWriter write, extsConf) -> do
exts <- applyExtensionsDiff extsConf format
write def{writerExtensions = exts} (B.doc mempty)
_ -> error "Expected a text writer"
result @?= "smart extension is enabled;\ncitations extension is enabled\n"
]