message_spec.lua 1.7 KB

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