testutil.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local api = n.api
  4. local write_file = t.write_file
  5. local concat_tables = t.concat_tables
  6. local tmpname = t.tmpname()
  7. -- o={
  8. -- args=…,
  9. -- args_rm=…,
  10. -- shadafile=…,
  11. -- }
  12. local function reset(o)
  13. assert(o == nil or type(o) == 'table' or type(o) == 'string')
  14. o = o and o or {}
  15. local args_rm = o.args_rm or {}
  16. table.insert(args_rm, '-i')
  17. local args = {
  18. '-i',
  19. o.shadafile or tmpname,
  20. }
  21. if type(o) == 'string' then
  22. args = concat_tables(args, { '--cmd', o })
  23. elseif o.args then
  24. args = concat_tables(args, o.args)
  25. end
  26. n.clear {
  27. args_rm = args_rm,
  28. args = args,
  29. }
  30. api.nvim_set_var('tmpname', tmpname)
  31. end
  32. local clear = function()
  33. n.expect_exit(n.command, 'qall!')
  34. os.remove(tmpname)
  35. end
  36. local get_shada_rw = function(fname)
  37. local wshada = function(text)
  38. write_file(fname, text, true)
  39. end
  40. local sdrcmd = function(bang)
  41. return 'rshada' .. (bang and '!' or '') .. ' ' .. fname
  42. end
  43. local clean = function()
  44. os.remove(fname)
  45. local i = ('a'):byte()
  46. while i <= ('z'):byte() do
  47. if not os.remove(fname .. ('.tmp.%c'):format(i)) then
  48. break
  49. end
  50. i = i + 1
  51. end
  52. end
  53. return wshada, sdrcmd, fname, clean
  54. end
  55. local mpack_keys = { 'type', 'timestamp', 'length', 'value' }
  56. local read_shada_file = function(fname)
  57. local fd = io.open(fname, 'r')
  58. local mstring = fd:read('*a')
  59. fd:close()
  60. local unpack = vim.mpack.Unpacker()
  61. local ret = {}
  62. local cur, val
  63. local i = 0
  64. local off = 1
  65. while off <= #mstring do
  66. val, off = unpack(mstring, off)
  67. if i % 4 == 0 then
  68. cur = {}
  69. ret[#ret + 1] = cur
  70. end
  71. cur[mpack_keys[(i % 4) + 1]] = val
  72. i = i + 1
  73. end
  74. return ret
  75. end
  76. return {
  77. reset = reset,
  78. clear = clear,
  79. get_shada_rw = get_shada_rw,
  80. read_shada_file = read_shada_file,
  81. }