git.iko.soy/typeable/pandoc
on bc5624d help sign in
102 lines 4.0 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 Foreign.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")
68 , testCase "Count inlines via metatable catch-all" $
69 assertFilterConversion "filtering with metatable catch-all failed"
70 "metatable-catch-all.lua"
71 (doc . para $ "four words, three spaces")
72 (doc . para $ str "7")
74 , testCase "Count blocks via Block-specific catch-all" $
75 assertFilterConversion "filtering with Block catch-all failed"
76 "block-count.lua"
77 (doc $ para "one" <> para "two")
78 (doc $ para "2")
79 ]
81assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
82assertFilterConversion msg filterPath docIn docExpected = do
83 docRes <- runLuaFilter (Just "../data") ("lua" </> filterPath) [] docIn
84 assertEqual msg docExpected docRes
86roundtripEqual :: (Eq a, FromLuaStack a, ToLuaStack a) => a -> IO Bool
87roundtripEqual x = (x ==) <$> roundtripped
88 where
89 roundtripped :: (FromLuaStack a, ToLuaStack a) => IO a
90 roundtripped = runLua $ do
91 openlibs
92 pushPandocModule (Just "../data")
93 setglobal "pandoc"
94 oldSize <- gettop
95 push x
96 size <- gettop
97 when ((size - oldSize) /= 1) $
98 error ("not exactly one additional element on the stack: " ++ show size)
99 res <- peekEither (-1)
100 case res of
101 Left _ -> error "could not read from stack"
102 Right y -> return y