2017-10-28 05:28:29 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2017-03-20 15:17:03 +01:00
|
|
|
module Tests.Lua ( tests ) where
|
|
|
|
|
2017-04-06 21:00:38 +02:00
|
|
|
import Control.Monad (when)
|
2018-01-07 14:06:34 +01:00
|
|
|
import Data.Version (Version (versionBranch))
|
2017-03-20 15:17:03 +01:00
|
|
|
import System.FilePath ((</>))
|
2017-05-07 11:45:06 +02:00
|
|
|
import Test.Tasty (TestTree, localOption)
|
2017-03-20 15:17:03 +01:00
|
|
|
import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
|
2017-10-28 05:28:29 +02:00
|
|
|
import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
|
2017-04-06 21:00:38 +02:00
|
|
|
import Text.Pandoc.Arbitrary ()
|
2017-11-20 18:37:40 +01:00
|
|
|
import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
|
|
|
|
header, linebreak, para, plain, rawBlock,
|
|
|
|
singleQuoted, space, str, strong, (<>))
|
2017-12-13 21:15:41 +01:00
|
|
|
import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
|
2018-01-07 14:06:34 +01:00
|
|
|
import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc, pandocTypesVersion)
|
2017-12-13 21:15:41 +01:00
|
|
|
import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
|
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
|
|
|
|
2017-12-02 23:07:29 +01:00
|
|
|
import qualified Foreign.Lua as Lua
|
2017-04-06 21:00:38 +02:00
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
tests :: [TestTree]
|
2017-05-07 11:45:06 +02:00
|
|
|
tests = map (localOption (QuickCheckTests 20))
|
2017-04-14 23:24:52 +02:00
|
|
|
[ testProperty "inline elements can be round-tripped through the lua stack" $
|
|
|
|
\x -> ioProperty (roundtripEqual (x::Inline))
|
|
|
|
|
|
|
|
, testProperty "block elements can be round-tripped through the lua stack" $
|
|
|
|
\x -> ioProperty (roundtripEqual (x::Block))
|
|
|
|
|
|
|
|
, testProperty "meta blocks can be round-tripped through the lua stack" $
|
|
|
|
\x -> ioProperty (roundtripEqual (x::Meta))
|
|
|
|
|
|
|
|
, testProperty "documents can be round-tripped through the lua stack" $
|
|
|
|
\x -> ioProperty (roundtripEqual (x::Pandoc))
|
|
|
|
|
|
|
|
, 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")])
|
|
|
|
|
|
|
|
, 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
|
|
|
|
|
|
|
, 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
|
|
|
|
|
|
|
, testCase "Test module pandoc.utils" $
|
|
|
|
assertFilterConversion "pandoc.utils doesn't work as expected."
|
|
|
|
"test-pandoc-utils.lua"
|
|
|
|
(doc $ para "doesn't matter")
|
2017-12-23 22:39:05 +01:00
|
|
|
(doc $ mconcat [ plain (str "hierarchicalize: OK")
|
|
|
|
, plain (str "normalize_date: OK")
|
2017-12-19 21:31:30 +01:00
|
|
|
, plain (str "pipe: OK")
|
|
|
|
, plain (str "failing pipe: OK")
|
|
|
|
, plain (str "read: OK")
|
|
|
|
, plain (str "failing read: OK")
|
2017-12-23 13:35:27 +01:00
|
|
|
, plain (str "sha1: OK")
|
2017-12-22 20:08:51 +01:00
|
|
|
, plain (str "stringify: OK")
|
2017-12-23 11:53:26 +01:00
|
|
|
, plain (str "to_roman_numeral: OK")
|
2017-12-19 21:31:30 +01:00
|
|
|
])
|
2018-01-07 13:43:03 +01:00
|
|
|
|
|
|
|
, testCase "Pandoc version is set" . runPandocLua' $ do
|
|
|
|
Lua.getglobal' "table.concat"
|
|
|
|
Lua.getglobal "PANDOC_VERSION"
|
|
|
|
Lua.push ("." :: String) -- seperator
|
|
|
|
Lua.call 2 1
|
|
|
|
Lua.liftIO . assertEqual "pandoc version is wrong" pandocVersion
|
|
|
|
=<< Lua.peek Lua.stackTop
|
2018-01-07 14:06:34 +01:00
|
|
|
|
|
|
|
, testCase "Pandoc types version is set" . runPandocLua' $ do
|
|
|
|
let versionNums = versionBranch pandocTypesVersion
|
|
|
|
Lua.getglobal "PANDOC_API_VERSION"
|
2018-01-08 23:26:38 +01:00
|
|
|
Lua.liftIO . assertEqual "pandoc-types version is wrong" versionNums
|
2018-01-07 14:06:34 +01:00
|
|
|
=<< Lua.peek Lua.stackTop
|
2017-03-20 15:17:03 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
|
|
|
|
assertFilterConversion msg filterPath docIn docExpected = do
|
2017-12-13 21:15:41 +01:00
|
|
|
docEither <- runIOorExplode $ do
|
|
|
|
setUserDataDir (Just "../data")
|
2018-01-12 08:56:33 +01:00
|
|
|
runLuaFilter def ("lua" </> filterPath) [] docIn
|
2017-09-29 00:11:52 +02:00
|
|
|
case docEither of
|
2017-10-28 05:28:29 +02:00
|
|
|
Left _ -> fail "lua filter failed"
|
2017-09-29 00:11:52 +02:00
|
|
|
Right docRes -> assertEqual msg docExpected docRes
|
2017-04-06 21:00:38 +02:00
|
|
|
|
2017-12-02 23:07:29 +01:00
|
|
|
roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
|
2017-04-06 21:00:38 +02:00
|
|
|
roundtripEqual x = (x ==) <$> roundtripped
|
|
|
|
where
|
2017-12-02 23:07:29 +01:00
|
|
|
roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
|
2018-01-07 13:43:03 +01:00
|
|
|
roundtripped = runPandocLua' $ do
|
|
|
|
oldSize <- Lua.gettop
|
|
|
|
Lua.push x
|
|
|
|
size <- Lua.gettop
|
|
|
|
when (size - oldSize /= 1) $
|
|
|
|
error ("not exactly one additional element on the stack: " ++ show size)
|
|
|
|
res <- Lua.peekEither (-1)
|
2017-08-13 12:37:10 +02:00
|
|
|
case res of
|
2018-01-08 23:26:38 +01:00
|
|
|
Left e -> error (show e)
|
2017-10-28 05:28:29 +02:00
|
|
|
Right y -> return y
|
2018-01-07 13:43:03 +01:00
|
|
|
|
|
|
|
runPandocLua' :: Lua.Lua a -> IO a
|
|
|
|
runPandocLua' op = runIOorExplode $ do
|
|
|
|
setUserDataDir (Just "../data")
|
|
|
|
res <- runPandocLua op
|
|
|
|
case res of
|
|
|
|
Left e -> error (show e)
|
|
|
|
Right x -> return x
|