diff options
| author | Albert Krewinkel <[email protected]> | 2024-04-18 14:32:15 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2024-05-10 19:03:29 -0700 |
| commit | e5c135e68ecde81296ca045c9c362461b83ef4a4 (patch) | |
| tree | 7ea19c3b41552291d1a65f172da83fcde72968cf /pandoc-lua-engine/test | |
| parent | 673edabac0f01124cb412fecdc6f440aa77db6b4 (diff) | |
Lua: add a `pandoc.log` module.
Diffstat (limited to 'pandoc-lua-engine/test')
| -rw-r--r-- | pandoc-lua-engine/test/Tests/Lua.hs | 7 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/Tests/Lua/Module.hs | 2 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/lua/module/pandoc-log.lua | 75 |
3 files changed, 83 insertions, 1 deletions
diff --git a/pandoc-lua-engine/test/Tests/Lua.hs b/pandoc-lua-engine/test/Tests/Lua.hs index 644f18204..76f3f467d 100644 --- a/pandoc-lua-engine/test/Tests/Lua.hs +++ b/pandoc-lua-engine/test/Tests/Lua.hs @@ -24,10 +24,12 @@ import Text.Pandoc.Builder (bulletList, definitionList, displayMath, divWith, linebreak, math, orderedList, para, plain, rawBlock, singleQuoted, space, str, strong, HasMeta (setMeta)) -import Text.Pandoc.Class (runIOorExplode, setUserDataDir) +import Text.Pandoc.Class ( CommonState (stVerbosity) + , modifyCommonState, runIOorExplode, setUserDataDir) import Text.Pandoc.Definition (Attr, Block (BlockQuote, Div, Para), Pandoc, Inline (Emph, Str), pandocTypesVersion) import Text.Pandoc.Error (PandocError (PandocLuaError)) +import Text.Pandoc.Logging (Verbosity (ERROR)) import Text.Pandoc.Lua (Global (..), applyFilter, runLua, setGlobals) import Text.Pandoc.Options (def) import Text.Pandoc.Version (pandocVersionText) @@ -238,6 +240,9 @@ assertFilterConversion msg filterPath docIn expectedDoc = do runLuaTest :: HasCallStack => Lua.LuaE PandocError a -> IO a runLuaTest op = runIOorExplode $ do + -- Disable printing of warnings on stderr: some tests will generate + -- warnings, we don't want to see those messages. + modifyCommonState $ \st -> st { stVerbosity = ERROR } res <- runLua $ do setGlobals [ PANDOC_WRITER_OPTIONS def ] op diff --git a/pandoc-lua-engine/test/Tests/Lua/Module.hs b/pandoc-lua-engine/test/Tests/Lua/Module.hs index 47100be78..a5a9570bd 100644 --- a/pandoc-lua-engine/test/Tests/Lua/Module.hs +++ b/pandoc-lua-engine/test/Tests/Lua/Module.hs @@ -29,6 +29,8 @@ tests = ("lua" </> "module" </> "pandoc-image.lua") , testPandocLua "pandoc.json" ("lua" </> "module" </> "pandoc-json.lua") + , testPandocLua "pandoc.log" + ("lua" </> "module" </> "pandoc-log.lua") , testPandocLua "pandoc.mediabag" ("lua" </> "module" </> "pandoc-mediabag.lua") , testPandocLua "pandoc.path" diff --git a/pandoc-lua-engine/test/lua/module/pandoc-log.lua b/pandoc-lua-engine/test/lua/module/pandoc-log.lua new file mode 100644 index 000000000..923f03cd9 --- /dev/null +++ b/pandoc-lua-engine/test/lua/module/pandoc-log.lua @@ -0,0 +1,75 @@ +-- +-- Tests for the pandoc.log module +-- +-- ========================================= +-- PLEASE BE CAREFUL WHEN UPDATING THE TESTS +-- ========================================= +-- +-- Some tests here are very, very fragile, as their correctness depends on the +-- correct line number in this file. +local log = require 'pandoc.log' +local json = require 'pandoc.json' +local tasty = require 'tasty' + +local group = tasty.test_group +local test = tasty.test_case +local assert = tasty.assert + +return { + group 'info' { + test('is a function', function () + assert.are_equal(type(log.info), 'function') + end), + test('reports a warning', function () + log.info('info test') + local msg = json.decode(json.encode(PANDOC_STATE.log[1])) + assert.are_equal(msg.message, 'info test') + assert.are_equal(msg.type, 'ScriptingInfo') + end), + test('info includes the correct number', function () + log.info('line number test') + local msg = json.decode(json.encode(PANDOC_STATE.log[1])) + -- THIS NEEDS UPDATING if lines above are shifted. + assert.are_equal(msg.line, 30) + end), + }, + + group 'warn' { + test('is a function', function () + assert.are_equal(type(log.warn), 'function') + end), + test('reports a warning', function () + log.warn('testing') + local msg = json.decode(json.encode(PANDOC_STATE.log[1])) + assert.are_equal(msg.message, 'testing') + assert.are_equal(msg.type, 'ScriptingWarning') + end), + }, + + group 'silence' { + test('prevents info from being logged', function () + local current_messages = PANDOC_STATE.log + log.silence(log.info, 'Just so you know') + assert.are_same(#current_messages, #PANDOC_STATE.log) + for i = 1, #current_messages do + assert.are_equal( + json.encode(current_messages[i]), + json.encode(PANDOC_STATE.log[i]) + ) + end + end), + test('returns the messages raised by the called function', function () + local msgs = log.silence(log.info, 'Just so you know') + local msg = json.decode(json.encode(msgs[1])) + assert.are_equal(msg.message, 'Just so you know') + end), + test('returns function application results as additional return values', + function () + local l, x, y = log.silence(function (a, b) return b, a + b end, 5, 8) + assert.are_same(l, {}) + assert.are_equal(x, 8) + assert.are_equal(y, 13) + end + ) + } +} |
