Files
neovim/test/unit/search_spec.lua
Justin M. Keyes 6d8c8b18d5 test(harness): migrate away from magic globals
Problem:
The magic globals `it`, `describe`, etc., are more trouble than they are
worth.

- Hooking into `after_each` requires `getfenv()` hacks.
- They confuse luals/emmylua, because the top-level `.luarc.json` isn't
  merged with `test/.luarc.json` (apparently a luals limitation?)
- They totally defeat discoverability because the user just has to
  "know" about the various magic symbols.

So they harm DX, which means they serve no purpose at all.

Solution:
- Expose the test API from `testutil`, so tests can call `t.it()`,
  `t.describe()`, etc., in the conventional way.
- Drop `getfenv()` hacks.
- Drop the `setfenv()` injection in `load_chunk`.
- Drop `test/_meta.lua`.
2026-07-21 13:22:39 +02:00

60 lines
1.9 KiB
Lua

local t = require('test.unit.testutil')
local describe = t.describe
local itp = t.gen_itp(t.it)
local to_cstr = t.to_cstr
local eq = t.eq
local search = t.cimport('./src/nvim/search.h')
local globals = t.cimport('./src/nvim/globals.h')
local ffi = t.ffi
itp('pat_has_uppercase', function()
-- works on empty string
eq(false, search.pat_has_uppercase(to_cstr('')))
-- works with utf uppercase
eq(false, search.pat_has_uppercase(to_cstr('ä')))
eq(true, search.pat_has_uppercase(to_cstr('Ä')))
eq(true, search.pat_has_uppercase(to_cstr('äaÅ')))
-- works when pat ends with backslash
eq(false, search.pat_has_uppercase(to_cstr('\\')))
eq(false, search.pat_has_uppercase(to_cstr('ab$\\')))
-- skips escaped characters
eq(false, search.pat_has_uppercase(to_cstr('\\Ab')))
eq(true, search.pat_has_uppercase(to_cstr('\\AU')))
-- skips _X escaped characters
eq(false, search.pat_has_uppercase(to_cstr('\\_Ab')))
eq(true, search.pat_has_uppercase(to_cstr('\\_AU')))
-- skips %X escaped characters
eq(false, search.pat_has_uppercase(to_cstr('aa\\%Ab')))
eq(true, search.pat_has_uppercase(to_cstr('aab\\%AU')))
end)
describe('search_regcomp', function()
local search_regcomp = function(pat, patlen, pat_save, pat_use, options)
local regmatch = ffi.new('regmmatch_T')
local fail =
search.search_regcomp(to_cstr(pat), patlen, nil, pat_save, pat_use, options, regmatch)
return fail, regmatch
end
local get_search_pat = function()
return t.internalize(search.get_search_pat())
end
itp('accepts regexp pattern with invalid utf', function()
--crafted to call reverse_text with invalid utf
globals.curwin.w_onebuf_opt.wo_rl = 1
globals.curwin.w_onebuf_opt.wo_rlc = to_cstr('s')
globals.cmdmod.cmod_flags = globals.CMOD_KEEPPATTERNS
local fail = search_regcomp('a\192', 2, 0, 0, 0)
eq(1, fail)
eq('\192a', get_search_pat())
end)
end)