Files
neovim/test/unit/message_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

61 lines
1.7 KiB
Lua

local t = require('test.unit.testutil')
local describe = t.describe
local itp = t.gen_itp(t.it)
local ffi = t.ffi
local eq = t.eq
local to_cstr = t.to_cstr
local cimp = t.cimport('./src/nvim/message.h', './src/nvim/memory.h', './src/nvim/strings.h')
describe('trunc_string', function()
local buflen = 40
local function test_inplace(s, expected, room)
room = room and room or 20
local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
ffi.C.strcpy(buf, s)
cimp.trunc_string(buf, buf, room, buflen)
eq(expected, ffi.string(buf))
cimp.xfree(buf)
end
local function test_copy(s, expected, room)
room = room and room or 20
local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
local str = cimp.xstrdup(to_cstr(s))
cimp.trunc_string(str, buf, room, buflen)
eq(expected, ffi.string(buf))
cimp.xfree(buf)
cimp.xfree(str)
end
local permutations = {
{ ['desc'] = 'in-place', ['func'] = test_inplace },
{ ['desc'] = 'by copy', ['func'] = test_copy },
}
for _, q in ipairs(permutations) do
describe('populates buf ' .. q.desc, function()
itp('with a small string', function()
q.func('text', 'text')
end)
itp('with a medium string', function()
q.func('a short text', 'a short text')
end)
itp('with a string of length == 1/2 room', function()
q.func('a text that fits', 'a text that fits', 34)
end)
itp('with a string exactly the truncate size', function()
q.func('a text tha just fits', 'a text tha just fits')
end)
itp('with a string that must be truncated', function()
q.func('a text that nott fits', 'a text t...nott fits')
end)
end)
end
end)