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