git.iko.soy/typeable/pandoc
on ca71298 help sign in
138 lines 5.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 (QuickCheckTests (..), ioProperty, testProperty)
9import Text.Pandoc.Arbitrary ()
10import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
11 header, linebreak, para, plain, rawBlock,
12 singleQuoted, space, str, strong, (<>))
13import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
14import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
15import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
17import qualified Foreign.Lua as Lua
19tests :: [TestTree]
20tests = map (localOption (QuickCheckTests 20))
21 [ testProperty "inline elements can be round-tripped through the lua stack" $
22 \x -> ioProperty (roundtripEqual (x::Inline))
24 , testProperty "block elements can be round-tripped through the lua stack" $
25 \x -> ioProperty (roundtripEqual (x::Block))
27 , testProperty "meta blocks can be round-tripped through the lua stack" $
28 \x -> ioProperty (roundtripEqual (x::Meta))
30 , testProperty "documents can be round-tripped through the lua stack" $
31 \x -> ioProperty (roundtripEqual (x::Pandoc))
33 , testCase "macro expansion via filter" $
34 assertFilterConversion "a '{{helloworld}}' string is expanded"
35 "strmacro.lua"
36 (doc . para $ str "{{helloworld}}")
37 (doc . para . emph $ str "Hello, World")
39 , testCase "convert all plains to paras" $
40 assertFilterConversion "plains become para"
41 "plain-to-para.lua"
42 (doc $ bulletList [plain (str "alfa"), plain (str "bravo")])
43 (doc $ bulletList [para (str "alfa"), para (str "bravo")])
45 , testCase "make hello world document" $
46 assertFilterConversion "Document contains 'Hello, World!'"
47 "hello-world-doc.lua"
48 (doc . para $ str "Hey!" <> linebreak <> str "What's up?")
49 (doc . para $ str "Hello," <> space <> str "World!")
51 , testCase "implicit doc filter" $
52 assertFilterConversion "Document contains 'Hello, World!'"
53 "implicit-doc-filter.lua"
54 (doc . plain $ linebreak)
55 (doc . para $ str "Hello," <> space <> str "World!")
57 , testCase "parse raw markdown blocks" $
58 assertFilterConversion "raw markdown block is converted"
59 "markdown-reader.lua"
60 (doc $ rawBlock "markdown" "*charly* **delta**")
61 (doc . para $ emph "charly" <> space <> strong "delta")
63 , testCase "allow shorthand functions for quote types" $
64 assertFilterConversion "single quoted becomes double quoted string"
65 "single-to-double-quoted.lua"
66 (doc . para . singleQuoted $ str "simple")
67 (doc . para . doubleQuoted $ str "simple")
69 , testCase "Count inlines via metatable catch-all" $
70 assertFilterConversion "filtering with metatable catch-all failed"
71 "metatable-catch-all.lua"
72 (doc . para $ "four words, three spaces")
73 (doc . para $ str "7")
75 , testCase "Count blocks via Block-specific catch-all" $
76 assertFilterConversion "filtering with Block catch-all failed"
77 "block-count.lua"
78 (doc $ para "one" <> para "two")
79 (doc $ para "2")
81 , testCase "Convert header upper case" $
82 assertFilterConversion "converting header to upper case failed"
83 "uppercase-header.lua"
84 (doc $ header 1 "les états-unis" <> para "text")
85 (doc $ header 1 "LES ÉTATS-UNIS" <> para "text")
87 , testCase "Attribute lists are convenient to use" $
88 let kv_before = [("one", "1"), ("two", "2"), ("three", "3")]
89 kv_after = [("one", "eins"), ("three", "3"), ("five", "5")]
90 in assertFilterConversion "Attr doesn't behave as expected"
91 "attr-test.lua"
92 (doc $ divWith ("", [], kv_before) (para "nil"))
93 (doc $ divWith ("", [], kv_after) (para "nil"))
95 , testCase "Test module pandoc.utils" $
96 assertFilterConversion "pandoc.utils doesn't work as expected."
97 "test-pandoc-utils.lua"
98 (doc $ para "doesn't matter")
99 (doc $ mconcat [ plain (str "hierarchicalize: OK")
100 , plain (str "normalize_date: OK")
101 , plain (str "pipe: OK")
102 , plain (str "failing pipe: OK")
103 , plain (str "read: OK")
104 , plain (str "failing read: OK")
105 , plain (str "sha1: OK")
106 , plain (str "stringify: OK")
107 , plain (str "to_roman_numeral: OK")
108 ])
109 ]
111assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
112assertFilterConversion msg filterPath docIn docExpected = do
113 docEither <- runIOorExplode $ do
114 setUserDataDir (Just "../data")
115 runLuaFilter ("lua" </> filterPath) [] docIn
116 case docEither of
117 Left _ -> fail "lua filter failed"
118 Right docRes -> assertEqual msg docExpected docRes
120roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
121roundtripEqual x = (x ==) <$> roundtripped
122 where
123 roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
124 roundtripped = runIOorExplode $ do
125 setUserDataDir (Just "../data")
126 res <- runPandocLua $ do
127 oldSize <- Lua.gettop
128 Lua.push x
129 size <- Lua.gettop
130 when (size - oldSize /= 1) $
131 error ("not exactly one additional element on the stack: " ++ show size)
132 res <- Lua.peekEither (-1)
133 case res of
134 Left _ -> error "could not read from stack"
135 Right y -> return y
136 case res of
137 Left e -> error (show e)
138 Right y -> return y