2017-03-11 13:26:33 -07:00
|
|
|
local helpers = require("test.unit.helpers")(after_each)
|
|
|
|
local itp = helpers.gen_itp(it)
|
2017-03-06 08:44:07 -07:00
|
|
|
|
|
|
|
local ffi = helpers.ffi
|
|
|
|
local eq = helpers.eq
|
|
|
|
local to_cstr = helpers.to_cstr
|
|
|
|
|
2017-03-06 10:14:39 -07:00
|
|
|
local cimp = helpers.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 },
|
|
|
|
}
|
|
|
|
|
|
|
|
for _,t in ipairs(permutations) do
|
|
|
|
describe('populates buf '..t.desc, function()
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a small string', function()
|
2017-03-06 08:44:07 -07:00
|
|
|
t.func('text', 'text')
|
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a medium string', function()
|
2017-03-06 08:44:07 -07:00
|
|
|
t.func('a short text', 'a short text')
|
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string of length == 1/2 room', function()
|
2017-03-06 10:14:39 -07:00
|
|
|
t.func('a text that fits', 'a text that fits', 34)
|
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string exactly the truncate size', function()
|
2017-03-06 08:44:07 -07:00
|
|
|
t.func('a text tha just fits', 'a text tha just fits')
|
|
|
|
end)
|
|
|
|
|
2017-03-11 13:26:33 -07:00
|
|
|
itp('with a string that must be truncated', function()
|
2017-03-06 08:44:07 -07:00
|
|
|
t.func('a text that nott fits', 'a text t...nott fits')
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|