textyankpost_spec.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear, eval, eq = helpers.clear, helpers.eval, helpers.eq
  3. local feed, command, expect = helpers.feed, helpers.command, helpers.expect
  4. local curbufmeths, funcs, neq = helpers.curbufmeths, helpers.funcs, helpers.neq
  5. describe('TextYankPost', function()
  6. before_each(function()
  7. clear()
  8. -- emulate the clipboard so system clipboard isn't affected
  9. command('let &rtp = "test/functional/fixtures,".&rtp')
  10. command('let g:count = 0')
  11. command('autocmd TextYankPost * let g:event = copy(v:event)')
  12. command('autocmd TextYankPost * let g:count += 1')
  13. curbufmeths.set_lines(0, -1, true, {
  14. 'foo\0bar',
  15. 'baz text',
  16. })
  17. end)
  18. it('is executed after yank and handles register types', function()
  19. feed('yy')
  20. eq({
  21. operator = 'y',
  22. regcontents = { 'foo\nbar' },
  23. regname = '',
  24. regtype = 'V'
  25. }, eval('g:event'))
  26. eq(1, eval('g:count'))
  27. -- v:event is cleared after the autocommand is done
  28. eq({}, eval('v:event'))
  29. feed('+yw')
  30. eq({
  31. operator = 'y',
  32. regcontents = { 'baz ' },
  33. regname = '',
  34. regtype = 'v'
  35. }, eval('g:event'))
  36. eq(2, eval('g:count'))
  37. feed('<c-v>eky')
  38. eq({
  39. operator = 'y',
  40. regcontents = { 'foo', 'baz' },
  41. regname = '',
  42. regtype = "\0223" -- ^V + block width
  43. }, eval('g:event'))
  44. eq(3, eval('g:count'))
  45. end)
  46. it('makes v:event immutable', function()
  47. feed('yy')
  48. eq({
  49. operator = 'y',
  50. regcontents = { 'foo\nbar' },
  51. regname = '',
  52. regtype = 'V'
  53. }, eval('g:event'))
  54. command('set debug=msg')
  55. -- the regcontents should not be changed without copy.
  56. local status, err = pcall(command,'call extend(g:event.regcontents, ["more text"])')
  57. eq(status,false)
  58. neq(nil, string.find(err, ':E742:'))
  59. -- can't mutate keys inside the autocommand
  60. command('autocmd! TextYankPost * let v:event.regcontents = 0')
  61. status, err = pcall(command,'normal yy')
  62. eq(status,false)
  63. neq(nil, string.find(err, ':E46:'))
  64. -- can't add keys inside the autocommand
  65. command('autocmd! TextYankPost * let v:event.mykey = 0')
  66. status, err = pcall(command,'normal yy')
  67. eq(status,false)
  68. neq(nil, string.find(err, ':E742:'))
  69. end)
  70. it('is not invoked recursively', function()
  71. command('autocmd TextYankPost * normal "+yy')
  72. feed('yy')
  73. eq({
  74. operator = 'y',
  75. regcontents = { 'foo\nbar' },
  76. regname = '',
  77. regtype = 'V'
  78. }, eval('g:event'))
  79. eq(1, eval('g:count'))
  80. eq({ 'foo\nbar' }, funcs.getreg('+',1,1))
  81. end)
  82. it('is executed after delete and change', function()
  83. feed('dw')
  84. eq({
  85. operator = 'd',
  86. regcontents = { 'foo' },
  87. regname = '',
  88. regtype = 'v'
  89. }, eval('g:event'))
  90. eq(1, eval('g:count'))
  91. feed('dd')
  92. eq({
  93. operator = 'd',
  94. regcontents = { '\nbar' },
  95. regname = '',
  96. regtype = 'V'
  97. }, eval('g:event'))
  98. eq(2, eval('g:count'))
  99. feed('cwspam<esc>')
  100. eq({
  101. operator = 'c',
  102. regcontents = { 'baz' },
  103. regname = '',
  104. regtype = 'v'
  105. }, eval('g:event'))
  106. eq(3, eval('g:count'))
  107. end)
  108. it('is not executed after black-hole operation', function()
  109. feed('"_dd')
  110. eq(0, eval('g:count'))
  111. feed('"_cwgood<esc>')
  112. eq(0, eval('g:count'))
  113. expect([[
  114. good text]])
  115. feed('"_yy')
  116. eq(0, eval('g:count'))
  117. command('delete _')
  118. eq(0, eval('g:count'))
  119. end)
  120. it('gives the correct register name', function()
  121. feed('$"byiw')
  122. eq({
  123. operator = 'y',
  124. regcontents = { 'bar' },
  125. regname = 'b',
  126. regtype = 'v'
  127. }, eval('g:event'))
  128. feed('"*yy')
  129. eq({
  130. operator = 'y',
  131. regcontents = { 'foo\nbar' },
  132. regname = '*',
  133. regtype = 'V'
  134. }, eval('g:event'))
  135. command("set clipboard=unnamed")
  136. -- regname still shows the name the user requested
  137. feed('yy')
  138. eq({
  139. operator = 'y',
  140. regcontents = { 'foo\nbar' },
  141. regname = '',
  142. regtype = 'V'
  143. }, eval('g:event'))
  144. feed('"*yy')
  145. eq({
  146. operator = 'y',
  147. regcontents = { 'foo\nbar' },
  148. regname = '*',
  149. regtype = 'V'
  150. }, eval('g:event'))
  151. end)
  152. it('works with Ex commands', function()
  153. command('1delete +')
  154. eq({
  155. operator = 'd',
  156. regcontents = { 'foo\nbar' },
  157. regname = '+',
  158. regtype = 'V'
  159. }, eval('g:event'))
  160. eq(1, eval('g:count'))
  161. command('yank')
  162. eq({
  163. operator = 'y',
  164. regcontents = { 'baz text' },
  165. regname = '',
  166. regtype = 'V'
  167. }, eval('g:event'))
  168. eq(2, eval('g:count'))
  169. command('normal yw')
  170. eq({
  171. operator = 'y',
  172. regcontents = { 'baz ' },
  173. regname = '',
  174. regtype = 'v'
  175. }, eval('g:event'))
  176. eq(3, eval('g:count'))
  177. command('normal! dd')
  178. eq({
  179. operator = 'd',
  180. regcontents = { 'baz text' },
  181. regname = '',
  182. regtype = 'V'
  183. }, eval('g:event'))
  184. eq(4, eval('g:count'))
  185. end)
  186. end)