aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/test/lua/module
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2024-06-07 07:53:57 +0200
committerAlbert Krewinkel <[email protected]>2024-06-07 08:36:24 +0200
commit430d525378bc7b318823fb1d5cba09da13ba9453 (patch)
treec0ba774232132d3be2f575dd24e1ee8abd2cf728 /pandoc-lua-engine/test/lua/module
parent3d6a4d870ad5def20eefd66c1bf273a70b5cc952 (diff)
Lua: allow passing an environment to `run_lua_filter`.
The default is now to use a *copy* of the global environment when running a filter; this ensures better separation when `run_lua_filter` is used multiple times. A custom environment can be specified via the optional third parameter.
Diffstat (limited to 'pandoc-lua-engine/test/lua/module')
-rw-r--r--pandoc-lua-engine/test/lua/module/pandoc-utils.lua44
1 files changed, 44 insertions, 0 deletions
diff --git a/pandoc-lua-engine/test/lua/module/pandoc-utils.lua b/pandoc-lua-engine/test/lua/module/pandoc-utils.lua
index a31250bfe..573a5987a 100644
--- a/pandoc-lua-engine/test/lua/module/pandoc-utils.lua
+++ b/pandoc-lua-engine/test/lua/module/pandoc-utils.lua
@@ -1,5 +1,7 @@
local tasty = require 'tasty'
+local pandoc = require 'pandoc'
local utils = require 'pandoc.utils'
+local io = require 'io'
local assert = tasty.assert
local test = tasty.test_case
@@ -127,6 +129,48 @@ return {
end)
},
+ group 'run_lua_filter' {
+ test('runs a filter', function ()
+ local doc = pandoc.Pandoc("indivisible words")
+ pandoc.system.with_temporary_directory('lua-filter', function (dir)
+ local filter_path = pandoc.path.join{dir, 'test.lua'}
+ local filter = 'function Space() return " " end'
+ local fh = io.open(filter_path, 'wb')
+ fh:write(filter)
+ fh:close()
+ assert.are_equal(
+ utils.run_lua_filter(doc, filter_path),
+ pandoc.Pandoc(
+ pandoc.Plain(pandoc.Inlines{"indivisible", " ", "words"})
+ )
+ )
+ end)
+ end),
+ test("doesn't change the local environment by default", function ()
+ pandoc.system.with_temporary_directory('lua-filter', function (dir)
+ local filter_path = pandoc.path.join{dir, 'test.lua'}
+ local foo
+ local filter = 'foo = 42'
+ local fh = io.open(filter_path, 'wb')
+ fh:write(filter)
+ fh:close()
+ utils.run_lua_filter(pandoc.Pandoc{}, filter_path)
+ assert.is_nil(foo)
+ end)
+ end),
+ test("accepts an environment in which the filter is executed", function ()
+ pandoc.system.with_temporary_directory('lua-filter', function (dir)
+ local filter_path = pandoc.path.join{dir, 'test.lua'}
+ local filter = 'foo = 42'
+ local fh = io.open(filter_path, 'wb')
+ fh:write(filter)
+ fh:close()
+ utils.run_lua_filter(pandoc.Pandoc{}, filter_path, _ENV)
+ assert.are_equal(_ENV.foo, 42)
+ end)
+ end)
+ },
+
group 'sha1' {
test('hashing', function ()
local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'