variables_spec.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -- ShaDa variables saving/reading support
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local t_shada = require('test.functional.shada.testutil')
  5. local api, fn, nvim_command, eq, eval = n.api, n.fn, n.command, t.eq, n.eval
  6. local expect_exit = n.expect_exit
  7. local reset, clear = t_shada.reset, t_shada.clear
  8. describe('ShaDa support code', function()
  9. before_each(reset)
  10. after_each(clear)
  11. it('is able to dump and read back string variable', function()
  12. api.nvim_set_var('STRVAR', 'foo')
  13. nvim_command('set shada+=!')
  14. nvim_command('wshada')
  15. reset()
  16. nvim_command('set shada+=!')
  17. nvim_command('rshada')
  18. eq('foo', api.nvim_get_var('STRVAR'))
  19. end)
  20. local autotest = function(tname, varname, varval, val_is_expr)
  21. it('is able to dump and read back ' .. tname .. ' variable automatically', function()
  22. reset('set shada+=!')
  23. if val_is_expr then
  24. nvim_command('let g:' .. varname .. ' = ' .. varval)
  25. varval = api.nvim_get_var(varname)
  26. else
  27. api.nvim_set_var(varname, varval)
  28. end
  29. local vartype = eval('type(g:' .. varname .. ')')
  30. -- Exit during `reset` is not a regular exit: it does not write shada
  31. -- automatically
  32. expect_exit(nvim_command, 'qall')
  33. reset('set shada+=!')
  34. eq(vartype, eval('type(g:' .. varname .. ')'))
  35. eq(varval, api.nvim_get_var(varname))
  36. end)
  37. end
  38. autotest('string', 'STRVAR', 'foo')
  39. autotest('number', 'NUMVAR', 42)
  40. autotest('float', 'FLTVAR', 42.5)
  41. autotest('dictionary', 'DCTVAR', { a = 10 })
  42. autotest('list', 'LSTVAR', { { a = 10 }, { b = 10.5 }, { c = 'str' } })
  43. autotest('true', 'TRUEVAR', true)
  44. autotest('false', 'FALSEVAR', false)
  45. autotest('null', 'NULLVAR', 'v:null', true)
  46. autotest('ext', 'EXTVAR', '{"_TYPE": v:msgpack_types.ext, "_VAL": [2, ["", ""]]}', true)
  47. autotest('blob', 'BLOBVAR', '0z12ab34cd', true)
  48. autotest('blob (with NULs)', 'BLOBVARNULS', '0z004e554c7300', true)
  49. it('does not read back variables without `!` in &shada', function()
  50. api.nvim_set_var('STRVAR', 'foo')
  51. nvim_command('set shada+=!')
  52. nvim_command('wshada')
  53. reset('set shada-=!')
  54. nvim_command('rshada')
  55. eq(0, fn.exists('g:STRVAR'))
  56. end)
  57. it('does not dump variables without `!` in &shada', function()
  58. nvim_command('set shada-=!')
  59. api.nvim_set_var('STRVAR', 'foo')
  60. nvim_command('wshada')
  61. reset()
  62. nvim_command('set shada+=!')
  63. nvim_command('rshada')
  64. eq(0, fn.exists('g:STRVAR'))
  65. end)
  66. it('does not dump session variables', function()
  67. nvim_command('set shada+=!')
  68. api.nvim_set_var('StrVar', 'foo')
  69. nvim_command('wshada')
  70. reset()
  71. nvim_command('set shada+=!')
  72. nvim_command('rshada')
  73. eq(0, fn.exists('g:StrVar'))
  74. end)
  75. it('does not dump regular variables', function()
  76. nvim_command('set shada+=!')
  77. api.nvim_set_var('str_var', 'foo')
  78. nvim_command('wshada')
  79. reset()
  80. nvim_command('set shada+=!')
  81. nvim_command('rshada')
  82. eq(0, fn.exists('g:str_var'))
  83. end)
  84. it('dumps and loads variables correctly with utf-8 strings', function()
  85. reset()
  86. api.nvim_set_var('STRVAR', '«')
  87. api.nvim_set_var('LSTVAR', { '«' })
  88. api.nvim_set_var('DCTVAR', { ['«'] = '«' })
  89. api.nvim_set_var('NESTEDVAR', { ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } })
  90. expect_exit(nvim_command, 'qall')
  91. reset()
  92. eq('«', api.nvim_get_var('STRVAR'))
  93. eq({ '«' }, api.nvim_get_var('LSTVAR'))
  94. eq({ ['«'] = '«' }, api.nvim_get_var('DCTVAR'))
  95. eq({ ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } }, api.nvim_get_var('NESTEDVAR'))
  96. end)
  97. it('dumps and loads variables correctly with 8-bit strings', function()
  98. reset()
  99. -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
  100. -- This is invalid unicode, but we should still dump and restore it.
  101. api.nvim_set_var('STRVAR', '\171')
  102. api.nvim_set_var('LSTVAR', { '\171' })
  103. api.nvim_set_var('DCTVAR', { ['«\171'] = '«\171' })
  104. api.nvim_set_var(
  105. 'NESTEDVAR',
  106. { ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } }
  107. )
  108. expect_exit(nvim_command, 'qall')
  109. reset()
  110. eq('\171', api.nvim_get_var('STRVAR'))
  111. eq({ '\171' }, api.nvim_get_var('LSTVAR'))
  112. eq({ ['«\171'] = '«\171' }, api.nvim_get_var('DCTVAR'))
  113. eq(
  114. { ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } },
  115. api.nvim_get_var('NESTEDVAR')
  116. )
  117. end)
  118. it('ignore when a funcref is stored in a variable', function()
  119. nvim_command('let F = function("tr")')
  120. api.nvim_set_var('U', '10')
  121. nvim_command('set shada+=!')
  122. nvim_command('wshada')
  123. reset()
  124. nvim_command('set shada+=!')
  125. nvim_command('rshada')
  126. eq('10', api.nvim_get_var('U'))
  127. end)
  128. it('ignore when a partial is stored in a variable', function()
  129. nvim_command('let P = { -> 1 }')
  130. api.nvim_set_var('U', '10')
  131. nvim_command('set shada+=!')
  132. nvim_command('wshada')
  133. reset()
  134. nvim_command('set shada+=!')
  135. nvim_command('rshada')
  136. eq('10', api.nvim_get_var('U'))
  137. end)
  138. it('ignore when a self-referencing list is stored in a variable', function()
  139. api.nvim_set_var('L', {})
  140. nvim_command('call add(L, L)')
  141. api.nvim_set_var('U', '10')
  142. nvim_command('set shada+=!')
  143. nvim_command('wshada')
  144. reset()
  145. nvim_command('rshada')
  146. eq('10', api.nvim_get_var('U'))
  147. end)
  148. end)