cdo_spec.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. -- Tests for the :cdo, :cfdo, :ldo and :lfdo commands
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local nvim, clear = helpers.meths, helpers.clear
  4. local call, feed = helpers.call, helpers.feed
  5. local source, eq = helpers.source, helpers.eq
  6. local function expected_empty()
  7. eq({}, nvim.get_vvar('errors'))
  8. end
  9. describe('cdo', function()
  10. before_each(function()
  11. clear()
  12. call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile1')
  13. call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile2')
  14. call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile3')
  15. source([=[
  16. " Returns the current line in '<filename> <linenum>L <column>C' format
  17. function GetRuler()
  18. return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C'
  19. endfunction
  20. " Tests for the :cdo and :ldo commands
  21. function XdoTests(cchar)
  22. enew
  23. " Shortcuts for calling the cdo and ldo commands
  24. let Xdo = a:cchar . 'do'
  25. let Xgetexpr = a:cchar . 'getexpr'
  26. let Xprev = a:cchar. 'prev'
  27. let XdoCmd = Xdo . ' call add(l, GetRuler())'
  28. " Try with an empty list
  29. let l = []
  30. exe XdoCmd
  31. call assert_equal([], l)
  32. " Populate the list and then try
  33. exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:3:1:Line3']"
  34. let l = []
  35. exe XdoCmd
  36. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
  37. " Run command only on selected error lines
  38. let l = []
  39. enew
  40. exe "2,3" . XdoCmd
  41. call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
  42. " Boundary condition tests
  43. let l = []
  44. enew
  45. exe "1,1" . XdoCmd
  46. call assert_equal(['Xtestfile1 1L 3C'], l)
  47. let l = []
  48. enew
  49. exe "3" . XdoCmd
  50. call assert_equal(['Xtestfile3 3L 1C'], l)
  51. " Range test commands
  52. let l = []
  53. enew
  54. exe "%" . XdoCmd
  55. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
  56. let l = []
  57. enew
  58. exe "1,$" . XdoCmd
  59. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
  60. let l = []
  61. enew
  62. exe Xprev
  63. exe "." . XdoCmd
  64. call assert_equal(['Xtestfile2 2L 2C'], l)
  65. let l = []
  66. enew
  67. exe "+" . XdoCmd
  68. call assert_equal(['Xtestfile3 3L 1C'], l)
  69. " Invalid error lines test
  70. let l = []
  71. enew
  72. exe "silent! 27" . XdoCmd
  73. exe "silent! 4,5" . XdoCmd
  74. call assert_equal([], l)
  75. " Run commands from an unsaved buffer when 'hidden' is unset
  76. set nohidden
  77. let v:errmsg=''
  78. let l = []
  79. enew
  80. setlocal modified
  81. exe "silent! 2,2" . XdoCmd
  82. if v:errmsg !~# 'No write since last change'
  83. call add(v:errors, 'Unsaved file change test failed')
  84. endif
  85. " If the executed command fails, then the operation should be aborted
  86. enew!
  87. let subst_count = 0
  88. exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1"
  89. if subst_count != 1 || getline('.') != 'xLine1'
  90. call add(v:errors, 'Abort command on error test failed')
  91. endif
  92. set hidden
  93. let l = []
  94. exe "2,2" . Xdo . "! call add(l, GetRuler())"
  95. call assert_equal(['Xtestfile2 2L 2C'], l)
  96. " List with no valid error entries
  97. let l = []
  98. edit! +2 Xtestfile1
  99. exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']"
  100. exe XdoCmd
  101. call assert_equal([], l)
  102. exe "silent! 2" . XdoCmd
  103. call assert_equal([], l)
  104. let v:errmsg=''
  105. exe "%" . XdoCmd
  106. exe "1,$" . XdoCmd
  107. exe "." . XdoCmd
  108. call assert_equal('', v:errmsg)
  109. " List with only one valid entry
  110. let l = []
  111. exe Xgetexpr . " ['Xtestfile3:3:1:Line3']"
  112. exe XdoCmd
  113. call assert_equal(['Xtestfile3 3L 1C'], l)
  114. endfunction
  115. " Tests for the :cfdo and :lfdo commands
  116. function XfdoTests(cchar)
  117. enew
  118. " Shortcuts for calling the cfdo and lfdo commands
  119. let Xfdo = a:cchar . 'fdo'
  120. let Xgetexpr = a:cchar . 'getexpr'
  121. let XfdoCmd = Xfdo . ' call add(l, GetRuler())'
  122. let Xpfile = a:cchar. 'pfile'
  123. " Clear the quickfix/location list
  124. exe Xgetexpr . " []"
  125. " Try with an empty list
  126. let l = []
  127. exe XfdoCmd
  128. call assert_equal([], l)
  129. " Populate the list and then try
  130. exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'Xtestfile1:2:1:Line2', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:2:3:Line2', 'Xtestfile3:3:1:Line3']"
  131. let l = []
  132. exe XfdoCmd
  133. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  134. " Run command only on selected error lines
  135. let l = []
  136. exe "2,3" . XfdoCmd
  137. call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  138. " Boundary condition tests
  139. let l = []
  140. exe "3" . XfdoCmd
  141. call assert_equal(['Xtestfile3 2L 3C'], l)
  142. " Range test commands
  143. let l = []
  144. exe "%" . XfdoCmd
  145. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  146. let l = []
  147. exe "1,$" . XfdoCmd
  148. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  149. let l = []
  150. exe Xpfile
  151. exe "." . XfdoCmd
  152. call assert_equal(['Xtestfile2 2L 2C'], l)
  153. " List with only one valid entry
  154. let l = []
  155. exe Xgetexpr . " ['Xtestfile2:2:5:Line2']"
  156. exe XfdoCmd
  157. call assert_equal(['Xtestfile2 2L 5C'], l)
  158. endfunction
  159. ]=])
  160. end)
  161. after_each(function()
  162. os.remove('Xtestfile1')
  163. os.remove('Xtestfile2')
  164. os.remove('Xtestfile3')
  165. end)
  166. it('works for :cdo', function()
  167. -- call('XdoTests', 'c')
  168. feed(":call XdoTests('c')<CR><C-l>")
  169. expected_empty()
  170. end)
  171. it('works for :cfdo', function()
  172. -- call('XfdoTests', 'c')
  173. feed(":call XfdoTests('c')<CR><C-l>")
  174. expected_empty()
  175. end)
  176. it('works for :ldo', function()
  177. -- call('XdoTests', 'l')
  178. feed(":call XdoTests('l')<CR><C-l>")
  179. expected_empty()
  180. end)
  181. it('works for :lfdo', function()
  182. -- call('XfdoTests', 'l')
  183. feed(":call XfdoTests('l')<CR><C-l>")
  184. expected_empty()
  185. end)
  186. end)