arglist_spec.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. -- Test argument list commands
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local clear, command, eq = helpers.clear, helpers.command, helpers.eq
  4. local eval, exc_exec, neq = helpers.eval, helpers.exc_exec, helpers.neq
  5. describe('argument list commands', function()
  6. before_each(clear)
  7. local function init_abc()
  8. command('args a b c')
  9. command('next')
  10. end
  11. local function reset_arglist()
  12. command('arga a | %argd')
  13. end
  14. local function assert_fails(cmd, err)
  15. neq(exc_exec(cmd):find(err), nil)
  16. end
  17. it('test that argidx() works', function()
  18. command('args a b c')
  19. command('last')
  20. eq(2, eval('argidx()'))
  21. command('%argdelete')
  22. eq(0, eval('argidx()'))
  23. command('args a b c')
  24. eq(0, eval('argidx()'))
  25. command('next')
  26. eq(1, eval('argidx()'))
  27. command('next')
  28. eq(2, eval('argidx()'))
  29. command('1argdelete')
  30. eq(1, eval('argidx()'))
  31. command('1argdelete')
  32. eq(0, eval('argidx()'))
  33. command('1argdelete')
  34. eq(0, eval('argidx()'))
  35. end)
  36. it('test that argadd() works', function()
  37. command('%argdelete')
  38. command('argadd a b c')
  39. eq(0, eval('argidx()'))
  40. command('%argdelete')
  41. command('argadd a')
  42. eq(0, eval('argidx()'))
  43. command('argadd b c d')
  44. eq(0, eval('argidx()'))
  45. init_abc()
  46. command('argadd x')
  47. eq({'a', 'b', 'x', 'c'}, eval('argv()'))
  48. eq(1, eval('argidx()'))
  49. init_abc()
  50. command('0argadd x')
  51. eq({'x', 'a', 'b', 'c'}, eval('argv()'))
  52. eq(2, eval('argidx()'))
  53. init_abc()
  54. command('1argadd x')
  55. eq({'a', 'x', 'b', 'c'}, eval('argv()'))
  56. eq(2, eval('argidx()'))
  57. init_abc()
  58. command('$argadd x')
  59. eq({'a', 'b', 'c', 'x'}, eval('argv()'))
  60. eq(1, eval('argidx()'))
  61. init_abc()
  62. command('$argadd x')
  63. command('+2argadd y')
  64. eq({'a', 'b', 'c', 'x', 'y'}, eval('argv()'))
  65. eq(1, eval('argidx()'))
  66. command('%argd')
  67. command('edit d')
  68. command('arga')
  69. eq(1, eval('len(argv())'))
  70. eq('d', eval('get(argv(), 0, "")'))
  71. command('%argd')
  72. command('new')
  73. command('arga')
  74. eq(0, eval('len(argv())'))
  75. end)
  76. it('test for 0argadd and 0argedit', function()
  77. reset_arglist()
  78. command('arga a b c d')
  79. command('2argu')
  80. command('0arga added')
  81. eq({'added', 'a', 'b', 'c', 'd'}, eval('argv()'))
  82. command('%argd')
  83. command('arga a b c d')
  84. command('2argu')
  85. command('0arge edited')
  86. eq({'edited', 'a', 'b', 'c', 'd'}, eval('argv()'))
  87. command('2argu')
  88. command('arga third')
  89. eq({'edited', 'a', 'third', 'b', 'c', 'd'}, eval('argv()'))
  90. end)
  91. it('test for argc()', function()
  92. reset_arglist()
  93. eq(0, eval('argc()'))
  94. command('argadd a b')
  95. eq(2, eval('argc()'))
  96. end)
  97. it('test for arglistid()', function()
  98. reset_arglist()
  99. command('arga a b')
  100. eq(0, eval('arglistid()'))
  101. command('split')
  102. command('arglocal')
  103. eq(1, eval('arglistid()'))
  104. command('tabnew | tabfirst')
  105. eq(0, eval('arglistid(2)'))
  106. eq(1, eval('arglistid(1, 1)'))
  107. eq(0, eval('arglistid(2, 1)'))
  108. eq(1, eval('arglistid(1, 2)'))
  109. command('tabonly | only | enew!')
  110. command('argglobal')
  111. eq(0, eval('arglistid()'))
  112. end)
  113. it('test for argv()', function()
  114. reset_arglist()
  115. eq({}, eval('argv()'))
  116. eq('', eval('argv(2)'))
  117. command('argadd a b c d')
  118. eq('c', eval('argv(2)'))
  119. end)
  120. it('test for :argedit command', function()
  121. reset_arglist()
  122. command('argedit a')
  123. eq({'a'}, eval('argv()'))
  124. eq('a', eval('expand("%:t")'))
  125. command('argedit b')
  126. eq({'a', 'b'}, eval('argv()'))
  127. eq('b', eval('expand("%:t")'))
  128. command('argedit a')
  129. eq({'a', 'b', 'a'}, eval('argv()'))
  130. eq('a', eval('expand("%:t")'))
  131. command('argedit c')
  132. eq({'a', 'b', 'a', 'c'}, eval('argv()'))
  133. command('0argedit x')
  134. eq({'x', 'a', 'b', 'a', 'c'}, eval('argv()'))
  135. command('enew! | set modified')
  136. assert_fails('argedit y', 'E37:')
  137. command('argedit! y')
  138. eq({'x', 'y', 'y', 'a', 'b', 'a', 'c'}, eval('argv()'))
  139. command('%argd')
  140. command('argedit a b')
  141. eq({'a', 'b'}, eval('argv()'))
  142. end)
  143. it('test for :argdelete command', function()
  144. reset_arglist()
  145. command('args aa a aaa b bb')
  146. command('argdelete a*')
  147. eq({'b', 'bb'}, eval('argv()'))
  148. eq('aa', eval('expand("%:t")'))
  149. command('last')
  150. command('argdelete %')
  151. eq({'b'}, eval('argv()'))
  152. assert_fails('argdelete', 'E610:')
  153. assert_fails('1,100argdelete', 'E16:')
  154. reset_arglist()
  155. command('args a b c d')
  156. command('next')
  157. command('argdel')
  158. eq({'a', 'c', 'd'}, eval('argv()'))
  159. command('%argdel')
  160. end)
  161. it('test for the :next, :prev, :first, :last, :rewind commands', function()
  162. reset_arglist()
  163. command('args a b c d')
  164. command('last')
  165. eq(3, eval('argidx()'))
  166. assert_fails('next', 'E165:')
  167. command('prev')
  168. eq(2, eval('argidx()'))
  169. command('Next')
  170. eq(1, eval('argidx()'))
  171. command('first')
  172. eq(0, eval('argidx()'))
  173. assert_fails('prev', 'E164:')
  174. command('3next')
  175. eq(3, eval('argidx()'))
  176. command('rewind')
  177. eq(0, eval('argidx()'))
  178. command('%argd')
  179. end)
  180. it('test for autocommand that redefines the argument list, when doing ":all"', function()
  181. command('autocmd BufReadPost Xxx2 next Xxx2 Xxx1')
  182. command("call writefile(['test file Xxx1'], 'Xxx1')")
  183. command("call writefile(['test file Xxx2'], 'Xxx2')")
  184. command("call writefile(['test file Xxx3'], 'Xxx3')")
  185. command('new')
  186. -- redefine arglist; go to Xxx1
  187. command('next! Xxx1 Xxx2 Xxx3')
  188. -- open window for all args
  189. command('all')
  190. eq('test file Xxx1', eval('getline(1)'))
  191. command('wincmd w')
  192. command('wincmd w')
  193. eq('test file Xxx1', eval('getline(1)'))
  194. -- should now be in Xxx2
  195. command('rewind')
  196. eq('test file Xxx2', eval('getline(1)'))
  197. command('autocmd! BufReadPost Xxx2')
  198. command('enew! | only')
  199. command("call delete('Xxx1')")
  200. command("call delete('Xxx2')")
  201. command("call delete('Xxx3')")
  202. command('argdelete Xxx*')
  203. command('bwipe! Xxx1 Xxx2 Xxx3')
  204. end)
  205. end)