git.iko.soy/typeable/pandoc
on 41ebdee help sign in
76 lines 2.8 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, emph, linebreak, rawBlock
12 , para, plain, space, str, strong)
13import Text.Pandoc.Lua
15import qualified Scripting.Lua as Lua
17tests :: [TestTree]
18tests =
19 [ testCase "macro expansion via filter" $
20 assertFilterConversion "a '{{helloworld}}' string is expanded"
21 "strmacro.lua"
22 (doc . para $ str "{{helloworld}}")
23 (doc . para . emph $ str "Hello, World")
25 , testCase "convert all plains to paras" $
26 assertFilterConversion "plains become para"
27 "plain-to-para.lua"
28 (doc $ bulletList [plain (str "alfa"), plain (str "bravo")])
29 (doc $ bulletList [para (str "alfa"), para (str "bravo")])
31 , testCase "make hello world document" $
32 assertFilterConversion "Document contains 'Hello, World!'"
33 "hello-world-doc.lua"
34 (doc . para $ str "Hey!" <> linebreak <> str "What's up?")
35 (doc . para $ str "Hello," <> space <> str "World!")
37 , testCase "parse raw markdown blocks" $
38 assertFilterConversion "raw markdown block is converted"
39 "markdown-reader.lua"
40 (doc $ rawBlock "markdown" "*charly* **delta**")
41 (doc . para $ emph "charly" <> space <> strong "delta")
43 , testProperty "inline elements can be round-tripped through the lua stack" $
44 \x -> ioProperty (roundtripEqual (x::Inline))
46 , testProperty "block elements can be round-tripped through the lua stack" $
47 \x -> ioProperty (roundtripEqual (x::Block))
49 , testProperty "meta blocks can be round-tripped through the lua stack" $
50 \x -> ioProperty (roundtripEqual (x::Meta))
52 , testProperty "documents can be round-tripped through the lua stack" $
53 \x -> ioProperty (roundtripEqual (x::Pandoc))
54 ]
56assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
57assertFilterConversion msg filterPath docIn docExpected = do
58 docRes <- runLuaFilter ("lua" </> filterPath) [] docIn
59 assertEqual msg docExpected docRes
61roundtripEqual :: (Eq a, Lua.StackValue a) => a -> IO Bool
62roundtripEqual x = (x ==) <$> roundtripped
63 where
64 roundtripped :: (Lua.StackValue a) => IO a
65 roundtripped = do
66 lua <- Lua.newstate
67 Lua.push lua x
68 size <- Lua.gettop lua
69 when (size /= 1) $
70 error ("not exactly one element on the stack: " ++ show size)
71 res <- Lua.peek lua (-1)
72 retval <- case res of
73 Nothing -> error "could not read from stack"
74 Just y -> return y
75 Lua.close lua
76 return retval