aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-08-23 14:03:44 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-08-23 14:04:41 +0100
commit6e8df5589dbd3246ae5c6c50a82a78ffb7ab3a75 (patch)
tree134a79f86b4f65677f5c280638246eeca55cbd6c /test/lua
parent93d4fba9e2889fa05e1b526173fa2028e0dff571 (diff)
downloadrspamd-6e8df5589dbd3246ae5c6c50a82a78ffb7ab3a75.tar.gz
rspamd-6e8df5589dbd3246ae5c6c50a82a78ffb7ab3a75.zip
[Minor] Lua_util: Rework and add tests for callback_from_string
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/unit/lua_util.misc.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/lua/unit/lua_util.misc.lua b/test/lua/unit/lua_util.misc.lua
new file mode 100644
index 000000000..b19b4d6f1
--- /dev/null
+++ b/test/lua/unit/lua_util.misc.lua
@@ -0,0 +1,30 @@
+local util = require 'lua_util'
+
+context("Lua util - callback_from_string", function()
+ local cases = {
+ {'return function', 'return function(a, b) return a + b end'},
+ {'function', 'function(a, b) return a + b end'},
+ {'plain ops', 'local c = select(1, ...)\nreturn c + select(2, ...)'},
+ }
+ local fail_cases = {
+ nil,
+ '',
+ 'return function(a, b) ( end',
+ 'function(a, b) ( end',
+ 'return a + b'
+ }
+
+ for _,c in ipairs(cases) do
+ test('Success case: ' .. c[1], function()
+ local ret,f = util.callback_from_string(c[2])
+ assert_true(ret, f)
+ assert_equal(f(2, 2), 4)
+ end)
+ end
+ for i,c in ipairs(fail_cases) do
+ test('Failure case: ' .. tostring(i), function()
+ local ret,f = util.callback_from_string(c)
+ assert_false(ret)
+ end)
+ end
+end) \ No newline at end of file