commit de2f7aa: [Minor] Add tests and fix some minor issues

Vsevolod Stakhov vsevolod at rspamd.com
Tue Nov 22 23:56:05 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-11-22 23:50:55 +0000
URL: https://github.com/rspamd/rspamd/commit/de2f7aa04e13e3691d91dfa4cf4d15fe744819ea (HEAD -> master)

[Minor] Add tests and fix some minor issues

---
 lualib/lua_selectors/init.lua |  2 +-
 test/lua/unit/selectors.lua   | 35 ++++++++++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/lualib/lua_selectors/init.lua b/lualib/lua_selectors/init.lua
index 01b43756a..12bc7f2b9 100644
--- a/lualib/lua_selectors/init.lua
+++ b/lualib/lua_selectors/init.lua
@@ -546,7 +546,7 @@ exports.kv_table_from_pairs = function(log_obj, selectors, _)
           tbl_len)
       return
     end
-    for i=1,#tbl_len,2 do
+    for i=1,tbl_len,2 do
       local k = tostring(tbl[i])
       local v = tbl[i + 1]
       if type(v) == 'string' then
diff --git a/test/lua/unit/selectors.lua b/test/lua/unit/selectors.lua
index c2fe4d7bd..6362e5cde 100644
--- a/test/lua/unit/selectors.lua
+++ b/test/lua/unit/selectors.lua
@@ -34,14 +34,21 @@ context("Selectors test", function()
     end
   end)
 
-  local function check_selector(selector_string)
+  local function check_selector_plain(selector_string)
     local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil,
         function(_, res, _) return res end)
     local elts = sels(task)
     return elts
   end
 
-  local cases = {
+  local function check_selector_kv(selector_string)
+    local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil,
+        lua_selectors.kv_table_from_pairs)
+    local elts = sels(task)
+    return elts
+  end
+
+  local cases_plain = {
     ["ip"] = {
                 selector = "ip",
                 expect = {"198.172.22.91"}
@@ -379,9 +386,27 @@ context("Selectors test", function()
     },
   }
 
-  for case_name, case in lua_util.spairs(cases) do
-    test("case " .. case_name, function()
-      local elts = check_selector(case.selector)
+  for case_name, case in lua_util.spairs(cases_plain) do
+    test("plain case " .. case_name, function()
+      local elts = check_selector_plain(case.selector)
+      assert_not_nil(elts)
+      assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect})
+    end)
+  end
+
+  local cases_kv = {
+    ["ip"] = {
+      selector = "id('ip');ip",
+      expect = { ip = "198.172.22.91" }
+    },
+    ["ip+words"] = {
+      selector = "id('ip');ip;id('words');words('full'):2",
+      expect = { ip = "198.172.22.91", words = {'hello', 'world', '', 'mail', 'me'} }
+    },
+  }
+  for case_name, case in lua_util.spairs(cases_kv) do
+    test("kv case " .. case_name, function()
+      local elts = check_selector_kv(case.selector)
       assert_not_nil(elts)
       assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect})
     end)


More information about the Commits mailing list