blob: dbed7df4ac9f84a48d9d7d6418f7553df13de259 [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
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02007func SetUp()
Bram Moolenaar57d79712015-12-28 14:04:47 +01008 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile1')
9 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile2')
10 call writefile(["Line1", "Line2", "Line3"], 'Xtestfile3')
Bram Moolenaar14798ab2020-05-28 21:30:11 +020011endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +010012
13" Remove the files used by the tests
Bram Moolenaar14798ab2020-05-28 21:30:11 +020014func TearDown()
Bram Moolenaar57d79712015-12-28 14:04:47 +010015 call delete('Xtestfile1')
16 call delete('Xtestfile2')
17 call delete('Xtestfile3')
Bram Moolenaar14798ab2020-05-28 21:30:11 +020018endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +010019
20" Returns the current line in '<filename> <linenum>L <column>C' format
Bram Moolenaar14798ab2020-05-28 21:30:11 +020021func GetRuler()
Bram Moolenaar57d79712015-12-28 14:04:47 +010022 return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C'
Bram Moolenaar14798ab2020-05-28 21:30:11 +020023endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +010024
25" Tests for the :cdo and :ldo commands
Bram Moolenaar14798ab2020-05-28 21:30:11 +020026func XdoTests(cchar)
Bram Moolenaar57d79712015-12-28 14:04:47 +010027 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
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200135endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +0100136
137" Tests for the :cfdo and :lfdo commands
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200138func XfdoTests(cchar)
Bram Moolenaar57d79712015-12-28 14:04:47 +0100139 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
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200192endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +0100193
194" Tests for cdo and cfdo
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200195func Test_cdo()
Bram Moolenaar57d79712015-12-28 14:04:47 +0100196 call XdoTests('c')
197 call XfdoTests('c')
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200198endfunc
Bram Moolenaar57d79712015-12-28 14:04:47 +0100199
200" Tests for ldo and lfdo
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200201func Test_ldo()
Bram Moolenaar57d79712015-12-28 14:04:47 +0100202 call XdoTests('l')
203 call XfdoTests('l')
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200204endfunc
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