xref: /vim-8.2.3635/src/testdir/test_cdo.vim (revision 14798ab9)
1" Tests for the :cdo, :cfdo, :ldo and :lfdo commands
2
3source check.vim
4CheckFeature quickfix
5
6" Create the files used by the tests
7func SetUp()
8  call writefile(["Line1", "Line2", "Line3"], 'Xtestfile1')
9  call writefile(["Line1", "Line2", "Line3"], 'Xtestfile2')
10  call writefile(["Line1", "Line2", "Line3"], 'Xtestfile3')
11endfunc
12
13" Remove the files used by the tests
14func TearDown()
15  call delete('Xtestfile1')
16  call delete('Xtestfile2')
17  call delete('Xtestfile3')
18endfunc
19
20" Returns the current line in '<filename> <linenum>L <column>C' format
21func GetRuler()
22  return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C'
23endfunc
24
25" Tests for the :cdo and :ldo commands
26func XdoTests(cchar)
27  enew
28
29  " Shortcuts for calling the cdo and ldo commands
30  let Xdo = a:cchar . 'do'
31  let Xgetexpr = a:cchar . 'getexpr'
32  let Xprev = a:cchar. 'prev'
33  let XdoCmd = Xdo . ' call add(l, GetRuler())'
34
35  " Try with an empty list
36  let l = []
37  exe XdoCmd
38  call assert_equal([], l)
39
40  " Populate the list and then try
41  exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:3:1:Line3']"
42
43  let l = []
44  exe XdoCmd
45  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
46
47  " Run command only on selected error lines
48  let l = []
49  enew
50  exe "2,3" . XdoCmd
51  call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
52
53  " Boundary condition tests
54  let l = []
55  enew
56  exe "1,1" . XdoCmd
57  call assert_equal(['Xtestfile1 1L 3C'], l)
58
59  let l = []
60  enew
61  exe "3" . XdoCmd
62  call assert_equal(['Xtestfile3 3L 1C'], l)
63
64  " Range test commands
65  let l = []
66  enew
67  exe "%" . XdoCmd
68  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
69
70  let l = []
71  enew
72  exe "1,$" . XdoCmd
73  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l)
74
75  let l = []
76  enew
77  exe Xprev
78  exe "." . XdoCmd
79  call assert_equal(['Xtestfile2 2L 2C'], l)
80
81  let l = []
82  enew
83  exe "+" . XdoCmd
84  call assert_equal(['Xtestfile3 3L 1C'], l)
85
86  " Invalid error lines test
87  let l = []
88  enew
89  exe "silent! 27" . XdoCmd
90  exe "silent! 4,5" . XdoCmd
91  call assert_equal([], l)
92
93  " Run commands from an unsaved buffer
94  let v:errmsg=''
95  let l = []
96  enew
97  setlocal modified
98  exe "silent! 2,2" . XdoCmd
99  if v:errmsg !~# 'No write since last change'
100    call add(v:errors, 'Unsaved file change test failed')
101  endif
102
103  " If the executed command fails, then the operation should be aborted
104  enew!
105  let subst_count = 0
106  exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1"
107  if subst_count != 1 || getline('.') != 'xLine1'
108    call add(v:errors, 'Abort command on error test failed')
109  endif
110
111  let l = []
112  exe "2,2" . Xdo . "! call add(l, GetRuler())"
113  call assert_equal(['Xtestfile2 2L 2C'], l)
114
115  " List with no valid error entries
116  let l = []
117  edit! +2 Xtestfile1
118  exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']"
119  exe XdoCmd
120  call assert_equal([], l)
121  exe "silent! 2" . XdoCmd
122  call assert_equal([], l)
123  let v:errmsg=''
124  exe "%" . XdoCmd
125  exe "1,$" . XdoCmd
126  exe "." . XdoCmd
127  call assert_equal('', v:errmsg)
128
129  " List with only one valid entry
130  let l = []
131  exe Xgetexpr . " ['Xtestfile3:3:1:Line3']"
132  exe XdoCmd
133  call assert_equal(['Xtestfile3 3L 1C'], l)
134
135endfunc
136
137" Tests for the :cfdo and :lfdo commands
138func XfdoTests(cchar)
139  enew
140
141  " Shortcuts for calling the cfdo and lfdo commands
142  let Xfdo = a:cchar . 'fdo'
143  let Xgetexpr = a:cchar . 'getexpr'
144  let XfdoCmd = Xfdo . ' call add(l, GetRuler())'
145  let Xpfile = a:cchar. 'pfile'
146
147  " Clear the quickfix/location list
148  exe Xgetexpr . " []"
149
150  " Try with an empty list
151  let l = []
152  exe XfdoCmd
153  call assert_equal([], l)
154
155  " Populate the list and then try
156  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']"
157
158  let l = []
159  exe XfdoCmd
160  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
161
162  " Run command only on selected error lines
163  let l = []
164  exe "2,3" . XfdoCmd
165  call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
166
167  " Boundary condition tests
168  let l = []
169  exe "3" . XfdoCmd
170  call assert_equal(['Xtestfile3 2L 3C'], l)
171
172  " Range test commands
173  let l = []
174  exe "%" . XfdoCmd
175  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
176
177  let l = []
178  exe "1,$" . XfdoCmd
179  call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l)
180
181  let l = []
182  exe Xpfile
183  exe "." . XfdoCmd
184  call assert_equal(['Xtestfile2 2L 2C'], l)
185
186  " List with only one valid entry
187  let l = []
188  exe Xgetexpr . " ['Xtestfile2:2:5:Line2']"
189  exe XfdoCmd
190  call assert_equal(['Xtestfile2 2L 5C'], l)
191
192endfunc
193
194" Tests for cdo and cfdo
195func Test_cdo()
196  call XdoTests('c')
197  call XfdoTests('c')
198endfunc
199
200" Tests for ldo and lfdo
201func Test_ldo()
202  call XdoTests('l')
203  call XfdoTests('l')
204endfunc
205
206" Test for making 'shm' doesn't interfere with the output.
207func Test_cdo_print()
208  enew | only!
209  cgetexpr ["Xtestfile1:1:Line1", "Xtestfile2:1:Line1", "Xtestfile3:1:Line1"]
210  cdo print
211  call assert_equal('Line1', Screenline(&lines))
212  call assert_equal('Line1', Screenline(&lines - 3))
213  call assert_equal('Line1', Screenline(&lines - 6))
214endfunc
215
216" vim: shiftwidth=2 sts=2 expandtab
217