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