From fb54f3d6792d2f8e7b05e458b59142f8ae6bb3e2 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Wed, 25 Mar 2020 22:16:27 +0100 Subject: API change: use PandocError for exceptions in Lua subsystem The PandocError type is used throughout the Lua subsystem, all Lua functions throw an exception of this type if an error occurs. The `LuaException` type is removed and no longer exported from `Text.Pandoc.Lua`. In its place, a new constructor `PandocLuaError` is added to PandocError. --- test/Tests/Lua.hs | 15 ++++++++------- test/lua/module/pandoc-types.lua | 7 +++---- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/Tests/Lua.hs b/test/Tests/Lua.hs index 0943b17aa..14800f7bb 100644 --- a/test/Tests/Lua.hs +++ b/test/Tests/Lua.hs @@ -1,5 +1,6 @@ {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} {- | Module : Tests.Lua Copyright : © 2017-2020 Albert Krewinkel @@ -28,11 +29,13 @@ import Text.Pandoc.Builder (bulletList, definitionList, displayMath, divWith, import Text.Pandoc.Class (runIOorExplode, setUserDataDir) import Text.Pandoc.Definition (Block (BlockQuote, Div, Para), Inline (Emph, Str), Attr, Meta, Pandoc, pandocTypesVersion) +import Text.Pandoc.Error (PandocError (PandocLuaError)) import Text.Pandoc.Filter (Filter (LuaFilter), applyFilters) import Text.Pandoc.Lua (runLua) import Text.Pandoc.Options (def) import Text.Pandoc.Shared (pandocVersion) +import qualified Control.Monad.Catch as Catch import qualified Foreign.Lua as Lua import qualified Data.Text as T import qualified Data.Text.Encoding as TE @@ -197,12 +200,13 @@ tests = map (localOption (QuickCheckTests 20)) , testCase "informative error messages" . runLuaTest $ do Lua.pushboolean True - err <- Lua.peekEither Lua.stackTop - case (err :: Either String Pandoc) of - Left msg -> do + eitherPandoc <- Catch.try (Lua.peek Lua.stackTop :: Lua.Lua Pandoc) + case eitherPandoc of + Left (PandocLuaError msg) -> do let expectedMsg = "Could not get Pandoc value: " <> "table expected, got boolean" Lua.liftIO $ assertEqual "unexpected error message" expectedMsg msg + Left e -> error ("Expected a Lua error, but got " <> show e) Right _ -> error "Getting a Pandoc element from a bool should fail." ] @@ -223,10 +227,7 @@ roundtripEqual x = (x ==) <$> roundtripped size <- Lua.gettop when (size - oldSize /= 1) $ error ("not exactly one additional element on the stack: " ++ show size) - res <- Lua.peekEither (-1) - case res of - Left e -> error (show e) - Right y -> return y + Lua.peek (-1) runLuaTest :: Lua.Lua a -> IO a runLuaTest op = runIOorExplode $ do diff --git a/test/lua/module/pandoc-types.lua b/test/lua/module/pandoc-types.lua index 880dd567e..d4e063a5c 100644 --- a/test/lua/module/pandoc-types.lua +++ b/test/lua/module/pandoc-types.lua @@ -26,10 +26,9 @@ return { ) end), test('non-version string is rejected', function () - assert.error_matches( - function () Version '11friends' end, - '11friends' - ) + local success, msg = pcall(function () Version '11friends' end) + assert.is_falsy(success) + assert.is_truthy(tostring(msg):match('11friends')) end) }, -- cgit v1.2.3