encode_spec.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. local t = require('test.unit.testutil')
  2. local itp = t.gen_itp(it)
  3. local t_eval = require('test.unit.eval.testutil')
  4. local cimport = t.cimport
  5. local to_cstr = t.to_cstr
  6. local eq = t.eq
  7. local list = t_eval.list
  8. local lst2tbl = t_eval.lst2tbl
  9. local type_key = t_eval.type_key
  10. local list_type = t_eval.list_type
  11. local null_string = t_eval.null_string
  12. local encode = cimport('./src/nvim/eval/encode.h')
  13. describe('encode_list_write()', function()
  14. local encode_list_write = function(l, s)
  15. return encode.encode_list_write(l, to_cstr(s), #s)
  16. end
  17. itp('writes empty string', function()
  18. local l = list()
  19. encode_list_write(l, '')
  20. eq({ [type_key] = list_type }, lst2tbl(l))
  21. end)
  22. itp('writes ASCII string literal with printable characters', function()
  23. local l = list()
  24. encode_list_write(l, 'abc')
  25. eq({ 'abc' }, lst2tbl(l))
  26. end)
  27. itp('writes string starting with NL', function()
  28. local l = list()
  29. encode_list_write(l, '\nabc')
  30. eq({ null_string, 'abc' }, lst2tbl(l))
  31. end)
  32. itp('writes string starting with NL twice', function()
  33. local l = list()
  34. encode_list_write(l, '\nabc')
  35. eq({ null_string, 'abc' }, lst2tbl(l))
  36. encode_list_write(l, '\nabc')
  37. eq({ null_string, 'abc', 'abc' }, lst2tbl(l))
  38. end)
  39. itp('writes string ending with NL', function()
  40. local l = list()
  41. encode_list_write(l, 'abc\n')
  42. eq({ 'abc', null_string }, lst2tbl(l))
  43. end)
  44. itp('writes string ending with NL twice', function()
  45. local l = list()
  46. encode_list_write(l, 'abc\n')
  47. eq({ 'abc', null_string }, lst2tbl(l))
  48. encode_list_write(l, 'abc\n')
  49. eq({ 'abc', 'abc', null_string }, lst2tbl(l))
  50. end)
  51. itp('writes string starting, ending and containing NL twice', function()
  52. local l = list()
  53. encode_list_write(l, '\na\nb\n')
  54. eq({ null_string, 'a', 'b', null_string }, lst2tbl(l))
  55. encode_list_write(l, '\na\nb\n')
  56. eq({ null_string, 'a', 'b', null_string, 'a', 'b', null_string }, lst2tbl(l))
  57. end)
  58. itp('writes string starting, ending and containing NUL with NL between twice', function()
  59. local l = list()
  60. encode_list_write(l, '\0\n\0\n\0')
  61. eq({ '\n', '\n', '\n' }, lst2tbl(l))
  62. encode_list_write(l, '\0\n\0\n\0')
  63. eq({ '\n', '\n', '\n\n', '\n', '\n' }, lst2tbl(l))
  64. end)
  65. itp('writes string starting, ending and containing NL with NUL between twice', function()
  66. local l = list()
  67. encode_list_write(l, '\n\0\n\0\n')
  68. eq({ null_string, '\n', '\n', null_string }, lst2tbl(l))
  69. encode_list_write(l, '\n\0\n\0\n')
  70. eq({ null_string, '\n', '\n', null_string, '\n', '\n', null_string }, lst2tbl(l))
  71. end)
  72. itp('writes string containing a single NL twice', function()
  73. local l = list()
  74. encode_list_write(l, '\n')
  75. eq({ null_string, null_string }, lst2tbl(l))
  76. encode_list_write(l, '\n')
  77. eq({ null_string, null_string, null_string }, lst2tbl(l))
  78. end)
  79. itp('writes string containing a few NLs twice', function()
  80. local l = list()
  81. encode_list_write(l, '\n\n\n')
  82. eq({ null_string, null_string, null_string, null_string }, lst2tbl(l))
  83. encode_list_write(l, '\n\n\n')
  84. eq(
  85. { null_string, null_string, null_string, null_string, null_string, null_string, null_string },
  86. lst2tbl(l)
  87. )
  88. end)
  89. end)