aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/regxep.lua
blob: 8cc2db2bdcd523552cfd3efa5c86275fa08a2ba3 (plain)
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
context("Regexp unit tests", function()
  local re = require("rspamd_regexp")
  
  test("Regexp creation", function()
    assert_not_nil(re.create_cached('/test$/m'))
    assert_not_nil(re.create_cached('^test$', 'm'))
    assert_not_nil(re.create_cached('m,test,m'))
    assert_not_nil(re.create_cached('m|test|m'))
  end)
  test("Regexp match", function()
    local cases = {
      {'/test$/m', '123test', true},
      {'/^test$/m', '123test', false},
      {'m,test,', 'test', true},
      {'m,test,', 'test123', false},
      {'m{https?://[^/?\\s]+?:\\d+(?<!:80)(?<!:443)(?<!:8080)(?:/|\\s|$)}', '', false},
      {'/test/i', 'TeSt123', true},
      {'/ТесТ/iu', 'тест', true},
      -- Raw regexp
      {'/\\S<[-\\w\\.]+\\@[-\\w\\.]+>/r', 'some<example@example.com>', true},
      -- Cyrillic utf8 letter
      {'/\\S<[-\\w\\.]+\\@[-\\w\\.]+>/r', 'some<example@exаmple.com>', false},
    }
    
    for _,c in ipairs(cases) do
      local r = re.create_cached(c[1])
      assert_not_nil(r, "cannot parse " .. c[1])
      local res = r:match(c[2])
      
      assert_equal(res, c[3], string.format("'%s' doesn't match with '%s'",
        c[2], c[1]))
    end
  end)
  
  test("Regexp split", function()
    local cases = {
      {'\\s', 'one two', {'one', 'two'}}, -- trivial
      {'\\s', 'one   two', {'one', 'two'}}, -- multiple delimiters
      {'\\s', '  one   two  ', {'one', 'two'}}, -- multiple delimiters
      {'\\s', '  one   ', {'one', 'two'}}, -- multiple delimiters
      {'[:,]', ',,,:::one,two,,', {'one', 'two'}}, -- multiple delimiters
    }
  
    for _,c in ipairs(cases) do
      local r = re.create_cached(c[1])
      assert_not_nil(r, "cannot parse " .. c[1])
      
      local res = r:split(c[2])
      assert_not_nil(res, "cannot split " .. c[2])
      
      for i,r in ipairs(res) do
        assert_equal(r, c[3][i])
      end
    end
  end)
  
  end
)