1" Tests for multi-line regexps with ":s". 2 3func Test_multiline_subst() 4 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! 41endfunc 42 43func Test_substitute_variants() 44 " 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 108endfunc 109 110" Test the l, p, # flags. 111func Test_substitute_flags_lp() 112 new 113 call setline(1, "abc\tdef\<C-h>ghi") 114 115 let a = execute('s/a/a/p') 116 call assert_equal("\nabc def^Hghi", a) 117 118 let a = execute('s/a/a/l') 119 call assert_equal("\nabc^Idef^Hghi$", a) 120 121 let a = execute('s/a/a/#') 122 call assert_equal("\n 1 abc def^Hghi", a) 123 124 let a = execute('s/a/a/p#') 125 call assert_equal("\n 1 abc def^Hghi", a) 126 127 let a = execute('s/a/a/l#') 128 call assert_equal("\n 1 abc^Idef^Hghi$", a) 129 130 let a = execute('s/a/a/') 131 call assert_equal("", a) 132 133 bwipe! 134endfunc 135 136func Test_substitute_repeat() 137 " This caused an invalid memory access. 138 split Xfile 139 s/^/x 140 call feedkeys("Qsc\<CR>y", 'tx') 141 bwipe! 142endfunc 143 144" Test %s/\n// which is implemented as a special case to use a 145" more efficient join rather than doing a regular substitution. 146func Test_substitute_join() 147 new 148 149 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 150 let a = execute('%s/\n//') 151 call assert_equal("", a) 152 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 153 call assert_equal('\n', histget("search", -1)) 154 155 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 156 let a = execute('%s/\n//g') 157 call assert_equal("", a) 158 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 159 call assert_equal('\n', histget("search", -1)) 160 161 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 162 let a = execute('%s/\n//p') 163 call assert_equal("\nfoo barbar^Hfoo", a) 164 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 165 call assert_equal('\n', histget("search", -1)) 166 167 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 168 let a = execute('%s/\n//l') 169 call assert_equal("\nfoo^Ibarbar^Hfoo$", a) 170 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 171 call assert_equal('\n', histget("search", -1)) 172 173 call setline(1, ["foo\tbar", "bar\<C-H>foo"]) 174 let a = execute('%s/\n//#') 175 call assert_equal("\n 1 foo barbar^Hfoo", a) 176 call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) 177 call assert_equal('\n', histget("search", -1)) 178 179 bwipe! 180endfunc 181 182func Test_substitute_count() 183 new 184 call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']) 185 2 186 187 s/foo/bar/3 188 call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'], 189 \ getline(1, '$')) 190 191 call assert_fails('s/foo/bar/0', 'E939:') 192 193 bwipe! 194endfunc 195 196" Test substitute 'n' flag (report number of matches, do not substitute). 197func Test_substitute_flag_n() 198 new 199 let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'] 200 call setline(1, lines) 201 202 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n')) 203 call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn')) 204 205 " c flag (confirm) should be ignored when using n flag. 206 call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc')) 207 208 " No substitution should have been done. 209 call assert_equal(lines, getline(1, '$')) 210 211 bwipe! 212endfunc 213 214func Test_substitute_errors() 215 new 216 call setline(1, 'foobar') 217 218 call assert_fails('s/FOO/bar/', 'E486:') 219 call assert_fails('s/foo/bar/@', 'E488:') 220 call assert_fails('s/\(/bar/', 'E476:') 221 222 setl nomodifiable 223 call assert_fails('s/foo/bar/', 'E21:') 224 225 bwipe! 226endfunc 227 228" Test for *sub-replace-special* and *sub-replace-expression* on substitute(). 229func Test_sub_replace_1() 230 " Run the tests with 'magic' on 231 set magic 232 set cpo& 233 call assert_equal('AA', substitute('A', 'A', '&&', '')) 234 call assert_equal('&', substitute('B', 'B', '\&', '')) 235 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) 236 call assert_equal('d', substitute('D', 'D', 'd', '')) 237 call assert_equal('~', substitute('E', 'E', '~', '')) 238 call assert_equal('~', substitute('F', 'F', '\~', '')) 239 call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) 240 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) 241 call assert_equal('iI', substitute('I', 'I', '\lII', '')) 242 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) 243 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) 244 call assert_equal("l\<C-V>\<C-M>l", 245 \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) 246 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) 247 call assert_equal("n\<C-V>\<C-M>n", 248 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) 249 call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) 250 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) 251 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) 252 call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) 253 call assert_equal('scs', substitute('sSs', 'S', '\c', '')) 254 call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) 255 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) 256 call assert_equal("w\\w", substitute('wWw', 'W', "\\", '')) 257 call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", '')) 258 call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', '')) 259 call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', '')) 260endfunc 261 262func Test_sub_replace_2() 263 " Run the tests with 'magic' off 264 set nomagic 265 set cpo& 266 call assert_equal('AA', substitute('A', 'A', '&&', '')) 267 call assert_equal('&', substitute('B', 'B', '\&', '')) 268 call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) 269 call assert_equal('d', substitute('D', 'D', 'd', '')) 270 call assert_equal('~', substitute('E', 'E', '~', '')) 271 call assert_equal('~', substitute('F', 'F', '\~', '')) 272 call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) 273 call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) 274 call assert_equal('iI', substitute('I', 'I', '\lII', '')) 275 call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) 276 call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) 277 call assert_equal("l\<C-V>\<C-M>l", 278 \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) 279 call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) 280 call assert_equal("n\<C-V>\<C-M>n", 281 \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) 282 call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) 283 call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) 284 call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) 285 call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) 286 call assert_equal('scs', substitute('sSs', 'S', '\c', '')) 287 call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", '')) 288 call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) 289 call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) 290 call assert_equal('w\w', substitute('wWw', 'W', "\\", '')) 291 call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', '')) 292 call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', '')) 293endfunc 294 295func Test_sub_replace_3() 296 set magic& 297 set cpo& 298 call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', '')) 299 call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', '')) 300 call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", '')) 301 call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", '')) 302 call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", '')) 303 call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', '')) 304 call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', '')) 305 call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', '')) 306 call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', '')) 307endfunc 308 309" Test for submatch() on substitute(). 310func Test_sub_replace_4() 311 set magic& 312 set cpo& 313 call assert_equal('a\a', substitute('aAa', 'A', 314 \ '\=substitute(submatch(0), ".", "\\", "")', '')) 315 call assert_equal('b\b', substitute('bBb', 'B', 316 \ '\=substitute(submatch(0), ".", "\\\\", "")', '')) 317 call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', '')) 318 call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', '')) 319 call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', '')) 320 call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', '')) 321 call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', '')) 322 call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', '')) 323 call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', '')) 324endfunc 325 326func Test_sub_replace_5() 327 set magic& 328 set cpo& 329 call assert_equal('A123456789987654321', substitute('A123456789', 330 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 331 \ '\=submatch(0) . submatch(9) . submatch(8) . ' . 332 \ 'submatch(7) . submatch(6) . submatch(5) . ' . 333 \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)', 334 \ '')) 335 call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " . 336 \ "['5'], ['4'], ['3'], ['2'], ['1']]", 337 \ substitute('A123456789', 338 \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 339 \ '\=string([submatch(0, 1), submatch(9, 1), ' . 340 \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' . 341 \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' . 342 \ 'submatch(2, 1), submatch(1, 1)])', 343 \ '')) 344endfunc 345 346func Test_sub_replace_6() 347 set magic& 348 set cpo+=/ 349 call assert_equal('a', substitute('A', 'A', 'a', '')) 350 call assert_equal('%', substitute('B', 'B', '%', '')) 351 set cpo-=/ 352 call assert_equal('c', substitute('C', 'C', 'c', '')) 353 call assert_equal('%', substitute('D', 'D', '%', '')) 354endfunc 355 356func Test_sub_replace_7() 357 set magic& 358 set cpo& 359 call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', '')) 360 call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', '')) 361 call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')) 362 call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g')) 363 call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g')) 364endfunc 365 366" Test for *:s%* on :substitute. 367func Test_sub_replace_8() 368 new 369 set magic& 370 set cpo& 371 $put =',,X' 372 s/\(^\|,\)\ze\(,\|X\)/\1N/g 373 call assert_equal('N,,NX', getline("$")) 374 $put =',,Y' 375 let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc' 376 call feedkeys(cmd . "\<CR>a", "xt") 377 call assert_equal('N,,NY', getline("$")) 378 :$put =',,Z' 379 let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc' 380 call feedkeys(cmd . "\<CR>yy", "xt") 381 call assert_equal('N,,NZ', getline("$")) 382 enew! | close 383endfunc 384 385func Test_sub_replace_9() 386 new 387 set magic& 388 set cpo& 389 $put ='xxx' 390 call feedkeys(":s/x/X/gc\<CR>yyq", "xt") 391 call assert_equal('XXx', getline("$")) 392 enew! | close 393endfunc 394 395func Test_sub_replace_10() 396 set magic& 397 set cpo& 398 call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g')) 399 call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g')) 400 call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g')) 401 call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g')) 402 call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g')) 403 call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g')) 404 call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g')) 405 call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g')) 406endfunc 407 408func SubReplacer(text, submatches) 409 return a:text .. a:submatches[0] .. a:text 410endfunc 411func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches) 412 return a:t3 .. a:submatches[0] .. a:t11 413endfunc 414 415func Test_substitute_partial() 416 call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g')) 417 418 " 19 arguments plus one is just OK 419 let Replacer = function('SubReplacer20', repeat(['foo'], 19)) 420 call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g')) 421 422 " 20 arguments plus one is too many 423 let Replacer = function('SubReplacer20', repeat(['foo'], 20)) 424 call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118') 425endfunc 426 427" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. 428 429" Execute a list of :substitute command tests 430func Run_SubCmd_Tests(tests) 431 enew! 432 for t in a:tests 433 let start = line('.') + 1 434 let end = start + len(t[2]) - 1 435 " TODO: why is there a one second delay the first time we get here? 436 exe "normal o" . t[0] 437 call cursor(start, 1) 438 exe t[1] 439 call assert_equal(t[2], getline(start, end), t[1]) 440 endfor 441 enew! 442endfunc 443 444func Test_sub_cmd_1() 445 set magic 446 set cpo& 447 448 " List entry format: [input, cmd, output] 449 let tests = [['A', 's/A/&&/', ['AA']], 450 \ ['B', 's/B/\&/', ['&']], 451 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 452 \ ['D', 's/D/d/', ['d']], 453 \ ['E', 's/E/~/', ['d']], 454 \ ['F', 's/F/\~/', ['~']], 455 \ ['G', 's/G/\ugg/', ['Gg']], 456 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 457 \ ['I', 's/I/\lII/', ['iI']], 458 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 459 \ ['K', 's/K/\Uk\ek/', ['Kk']], 460 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 461 \ ['mMm', 's/M/\r/', ['m', 'm']], 462 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 463 \ ['oOo', 's/O/\n/', ["o\no"]], 464 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 465 \ ['qQq', 's/Q/\t/', ["q\tq"]], 466 \ ['rRr', 's/R/\\/', ['r\r']], 467 \ ['sSs', 's/S/\c/', ['scs']], 468 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 469 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 470 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 471 \ ] 472 call Run_SubCmd_Tests(tests) 473endfunc 474 475func Test_sub_cmd_2() 476 set nomagic 477 set cpo& 478 479 " List entry format: [input, cmd, output] 480 let tests = [['A', 's/A/&&/', ['&&']], 481 \ ['B', 's/B/\&/', ['B']], 482 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 483 \ ['D', 's/D/d/', ['d']], 484 \ ['E', 's/E/~/', ['~']], 485 \ ['F', 's/F/\~/', ['~']], 486 \ ['G', 's/G/\ugg/', ['Gg']], 487 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 488 \ ['I', 's/I/\lII/', ['iI']], 489 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 490 \ ['K', 's/K/\Uk\ek/', ['Kk']], 491 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 492 \ ['mMm', 's/M/\r/', ['m', 'm']], 493 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 494 \ ['oOo', 's/O/\n/', ["o\no"]], 495 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 496 \ ['qQq', 's/Q/\t/', ["q\tq"]], 497 \ ['rRr', 's/R/\\/', ['r\r']], 498 \ ['sSs', 's/S/\c/', ['scs']], 499 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 500 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 501 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 502 \ ] 503 call Run_SubCmd_Tests(tests) 504endfunc 505 506func Test_sub_cmd_3() 507 set nomagic 508 set cpo& 509 510 " List entry format: [input, cmd, output] 511 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']], 512 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']], 513 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']], 514 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']], 515 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']], 516 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']], 517 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']], 518 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']], 519 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']], 520 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']], 521 \ ['kKk', 's/K/\="\r"/', ['k', 'k']], 522 \ ['lLl', 's/L/\="\n"/', ['l', 'l']] 523 \ ] 524 call Run_SubCmd_Tests(tests) 525endfunc 526 527" Test for submatch() on :substitute. 528func Test_sub_cmd_4() 529 set magic& 530 set cpo& 531 532 " List entry format: [input, cmd, output] 533 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/", 534 \ ['a\a']], 535 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/", 536 \ ['b\b']], 537 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/", 538 \ ["c\<C-V>", 'c']], 539 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/", 540 \ ["d\<C-V>", 'd']], 541 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/", 542 \ ["e\\\<C-V>", 'e']], 543 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/", 544 \ ['f', 'f']], 545 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/', 546 \ ["g\<C-V>", 'g']], 547 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/', 548 \ ["h\<C-V>", 'h']], 549 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/', 550 \ ["i\\\<C-V>", 'i']], 551 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/", 552 \ ['j', 'j']], 553 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/", 554 \ ['k', 'k']], 555 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/", 556 \ ['l', 'l']], 557 \ ] 558 call Run_SubCmd_Tests(tests) 559endfunc 560 561func Test_sub_cmd_5() 562 set magic& 563 set cpo& 564 565 " List entry format: [input, cmd, output] 566 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']], 567 \ ['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']]"]], 568 \ ] 569 call Run_SubCmd_Tests(tests) 570endfunc 571 572" Test for *:s%* on :substitute. 573func Test_sub_cmd_6() 574 set magic& 575 set cpo+=/ 576 577 " List entry format: [input, cmd, output] 578 let tests = [ ['A', 's/A/a/', ['a']], 579 \ ['B', 's/B/%/', ['a']], 580 \ ] 581 call Run_SubCmd_Tests(tests) 582 583 set cpo-=/ 584 let tests = [ ['C', 's/C/c/', ['c']], 585 \ ['D', 's/D/%/', ['%']], 586 \ ] 587 call Run_SubCmd_Tests(tests) 588 589 set cpo& 590endfunc 591 592" Test for :s replacing \n with line break. 593func Test_sub_cmd_7() 594 set magic& 595 set cpo& 596 597 " List entry format: [input, cmd, output] 598 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']], 599 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']], 600 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]], 601 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]], 602 \ ["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']")]], 603 \ ] 604 call Run_SubCmd_Tests(tests) 605 606 exe "normal oQ\nQ\<Esc>k" 607 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486') 608 enew! 609endfunc 610 611func TitleString() 612 let check = 'foo' =~ 'bar' 613 return "" 614endfunc 615 616func Test_sub_cmd_8() 617 set titlestring=%{TitleString()} 618 619 enew! 620 call append(0, ['', 'test_one', 'test_two']) 621 call cursor(1,1) 622 /^test_one/s/.*/\="foo\nbar"/ 623 call assert_equal('foo', getline(2)) 624 call assert_equal('bar', getline(3)) 625 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t") 626 call feedkeys("\<CR>y", "xt") 627 call assert_equal('foo', getline(4)) 628 call assert_equal('bar', getline(5)) 629 630 enew! 631 set titlestring& 632endfunc 633 634func Test_sub_cmd_9() 635 new 636 let input = ['1 aaa', '2 aaa', '3 aaa'] 637 call setline(1, input) 638 func Foo() 639 return submatch(0) 640 endfunc 641 %s/aaa/\=Foo()/gn 642 call assert_equal(input, getline(1, '$')) 643 call assert_equal(1, &modifiable) 644 645 delfunc Foo 646 bw! 647endfunc 648 649func Test_nocatch_sub_failure_handling() 650 " normal error results in all replacements 651 func Foo() 652 foobar 653 endfunc 654 new 655 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 656 %s/aaa/\=Foo()/g 657 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3)) 658 659 " Trow without try-catch causes abort after the first line. 660 " We cannot test this, since it would stop executing the test script. 661 662 " try/catch does not result in any changes 663 func! Foo() 664 throw 'error' 665 endfunc 666 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 667 let error_caught = 0 668 try 669 %s/aaa/\=Foo()/g 670 catch 671 let error_caught = 1 672 endtry 673 call assert_equal(1, error_caught) 674 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 675 676 " Same, but using "n" flag so that "sandbox" gets set 677 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 678 let error_caught = 0 679 try 680 %s/aaa/\=Foo()/gn 681 catch 682 let error_caught = 1 683 endtry 684 call assert_equal(1, error_caught) 685 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 686 687 delfunc Foo 688 bwipe! 689endfunc 690 691" Test ":s/pat/sub/" with different ~s in sub. 692func Test_replace_with_tilde() 693 new 694 " Set the last replace string to empty 695 s/^$// 696 call append(0, ['- Bug in "vPPPP" on this text:']) 697 normal gg 698 s/u/~u~/ 699 call assert_equal('- Bug in "vPPPP" on this text:', getline(1)) 700 s/i/~u~/ 701 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1)) 702 s/o/~~~/ 703 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1)) 704 close! 705endfunc 706 707func Test_replace_keeppatterns() 708 new 709 a 710foobar 711 712substitute foo asdf 713 714one two 715. 716 717 normal gg 718 /^substitute 719 s/foo/bar/ 720 call assert_equal('foo', @/) 721 call assert_equal('substitute bar asdf', getline('.')) 722 723 /^substitute 724 keeppatterns s/asdf/xyz/ 725 call assert_equal('^substitute', @/) 726 call assert_equal('substitute bar xyz', getline('.')) 727 728 exe "normal /bar /e\<CR>" 729 call assert_equal(15, col('.')) 730 normal - 731 keeppatterns /xyz 732 call assert_equal('bar ', @/) 733 call assert_equal('substitute bar xyz', getline('.')) 734 exe "normal 0dn" 735 call assert_equal('xyz', getline('.')) 736 737 close! 738endfunc 739 740func Test_sub_beyond_end() 741 new 742 call setline(1, '#') 743 let @/ = '^#\n\zs' 744 s///e 745 call assert_equal('#', getline(1)) 746 bwipe! 747endfunc 748