summaryrefslogtreecommitdiff
path: root/test/gnu_sets.lua
blob: 519705aa3d1509e6c08503164bef7ca7d3c44d7a (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
-- See Copyright Notice in the file LICENSE

-- This file should contain only test sets that behave identically
-- when being run with pcre or posix regex libraries.

local luatest = require "luatest"
local N = luatest.NT

local function norm(a) return a==nil and N or a end

local function set_f_gmatch (lib, flg)
  local downcase = {}
  for i = 0, 255 do -- 255 == UCHAR_MAX
    downcase[i] = string.gsub(string.char (i), ".", function (s) return string.lower(s) end)
  end
  -- gmatch (s, p, [cf], [ef], [tr])
  local function test_gmatch (subj, patt)
    local out, guard = {}, 10
    for a, b in lib.gmatch (subj, patt, nil, nil, downcase) do
      table.insert (out, { norm(a), norm(b) })
      guard = guard - 1
      if guard == 0 then break end
    end
    return unpack (out)
  end
  return {
    Name = "Function gmatch",
    Func = test_gmatch,
  --{  subj             patt         results }
    { {"abA",           "a"},        {{"a",N}, {"A",N} } },
  }
end

local function set_f_match (lib, flg)
return {
  Name = "Function match",
  Func = lib.match,
  --{subj,   patt,      st,cf,ef},           { results }
  { {"abcd", ".+",      5},                  { N }    }, -- failing st
  { {"abc",  "^abc"},                        {"abc" } }, -- anchor
  { {"abc",  "^abc",    N,N,flg.not_bol},    { N }    }, -- anchor + ef
  { {"cabcaab", "ca+b", N,N,flg.reverse},    {"caab" }  }, -- reverse search
}
end

return function (libname)
  local lib = require (libname)
  local flags = lib.flags ()
  return {
    set_f_match     (lib, flags),
    set_f_gmatch    (lib),
  }
end