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