File: lua_util.misc.lua

package info (click to toggle)
rspamd 3.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,056 kB
  • sloc: ansic: 243,746; cpp: 105,657; javascript: 29,539; asm: 2,512; perl: 2,440; pascal: 1,625; python: 1,274; sql: 313; sh: 281; makefile: 140; xml: 74
file content (61 lines) | stat: -rw-r--r-- 1,528 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)

context("Lua util - str_endswith", function()
  local ending = {
    {'a', 'a'},
    {'ab', 'b'},
    {'ab', 'ab'},
    {'abc', 'bc'},
    {'any', ''},
  }
  local not_ending = {
    {'a', 'b'},
    {'', 'a'},
    {'ab', 'a'},
    {'ab', 'ba'},
    {'ab', 'lab'},
    {'abc', 'ab'},
    {'abcd', 'bc'},
    {'a', 'A'},
    {'aB', 'b'},
  }
  for _, c in ipairs(ending) do
    test(string.format('True case: str_endswith("%s", "%s")', c[1], c[2]), function()
      assert_true(util.str_endswith(c[1], c[2]))
    end)
  end
  for _, c in ipairs(not_ending) do
    test(string.format('False case: str_endswith("%s", "%s")', c[1], c[2]), function()
      assert_false(util.str_endswith(c[1], c[2]))
    end)
  end
end)