Files
neovim/test/functional/lua/ffi_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

91 lines
2.0 KiB
Lua

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
local eq = t.eq
local exec_lua = n.exec_lua
local clear = n.clear
before_each(clear)
describe('ffi.cdef', function()
it('can use Neovim core functions', function()
if not exec_lua("return pcall(require, 'ffi')") then
pending('N/A: missing LuaJIT FFI')
end
eq(
12,
exec_lua(function()
local ffi = require('ffi')
ffi.cdef [[
typedef struct window_S win_T;
int win_col_off(win_T *wp);
extern win_T *curwin;
]]
vim.cmd('set number numberwidth=4 signcolumn=yes:4')
return ffi.C.win_col_off(ffi.C.curwin)
end)
)
eq(
20,
exec_lua(function()
local ffi = require('ffi')
ffi.cdef [[
typedef struct {} stl_hlrec_t;
typedef struct {} StlClickRecord;
typedef struct {} statuscol_T;
typedef struct {} Error;
win_T *find_window_by_handle(int Window, Error *err);
int build_stl_str_hl(
win_T *wp,
char *out,
size_t outlen,
char *fmt,
int opt_idx,
int opt_scope,
int fillchar,
int maxwidth,
stl_hlrec_t **hltab,
StlClickRecord **tabtab,
statuscol_T *scp
);
]]
return ffi.C.build_stl_str_hl(
ffi.C.find_window_by_handle(0, ffi.new('Error')),
ffi.new('char[1024]'),
1024,
ffi.cast('char*', 'StatusLineOfLength20'),
-1,
0,
0,
0,
nil,
nil,
nil
)
end)
)
-- Check that extern symbols are exported and accessible
eq(
true,
exec_lua(function()
local ffi = require('ffi')
ffi.cdef('uint64_t display_tick;')
return ffi.C.display_tick >= 0
end)
)
end)
end)