encode_spec.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. local helpers = require('test.unit.helpers')(after_each)
  2. local itp = helpers.gen_itp(it)
  3. local eval_helpers = require('test.unit.eval.helpers')
  4. local cimport = helpers.cimport
  5. local to_cstr = helpers.to_cstr
  6. local eq = helpers.eq
  7. local list = eval_helpers.list
  8. local lst2tbl = eval_helpers.lst2tbl
  9. local type_key = eval_helpers.type_key
  10. local list_type = eval_helpers.list_type
  11. local null_string = eval_helpers.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. eq(0, 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. eq(0, 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. eq(0, 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. eq(0, encode_list_write(l, '\nabc'))
  35. eq({null_string, 'abc'}, lst2tbl(l))
  36. eq(0, 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. eq(0, 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. eq(0, encode_list_write(l, 'abc\n'))
  47. eq({'abc', null_string}, lst2tbl(l))
  48. eq(0, 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. eq(0, encode_list_write(l, '\na\nb\n'))
  54. eq({null_string, 'a', 'b', null_string}, lst2tbl(l))
  55. eq(0, 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. eq(0, encode_list_write(l, '\0\n\0\n\0'))
  61. eq({'\n', '\n', '\n'}, lst2tbl(l))
  62. eq(0, 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. eq(0, encode_list_write(l, '\n\0\n\0\n'))
  68. eq({null_string, '\n', '\n', null_string}, lst2tbl(l))
  69. eq(0, 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. eq(0, encode_list_write(l, '\n'))
  75. eq({null_string, null_string}, lst2tbl(l))
  76. eq(0, 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. eq(0, encode_list_write(l, '\n\n\n'))
  82. eq({null_string, null_string, null_string, null_string}, lst2tbl(l))
  83. eq(0, encode_list_write(l, '\n\n\n'))
  84. eq({null_string, null_string, null_string, null_string, null_string, null_string, null_string}, lst2tbl(l))
  85. end)
  86. end)