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