blob: 8a6b7467182f21a5f7283f1a00f7c4372a06dbe4 [file] [log] [blame]
Bram Moolenaar57d79712015-12-28 14:04:47 +01001" Tests for the :cdo, :cfdo, :ldo and :lfdo commands
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
4CheckFeature quickfix
Bram Moolenaar57d79712015-12-28 14:04:47 +01005
6" Create the files used by the tests
7function SetUp()
8 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile1')
9 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile2')
10 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile3')
11endfunction
12
13" Remove the files used by the tests
14function TearDown()
15 call delete('Xtestfile1')
16 call delete('Xtestfile2')
17 call delete('Xtestfile3')
18endfunction
19
20" Returns the current line in '<filename> <linenum>L <column>C' format
21function GetRuler()
22 return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C'
23endfunction
24
25" Tests for the :cdo and :ldo commands
26function 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
135endfunction
136
137" Tests for the :cfdo and :lfdo commands
138function 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
192endfunction
193
194" Tests for cdo and cfdo
195function Test_cdo()
196 call XdoTests('c')
197 call XfdoTests('c')
198endfunction
199
200" Tests for ldo and lfdo
201function Test_ldo()
202 call XdoTests('l')
203 call XfdoTests('l')
204endfunction