cdo_spec.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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
  76. let v:errmsg=''
  77. let l = []
  78. enew
  79. setlocal modified
  80. exe "silent! 2,2" . XdoCmd
  81. if v:errmsg !~# 'No write since last change'
  82. call add(v:errors, 'Unsaved file change test failed')
  83. endif
  84. " If the executed command fails, then the operation should be aborted
  85. enew!
  86. let subst_count = 0
  87. exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1"
  88. if subst_count != 1 || getline('.') != 'xLine1'
  89. call add(v:errors, 'Abort command on error test failed')
  90. endif
  91. let l = []
  92. exe "2,2" . Xdo . "! call add(l, GetRuler())"
  93. call assert_equal(['Xtestfile2 2L 2C'], l)
  94. " List with no valid error entries
  95. let l = []
  96. edit! +2 Xtestfile1
  97. exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']"
  98. exe XdoCmd
  99. call assert_equal([], l)
  100. exe "silent! 2" . XdoCmd
  101. call assert_equal([], l)
  102. let v:errmsg=''
  103. exe "%" . XdoCmd
  104. exe "1,$" . XdoCmd
  105. exe "." . XdoCmd
  106. call assert_equal('', v:errmsg)
  107. " List with only one valid entry
  108. let l = []
  109. exe Xgetexpr . " ['Xtestfile3:3:1:Line3']"
  110. exe XdoCmd
  111. call assert_equal(['Xtestfile3 3L 1C'], l)
  112. endfunction
  113. " Tests for the :cfdo and :lfdo commands
  114. function XfdoTests(cchar)
  115. enew
  116. " Shortcuts for calling the cfdo and lfdo commands
  117. let Xfdo = a:cchar . 'fdo'
  118. let Xgetexpr = a:cchar . 'getexpr'
  119. let XfdoCmd = Xfdo . ' call add(l, GetRuler())'
  120. let Xpfile = a:cchar. 'pfile'
  121. " Clear the quickfix/location list
  122. exe Xgetexpr . " []"
  123. " Try with an empty list
  124. let l = []
  125. exe XfdoCmd
  126. call assert_equal([], l)
  127. " Populate the list and then try
  128. 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']"
  129. let l = []
  130. exe XfdoCmd
  131. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  132. " Run command only on selected error lines
  133. let l = []
  134. exe "2,3" . XfdoCmd
  135. call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  136. " Boundary condition tests
  137. let l = []
  138. exe "3" . XfdoCmd
  139. call assert_equal(['Xtestfile3 2L 3C'], l)
  140. " Range test commands
  141. let l = []
  142. exe "%" . XfdoCmd
  143. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  144. let l = []
  145. exe "1,$" . XfdoCmd
  146. call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
  147. let l = []
  148. exe Xpfile
  149. exe "." . XfdoCmd
  150. call assert_equal(['Xtestfile2 2L 2C'], l)
  151. " List with only one valid entry
  152. let l = []
  153. exe Xgetexpr . " ['Xtestfile2:2:5:Line2']"
  154. exe XfdoCmd
  155. call assert_equal(['Xtestfile2 2L 5C'], l)
  156. endfunction
  157. ]=])
  158. end)
  159. after_each(function()
  160. os.remove('Xtestfile1')
  161. os.remove('Xtestfile2')
  162. os.remove('Xtestfile3')
  163. end)
  164. it('works for :cdo', function()
  165. -- call('XdoTests', 'c')
  166. feed(":call XdoTests('c')<CR><C-l>")
  167. expected_empty()
  168. end)
  169. it('works for :cfdo', function()
  170. -- call('XfdoTests', 'c')
  171. feed(":call XfdoTests('c')<CR><C-l>")
  172. expected_empty()
  173. end)
  174. it('works for :ldo', function()
  175. -- call('XdoTests', 'l')
  176. feed(":call XdoTests('l')<CR><C-l>")
  177. expected_empty()
  178. end)
  179. it('works for :lfdo', function()
  180. -- call('XfdoTests', 'l')
  181. feed(":call XfdoTests('l')<CR><C-l>")
  182. expected_empty()
  183. end)
  184. end)