1" Tests for multi-line regexps with ":s". 2 3source shared.vim 4 5func Test_multiline_subst() 6 enew! 7 call append(0, ["1 aa", 8 \ "bb", 9 \ "cc", 10 \ "2 dd", 11 \ "ee", 12 \ "3 ef", 13 \ "gh", 14 \ "4 ij", 15 \ "5 a8", 16 \ "8b c9", 17 \ "9d", 18 \ "6 e7", 19 \ "77f", 20 \ "xxxxx"]) 21 22 1 23 " test if replacing a line break works with a back reference 24 /^1/,/^2/s/\n\(.\)/ \1/ 25 " test if inserting a line break works with a back reference 26 /^3/,/^4/s/\(.\)$/\r\1/ 27 " test if replacing a line break with another line break works 28 /^5/,/^6/s/\(\_d\{3}\)/x\1x/ 29 call assert_equal('1 aa bb cc 2 dd ee', getline(1)) 30 call assert_equal('3 e', getline(2)) 31 call assert_equal('f', getline(3)) 32 call assert_equal('g', getline(4)) 33 call assert_equal('h', getline(5)) 34 call assert_equal('4 i', getline(6)) 35 call assert_equal('j', getline(7)) 36 call assert_equal('5 ax8', getline(8)) 37 call assert_equal('8xb cx9', getline(9)) 38 call assert_equal('9xd', getline(10)) 39 call assert_equal('6 ex7', getline(11)) 40 call assert_equal('7x7f', getline(12)) 41 call assert_equal('xxxxx', getline(13)) 42 enew! 43endfunc 44 45func Test_substitute_variants() 46 " Validate that all the 2-/3-letter variants which embed the flags into the 47 " command name actually work. 48 enew! 49 let ln = 'Testing string' 50 let variants = [ 51 \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' }, 52 \ { 'cmd': ':s/foo/bar/ce', 'exp': ln }, 53 \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' }, 54 \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' }, 55 \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' }, 56 \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' }, 57 \ { 'cmd': ':s/t/r/cn', 'exp': ln }, 58 \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' }, 59 \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' }, 60 \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' }, 61 \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' }, 62 \ { 'cmd': ':s/foo/bar/ge', 'exp': ln }, 63 \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' }, 64 \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' }, 65 \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' }, 66 \ { 'cmd': ':s/t/r/gn', 'exp': ln }, 67 \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' }, 68 \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' }, 69 \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' }, 70 \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' }, 71 \ { 'cmd': ':s/foo/bar/ie', 'exp': ln }, 72 \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' }, 73 \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' }, 74 \ { 'cmd': ':s/t/r/in', 'exp': ln }, 75 \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' }, 76 \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' }, 77 \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' }, 78 \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln }, 79 \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' }, 80 \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' }, 81 \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' }, 82 \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' }, 83 \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' }, 84 \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' }, 85 \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' }, 86 \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' }, 87 \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' }, 88 \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' }, 89 \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' }, 90 \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' }, 91 \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' }, 92 \ { 'cmd': ':s//r/r', 'exp': 'Testr string' }, 93 \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' }, 94 \] 95 96 for var in variants 97 for run in [1, 2] 98 let cmd = var.cmd 99 if run == 2 && cmd =~ "/.*/.*/." 100 " Change :s/from/to/{flags} to :s{flags} 101 let cmd = substitute(cmd, '/.*/', '', '') 102 endif 103 call setline(1, [ln]) 104 let msg = printf('using "%s"', cmd) 105 let @/='ing' 106 let v:errmsg = '' 107 call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx') 108 " No error should exist (matters for testing e flag) 109 call assert_equal('', v:errmsg, msg) 110 call assert_equal(var.exp, getline('.'), msg) 111 endfor 112 endfor 113endfunc 114 115" Test the l, p, # flags. 116func Test_substitute_flags_lp() 117 new 118 call setline(1, "abc\tdef\<C-h>ghi") 119 120 let a = execute('s/a/a/p') 121 call assert_equal("\nabc def^Hghi", a) 122 123 let a = execute('s/a/a/l') 124 call assert_equal("\nabc^Idef^Hghi$", a) 125 126 let a = execute('s/a/a/#') 127 call assert_equal("\n 1 abc def^Hghi", a) 128 129 let a = execute('s/a/a/p#') 130 call assert_equal("\n 1 abc def^Hghi", a) 131 132 let a = execute('s/a/a/l#') 133 call assert_equal("\n 1 abc^Idef^Hghi$", a) 134 135 let a = execute('s/a/a/') 136 call assert_equal("", a) 137 138 bwipe! 139endfunc 140 141func Test_substitute_repeat() 142 " This caused an invalid memory access. 143 split Xfile 144 s/^/x 145 call feedkeys("Qsc\<CR>y", 'tx') 146 bwipe! 147endfunc 148 149" Test %s/\n// which is implemented as a special case to use a 150" more efficient join rather than doing a regular substitution. 151func Test_substitute_join() 152 new 153 154 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 155 let a = execute('%s/\n//') 156 call assert_equal("", a) 157 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 158 call assert_equal('\n', histget("search", -1)) 159 160 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 161 let a = execute('%s/\n//g') 162 call assert_equal("", a) 163 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 164 call assert_equal('\n', histget("search", -1)) 165 166 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 167 let a = execute('%s/\n//p') 168 call assert_equal("\nfoo barbar^Hfoo", a) 169 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 170 call assert_equal('\n', histget("search", -1)) 171 172 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 173 let a = execute('%s/\n//l') 174 call assert_equal("\nfoo^Ibarbar^Hfoo$", a) 175 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 176 call assert_equal('\n', histget("search", -1)) 177 178 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 179 let a = execute('%s/\n//#') 180 call assert_equal("\n 1 foo barbar^Hfoo", a) 181 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 182 call assert_equal('\n', histget("search", -1)) 183 184 call setline(1, ['foo', 'bar', 'baz', 'qux']) 185 call execute('1,2s/\n//') 186 call assert_equal(['foobarbaz', 'qux'], getline(1, '$')) 187 188 bwipe! 189endfunc 190 191func Test_substitute_count() 192 new 193 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']) 194 2 195 196 s/foo/bar/3 197 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'], 198 \ getline(1, '$')) 199 200 call assert_fails('s/foo/bar/0', 'E939:') 201 202 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']) 203 2,4s/foo/bar/ 10 204 call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'], 205 \ getline(1, '$')) 206 207 bwipe! 208endfunc 209 210" Test substitute 'n' flag (report number of matches, do not substitute). 211func Test_substitute_flag_n() 212 new 213 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'] 214 call setline(1, lines) 215 216 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n')) 217 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn')) 218 219 " c flag (confirm) should be ignored when using n flag. 220 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc')) 221 222 " No substitution should have been done. 223 call assert_equal(lines, getline(1, '$')) 224 225 %delete _ 226 call setline(1, ['A', 'Bar', 'Baz']) 227 call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn')) 228 229 bwipe! 230endfunc 231 232func Test_substitute_errors() 233 new 234 call setline(1, 'foobar') 235 236 call assert_fails('s/FOO/bar/', 'E486:') 237 call assert_fails('s/foo/bar/@', 'E488:') 238 call assert_fails('s/\(/bar/', 'E54:') 239 call assert_fails('s afooabara', 'E146:') 240 call assert_fails('s\\a', 'E10:') 241 242 setl nomodifiable 243 call assert_fails('s/foo/bar/', 'E21:') 244 245 call assert_fails("let s=substitute([], 'a', 'A', 'g')", 'E730:') 246 call assert_fails("let s=substitute('abcda', [], 'A', 'g')", 'E730:') 247 call assert_fails("let s=substitute('abcda', 'a', [], 'g')", 'E730:') 248 call assert_fails("let s=substitute('abcda', 'a', 'A', [])", 'E730:') 249 call assert_fails("let s=substitute('abc', '\\%(', 'A', 'g')", 'E53:') 250 251 bwipe! 252endfunc 253 254" Test for *sub-replace-special* and *sub-replace-expression* on substitute(). 255func Test_sub_replace_1() 256 " Run the tests with 'magic' on 257 set magic 258 set cpo& 259 call assert_equal('AA', substitute('A', 'A', '&&', '')) 260 call assert_equal('&', substitute('B', 'B', '\&', '')) 261 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) 262 call assert_equal('d', substitute('D', 'D', 'd', '')) 263 call assert_equal('~', substitute('E', 'E', '~', '')) 264 call assert_equal('~', substitute('F', 'F', '\~', '')) 265 call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) 266 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) 267 call assert_equal('iI', substitute('I', 'I', '\lII', '')) 268 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) 269 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) 270 call assert_equal("l\<C-V>\<C-M>l", 271 \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) 272 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) 273 call assert_equal("n\<C-V>\<C-M>n", 274 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) 275 call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) 276 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) 277 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) 278 call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) 279 call assert_equal('scs', substitute('sSs', 'S', '\c', '')) 280 call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) 281 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) 282 call assert_equal("w\\w", substitute('wWw', 'W', "\\", '')) 283 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", '')) 284 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', '')) 285 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', '')) 286 " \v or \V after $ 287 call assert_equal('abxx', substitute('abcd', 'xy$\v|cd$', 'xx', '')) 288 call assert_equal('abxx', substitute('abcd', 'xy$\V\|cd\$', 'xx', '')) 289endfunc 290 291func Test_sub_replace_2() 292 " Run the tests with 'magic' off 293 set nomagic 294 set cpo& 295 call assert_equal('AA', substitute('A', 'A', '&&', '')) 296 call assert_equal('&', substitute('B', 'B', '\&', '')) 297 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) 298 call assert_equal('d', substitute('D', 'D', 'd', '')) 299 call assert_equal('~', substitute('E', 'E', '~', '')) 300 call assert_equal('~', substitute('F', 'F', '\~', '')) 301 call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) 302 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) 303 call assert_equal('iI', substitute('I', 'I', '\lII', '')) 304 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) 305 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) 306 call assert_equal("l\<C-V>\<C-M>l", 307 \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) 308 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) 309 call assert_equal("n\<C-V>\<C-M>n", 310 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) 311 call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) 312 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) 313 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) 314 call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) 315 call assert_equal('scs', substitute('sSs', 'S', '\c', '')) 316 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", '')) 317 call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) 318 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) 319 call assert_equal('w\w', substitute('wWw', 'W', "\\", '')) 320 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', '')) 321 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', '')) 322endfunc 323 324func Test_sub_replace_3() 325 set magic& 326 set cpo& 327 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', '')) 328 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', '')) 329 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", '')) 330 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", '')) 331 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", '')) 332 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', '')) 333 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', '')) 334 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', '')) 335 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', '')) 336endfunc 337 338" Test for submatch() on substitute(). 339func Test_sub_replace_4() 340 set magic& 341 set cpo& 342 call assert_equal('a\a', substitute('aAa', 'A', 343 \ '\=substitute(submatch(0), ".", "\\", "")', '')) 344 call assert_equal('b\b', substitute('bBb', 'B', 345 \ '\=substitute(submatch(0), ".", "\\\\", "")', '')) 346 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', '')) 347 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', '')) 348 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', '')) 349 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', '')) 350 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', '')) 351 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', '')) 352 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', '')) 353endfunc 354 355func Test_sub_replace_5() 356 set magic& 357 set cpo& 358 call assert_equal('A123456789987654321', substitute('A123456789', 359 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 360 \ '\=submatch(0) . submatch(9) . submatch(8) . ' . 361 \ 'submatch(7) . submatch(6) . submatch(5) . ' . 362 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)', 363 \ '')) 364 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " . 365 \ "['5'], ['4'], ['3'], ['2'], ['1']]", 366 \ substitute('A123456789', 367 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 368 \ '\=string([submatch(0, 1), submatch(9, 1), ' . 369 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' . 370 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' . 371 \ 'submatch(2, 1), submatch(1, 1)])', 372 \ '')) 373endfunc 374 375func Test_sub_replace_6() 376 set magic& 377 set cpo+=/ 378 call assert_equal('a', substitute('A', 'A', 'a', '')) 379 call assert_equal('%', substitute('B', 'B', '%', '')) 380 set cpo-=/ 381 call assert_equal('c', substitute('C', 'C', 'c', '')) 382 call assert_equal('%', substitute('D', 'D', '%', '')) 383endfunc 384 385func Test_sub_replace_7() 386 set magic& 387 set cpo& 388 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', '')) 389 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', '')) 390 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')) 391 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g')) 392 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g')) 393endfunc 394 395" Test for *:s%* on :substitute. 396func Test_sub_replace_8() 397 new 398 set magic& 399 set cpo& 400 $put =',,X' 401 s/\(^\|,\)\ze\(,\|X\)/\1N/g 402 call assert_equal('N,,NX', getline("$")) 403 $put =',,Y' 404 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc' 405 call feedkeys(cmd . "\<CR>a", "xt") 406 call assert_equal('N,,NY', getline("$")) 407 :$put =',,Z' 408 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc' 409 call feedkeys(cmd . "\<CR>yy", "xt") 410 call assert_equal('N,,NZ', getline("$")) 411 enew! | close 412endfunc 413 414func Test_sub_replace_9() 415 new 416 set magic& 417 set cpo& 418 $put ='xxx' 419 call feedkeys(":s/x/X/gc\<CR>yyq", "xt") 420 call assert_equal('XXx', getline("$")) 421 enew! | close 422endfunc 423 424func Test_sub_replace_10() 425 set magic& 426 set cpo& 427 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g')) 428 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g')) 429 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g')) 430 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g')) 431 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g')) 432 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g')) 433 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g')) 434 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g')) 435endfunc 436 437func SubReplacer(text, submatches) 438 return a:text .. a:submatches[0] .. a:text 439endfunc 440func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches) 441 return a:t3 .. a:submatches[0] .. a:t11 442endfunc 443 444func Test_substitute_partial() 445 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g')) 446 447 " 19 arguments plus one is just OK 448 let Replacer = function('SubReplacer20', repeat(['foo'], 19)) 449 call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g')) 450 451 " 20 arguments plus one is too many 452 let Replacer = function('SubReplacer20', repeat(['foo'], 20)) 453 call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:') 454endfunc 455 456" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. 457 458" Execute a list of :substitute command tests 459func Run_SubCmd_Tests(tests) 460 enew! 461 for t in a:tests 462 let start = line('.') + 1 463 let end = start + len(t[2]) - 1 464 " TODO: why is there a one second delay the first time we get here? 465 exe "normal o" . t[0] 466 call cursor(start, 1) 467 exe t[1] 468 call assert_equal(t[2], getline(start, end), t[1]) 469 endfor 470 enew! 471endfunc 472 473func Test_sub_cmd_1() 474 set magic 475 set cpo& 476 477 " List entry format: [input, cmd, output] 478 let tests = [['A', 's/A/&&/', ['AA']], 479 \ ['B', 's/B/\&/', ['&']], 480 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 481 \ ['D', 's/D/d/', ['d']], 482 \ ['E', 's/E/~/', ['d']], 483 \ ['F', 's/F/\~/', ['~']], 484 \ ['G', 's/G/\ugg/', ['Gg']], 485 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 486 \ ['I', 's/I/\lII/', ['iI']], 487 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 488 \ ['K', 's/K/\Uk\ek/', ['Kk']], 489 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 490 \ ['mMm', 's/M/\r/', ['m', 'm']], 491 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 492 \ ['oOo', 's/O/\n/', ["o\no"]], 493 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 494 \ ['qQq', 's/Q/\t/', ["q\tq"]], 495 \ ['rRr', 's/R/\\/', ['r\r']], 496 \ ['sSs', 's/S/\c/', ['scs']], 497 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 498 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 499 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 500 \ ] 501 call Run_SubCmd_Tests(tests) 502endfunc 503 504func Test_sub_cmd_2() 505 set nomagic 506 set cpo& 507 508 " List entry format: [input, cmd, output] 509 let tests = [['A', 's/A/&&/', ['&&']], 510 \ ['B', 's/B/\&/', ['B']], 511 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 512 \ ['D', 's/D/d/', ['d']], 513 \ ['E', 's/E/~/', ['~']], 514 \ ['F', 's/F/\~/', ['~']], 515 \ ['G', 's/G/\ugg/', ['Gg']], 516 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 517 \ ['I', 's/I/\lII/', ['iI']], 518 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 519 \ ['K', 's/K/\Uk\ek/', ['Kk']], 520 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 521 \ ['mMm', 's/M/\r/', ['m', 'm']], 522 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 523 \ ['oOo', 's/O/\n/', ["o\no"]], 524 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 525 \ ['qQq', 's/Q/\t/', ["q\tq"]], 526 \ ['rRr', 's/R/\\/', ['r\r']], 527 \ ['sSs', 's/S/\c/', ['scs']], 528 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 529 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 530 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 531 \ ] 532 call Run_SubCmd_Tests(tests) 533endfunc 534 535func Test_sub_cmd_3() 536 set nomagic 537 set cpo& 538 539 " List entry format: [input, cmd, output] 540 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']], 541 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']], 542 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']], 543 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']], 544 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']], 545 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']], 546 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']], 547 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']], 548 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']], 549 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']], 550 \ ['kKk', 's/K/\="\r"/', ['k', 'k']], 551 \ ['lLl', 's/L/\="\n"/', ['l', 'l']] 552 \ ] 553 call Run_SubCmd_Tests(tests) 554endfunc 555 556" Test for submatch() on :substitute. 557func Test_sub_cmd_4() 558 set magic& 559 set cpo& 560 561 " List entry format: [input, cmd, output] 562 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/", 563 \ ['a\a']], 564 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/", 565 \ ['b\b']], 566 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/", 567 \ ["c\<C-V>", 'c']], 568 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/", 569 \ ["d\<C-V>", 'd']], 570 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/", 571 \ ["e\\\<C-V>", 'e']], 572 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/", 573 \ ['f', 'f']], 574 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/', 575 \ ["g\<C-V>", 'g']], 576 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/', 577 \ ["h\<C-V>", 'h']], 578 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/', 579 \ ["i\\\<C-V>", 'i']], 580 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/", 581 \ ['j', 'j']], 582 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/", 583 \ ['k', 'k']], 584 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/", 585 \ ['l', 'l']], 586 \ ] 587 call Run_SubCmd_Tests(tests) 588endfunc 589 590func Test_sub_cmd_5() 591 set magic& 592 set cpo& 593 594 " List entry format: [input, cmd, output] 595 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']], 596 \ ['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']]"]], 597 \ ] 598 call Run_SubCmd_Tests(tests) 599endfunc 600 601" Test for *:s%* on :substitute. 602func Test_sub_cmd_6() 603 set magic& 604 set cpo+=/ 605 606 " List entry format: [input, cmd, output] 607 let tests = [ ['A', 's/A/a/', ['a']], 608 \ ['B', 's/B/%/', ['a']], 609 \ ] 610 call Run_SubCmd_Tests(tests) 611 612 set cpo-=/ 613 let tests = [ ['C', 's/C/c/', ['c']], 614 \ ['D', 's/D/%/', ['%']], 615 \ ] 616 call Run_SubCmd_Tests(tests) 617 618 set cpo& 619endfunc 620 621" Test for :s replacing \n with line break. 622func Test_sub_cmd_7() 623 set magic& 624 set cpo& 625 626 " List entry format: [input, cmd, output] 627 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']], 628 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']], 629 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]], 630 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]], 631 \ ["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']")]], 632 \ ] 633 call Run_SubCmd_Tests(tests) 634 635 exe "normal oQ\nQ\<Esc>k" 636 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486:') 637 enew! 638endfunc 639 640func TitleString() 641 let check = 'foo' =~ 'bar' 642 return "" 643endfunc 644 645func Test_sub_cmd_8() 646 set titlestring=%{TitleString()} 647 648 enew! 649 call append(0, ['', 'test_one', 'test_two']) 650 call cursor(1,1) 651 /^test_one/s/.*/\="foo\nbar"/ 652 call assert_equal('foo', getline(2)) 653 call assert_equal('bar', getline(3)) 654 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t") 655 call feedkeys("\<CR>y", "xt") 656 call assert_equal('foo', getline(4)) 657 call assert_equal('bar', getline(5)) 658 659 enew! 660 set titlestring& 661endfunc 662 663func Test_sub_cmd_9() 664 new 665 let input = ['1 aaa', '2 aaa', '3 aaa'] 666 call setline(1, input) 667 func Foo() 668 return submatch(0) 669 endfunc 670 %s/aaa/\=Foo()/gn 671 call assert_equal(input, getline(1, '$')) 672 call assert_equal(1, &modifiable) 673 674 delfunc Foo 675 bw! 676endfunc 677 678func Test_nocatch_sub_failure_handling() 679 " normal error results in all replacements 680 func Foo() 681 foobar 682 endfunc 683 new 684 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 685 %s/aaa/\=Foo()/g 686 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3)) 687 688 " Trow without try-catch causes abort after the first line. 689 " We cannot test this, since it would stop executing the test script. 690 691 " try/catch does not result in any changes 692 func! Foo() 693 throw 'error' 694 endfunc 695 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 696 let error_caught = 0 697 try 698 %s/aaa/\=Foo()/g 699 catch 700 let error_caught = 1 701 endtry 702 call assert_equal(1, error_caught) 703 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 704 705 " Same, but using "n" flag so that "sandbox" gets set 706 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 707 let error_caught = 0 708 try 709 %s/aaa/\=Foo()/gn 710 catch 711 let error_caught = 1 712 endtry 713 call assert_equal(1, error_caught) 714 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 715 716 delfunc Foo 717 bwipe! 718endfunc 719 720" Test ":s/pat/sub/" with different ~s in sub. 721func Test_replace_with_tilde() 722 new 723 " Set the last replace string to empty 724 s/^$// 725 call append(0, ['- Bug in "vPPPP" on this text:']) 726 normal gg 727 s/u/~u~/ 728 call assert_equal('- Bug in "vPPPP" on this text:', getline(1)) 729 s/i/~u~/ 730 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1)) 731 s/o/~~~/ 732 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1)) 733 close! 734endfunc 735 736func Test_replace_keeppatterns() 737 new 738 a 739foobar 740 741substitute foo asdf 742 743one two 744. 745 746 normal gg 747 /^substitute 748 s/foo/bar/ 749 call assert_equal('foo', @/) 750 call assert_equal('substitute bar asdf', getline('.')) 751 752 /^substitute 753 keeppatterns s/asdf/xyz/ 754 call assert_equal('^substitute', @/) 755 call assert_equal('substitute bar xyz', getline('.')) 756 757 exe "normal /bar /e\<CR>" 758 call assert_equal(15, col('.')) 759 normal - 760 keeppatterns /xyz 761 call assert_equal('bar ', @/) 762 call assert_equal('substitute bar xyz', getline('.')) 763 exe "normal 0dn" 764 call assert_equal('xyz', getline('.')) 765 766 close! 767endfunc 768 769func Test_sub_beyond_end() 770 new 771 call setline(1, '#') 772 let @/ = '^#\n\zs' 773 s///e 774 call assert_equal('#', getline(1)) 775 bwipe! 776endfunc 777 778" Test for repeating last substitution using :~ and :&r 779func Test_repeat_last_sub() 780 new 781 call setline(1, ['blue green yellow orange white']) 782 s/blue/red/ 783 let @/ = 'yellow' 784 ~ 785 let @/ = 'white' 786 :&r 787 let @/ = 'green' 788 s//gray 789 call assert_equal('red gray red orange red', getline(1)) 790 close! 791endfunc 792 793" Test for Vi compatible substitution: 794" \/{string}/, \?{string}? and \&{string}& 795func Test_sub_vi_compatibility() 796 new 797 call setline(1, ['blue green yellow orange blue']) 798 let @/ = 'orange' 799 s\/white/ 800 let @/ = 'blue' 801 s\?amber? 802 let @/ = 'white' 803 s\&green& 804 call assert_equal('amber green yellow white green', getline(1)) 805 close! 806endfunc 807 808" Test for substitute with the new text longer than the original text 809func Test_sub_expand_text() 810 new 811 call setline(1, 'abcabcabcabcabcabcabcabc') 812 s/b/\=repeat('B', 10)/g 813 call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1)) 814 close! 815endfunc 816 817" Test for command failures when the last substitute pattern is not set. 818func Test_sub_with_no_last_pat() 819 let lines =<< trim [SCRIPT] 820 call assert_fails('~', 'E33:') 821 call assert_fails('s//abc/g', 'E35:') 822 call assert_fails('s\/bar', 'E35:') 823 call assert_fails('s\&bar&', 'E33:') 824 call writefile(v:errors, 'Xresult') 825 qall! 826 [SCRIPT] 827 call writefile(lines, 'Xscript') 828 if RunVim([], [], '--clean -S Xscript') 829 call assert_equal([], readfile('Xresult')) 830 endif 831 832 let lines =<< trim [SCRIPT] 833 set cpo+=/ 834 call assert_fails('s/abc/%/', 'E33:') 835 call writefile(v:errors, 'Xresult') 836 qall! 837 [SCRIPT] 838 call writefile(lines, 'Xscript') 839 if RunVim([], [], '--clean -S Xscript') 840 call assert_equal([], readfile('Xresult')) 841 endif 842 843 call delete('Xscript') 844 call delete('Xresult') 845endfunc 846 847func Test_substitute() 848 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g')) 849 " Substitute with special keys 850 call assert_equal("a\<End>c", substitute('abc', "a.c", "a\<End>c", '')) 851endfunc 852 853func Test_substitute_expr() 854 let g:val = 'XXX' 855 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', '')) 856 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, '')) 857 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)', 858 \ '\=nr2char("0x" . submatch(1))', 'g')) 859 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)', 860 \ {-> nr2char("0x" . submatch(1))}, 'g')) 861 862 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)', 863 \ {-> submatch(2) . submatch(3) . submatch(1)}, '')) 864 865 func Recurse() 866 return substitute('yyy', 'y\(.\)y', {-> submatch(1)}, '') 867 endfunc 868 " recursive call works 869 call assert_equal('-y-x-', substitute('xxx', 'x\(.\)x', {-> '-' . Recurse() . '-' . submatch(1) . '-'}, '')) 870 871 call assert_fails("let s=submatch([])", 'E745:') 872 call assert_fails("let s=submatch(2, [])", 'E745:') 873endfunc 874 875func Test_invalid_submatch() 876 " This was causing invalid memory access in Vim-7.4.2232 and older 877 call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:') 878 call assert_fails('eval submatch(-1)', 'E935:') 879 call assert_equal('', submatch(0)) 880 call assert_equal('', submatch(1)) 881 call assert_equal([], submatch(0, 1)) 882 call assert_equal([], submatch(1, 1)) 883endfunc 884 885func Test_submatch_list_concatenate() 886 let pat = 'A\(.\)' 887 let Rep = {-> string([submatch(0, 1)] + [[submatch(1)]])} 888 call substitute('A1', pat, Rep, '')->assert_equal("[['A1'], ['1']]") 889endfunc 890 891func Test_substitute_expr_arg() 892 call assert_equal('123456789-123456789=', substitute('123456789', 893 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 894 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 895 896 call assert_equal('123456-123456=789', substitute('123456789', 897 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)', 898 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 899 900 call assert_equal('123456789-123456789x=', substitute('123456789', 901 \ '\(.\)\(.\)\(.*\)', 902 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 903 904 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:') 905 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:') 906 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:') 907 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:') 908endfunc 909 910" Test for using a function to supply the substitute string 911func Test_substitute_using_func() 912 func Xfunc() 913 return '1234' 914 endfunc 915 call assert_equal('a1234f', substitute('abcdef', 'b..e', 916 \ function("Xfunc"), '')) 917 delfunc Xfunc 918endfunc 919 920" Test for using submatch() with a multiline match 921func Test_substitute_multiline_submatch() 922 new 923 call setline(1, ['line1', 'line2', 'line3', 'line4']) 924 %s/^line1\(\_.\+\)line4$/\=submatch(1)/ 925 call assert_equal(['', 'line2', 'line3', ''], getline(1, '$')) 926 close! 927endfunc 928 929" vim: shiftwidth=2 sts=2 expandtab 930