]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add tests and fix some minor issues
authorVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 22 Nov 2022 23:50:55 +0000 (23:50 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 22 Nov 2022 23:50:55 +0000 (23:50 +0000)
lualib/lua_selectors/init.lua
test/lua/unit/selectors.lua

index 01b43756ad9d636d67628052f49f3f89ff2e7856..12bc7f2b9f7d1c2623e3f07c5f70ed3a0b29d719 100644 (file)
@@ -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
index c2fe4d7bdc7504d11cad5e54287bc71c5cf24901..6362e5cdea5ce3e0aadb6bc6209742c272ebe3ab 100644 (file)
@@ -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)