2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.unit.testutil')
|
2024-04-08 02:03:20 -07:00
|
|
|
local itp = t.gen_itp(it)
|
2017-03-06 08:44:07 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local ffi = t.ffi
|
|
|
|
local eq = t.eq
|
|
|
|
local to_cstr = t.to_cstr
|
2017-03-06 08:44:07 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local cimp = t.cimport('./src/nvim/message.h', './src/nvim/memory.h', './src/nvim/strings.h')
|
2017-03-06 08:44:07 -07:00
|
|
|
|
|
|
|
describe('trunc_string', function()
|
2017-03-06 10:14:39 -07:00
|
|
|
local buflen = 40
|
|
|
|
local function test_inplace(s, expected, room)
|
|
|
|
room = room and room or 20
|
2023-04-02 01:11:42 -07:00
|
|
|
local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
|
2017-03-06 08:44:07 -07:00
|
|
|
ffi.C.strcpy(buf, s)
|
2017-03-06 10:14:39 -07:00
|
|
|
cimp.trunc_string(buf, buf, room, buflen)
|
2017-03-06 08:44:07 -07:00
|
|
|
eq(expected, ffi.string(buf))
|
2017-03-06 10:14:39 -07:00
|
|
|
cimp.xfree(buf)
|
2017-03-06 08:44:07 -07:00
|
|
|
end
|
|
|
|
|
2017-03-06 10:14:39 -07:00
|
|
|
local function test_copy(s, expected, room)
|
|
|
|
room = room and room or 20
|
2023-04-02 01:11:42 -07:00
|
|
|
local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
|
2022-08-26 14:11:25 -07:00
|
|
|
local str = cimp.xstrdup(to_cstr(s))
|
2017-03-06 10:14:39 -07:00
|
|
|
cimp.trunc_string(str, buf, room, buflen)
|
2017-03-06 08:44:07 -07:00
|
|
|
eq(expected, ffi.string(buf))
|
2017-03-06 10:14:39 -07:00
|
|
|
cimp.xfree(buf)
|
|
|
|
cimp.xfree(str)
|
2017-03-06 08:44:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local permutations = {
|
|
|
|
{ ['desc'] = 'in-place', ['func'] = test_inplace },
|
|
|
|
{ ['desc'] = 'by copy', ['func'] = test_copy },
|
|
|
|
}
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
for _, q in ipairs(permutations) do
|
|
|
|
describe('populates buf ' .. q.desc, function()
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a small string', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
q.func('text', 'text')
|
2017-03-06 08:44:07 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a medium string', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
q.func('a short text', 'a short text')
|
2017-03-06 08:44:07 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string of length == 1/2 room', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
q.func('a text that fits', 'a text that fits', 34)
|
2017-03-06 10:14:39 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string exactly the truncate size', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
q.func('a text tha just fits', 'a text tha just fits')
|
2017-03-06 08:44:07 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string that must be truncated', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
q.func('a text that nott fits', 'a text t...nott fits')
|
2017-03-06 08:44:07 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|