git.iko.soy/typeable/pandoc
on a29fa15 help sign in
87 lines 3.3 kB history raw
1{-# Language OverloadedStrings #-}
2module Tests.Lua ( tests ) where
4import Control.Monad (when)
5import System.FilePath ((</>))
6import Test.Tasty (TestTree)
7import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
8import Test.Tasty.QuickCheck (ioProperty, testProperty)
9import Text.Pandoc.Arbitrary ()
10import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
11import Text.Pandoc.Builder ( (<>), bulletList, doc, doubleQuoted, emph
12 , linebreak, rawBlock, singleQuoted, para, plain
13 , space, str, strong)
14import Text.Pandoc.Lua
16import qualified Scripting.Lua as Lua
18tests :: [TestTree]
19tests =
20 [ testProperty "inline elements can be round-tripped through the lua stack" $
21 \x -> ioProperty (roundtripEqual (x::Inline))
23 , testProperty "block elements can be round-tripped through the lua stack" $
24 \x -> ioProperty (roundtripEqual (x::Block))
26 , testProperty "meta blocks can be round-tripped through the lua stack" $
27 \x -> ioProperty (roundtripEqual (x::Meta))
29 , testProperty "documents can be round-tripped through the lua stack" $
30 \x -> ioProperty (roundtripEqual (x::Pandoc))
32 , testCase "macro expansion via filter" $
33 assertFilterConversion "a '{{helloworld}}' string is expanded"
34 "strmacro.lua"
35 (doc . para $ str "{{helloworld}}")
36 (doc . para . emph $ str "Hello, World")
38 , testCase "convert all plains to paras" $
39 assertFilterConversion "plains become para"
40 "plain-to-para.lua"
41 (doc $ bulletList [plain (str "alfa"), plain (str "bravo")])
42 (doc $ bulletList [para (str "alfa"), para (str "bravo")])
44 , testCase "make hello world document" $
45 assertFilterConversion "Document contains 'Hello, World!'"
46 "hello-world-doc.lua"
47 (doc . para $ str "Hey!" <> linebreak <> str "What's up?")
48 (doc . para $ str "Hello," <> space <> str "World!")
50 , testCase "parse raw markdown blocks" $
51 assertFilterConversion "raw markdown block is converted"
52 "markdown-reader.lua"
53 (doc $ rawBlock "markdown" "*charly* **delta**")
54 (doc . para $ emph "charly" <> space <> strong "delta")
56 , testCase "allow shorthand functions for quote types" $
57 assertFilterConversion "single quoted becomes double quoted string"
58 "single-to-double-quoted.lua"
59 (doc . para . singleQuoted $ str "simple")
60 (doc . para . doubleQuoted $ str "simple")
61 ]
63assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
64assertFilterConversion msg filterPath docIn docExpected = do
65 docRes <- runLuaFilter ("lua" </> filterPath) [] docIn
66 assertEqual msg docExpected docRes
68roundtripEqual :: (Eq a, Lua.StackValue a) => a -> IO Bool
69roundtripEqual x = (x ==) <$> roundtripped
70 where
71 roundtripped :: (Lua.StackValue a) => IO a
72 roundtripped = do
73 lua <- Lua.newstate
74 Lua.openlibs lua
75 pushPandocModule lua
76 Lua.setglobal lua "pandoc"
77 oldSize <- Lua.gettop lua
78 Lua.push lua x
79 size <- Lua.gettop lua
80 when ((size - oldSize) /= 1) $
81 error ("not exactly one additional element on the stack: " ++ show size)
82 res <- Lua.peek lua (-1)
83 retval <- case res of
84 Nothing -> error "could not read from stack"
85 Just y -> return y
86 Lua.close lua
87 return retval