mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
Provide inbucket object in Lua (#351)
* fix delve fortify thingy * Expose inbucket.after.message_stored in lua * Expose inbucket.after.message_deleted in lua * Expose inbucket.before.mail_accepted in lua
This commit is contained in:
102
pkg/extension/luahost/bind_inbucket_test.go
Normal file
102
pkg/extension/luahost/bind_inbucket_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package luahost
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func TestInbucketAfterFuncs(t *testing.T) {
|
||||
// This Script registers each function and calls it. No effort is made to use the arguments
|
||||
// that Inbucket expects, this is only to validate the inbucket.after data structure getters
|
||||
// and setters.
|
||||
script := `
|
||||
assert(inbucket, "inbucket should not be nil")
|
||||
assert(inbucket.after, "inbucket.after should not be nil")
|
||||
|
||||
local fns = { "message_deleted", "message_stored" }
|
||||
|
||||
-- Verify functions start off nil.
|
||||
for i, name in ipairs(fns) do
|
||||
assert(inbucket.after[name] == nil, "after." .. name .. " should be nil")
|
||||
end
|
||||
|
||||
-- Test function to track func calls made, ensures no crossed wires.
|
||||
local calls = {}
|
||||
function makeTestFunc(create_name)
|
||||
return function(call_name)
|
||||
calls[create_name] = call_name
|
||||
end
|
||||
end
|
||||
|
||||
-- Set after functions, verify not nil, and call them.
|
||||
for i, name in ipairs(fns) do
|
||||
inbucket.after[name] = makeTestFunc(name)
|
||||
assert(inbucket.after[name], "after." .. name .. " should not be nil")
|
||||
end
|
||||
|
||||
-- Call each function. Separate loop to verify final state in 'calls'.
|
||||
for i, name in ipairs(fns) do
|
||||
inbucket.after[name](name)
|
||||
end
|
||||
|
||||
-- Verify functions were called.
|
||||
for i, name in ipairs(fns) do
|
||||
assert(calls[name], "after." .. name .. " should have been called")
|
||||
assert(calls[name] == name,
|
||||
string.format("after.%s was called with incorrect argument %s", name, calls[name]))
|
||||
end
|
||||
`
|
||||
|
||||
ls := lua.NewState()
|
||||
registerInbucketTypes(ls)
|
||||
require.NoError(t, ls.DoString(script))
|
||||
}
|
||||
|
||||
func TestInbucketBeforeFuncs(t *testing.T) {
|
||||
// This Script registers each function and calls it. No effort is made to use the arguments
|
||||
// that Inbucket expects, this is only to validate the inbucket.before data structure getters
|
||||
// and setters.
|
||||
script := `
|
||||
assert(inbucket, "inbucket should not be nil")
|
||||
assert(inbucket.before, "inbucket.before should not be nil")
|
||||
|
||||
local fns = { "mail_accepted" }
|
||||
|
||||
-- Verify functions start off nil.
|
||||
for i, name in ipairs(fns) do
|
||||
assert(inbucket.before[name] == nil, "before." .. name .. " should be nil")
|
||||
end
|
||||
|
||||
-- Test function to track func calls made, ensures no crossed wires.
|
||||
local calls = {}
|
||||
function makeTestFunc(create_name)
|
||||
return function(call_name)
|
||||
calls[create_name] = call_name
|
||||
end
|
||||
end
|
||||
|
||||
-- Set before functions, verify not nil, and call them.
|
||||
for i, name in ipairs(fns) do
|
||||
inbucket.before[name] = makeTestFunc(name)
|
||||
assert(inbucket.before[name], "before." .. name .. " should not be nil")
|
||||
end
|
||||
|
||||
-- Call each function. Separate loop to verify final state in 'calls'.
|
||||
for i, name in ipairs(fns) do
|
||||
inbucket.before[name](name)
|
||||
end
|
||||
|
||||
-- Verify functions were called.
|
||||
for i, name in ipairs(fns) do
|
||||
assert(calls[name], "before." .. name .. " should have been called")
|
||||
assert(calls[name] == name,
|
||||
string.format("before.%s was called with incorrect argument %s", name, calls[name]))
|
||||
end
|
||||
`
|
||||
|
||||
ls := lua.NewState()
|
||||
registerInbucketTypes(ls)
|
||||
require.NoError(t, ls.DoString(script))
|
||||
}
|
||||
Reference in New Issue
Block a user