git.iko.soy/typeable/pandoc
on master help sign in
Ensure correctness of StackValue instances
commitd412c38c714b810040d583bbe40af4937f8ef138 authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 6 Apr 2017, 21:00 (10 years ago) parent9278a63
2 files changed+29
pandoc.cabal+1 test/Tests/Lua.hs+28
pandoc.cabal+1
@@ -531,6 +531,7 @@ Test-Suite test-pandoc
531531 text >= 0.11 && < 1.3,
532532 directory >= 1 && < 1.4,
533533 filepath >= 1.1 && < 1.5,
534+ hslua >= 0.4 && < 0.6,
534535 process >= 1.2.3 && < 1.5,
535536 skylighting >= 0.3.1 && < 0.4,
536537 temporary >= 1.1 && < 1.3,
test/Tests/Lua.hs+28
@@ -1,12 +1,17 @@
11{-# Language OverloadedStrings #-}
22module Tests.Lua ( tests ) where
33
4+import Control.Monad (when)
45import System.FilePath ((</>))
56import Test.Tasty (TestTree)
67import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
8+import Test.Tasty.QuickCheck (ioProperty, testProperty)
9+import Text.Pandoc.Arbitrary ()
710import Text.Pandoc.Builder
811import Text.Pandoc.Lua
912
13+import qualified Scripting.Lua as Lua
14+
1015tests :: [TestTree]
1116tests =
1217 [ testCase "macro expansion via filter" $
@@ -32,9 +37,32 @@ tests =
3237 "markdown-reader.lua"
3338 (doc $ rawBlock "markdown" "*charly* **delta**")
3439 (doc . para $ emph "charly" <> space <> strong "delta")
40+
41+ , testProperty "inline elements can be round-tripped through the lua stack" $
42+ \x -> ioProperty (roundtripEqual (x::Inline))
43+
44+ , testProperty "block elements can be round-tripped through the lua stack" $
45+ \x -> ioProperty (roundtripEqual (x::Block))
3546 ]
3647
3748assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
3849assertFilterConversion msg filterPath docIn docExpected = do
3950 docRes <- runLuaFilter ("lua" </> filterPath) [] docIn
4051 assertEqual msg docExpected docRes
52+
53+roundtripEqual :: (Eq a, Lua.StackValue a) => a -> IO Bool
54+roundtripEqual x = (x ==) <$> roundtripped
55+ where
56+ roundtripped :: (Lua.StackValue a) => IO a
57+ roundtripped = do
58+ lua <- Lua.newstate
59+ Lua.push lua x
60+ size <- Lua.gettop lua
61+ when (size /= 1) $
62+ error ("not exactly one element on the stack: " ++ show size)
63+ res <- Lua.peek lua (-1)
64+ retval <- case res of
65+ Nothing -> error "could not read from stack"
66+ Just y -> return y
67+ Lua.close lua
68+ return retval