variables_spec.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. -- ShaDa variables saving/reading support
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local meths, funcs, nvim_command, eq, eval =
  4. helpers.meths, helpers.funcs, helpers.command, helpers.eq, helpers.eval
  5. local shada_helpers = require('test.functional.shada.helpers')
  6. local reset, clear = shada_helpers.reset, shada_helpers.clear
  7. describe('ShaDa support code', function()
  8. before_each(reset)
  9. after_each(clear)
  10. it('is able to dump and read back string variable', function()
  11. meths.set_var('STRVAR', 'foo')
  12. nvim_command('set shada+=!')
  13. nvim_command('wshada')
  14. reset()
  15. nvim_command('set shada+=!')
  16. nvim_command('rshada')
  17. eq('foo', meths.get_var('STRVAR'))
  18. end)
  19. local autotest = function(tname, varname, varval, val_is_expr)
  20. it('is able to dump and read back ' .. tname .. ' variable automatically',
  21. function()
  22. reset('set shada+=!')
  23. if val_is_expr then
  24. nvim_command('let g:' .. varname .. ' = ' .. varval)
  25. varval = meths.get_var(varname)
  26. else
  27. meths.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. nvim_command('qall')
  33. reset('set shada+=!')
  34. eq(vartype, eval('type(g:' .. varname .. ')'))
  35. eq(varval, meths.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. meths.set_var('STRVAR', 'foo')
  51. nvim_command('set shada+=!')
  52. nvim_command('wshada')
  53. reset('set shada-=!')
  54. nvim_command('rshada')
  55. eq(0, funcs.exists('g:STRVAR'))
  56. end)
  57. it('does not dump variables without `!` in &shada', function()
  58. nvim_command('set shada-=!')
  59. meths.set_var('STRVAR', 'foo')
  60. nvim_command('wshada')
  61. reset()
  62. nvim_command('set shada+=!')
  63. nvim_command('rshada')
  64. eq(0, funcs.exists('g:STRVAR'))
  65. end)
  66. it('does not dump session variables', function()
  67. nvim_command('set shada+=!')
  68. meths.set_var('StrVar', 'foo')
  69. nvim_command('wshada')
  70. reset()
  71. nvim_command('set shada+=!')
  72. nvim_command('rshada')
  73. eq(0, funcs.exists('g:StrVar'))
  74. end)
  75. it('does not dump regular variables', function()
  76. nvim_command('set shada+=!')
  77. meths.set_var('str_var', 'foo')
  78. nvim_command('wshada')
  79. reset()
  80. nvim_command('set shada+=!')
  81. nvim_command('rshada')
  82. eq(0, funcs.exists('g:str_var'))
  83. end)
  84. it('dumps and loads variables correctly with utf-8 strings',
  85. function()
  86. reset()
  87. meths.set_var('STRVAR', '«')
  88. meths.set_var('LSTVAR', {'«'})
  89. meths.set_var('DCTVAR', {['«']='«'})
  90. meths.set_var('NESTEDVAR', {['«']={{'«'}, {['«']='«'}, {a='Test'}}})
  91. nvim_command('qall')
  92. reset()
  93. eq('«', meths.get_var('STRVAR'))
  94. eq({'«'}, meths.get_var('LSTVAR'))
  95. eq({['«']='«'}, meths.get_var('DCTVAR'))
  96. eq({['«']={{'«'}, {['«']='«'}, {a='Test'}}}, meths.get_var('NESTEDVAR'))
  97. end)
  98. it('dumps and loads variables correctly with 8-bit strings',
  99. function()
  100. reset()
  101. -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
  102. -- This is invalid unicode, but we should still dump and restore it.
  103. meths.set_var('STRVAR', '\171')
  104. meths.set_var('LSTVAR', {'\171'})
  105. meths.set_var('DCTVAR', {['«\171']='«\171'})
  106. meths.set_var('NESTEDVAR', {['\171']={{'\171«'}, {['\171']='\171'},
  107. {a='Test'}}})
  108. nvim_command('qall')
  109. reset()
  110. eq('\171', meths.get_var('STRVAR'))
  111. eq({'\171'}, meths.get_var('LSTVAR'))
  112. eq({['«\171']='«\171'}, meths.get_var('DCTVAR'))
  113. eq({['\171']={{'\171«'}, {['\171']='\171'}, {a='Test'}}},
  114. meths.get_var('NESTEDVAR'))
  115. end)
  116. it('ignore when a funcref is stored in a variable',
  117. function()
  118. nvim_command('let F = function("tr")')
  119. meths.set_var('U', '10')
  120. nvim_command('set shada+=!')
  121. nvim_command('wshada')
  122. reset()
  123. nvim_command('set shada+=!')
  124. nvim_command('rshada')
  125. eq('10', meths.get_var('U'))
  126. end)
  127. it('ignore when a partial is stored in a variable',
  128. function()
  129. nvim_command('let P = { -> 1 }')
  130. meths.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', meths.get_var('U'))
  137. end)
  138. it('ignore when a self-referencing list is stored in a variable',
  139. function()
  140. meths.set_var('L', {})
  141. nvim_command('call add(L, L)')
  142. meths.set_var('U', '10')
  143. nvim_command('set shada+=!')
  144. nvim_command('wshada')
  145. reset()
  146. nvim_command('rshada')
  147. eq('10', meths.get_var('U'))
  148. end)
  149. end)