changedtick_spec.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local eval = n.eval
  5. local feed = n.feed
  6. local clear = n.clear
  7. local fn = n.fn
  8. local api = n.api
  9. local command = n.command
  10. local exc_exec = n.exc_exec
  11. local pcall_err = t.pcall_err
  12. local exec_capture = n.exec_capture
  13. before_each(clear)
  14. local function changedtick()
  15. local ct = api.nvim_buf_get_changedtick(0)
  16. eq(ct, api.nvim_buf_get_var(0, 'changedtick'))
  17. eq(ct, api.nvim_buf_get_var(0, 'changedtick'))
  18. eq(ct, eval('b:changedtick'))
  19. eq(ct, eval('b:["changedtick"]'))
  20. eq(ct, eval('b:.changedtick'))
  21. eq(ct, fn.getbufvar('%', 'changedtick'))
  22. eq(ct, fn.getbufvar('%', '').changedtick)
  23. eq(ct, eval('b:').changedtick)
  24. return ct
  25. end
  26. describe('b:changedtick', function()
  27. -- Ported tests from Vim-8.0.333
  28. it('increments', function() -- Test_changedtick_increments
  29. -- New buffer has an empty line, tick starts at 2
  30. eq(2, changedtick())
  31. fn.setline(1, 'hello')
  32. eq(3, changedtick())
  33. eq(0, exc_exec('undo'))
  34. -- Somehow undo counts as two changes
  35. eq(5, changedtick())
  36. end)
  37. it('is present in b: dict', function()
  38. eq(2, changedtick())
  39. command('let d = b:')
  40. eq(2, api.nvim_get_var('d').changedtick)
  41. end)
  42. it('increments at bdel', function()
  43. command('new')
  44. eq(2, changedtick())
  45. local bnr = api.nvim_buf_get_number(0)
  46. eq(2, bnr)
  47. command('bdel')
  48. eq(3, fn.getbufvar(bnr, 'changedtick'))
  49. eq(1, api.nvim_buf_get_number(0))
  50. end)
  51. it('fails to be changed by user', function()
  52. local ct = changedtick()
  53. local ctn = ct + 100500
  54. eq(0, exc_exec('let d = b:'))
  55. eq(
  56. 'Vim(let):E46: Cannot change read-only variable "b:changedtick"',
  57. pcall_err(command, 'let b:changedtick = ' .. ctn)
  58. )
  59. eq(
  60. 'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
  61. pcall_err(command, 'let b:["changedtick"] = ' .. ctn)
  62. )
  63. eq(
  64. 'Vim(let):E46: Cannot change read-only variable "b:.changedtick"',
  65. pcall_err(command, 'let b:.changedtick = ' .. ctn)
  66. )
  67. eq(
  68. 'Vim(let):E46: Cannot change read-only variable "d.changedtick"',
  69. pcall_err(command, 'let d.changedtick = ' .. ctn)
  70. )
  71. eq('Key is read-only: changedtick', pcall_err(api.nvim_buf_set_var, 0, 'changedtick', ctn))
  72. eq(
  73. 'Vim(unlet):E795: Cannot delete variable b:changedtick',
  74. pcall_err(command, 'unlet b:changedtick')
  75. )
  76. eq(
  77. 'Vim(unlet):E46: Cannot change read-only variable "b:.changedtick"',
  78. pcall_err(command, 'unlet b:.changedtick')
  79. )
  80. eq(
  81. 'Vim(unlet):E46: Cannot change read-only variable "b:["changedtick"]"',
  82. pcall_err(command, 'unlet b:["changedtick"]')
  83. )
  84. eq(
  85. 'Vim(unlet):E46: Cannot change read-only variable "d.changedtick"',
  86. pcall_err(command, 'unlet d.changedtick')
  87. )
  88. eq('Key is read-only: changedtick', pcall_err(api.nvim_buf_del_var, 0, 'changedtick'))
  89. eq(ct, changedtick())
  90. eq(
  91. 'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
  92. pcall_err(command, 'let b:["changedtick"] += ' .. ctn)
  93. )
  94. eq(
  95. 'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
  96. pcall_err(command, 'let b:["changedtick"] -= ' .. ctn)
  97. )
  98. eq(
  99. 'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
  100. pcall_err(command, 'let b:["changedtick"] .= ' .. ctn)
  101. )
  102. eq(ct, changedtick())
  103. fn.setline(1, 'hello')
  104. eq(ct + 1, changedtick())
  105. end)
  106. it('is listed in :let output', function()
  107. eq('b:changedtick #2', exec_capture(':let b:'))
  108. end)
  109. it('fails to unlock b:changedtick', function()
  110. eq(0, exc_exec('let d = b:'))
  111. eq(0, fn.islocked('b:changedtick'))
  112. eq(0, fn.islocked('d.changedtick'))
  113. eq(
  114. 'Vim(unlockvar):E940: Cannot lock or unlock variable b:changedtick',
  115. pcall_err(command, 'unlockvar b:changedtick')
  116. )
  117. eq(
  118. 'Vim(unlockvar):E46: Cannot change read-only variable "d.changedtick"',
  119. pcall_err(command, 'unlockvar d.changedtick')
  120. )
  121. eq(0, fn.islocked('b:changedtick'))
  122. eq(0, fn.islocked('d.changedtick'))
  123. eq(
  124. 'Vim(lockvar):E940: Cannot lock or unlock variable b:changedtick',
  125. pcall_err(command, 'lockvar b:changedtick')
  126. )
  127. eq(
  128. 'Vim(lockvar):E46: Cannot change read-only variable "d.changedtick"',
  129. pcall_err(command, 'lockvar d.changedtick')
  130. )
  131. eq(0, fn.islocked('b:changedtick'))
  132. eq(0, fn.islocked('d.changedtick'))
  133. end)
  134. it('is being completed', function()
  135. feed(':echo b:<Tab><Home>let cmdline="<End>"<CR>')
  136. eq('echo b:changedtick', api.nvim_get_var('cmdline'))
  137. end)
  138. it('cannot be changed by filter() or map()', function()
  139. eq(2, changedtick())
  140. eq(
  141. 'Vim(call):E795: Cannot delete variable filter() argument',
  142. pcall_err(command, 'call filter(b:, 0)')
  143. )
  144. eq(
  145. 'Vim(call):E742: Cannot change value of map() argument',
  146. pcall_err(command, 'call map(b:, 0)')
  147. )
  148. eq(
  149. 'Vim(call):E742: Cannot change value of map() argument',
  150. pcall_err(command, 'call map(b:, "v:val")')
  151. )
  152. eq(2, changedtick())
  153. end)
  154. it('cannot be remove()d', function()
  155. eq(2, changedtick())
  156. eq(
  157. 'Vim(call):E795: Cannot delete variable remove() argument',
  158. pcall_err(command, 'call remove(b:, "changedtick")')
  159. )
  160. eq(2, changedtick())
  161. end)
  162. it('does not inherit VAR_FIXED when copying dict over', function()
  163. eq(2, changedtick())
  164. eq('', exec_capture('let d1 = copy(b:)|let d1.changedtick = 42'))
  165. eq('', exec_capture('let d2 = copy(b:)|unlet d2.changedtick'))
  166. eq(2, changedtick())
  167. end)
  168. end)