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/lua/module | |
| parent | 673edabac0f01124cb412fecdc6f440aa77db6b4 (diff) | |
Lua: add a `pandoc.log` module.
Diffstat (limited to 'pandoc-lua-engine/test/lua/module')
| -rw-r--r-- | pandoc-lua-engine/test/lua/module/pandoc-log.lua | 75 |
1 files changed, 75 insertions, 0 deletions
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 + ) + } +} |
