message_spec.lua 1.7 KB

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