blob: d84daa72121590e41d0e342e267ac4e11d80f01c [file] [log] [blame]
Bram Moolenaarcd055da2016-09-02 19:50:48 +02001" Tests for multi-line regexps with ":s".
2
Bram Moolenaar1e115362019-01-09 23:01:02 +01003func Test_multiline_subst()
Bram Moolenaarcd055da2016-09-02 19:50:48 +02004 enew!
5 call append(0, ["1 aa",
6 \ "bb",
7 \ "cc",
8 \ "2 dd",
9 \ "ee",
10 \ "3 ef",
11 \ "gh",
12 \ "4 ij",
13 \ "5 a8",
14 \ "8b c9",
15 \ "9d",
16 \ "6 e7",
17 \ "77f",
18 \ "xxxxx"])
19
20 1
21 " test if replacing a line break works with a back reference
22 /^1/,/^2/s/\n\(.\)/ \1/
23 " test if inserting a line break works with a back reference
24 /^3/,/^4/s/\(.\)$/\r\1/
25 " test if replacing a line break with another line break works
26 /^5/,/^6/s/\(\_d\{3}\)/x\1x/
27 call assert_equal('1 aa bb cc 2 dd ee', getline(1))
28 call assert_equal('3 e', getline(2))
29 call assert_equal('f', getline(3))
30 call assert_equal('g', getline(4))
31 call assert_equal('h', getline(5))
32 call assert_equal('4 i', getline(6))
33 call assert_equal('j', getline(7))
34 call assert_equal('5 ax8', getline(8))
35 call assert_equal('8xb cx9', getline(9))
36 call assert_equal('9xd', getline(10))
37 call assert_equal('6 ex7', getline(11))
38 call assert_equal('7x7f', getline(12))
39 call assert_equal('xxxxx', getline(13))
40 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010041endfunc
Bram Moolenaar8c50d502017-02-17 18:28:24 +010042
Bram Moolenaar1e115362019-01-09 23:01:02 +010043func Test_substitute_variants()
Bram Moolenaar8c50d502017-02-17 18:28:24 +010044 " Validate that all the 2-/3-letter variants which embed the flags into the
45 " command name actually work.
46 enew!
47 let ln = 'Testing string'
48 let variants = [
49 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
50 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
51 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
52 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
53 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
54 \ { 'cmd': ':s/t/r/cn', 'exp': ln },
55 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
56 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
57 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
58 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
59 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
60 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
61 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
62 \ { 'cmd': ':s/t/r/gn', 'exp': ln },
63 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
64 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
65 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
66 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
67 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
68 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
69 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
70 \ { 'cmd': ':s/t/r/in', 'exp': ln },
71 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
72 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
73 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
74 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
75 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
76 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
77 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
78 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
79 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
80 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
81 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
82 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
83 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
84 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
85 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
86 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
87 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
88 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
89 \]
90
91 for var in variants
92 for run in [1, 2]
93 let cmd = var.cmd
94 if run == 2 && cmd =~ "/.*/.*/."
95 " Change :s/from/to/{flags} to :s{flags}
96 let cmd = substitute(cmd, '/.*/', '', '')
97 endif
98 call setline(1, [ln])
99 let msg = printf('using "%s"', cmd)
100 let @/='ing'
101 let v:errmsg = ''
102 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
103 " No error should exist (matters for testing e flag)
104 call assert_equal('', v:errmsg, msg)
105 call assert_equal(var.exp, getline('.'), msg)
106 endfor
107 endfor
Bram Moolenaar1e115362019-01-09 23:01:02 +0100108endfunc
Bram Moolenaarba748c82017-02-26 14:00:07 +0100109
110func Test_substitute_repeat()
111 " This caused an invalid memory access.
112 split Xfile
113 s/^/x
114 call feedkeys("Qsc\<CR>y", 'tx')
115 bwipe!
116endfunc
Bram Moolenaar1a333bc2017-08-30 20:21:58 +0200117
118" Test for *sub-replace-special* and *sub-replace-expression* on substitute().
119func Test_sub_replace_1()
120 " Run the tests with 'magic' on
121 set magic
122 set cpo&
123 call assert_equal('AA', substitute('A', 'A', '&&', ''))
124 call assert_equal('&', substitute('B', 'B', '\&', ''))
125 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
126 call assert_equal('d', substitute('D', 'D', 'd', ''))
127 call assert_equal('~', substitute('E', 'E', '~', ''))
128 call assert_equal('~', substitute('F', 'F', '\~', ''))
129 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
130 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
131 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
132 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
133 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
134 call assert_equal("l\<C-V>\<C-M>l",
135 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
136 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
137 call assert_equal("n\<C-V>\<C-M>n",
138 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
139 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
140 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
141 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
142 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
143 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
144 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
145 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
146 call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
147 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
148 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
149 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
150endfunc
151
152func Test_sub_replace_2()
153 " Run the tests with 'magic' off
154 set nomagic
155 set cpo&
156 call assert_equal('AA', substitute('A', 'A', '&&', ''))
157 call assert_equal('&', substitute('B', 'B', '\&', ''))
158 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
159 call assert_equal('d', substitute('D', 'D', 'd', ''))
160 call assert_equal('~', substitute('E', 'E', '~', ''))
161 call assert_equal('~', substitute('F', 'F', '\~', ''))
162 call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
163 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
164 call assert_equal('iI', substitute('I', 'I', '\lII', ''))
165 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
166 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
167 call assert_equal("l\<C-V>\<C-M>l",
168 \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
169 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
170 call assert_equal("n\<C-V>\<C-M>n",
171 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
172 call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
173 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
174 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
175 call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
176 call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
177 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
178 call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
179 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
180 call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
181 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
182 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
183endfunc
184
185func Test_sub_replace_3()
186 set magic&
187 set cpo&
188 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
189 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
190 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
191 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
192 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
193 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
194 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
195 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
196 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
197endfunc
198
199" Test for submatch() on substitute().
200func Test_sub_replace_4()
201 set magic&
202 set cpo&
203 call assert_equal('a\a', substitute('aAa', 'A',
204 \ '\=substitute(submatch(0), ".", "\\", "")', ''))
205 call assert_equal('b\b', substitute('bBb', 'B',
206 \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
207 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
208 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
209 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
210 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
211 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
212 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
213 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
214endfunc
215
216func Test_sub_replace_5()
217 set magic&
218 set cpo&
219 call assert_equal('A123456789987654321', substitute('A123456789',
220 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
221 \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
222 \ 'submatch(7) . submatch(6) . submatch(5) . ' .
223 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
224 \ ''))
225 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
226 \ "['5'], ['4'], ['3'], ['2'], ['1']]",
227 \ substitute('A123456789',
228 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
229 \ '\=string([submatch(0, 1), submatch(9, 1), ' .
230 \ 'submatch(8, 1), submatch(7, 1), submatch(6, 1), ' .
231 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
232 \ 'submatch(2, 1), submatch(1, 1)])',
233 \ ''))
234endfunc
235
236func Test_sub_replace_6()
237 set magic&
238 set cpo+=/
239 call assert_equal('a', substitute('A', 'A', 'a', ''))
240 call assert_equal('%', substitute('B', 'B', '%', ''))
241 set cpo-=/
242 call assert_equal('c', substitute('C', 'C', 'c', ''))
243 call assert_equal('%', substitute('D', 'D', '%', ''))
244endfunc
245
246func Test_sub_replace_7()
247 set magic&
248 set cpo&
249 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
250 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
251 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
252 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
253 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
254endfunc
255
256" Test for *:s%* on :substitute.
257func Test_sub_replace_8()
258 new
259 set magic&
260 set cpo&
261 $put =',,X'
262 s/\(^\|,\)\ze\(,\|X\)/\1N/g
263 call assert_equal('N,,NX', getline("$"))
264 $put =',,Y'
265 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
266 call feedkeys(cmd . "\<CR>a", "xt")
267 call assert_equal('N,,NY', getline("$"))
268 :$put =',,Z'
269 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
270 call feedkeys(cmd . "\<CR>yy", "xt")
271 call assert_equal('N,,NZ', getline("$"))
272 enew! | close
273endfunc
274
275func Test_sub_replace_9()
276 new
277 set magic&
278 set cpo&
279 $put ='xxx'
280 call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
281 call assert_equal('XXx', getline("$"))
282 enew! | close
283endfunc
284
285func Test_sub_replace_10()
286 set magic&
287 set cpo&
288 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
289 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
290 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
291 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
292 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
293 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
294 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
295 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
296endfunc
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200297
298" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
299
300" Execute a list of :substitute command tests
301func Run_SubCmd_Tests(tests)
302 enew!
303 for t in a:tests
304 let start = line('.') + 1
305 let end = start + len(t[2]) - 1
306 exe "normal o" . t[0]
307 call cursor(start, 1)
308 exe t[1]
309 call assert_equal(t[2], getline(start, end), t[1])
310 endfor
311 enew!
312endfunc
313
314func Test_sub_cmd_1()
315 set magic
316 set cpo&
317
318 " List entry format: [input, cmd, output]
319 let tests = [['A', 's/A/&&/', ['AA']],
320 \ ['B', 's/B/\&/', ['&']],
321 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
322 \ ['D', 's/D/d/', ['d']],
323 \ ['E', 's/E/~/', ['d']],
324 \ ['F', 's/F/\~/', ['~']],
325 \ ['G', 's/G/\ugg/', ['Gg']],
326 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
327 \ ['I', 's/I/\lII/', ['iI']],
328 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
329 \ ['K', 's/K/\Uk\ek/', ['Kk']],
330 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
331 \ ['mMm', 's/M/\r/', ['m', 'm']],
332 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
333 \ ['oOo', 's/O/\n/', ["o\no"]],
334 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
335 \ ['qQq', 's/Q/\t/', ["q\tq"]],
336 \ ['rRr', 's/R/\\/', ['r\r']],
337 \ ['sSs', 's/S/\c/', ['scs']],
338 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
339 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
340 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
341 \ ]
342 call Run_SubCmd_Tests(tests)
343endfunc
344
345func Test_sub_cmd_2()
346 set nomagic
347 set cpo&
348
349 " List entry format: [input, cmd, output]
350 let tests = [['A', 's/A/&&/', ['&&']],
351 \ ['B', 's/B/\&/', ['B']],
352 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
353 \ ['D', 's/D/d/', ['d']],
354 \ ['E', 's/E/~/', ['~']],
355 \ ['F', 's/F/\~/', ['~']],
356 \ ['G', 's/G/\ugg/', ['Gg']],
357 \ ['H', 's/H/\Uh\Eh/', ['Hh']],
358 \ ['I', 's/I/\lII/', ['iI']],
359 \ ['J', 's/J/\LJ\EJ/', ['jJ']],
360 \ ['K', 's/K/\Uk\ek/', ['Kk']],
361 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
362 \ ['mMm', 's/M/\r/', ['m', 'm']],
363 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
364 \ ['oOo', 's/O/\n/', ["o\no"]],
365 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
366 \ ['qQq', 's/Q/\t/', ["q\tq"]],
367 \ ['rRr', 's/R/\\/', ['r\r']],
368 \ ['sSs', 's/S/\c/', ['scs']],
369 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
370 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
371 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']]
372 \ ]
373 call Run_SubCmd_Tests(tests)
374endfunc
375
376func Test_sub_cmd_3()
377 set nomagic
378 set cpo&
379
380 " List entry format: [input, cmd, output]
381 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
382 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
383 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
384 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
385 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
386 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
387 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
388 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
389 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
390 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
391 \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
392 \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
393 \ ]
394 call Run_SubCmd_Tests(tests)
395endfunc
396
397" Test for submatch() on :substitue.
398func Test_sub_cmd_4()
399 set magic&
400 set cpo&
401
402 " List entry format: [input, cmd, output]
403 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100404 \ ['a\a']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200405 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
Bram Moolenaar1e115362019-01-09 23:01:02 +0100406 \ ['b\b']],
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200407 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
408 \ ["c\<C-V>", 'c']],
409 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
410 \ ["d\<C-V>", 'd']],
411 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
412 \ ["e\\\<C-V>", 'e']],
413 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
414 \ ['f', 'f']],
415 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
416 \ ["g\<C-V>", 'g']],
417 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
418 \ ["h\<C-V>", 'h']],
419 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
420 \ ["i\\\<C-V>", 'i']],
421 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
422 \ ['j', 'j']],
423 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
424 \ ['k', 'k']],
425 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
426 \ ['l', 'l']],
427 \ ]
428 call Run_SubCmd_Tests(tests)
429endfunc
430
431func Test_sub_cmd_5()
432 set magic&
433 set cpo&
434
435 " List entry format: [input, cmd, output]
436 let tests = [ ['A123456789', 's/A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . submatch(6) . submatch(5) . submatch(4) . submatch(3) . submatch(2) . submatch(1)/', ['A123456789987654321']],
437 \ ['B123456789', 's/B\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), submatch(3, 1), submatch(2, 1), submatch(1, 1)])/', ["[['B123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], ['2'], ['1']]"]],
438 \ ]
439 call Run_SubCmd_Tests(tests)
440endfunc
441
442" Test for *:s%* on :substitute.
443func Test_sub_cmd_6()
444 set magic&
445 set cpo+=/
446
447 " List entry format: [input, cmd, output]
448 let tests = [ ['A', 's/A/a/', ['a']],
449 \ ['B', 's/B/%/', ['a']],
450 \ ]
451 call Run_SubCmd_Tests(tests)
452
453 set cpo-=/
454 let tests = [ ['C', 's/C/c/', ['c']],
455 \ ['D', 's/D/%/', ['%']],
456 \ ]
457 call Run_SubCmd_Tests(tests)
458
459 set cpo&
460endfunc
461
462" Test for :s replacing \n with line break.
463func Test_sub_cmd_7()
464 set magic&
465 set cpo&
466
467 " List entry format: [input, cmd, output]
468 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
469 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
470 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
471 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
472 \ ["E\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>E", 's/E\_.\{-}E/\=strtrans(string(submatch(0, 1)))/', [strtrans("['E\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>E']")]],
473 \ ]
474 call Run_SubCmd_Tests(tests)
475
476 exe "normal oQ\nQ\<Esc>k"
477 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
478 enew!
479endfunc
480
481func TitleString()
482 let check = 'foo' =~ 'bar'
483 return ""
484endfunc
485
486func Test_sub_cmd_8()
487 set titlestring=%{TitleString()}
488
489 enew!
490 call append(0, ['', 'test_one', 'test_two'])
491 call cursor(1,1)
492 /^test_one/s/.*/\="foo\nbar"/
493 call assert_equal('foo', getline(2))
494 call assert_equal('bar', getline(3))
495 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
496 call feedkeys("\<CR>y", "xt")
497 call assert_equal('foo', getline(4))
498 call assert_equal('bar', getline(5))
499
500 enew!
501 set titlestring&
502endfunc