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