git.iko.soy/typeable/pandoc
on 91c989d help sign in
93 lines 3.6 kB history raw
1{-# Language OverloadedStrings #-}
2module Tests.Lua ( tests ) where
4import Control.Monad (when)
5import System.FilePath ((</>))
6import Test.Tasty (TestTree, localOption)
7import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
8import Test.Tasty.QuickCheck (ioProperty, testProperty, QuickCheckTests(..))
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 = map (localOption (QuickCheckTests 20))
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 "implicit doc filter" $
51 assertFilterConversion "Document contains 'Hello, World!'"
52 "implicit-doc-filter.lua"
53 (doc . plain $ linebreak)
54 (doc . para $ str "Hello," <> space <> str "World!")
56 , testCase "parse raw markdown blocks" $
57 assertFilterConversion "raw markdown block is converted"
58 "markdown-reader.lua"
59 (doc $ rawBlock "markdown" "*charly* **delta**")
60 (doc . para $ emph "charly" <> space <> strong "delta")
62 , testCase "allow shorthand functions for quote types" $
63 assertFilterConversion "single quoted becomes double quoted string"
64 "single-to-double-quoted.lua"
65 (doc . para . singleQuoted $ str "simple")
66 (doc . para . doubleQuoted $ str "simple")
67 ]
69assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
70assertFilterConversion msg filterPath docIn docExpected = do
71 docRes <- runLuaFilter Nothing ("lua" </> filterPath) [] docIn
72 assertEqual msg docExpected docRes
74roundtripEqual :: (Eq a, Lua.StackValue a) => a -> IO Bool
75roundtripEqual x = (x ==) <$> roundtripped
76 where
77 roundtripped :: (Lua.StackValue a) => IO a
78 roundtripped = do
79 lua <- Lua.newstate
80 Lua.openlibs lua
81 pushPandocModule Nothing lua
82 Lua.setglobal lua "pandoc"
83 oldSize <- Lua.gettop lua
84 Lua.push lua x
85 size <- Lua.gettop lua
86 when ((size - oldSize) /= 1) $
87 error ("not exactly one additional element on the stack: " ++ show size)
88 res <- Lua.peek lua (-1)
89 retval <- case res of
90 Nothing -> error "could not read from stack"
91 Just y -> return y
92 Lua.close lua
93 return retval