commit 9a0a491: [Test] Rework expressions tests

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Jun 17 13:35:08 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-06-16 15:44:23 +0100
URL: https://github.com/rspamd/rspamd/commit/9a0a491315d573fe19eae5187ab7463ac996aa03

[Test] Rework expressions tests

---
 test/lua/unit/expressions.lua | 147 ++++++++++++++++++++----------------------
 1 file changed, 71 insertions(+), 76 deletions(-)

diff --git a/test/lua/unit/expressions.lua b/test/lua/unit/expressions.lua
index 6072c6d02..34dc8e13a 100644
--- a/test/lua/unit/expressions.lua
+++ b/test/lua/unit/expressions.lua
@@ -17,36 +17,53 @@ context("Rspamd expressions", function()
     return token
   end
 
-  test("Expression creation function", function()
-    local function process_func(token, task)
-      -- Do something using token and task
-    end
+  local atoms = {
+    A = 1.0,
+    B = 0,
+    C = 1,
+    D = 0,
+    E = 1,
+    F = 0,
+    G = 0,
+    H = 0,
+    I = 0,
+    J = 0,
+    K = 0,
+  }
+  local function process_func(token, input)
+
+    --print(token)
+    local t = input[token]
+
+    return t
+  end
 
-    local pool = rspamd_mempool.create()
+  local pool = rspamd_mempool.create()
 
-    local cases = {
-       {'A & B | !C', '(C) ! (A) (B) & |'},
-       {'A & (B | !C)', '(A) (B) (C) ! | &'},
-       {'A & B &', nil},
-       -- Unbalanced braces
-       {'(((A))', nil},
-       -- Balanced braces
-       {'(((A)))', '(A)'},
-       -- Plus and comparison operators
-       {'A + B + C + D > 2', '2 (A) (B) (C) (D) +(4) >'},
-       -- Plus and logic operators
-       {'((A + B + C + D) > 2) & D', '(D) 2 (A) (B) (C) (D) +(4) > &'},
-       -- Associativity
-       {'A | B | C & D & E', '(A) (B) (C) (D) (E) &(3) |(3)'},
-       -- More associativity
-       {'1 | 0 & 0 | 0', '(1) (0) (0) (0) & |(3)'},
-       {'(A) & (B) & ((C) | (D) | (E) | (F))', '(A) (B) (C) (D) (E) (F) |(4) &(3)' },
-       -- Extra space
-       {'A & B | ! C', '(C) ! (A) (B) & |'},
-    }
-    for _,c in ipairs(cases) do
+  local cases = {
+    {'A & B | !C', '(C) ! (A) (B) & |'},
+    {'A & (B | !C)', '(A) (B) (C) ! | &'},
+    {'A & B &', nil},
+    -- Unbalanced braces
+    {'(((A))', nil},
+    -- Balanced braces
+    {'(((A)))', '(A)'},
+    -- Plus and comparison operators
+    {'A + B + C + D > 2', '2 (A) (B) (C) (D) +(4) >'},
+    -- Plus and logic operators
+    {'((A + B + C + D) > 2) & D', '(D) 2 (A) (B) (C) (D) +(4) > &'},
+    -- Associativity
+    {'A | B | C & D & E', '(A) (B) (C) (D) (E) &(3) |(3)'},
+    -- More associativity
+    {'1 | 0 & 0 | 0', '(1) (0) (0) (0) & |(3)'},
+    {'(A) & (B) & ((C) | (D) | (E) | (F))', '(A) (B) (C) (D) (E) (F) |(4) &(3)' },
+    -- Extra space
+    {'A & B | ! C', '(C) ! (A) (B) & |'},
+  }
+  for _,c in ipairs(cases) do
+    test("Expression creation function: " .. c[1], function()
       local expr,err = rspamd_expression.create(c[1],
-        {parse_func, process_func}, pool)
+          {parse_func, process_func}, pool)
 
       if not c[2] then
         assert_nil(expr, "Should not be able to parse " .. c[1])
@@ -55,59 +72,37 @@ context("Rspamd expressions", function()
         assert_equal(expr:to_string(), c[2], string.format("Evaluated expr to '%s', expected: '%s'",
             expr:to_string(), c[2]))
       end
-    end
-    -- Expression is destroyed when the corresponding pool is destroyed
-    pool:destroy()
-  end)
-  test("Expression process function", function()
-    local function process_func(token, input)
-
-      --print(token)
-      local t = input[token]
-
-      if t then return 1 end
-      return 0
-    end
-
-    local pool = rspamd_mempool.create()
-    local atoms = {
-      A = true,
-      B = false,
-      C = true,
-      D = false,
-      E = true,
-      F = false,
-      G = false,
-      H = false,
-      I = false,
-      J = false,
-      K = false,
-    }
-    local cases = {
-       {'A & B | !C', 0},
-       {'A & (!B | C)', 1},
-       {'A + B + C + D + E + F >= 2', 1},
-       {'((A + B + C + D) > 1) & F', 0},
-       {'(A + B + C + D) > 1 && F || E', 1},
-       {'(A + B + C + D) > 100 && F || !E', 0},
-       {'F && ((A + B + C + D) > 1)', 0},
-       {'(E) && ((B + B + B + B) >= 1)', 0},
-       {'!!C', 1},
-       {'(B) & (D) & ((G) | (H) | (I) | (A))', 0},
-       {'A & C & (!D || !C || !E)', 1},
-       {'A & C & !(D || C || E)', 0},
-    }
-    for _,c in ipairs(cases) do
+    end)
+  end
+  -- Expression is destroyed when the corresponding pool is destroyed
+  cases = {
+    {'A & B | !C', 0},
+    {'A & (!B | C)', 1},
+    {'A + B + C + D + E + F >= 2', 1},
+    {'((A + B + C + D) > 1) & F', 0},
+    {'(A + B + C + D) > 1 && F || E', 1},
+    {'(A + B + C + D) > 100 && F || !E', 0},
+    {'F && ((A + B + C + D) > 1)', 0},
+    {'(E) && ((B + B + B + B) >= 1)', 0},
+    {'!!C', 1},
+    {'(B) & (D) & ((G) | (H) | (I) | (A))', 0},
+    {'A & C & (!D || !C || !E)', 1},
+    {'A & C & !(D || C || E)', 0},
+    {'A + B + C', 2},
+    {'A * 2.0 + B + C', 3},
+    {'A * 2.0 + B - C', 0},
+    {'A / 2.0 + B - C', -0.5},
+  }
+  for _,c in ipairs(cases) do
+    test("Expression process function: " .. c[1], function()
       local expr,err = rspamd_expression.create(c[1],
-        {parse_func, process_func}, pool)
+          {parse_func, process_func}, pool)
 
       assert_not_nil(expr, "Cannot parse " .. c[1])
       --print(expr)
       res = expr:process(atoms)
       assert_equal(res, c[2], string.format("Processed expr '%s'{%s} returned '%d', expected: '%d'",
-        expr:to_string(), c[1], res, c[2]))
-    end
-
-    pool:destroy()
-  end)
+          expr:to_string(), c[1], res, c[2]))
+    end)
+  end
 end)


More information about the Commits mailing list