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 408" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. 409 410" Execute a list of :substitute command tests 411func Run_SubCmd_Tests(tests) 412 enew! 413 for t in a:tests 414 let start = line('.') + 1 415 let end = start + len(t[2]) - 1 416 " TODO: why is there a one second delay the first time we get here? 417 exe "normal o" . t[0] 418 call cursor(start, 1) 419 exe t[1] 420 call assert_equal(t[2], getline(start, end), t[1]) 421 endfor 422 enew! 423endfunc 424 425func Test_sub_cmd_1() 426 set magic 427 set cpo& 428 429 " List entry format: [input, cmd, output] 430 let tests = [['A', 's/A/&&/', ['AA']], 431 \ ['B', 's/B/\&/', ['&']], 432 \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 433 \ ['D', 's/D/d/', ['d']], 434 \ ['E', 's/E/~/', ['d']], 435 \ ['F', 's/F/\~/', ['~']], 436 \ ['G', 's/G/\ugg/', ['Gg']], 437 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 438 \ ['I', 's/I/\lII/', ['iI']], 439 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 440 \ ['K', 's/K/\Uk\ek/', ['Kk']], 441 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 442 \ ['mMm', 's/M/\r/', ['m', 'm']], 443 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 444 \ ['oOo', 's/O/\n/', ["o\no"]], 445 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 446 \ ['qQq', 's/Q/\t/', ["q\tq"]], 447 \ ['rRr', 's/R/\\/', ['r\r']], 448 \ ['sSs', 's/S/\c/', ['scs']], 449 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 450 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 451 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 452 \ ] 453 call Run_SubCmd_Tests(tests) 454endfunc 455 456func Test_sub_cmd_2() 457 set nomagic 458 set cpo& 459 460 " List entry format: [input, cmd, output] 461 let tests = [['A', 's/A/&&/', ['&&']], 462 \ ['B', 's/B/\&/', ['B']], 463 \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']], 464 \ ['D', 's/D/d/', ['d']], 465 \ ['E', 's/E/~/', ['~']], 466 \ ['F', 's/F/\~/', ['~']], 467 \ ['G', 's/G/\ugg/', ['Gg']], 468 \ ['H', 's/H/\Uh\Eh/', ['Hh']], 469 \ ['I', 's/I/\lII/', ['iI']], 470 \ ['J', 's/J/\LJ\EJ/', ['jJ']], 471 \ ['K', 's/K/\Uk\ek/', ['Kk']], 472 \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']], 473 \ ['mMm', 's/M/\r/', ['m', 'm']], 474 \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']], 475 \ ['oOo', 's/O/\n/', ["o\no"]], 476 \ ['pPp', 's/P/\b/', ["p\<C-H>p"]], 477 \ ['qQq', 's/Q/\t/', ["q\tq"]], 478 \ ['rRr', 's/R/\\/', ['r\r']], 479 \ ['sSs', 's/S/\c/', ['scs']], 480 \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]], 481 \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']], 482 \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']] 483 \ ] 484 call Run_SubCmd_Tests(tests) 485endfunc 486 487func Test_sub_cmd_3() 488 set nomagic 489 set cpo& 490 491 " List entry format: [input, cmd, output] 492 let tests = [['aAa', "s/A/\\='\\'/", ['a\a']], 493 \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']], 494 \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']], 495 \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']], 496 \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']], 497 \ ['fFf', "s/F/\\='\r'/", ['f', 'f']], 498 \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']], 499 \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']], 500 \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']], 501 \ ['jJj', "s/J/\\='\n'/", ['j', 'j']], 502 \ ['kKk', 's/K/\="\r"/', ['k', 'k']], 503 \ ['lLl', 's/L/\="\n"/', ['l', 'l']] 504 \ ] 505 call Run_SubCmd_Tests(tests) 506endfunc 507 508" Test for submatch() on :substitute. 509func Test_sub_cmd_4() 510 set magic& 511 set cpo& 512 513 " List entry format: [input, cmd, output] 514 let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/", 515 \ ['a\a']], 516 \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/", 517 \ ['b\b']], 518 \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/", 519 \ ["c\<C-V>", 'c']], 520 \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/", 521 \ ["d\<C-V>", 'd']], 522 \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/", 523 \ ["e\\\<C-V>", 'e']], 524 \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/", 525 \ ['f', 'f']], 526 \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/', 527 \ ["g\<C-V>", 'g']], 528 \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/', 529 \ ["h\<C-V>", 'h']], 530 \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/', 531 \ ["i\\\<C-V>", 'i']], 532 \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/", 533 \ ['j', 'j']], 534 \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/", 535 \ ['k', 'k']], 536 \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/", 537 \ ['l', 'l']], 538 \ ] 539 call Run_SubCmd_Tests(tests) 540endfunc 541 542func Test_sub_cmd_5() 543 set magic& 544 set cpo& 545 546 " List entry format: [input, cmd, output] 547 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']], 548 \ ['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']]"]], 549 \ ] 550 call Run_SubCmd_Tests(tests) 551endfunc 552 553" Test for *:s%* on :substitute. 554func Test_sub_cmd_6() 555 set magic& 556 set cpo+=/ 557 558 " List entry format: [input, cmd, output] 559 let tests = [ ['A', 's/A/a/', ['a']], 560 \ ['B', 's/B/%/', ['a']], 561 \ ] 562 call Run_SubCmd_Tests(tests) 563 564 set cpo-=/ 565 let tests = [ ['C', 's/C/c/', ['c']], 566 \ ['D', 's/D/%/', ['%']], 567 \ ] 568 call Run_SubCmd_Tests(tests) 569 570 set cpo& 571endfunc 572 573" Test for :s replacing \n with line break. 574func Test_sub_cmd_7() 575 set magic& 576 set cpo& 577 578 " List entry format: [input, cmd, output] 579 let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']], 580 \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']], 581 \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]], 582 \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]], 583 \ ["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']")]], 584 \ ] 585 call Run_SubCmd_Tests(tests) 586 587 exe "normal oQ\nQ\<Esc>k" 588 call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486') 589 enew! 590endfunc 591 592func TitleString() 593 let check = 'foo' =~ 'bar' 594 return "" 595endfunc 596 597func Test_sub_cmd_8() 598 set titlestring=%{TitleString()} 599 600 enew! 601 call append(0, ['', 'test_one', 'test_two']) 602 call cursor(1,1) 603 /^test_one/s/.*/\="foo\nbar"/ 604 call assert_equal('foo', getline(2)) 605 call assert_equal('bar', getline(3)) 606 call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t") 607 call feedkeys("\<CR>y", "xt") 608 call assert_equal('foo', getline(4)) 609 call assert_equal('bar', getline(5)) 610 611 enew! 612 set titlestring& 613endfunc 614 615func Test_sub_cmd_9() 616 new 617 let input = ['1 aaa', '2 aaa', '3 aaa'] 618 call setline(1, input) 619 func Foo() 620 return submatch(0) 621 endfunc 622 %s/aaa/\=Foo()/gn 623 call assert_equal(input, getline(1, '$')) 624 call assert_equal(1, &modifiable) 625 626 delfunc Foo 627 bw! 628endfunc 629 630func Test_nocatch_sub_failure_handling() 631 " normal error results in all replacements 632 func Foo() 633 foobar 634 endfunc 635 new 636 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 637 %s/aaa/\=Foo()/g 638 call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3)) 639 640 " Trow without try-catch causes abort after the first line. 641 " We cannot test this, since it would stop executing the test script. 642 643 " try/catch does not result in any changes 644 func! Foo() 645 throw 'error' 646 endfunc 647 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 648 let error_caught = 0 649 try 650 %s/aaa/\=Foo()/g 651 catch 652 let error_caught = 1 653 endtry 654 call assert_equal(1, error_caught) 655 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 656 657 " Same, but using "n" flag so that "sandbox" gets set 658 call setline(1, ['1 aaa', '2 aaa', '3 aaa']) 659 let error_caught = 0 660 try 661 %s/aaa/\=Foo()/gn 662 catch 663 let error_caught = 1 664 endtry 665 call assert_equal(1, error_caught) 666 call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3)) 667 668 delfunc Foo 669 bwipe! 670endfunc 671 672" Test ":s/pat/sub/" with different ~s in sub. 673func Test_replace_with_tilde() 674 new 675 " Set the last replace string to empty 676 s/^$// 677 call append(0, ['- Bug in "vPPPP" on this text:']) 678 normal gg 679 s/u/~u~/ 680 call assert_equal('- Bug in "vPPPP" on this text:', getline(1)) 681 s/i/~u~/ 682 call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1)) 683 s/o/~~~/ 684 call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1)) 685 close! 686endfunc 687 688func Test_replace_keeppatterns() 689 new 690 a 691foobar 692 693substitute foo asdf 694 695one two 696. 697 698 normal gg 699 /^substitute 700 s/foo/bar/ 701 call assert_equal('foo', @/) 702 call assert_equal('substitute bar asdf', getline('.')) 703 704 /^substitute 705 keeppatterns s/asdf/xyz/ 706 call assert_equal('^substitute', @/) 707 call assert_equal('substitute bar xyz', getline('.')) 708 709 exe "normal /bar /e\<CR>" 710 call assert_equal(15, col('.')) 711 normal - 712 keeppatterns /xyz 713 call assert_equal('bar ', @/) 714 call assert_equal('substitute bar xyz', getline('.')) 715 exe "normal 0dn" 716 call assert_equal('xyz', getline('.')) 717 718 close! 719endfunc 720 721func Test_sub_beyond_end() 722 new 723 call setline(1, '#') 724 let @/ = '^#\n\zs' 725 s///e 726 call assert_equal('#', getline(1)) 727 bwipe! 728endfunc 729