robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
1.7 KiB60 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local ffi = t.ffi
6local eq = t.eq
7local to_cstr = t.to_cstr
8
9local cimp = t.cimport('./src/nvim/message.h', './src/nvim/memory.h', './src/nvim/strings.h')
10
11describe('trunc_string', function()
12  local buflen = 40
13  local function test_inplace(s, expected, room)
14    room = room and room or 20
15    local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
16    ffi.C.strcpy(buf, s)
17    cimp.trunc_string(buf, buf, room, buflen)
18    eq(expected, ffi.string(buf))
19    cimp.xfree(buf)
20  end
21
22  local function test_copy(s, expected, room)
23    room = room and room or 20
24    local buf = cimp.xmalloc(ffi.sizeof('char') * buflen)
25    local str = cimp.xstrdup(to_cstr(s))
26    cimp.trunc_string(str, buf, room, buflen)
27    eq(expected, ffi.string(buf))
28    cimp.xfree(buf)
29    cimp.xfree(str)
30  end
31
32  local permutations = {
33    { ['desc'] = 'in-place', ['func'] = test_inplace },
34    { ['desc'] = 'by copy', ['func'] = test_copy },
35  }
36
37  for _, q in ipairs(permutations) do
38    describe('populates buf ' .. q.desc, function()
39      itp('with a small string', function()
40        q.func('text', 'text')
41      end)
42
43      itp('with a medium string', function()
44        q.func('a short text', 'a short text')
45      end)
46
47      itp('with a string of length == 1/2 room', function()
48        q.func('a text that fits', 'a text that fits', 34)
49      end)
50
51      itp('with a string exactly the truncate size', function()
52        q.func('a text tha just fits', 'a text tha just fits')
53      end)
54
55      itp('with a string that must be truncated', function()
56        q.func('a text that nott fits', 'a text t...nott fits')
57      end)
58    end)
59  end
60end)