2021-10-20 21:40:07 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-03-25 22:16:27 +01:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2021-10-20 21:40:07 +02:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2019-02-04 22:52:31 +01:00
|
|
|
{- |
|
|
|
|
Module : Tests.Lua
|
2022-01-01 20:02:31 +01:00
|
|
|
Copyright : © 2017-2022 Albert Krewinkel
|
2019-02-04 22:52:31 +01:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : Albert Krewinkel <albert@zeitkraut.de>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Unit and integration tests for pandoc's Lua subsystem.
|
|
|
|
-}
|
2019-05-20 18:52:28 +02:00
|
|
|
module Tests.Lua ( runLuaTest, tests ) where
|
2017-03-20 15:17:03 +01:00
|
|
|
|
2021-10-20 21:40:07 +02:00
|
|
|
import HsLua as Lua hiding (Operation (Div), error)
|
2017-03-20 15:17:03 +01:00
|
|
|
import System.FilePath ((</>))
|
2021-12-10 18:28:54 +01:00
|
|
|
import Test.Tasty (TestTree, testGroup)
|
2021-11-08 12:14:44 +01:00
|
|
|
import Test.Tasty.HUnit ((@=?), Assertion, HasCallStack, assertEqual, testCase)
|
2017-04-06 21:00:38 +02:00
|
|
|
import Text.Pandoc.Arbitrary ()
|
2019-01-13 16:51:15 +01:00
|
|
|
import Text.Pandoc.Builder (bulletList, definitionList, displayMath, divWith,
|
|
|
|
doc, doubleQuoted, emph, header, lineBlock,
|
|
|
|
linebreak, math, orderedList, para, plain, rawBlock,
|
2020-01-15 23:26:00 +01:00
|
|
|
singleQuoted, space, str, strong,
|
|
|
|
HasMeta (setMeta))
|
2017-12-13 21:15:41 +01:00
|
|
|
import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
|
2021-10-20 21:40:07 +02:00
|
|
|
import Text.Pandoc.Definition (Attr, Block (BlockQuote, Div, Para), Pandoc,
|
2021-12-10 18:28:54 +01:00
|
|
|
Inline (Emph, Str), pandocTypesVersion)
|
2020-03-25 22:16:27 +01:00
|
|
|
import Text.Pandoc.Error (PandocError (PandocLuaError))
|
2018-10-26 23:21:54 +02:00
|
|
|
import Text.Pandoc.Filter (Filter (LuaFilter), applyFilters)
|
2021-12-31 20:12:23 +01:00
|
|
|
import Text.Pandoc.Lua (Global (..), runLua, setGlobals)
|
2018-01-12 08:56:33 +01:00
|
|
|
import Text.Pandoc.Options (def)
|
2018-01-07 13:43:03 +01:00
|
|
|
import Text.Pandoc.Shared (pandocVersion)
|
2017-03-20 15:17:03 +01:00
|
|
|
|
2020-03-25 22:16:27 +01:00
|
|
|
import qualified Control.Monad.Catch as Catch
|
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884.
+ Use pandoc-types 1.20 and texmath 0.12.
+ Text is now used instead of String, with a few exceptions.
+ In the MediaBag module, some of the types using Strings
were switched to use FilePath instead (not Text).
+ In the Parsing module, new parsers `manyChar`, `many1Char`,
`manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`,
`mantyUntilChar` have been added: these are like their
unsuffixed counterparts but pack some or all of their output.
+ `glob` in Text.Pandoc.Class still takes String since it seems
to be intended as an interface to Glob, which uses strings.
It seems to be used only once in the package, in the EPUB writer,
so that is not hard to change.
2019-11-04 22:12:37 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
2017-04-06 21:00:38 +02:00
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
tests :: [TestTree]
|
2021-12-10 18:28:54 +01:00
|
|
|
tests =
|
|
|
|
[ testCase "macro expansion via filter" $
|
2017-03-20 15:17:03 +01:00
|
|
|
assertFilterConversion "a '{{helloworld}}' string is expanded"
|
|
|
|
"strmacro.lua"
|
|
|
|
(doc . para $ str "{{helloworld}}")
|
|
|
|
(doc . para . emph $ str "Hello, World")
|
|
|
|
|
|
|
|
, testCase "convert all plains to paras" $
|
|
|
|
assertFilterConversion "plains become para"
|
|
|
|
"plain-to-para.lua"
|
|
|
|
(doc $ bulletList [plain (str "alfa"), plain (str "bravo")])
|
|
|
|
(doc $ bulletList [para (str "alfa"), para (str "bravo")])
|
|
|
|
|
2018-04-29 15:20:38 +02:00
|
|
|
, testCase "convert display math to inline math" $
|
|
|
|
assertFilterConversion "display math becomes inline math"
|
|
|
|
"math.lua"
|
|
|
|
(doc $ para (displayMath "5+5"))
|
|
|
|
(doc $ para (math "5+5"))
|
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
, testCase "make hello world document" $
|
|
|
|
assertFilterConversion "Document contains 'Hello, World!'"
|
|
|
|
"hello-world-doc.lua"
|
|
|
|
(doc . para $ str "Hey!" <> linebreak <> str "What's up?")
|
|
|
|
(doc . para $ str "Hello," <> space <> str "World!")
|
2017-04-02 17:21:22 +02:00
|
|
|
|
2017-04-30 16:14:33 +02:00
|
|
|
, testCase "implicit doc filter" $
|
|
|
|
assertFilterConversion "Document contains 'Hello, World!'"
|
|
|
|
"implicit-doc-filter.lua"
|
|
|
|
(doc . plain $ linebreak)
|
|
|
|
(doc . para $ str "Hello," <> space <> str "World!")
|
|
|
|
|
2017-04-02 17:21:22 +02:00
|
|
|
, testCase "parse raw markdown blocks" $
|
|
|
|
assertFilterConversion "raw markdown block is converted"
|
|
|
|
"markdown-reader.lua"
|
|
|
|
(doc $ rawBlock "markdown" "*charly* **delta**")
|
|
|
|
(doc . para $ emph "charly" <> space <> strong "delta")
|
2017-04-06 21:00:38 +02:00
|
|
|
|
2017-04-14 23:24:52 +02:00
|
|
|
, testCase "allow shorthand functions for quote types" $
|
|
|
|
assertFilterConversion "single quoted becomes double quoted string"
|
|
|
|
"single-to-double-quoted.lua"
|
|
|
|
(doc . para . singleQuoted $ str "simple")
|
|
|
|
(doc . para . doubleQuoted $ str "simple")
|
2017-08-22 22:02:30 +02:00
|
|
|
|
|
|
|
, testCase "Count inlines via metatable catch-all" $
|
|
|
|
assertFilterConversion "filtering with metatable catch-all failed"
|
|
|
|
"metatable-catch-all.lua"
|
|
|
|
(doc . para $ "four words, three spaces")
|
|
|
|
(doc . para $ str "7")
|
2017-08-22 23:12:39 +02:00
|
|
|
|
|
|
|
, testCase "Count blocks via Block-specific catch-all" $
|
|
|
|
assertFilterConversion "filtering with Block catch-all failed"
|
|
|
|
"block-count.lua"
|
|
|
|
(doc $ para "one" <> para "two")
|
|
|
|
(doc $ para "2")
|
2017-11-18 22:24:06 +01:00
|
|
|
|
2019-01-13 16:51:15 +01:00
|
|
|
, testCase "Smart constructors" $
|
|
|
|
assertFilterConversion "smart constructors returned a wrong result"
|
|
|
|
"smart-constructors.lua"
|
|
|
|
(doc $ para "")
|
|
|
|
(doc $ mconcat
|
|
|
|
[ bulletList [para "Hello", para "World"]
|
|
|
|
, definitionList [("foo", [para "placeholder"])]
|
|
|
|
, lineBlock ["Moin", "Welt"]
|
|
|
|
, orderedList [plain "one", plain "two"]
|
|
|
|
])
|
|
|
|
|
2017-11-18 22:24:06 +01:00
|
|
|
, testCase "Convert header upper case" $
|
|
|
|
assertFilterConversion "converting header to upper case failed"
|
|
|
|
"uppercase-header.lua"
|
|
|
|
(doc $ header 1 "les états-unis" <> para "text")
|
|
|
|
(doc $ header 1 "LES ÉTATS-UNIS" <> para "text")
|
2017-11-20 18:37:40 +01:00
|
|
|
|
|
|
|
, testCase "Attribute lists are convenient to use" $
|
|
|
|
let kv_before = [("one", "1"), ("two", "2"), ("three", "3")]
|
|
|
|
kv_after = [("one", "eins"), ("three", "3"), ("five", "5")]
|
|
|
|
in assertFilterConversion "Attr doesn't behave as expected"
|
|
|
|
"attr-test.lua"
|
|
|
|
(doc $ divWith ("", [], kv_before) (para "nil"))
|
|
|
|
(doc $ divWith ("", [], kv_after) (para "nil"))
|
2017-12-19 21:31:30 +01:00
|
|
|
|
2020-01-15 23:26:00 +01:00
|
|
|
, testCase "Filter list of inlines" $
|
|
|
|
assertFilterConversion "List of inlines"
|
|
|
|
"inlines-filter.lua"
|
|
|
|
(doc $ para ("Hello," <> linebreak <> "World! Wassup?"))
|
|
|
|
(doc $ para "Hello, World! Wassup?")
|
|
|
|
|
|
|
|
, testCase "Filter list of blocks" $
|
|
|
|
assertFilterConversion "List of blocks"
|
|
|
|
"blocks-filter.lua"
|
|
|
|
(doc $ para "one." <> para "two." <> para "three.")
|
|
|
|
(doc $ plain "3")
|
|
|
|
|
|
|
|
, testCase "Filter Meta" $
|
|
|
|
let setMetaBefore = setMeta "old" ("old" :: T.Text)
|
|
|
|
. setMeta "bool" False
|
|
|
|
setMetaAfter = setMeta "new" ("new" :: T.Text)
|
|
|
|
. setMeta "bool" True
|
|
|
|
in assertFilterConversion "Meta filtering"
|
|
|
|
"meta.lua"
|
|
|
|
(setMetaBefore . doc $ mempty)
|
|
|
|
(setMetaAfter . doc $ mempty)
|
|
|
|
|
2018-02-24 21:59:50 +01:00
|
|
|
, testCase "Script filename is set" $
|
|
|
|
assertFilterConversion "unexpected script name"
|
|
|
|
"script-name.lua"
|
|
|
|
(doc $ para "ignored")
|
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884.
+ Use pandoc-types 1.20 and texmath 0.12.
+ Text is now used instead of String, with a few exceptions.
+ In the MediaBag module, some of the types using Strings
were switched to use FilePath instead (not Text).
+ In the Parsing module, new parsers `manyChar`, `many1Char`,
`manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`,
`mantyUntilChar` have been added: these are like their
unsuffixed counterparts but pack some or all of their output.
+ `glob` in Text.Pandoc.Class still takes String since it seems
to be intended as an interface to Glob, which uses strings.
It seems to be used only once in the package, in the EPUB writer,
so that is not hard to change.
2019-11-04 22:12:37 +01:00
|
|
|
(doc $ para (str $ T.pack $ "lua" </> "script-name.lua"))
|
2018-02-24 21:59:50 +01:00
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "Pandoc version is set" . runLuaTest $ do
|
2018-01-07 13:43:03 +01:00
|
|
|
Lua.getglobal "PANDOC_VERSION"
|
2019-05-19 15:26:00 +02:00
|
|
|
Lua.liftIO .
|
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884.
+ Use pandoc-types 1.20 and texmath 0.12.
+ Text is now used instead of String, with a few exceptions.
+ In the MediaBag module, some of the types using Strings
were switched to use FilePath instead (not Text).
+ In the Parsing module, new parsers `manyChar`, `many1Char`,
`manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`,
`mantyUntilChar` have been added: these are like their
unsuffixed counterparts but pack some or all of their output.
+ `glob` in Text.Pandoc.Class still takes String since it seems
to be intended as an interface to Glob, which uses strings.
It seems to be used only once in the package, in the EPUB writer,
so that is not hard to change.
2019-11-04 22:12:37 +01:00
|
|
|
assertEqual "pandoc version is wrong" (TE.encodeUtf8 pandocVersion)
|
2021-10-20 21:40:07 +02:00
|
|
|
=<< Lua.tostring' Lua.top
|
2018-01-07 14:06:34 +01:00
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "Pandoc types version is set" . runLuaTest $ do
|
2018-01-07 14:06:34 +01:00
|
|
|
Lua.getglobal "PANDOC_API_VERSION"
|
2019-05-19 15:26:00 +02:00
|
|
|
Lua.liftIO . assertEqual "pandoc-types version is wrong" pandocTypesVersion
|
2021-10-20 21:40:07 +02:00
|
|
|
=<< Lua.peek Lua.top
|
2018-01-12 21:26:34 +01:00
|
|
|
|
2020-05-12 17:10:30 +02:00
|
|
|
, testCase "require file" $
|
|
|
|
assertFilterConversion "requiring file failed"
|
|
|
|
"require-file.lua"
|
|
|
|
(doc $ para "ignored")
|
|
|
|
(doc $ para (str . T.pack $ "lua" </> "require-file.lua"))
|
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "Allow singleton inline in constructors" . runLuaTest $ do
|
2021-10-20 21:40:07 +02:00
|
|
|
Lua.liftIO . assertEqual "Not the expected Emph"
|
|
|
|
(Emph [Str "test"]) =<< do
|
|
|
|
Lua.OK <- Lua.dostring "return pandoc.Emph"
|
|
|
|
Lua.push @Inline (Str "test")
|
|
|
|
Lua.call 1 1
|
|
|
|
Lua.peek @Inline top
|
|
|
|
Lua.liftIO . assertEqual "Unexpected element"
|
|
|
|
(Para [Str "test"]) =<< do
|
|
|
|
Lua.getglobal' "pandoc.Para"
|
|
|
|
Lua.pushString "test"
|
|
|
|
Lua.call 1 1
|
|
|
|
Lua.peek @Block top
|
2018-01-13 22:29:16 +01:00
|
|
|
Lua.liftIO . assertEqual "Unexptected element"
|
|
|
|
(BlockQuote [Para [Str "foo"]]) =<< (
|
|
|
|
do
|
|
|
|
Lua.getglobal' "pandoc.BlockQuote"
|
|
|
|
Lua.push (Para [Str "foo"])
|
|
|
|
_ <- Lua.call 1 1
|
2021-10-20 21:40:07 +02:00
|
|
|
Lua.peek @Block Lua.top
|
2018-01-13 22:29:16 +01:00
|
|
|
)
|
2018-01-13 18:52:17 +01:00
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "Elements with Attr have `attr` accessor" . runLuaTest $ do
|
2018-01-13 23:05:42 +01:00
|
|
|
Lua.push (Div ("hi", ["moin"], [])
|
|
|
|
[Para [Str "ignored"]])
|
2021-10-20 21:40:07 +02:00
|
|
|
Lua.getfield Lua.top "attr"
|
2018-01-13 23:05:42 +01:00
|
|
|
Lua.liftIO . assertEqual "no accessor" (("hi", ["moin"], []) :: Attr)
|
2021-10-20 21:40:07 +02:00
|
|
|
=<< Lua.peek @Attr Lua.top
|
2018-01-13 23:05:42 +01:00
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "module `pandoc.system` is present" . runLuaTest $ do
|
2019-05-04 07:06:30 +02:00
|
|
|
Lua.getglobal' "pandoc.system"
|
2021-10-20 21:40:07 +02:00
|
|
|
ty <- Lua.ltype Lua.top
|
2019-05-04 07:06:30 +02:00
|
|
|
Lua.liftIO $ assertEqual "module should be a table" Lua.TypeTable ty
|
|
|
|
|
2021-11-17 08:47:30 +01:00
|
|
|
, testGroup "global modules"
|
|
|
|
[ testCase "module 'lpeg' is loaded into a global" . runLuaTest $ do
|
|
|
|
s <- Lua.dostring "assert(type(lpeg)=='table')"
|
|
|
|
Lua.liftIO $ Lua.OK @=? s
|
2021-11-08 12:14:44 +01:00
|
|
|
|
2021-11-17 08:47:30 +01:00
|
|
|
, testCase "module 're' is loaded into a global" . runLuaTest $ do
|
|
|
|
s <- Lua.dostring "assert(type(re)=='table')"
|
|
|
|
Lua.liftIO $ Lua.OK @=? s
|
|
|
|
|
|
|
|
, testCase "module 'lpeg' is available via `require`" . runLuaTest $ do
|
2021-11-20 06:54:55 +01:00
|
|
|
s <- Lua.dostring
|
|
|
|
"package.path = ''; package.cpath = ''; require 'lpeg'"
|
2021-11-17 08:47:30 +01:00
|
|
|
Lua.liftIO $ Lua.OK @=? s
|
|
|
|
|
|
|
|
, testCase "module 're' is available via `require`" . runLuaTest $ do
|
2021-11-20 06:54:55 +01:00
|
|
|
s <- Lua.dostring
|
|
|
|
"package.path = ''; package.cpath = ''; require 're'"
|
2021-11-17 08:47:30 +01:00
|
|
|
Lua.liftIO $ Lua.OK @=? s
|
|
|
|
]
|
2021-11-08 12:14:44 +01:00
|
|
|
|
2019-05-20 18:52:28 +02:00
|
|
|
, testCase "informative error messages" . runLuaTest $ do
|
2018-01-12 21:26:34 +01:00
|
|
|
Lua.pushboolean True
|
2021-10-20 21:40:07 +02:00
|
|
|
-- Lua.newtable
|
|
|
|
eitherPandoc <- Catch.try (peek @Pandoc Lua.top)
|
2020-03-25 22:16:27 +01:00
|
|
|
case eitherPandoc of
|
|
|
|
Left (PandocLuaError msg) -> do
|
2021-10-20 21:40:07 +02:00
|
|
|
let expectedMsg = "Pandoc expected, got boolean\n"
|
2021-11-28 02:08:01 +01:00
|
|
|
<> "\twhile retrieving Pandoc"
|
2018-01-12 21:26:34 +01:00
|
|
|
Lua.liftIO $ assertEqual "unexpected error message" expectedMsg msg
|
2020-03-25 22:16:27 +01:00
|
|
|
Left e -> error ("Expected a Lua error, but got " <> show e)
|
2018-01-12 21:26:34 +01:00
|
|
|
Right _ -> error "Getting a Pandoc element from a bool should fail."
|
2017-03-20 15:17:03 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
|
2018-10-26 23:21:54 +02:00
|
|
|
assertFilterConversion msg filterPath docIn expectedDoc = do
|
|
|
|
actualDoc <- runIOorExplode $ do
|
2017-12-13 21:15:41 +01:00
|
|
|
setUserDataDir (Just "../data")
|
2018-10-26 23:21:54 +02:00
|
|
|
applyFilters def [LuaFilter ("lua" </> filterPath)] ["HTML"] docIn
|
|
|
|
assertEqual msg expectedDoc actualDoc
|
2017-04-06 21:00:38 +02:00
|
|
|
|
2021-10-20 21:40:07 +02:00
|
|
|
runLuaTest :: HasCallStack => Lua.LuaE PandocError a -> IO a
|
2019-05-20 18:52:28 +02:00
|
|
|
runLuaTest op = runIOorExplode $ do
|
2021-12-31 20:12:23 +01:00
|
|
|
res <- runLua $ do
|
|
|
|
setGlobals [ PANDOC_WRITER_OPTIONS def ]
|
|
|
|
op
|
2018-01-07 13:43:03 +01:00
|
|
|
case res of
|
|
|
|
Left e -> error (show e)
|
|
|
|
Right x -> return x
|