Bram Moolenaar | cd055da | 2016-09-02 19:50:48 +0200 | [diff] [blame] | 1 | " Tests for multi-line regexps with ":s". |
| 2 | |
| 3 | function! 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! |
| 41 | endfunction |
Bram Moolenaar | 8c50d50 | 2017-02-17 18:28:24 +0100 | [diff] [blame] | 42 | |
| 43 | function! 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 |
| 108 | endfunction |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 109 | |
| 110 | func Test_substitute_repeat() |
| 111 | " This caused an invalid memory access. |
| 112 | split Xfile |
| 113 | s/^/x |
| 114 | call feedkeys("Qsc\<CR>y", 'tx') |
| 115 | bwipe! |
| 116 | endfunc |
Bram Moolenaar | 1a333bc | 2017-08-30 20:21:58 +0200 | [diff] [blame] | 117 | |
| 118 | " Test for *sub-replace-special* and *sub-replace-expression* on substitute(). |
| 119 | func Test_sub_replace_1() |
| 120 | " Run the tests with 'magic' on |
| 121 | set magic |
| 122 | set cpo& |
| 123 | call assert_equal('AA', substitute('A', 'A', '&&', '')) |
| 124 | call assert_equal('&', substitute('B', 'B', '\&', '')) |
| 125 | call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) |
| 126 | call assert_equal('d', substitute('D', 'D', 'd', '')) |
| 127 | call assert_equal('~', substitute('E', 'E', '~', '')) |
| 128 | call assert_equal('~', substitute('F', 'F', '\~', '')) |
| 129 | call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) |
| 130 | call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) |
| 131 | call assert_equal('iI', substitute('I', 'I', '\lII', '')) |
| 132 | call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) |
| 133 | call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) |
| 134 | call assert_equal("l\<C-V>\<C-M>l", |
| 135 | \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) |
| 136 | call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) |
| 137 | call assert_equal("n\<C-V>\<C-M>n", |
| 138 | \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) |
| 139 | call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) |
| 140 | call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) |
| 141 | call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) |
| 142 | call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) |
| 143 | call assert_equal('scs', substitute('sSs', 'S', '\c', '')) |
| 144 | call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) |
| 145 | call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) |
| 146 | call assert_equal("w\\w", substitute('wWw', 'W', "\\", '')) |
| 147 | call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", '')) |
| 148 | call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', '')) |
| 149 | call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', '')) |
| 150 | endfunc |
| 151 | |
| 152 | func Test_sub_replace_2() |
| 153 | " Run the tests with 'magic' off |
| 154 | set nomagic |
| 155 | set cpo& |
| 156 | call assert_equal('AA', substitute('A', 'A', '&&', '')) |
| 157 | call assert_equal('&', substitute('B', 'B', '\&', '')) |
| 158 | call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', '')) |
| 159 | call assert_equal('d', substitute('D', 'D', 'd', '')) |
| 160 | call assert_equal('~', substitute('E', 'E', '~', '')) |
| 161 | call assert_equal('~', substitute('F', 'F', '\~', '')) |
| 162 | call assert_equal('Gg', substitute('G', 'G', '\ugg', '')) |
| 163 | call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', '')) |
| 164 | call assert_equal('iI', substitute('I', 'I', '\lII', '')) |
| 165 | call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', '')) |
| 166 | call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', '')) |
| 167 | call assert_equal("l\<C-V>\<C-M>l", |
| 168 | \ substitute('lLl', 'L', "\<C-V>\<C-M>", '')) |
| 169 | call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', '')) |
| 170 | call assert_equal("n\<C-V>\<C-M>n", |
| 171 | \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", '')) |
| 172 | call assert_equal("o\no", substitute('oOo', 'O', '\n', '')) |
| 173 | call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', '')) |
| 174 | call assert_equal("q\tq", substitute('qQq', 'Q', '\t', '')) |
| 175 | call assert_equal('r\r', substitute('rRr', 'R', '\\', '')) |
| 176 | call assert_equal('scs', substitute('sSs', 'S', '\c', '')) |
| 177 | call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", '')) |
| 178 | call assert_equal("u\nu", substitute('uUu', 'U', "\n", '')) |
| 179 | call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", '')) |
| 180 | call assert_equal('w\w', substitute('wWw', 'W', "\\", '')) |
| 181 | call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', '')) |
| 182 | call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', '')) |
| 183 | endfunc |
| 184 | |
| 185 | func Test_sub_replace_3() |
| 186 | set magic& |
| 187 | set cpo& |
| 188 | call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', '')) |
| 189 | call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', '')) |
| 190 | call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", '')) |
| 191 | call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", '')) |
| 192 | call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", '')) |
| 193 | call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', '')) |
| 194 | call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', '')) |
| 195 | call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', '')) |
| 196 | call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', '')) |
| 197 | endfunc |
| 198 | |
| 199 | " Test for submatch() on substitute(). |
| 200 | func Test_sub_replace_4() |
| 201 | set magic& |
| 202 | set cpo& |
| 203 | call assert_equal('a\a', substitute('aAa', 'A', |
| 204 | \ '\=substitute(submatch(0), ".", "\\", "")', '')) |
| 205 | call assert_equal('b\b', substitute('bBb', 'B', |
| 206 | \ '\=substitute(submatch(0), ".", "\\\\", "")', '')) |
| 207 | call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', '')) |
| 208 | call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', '')) |
| 209 | call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', '')) |
| 210 | call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', '')) |
| 211 | call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', '')) |
| 212 | call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', '')) |
| 213 | call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', '')) |
| 214 | endfunc |
| 215 | |
| 216 | func Test_sub_replace_5() |
| 217 | set magic& |
| 218 | set cpo& |
| 219 | call assert_equal('A123456789987654321', substitute('A123456789', |
| 220 | \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', |
| 221 | \ '\=submatch(0) . submatch(9) . submatch(8) . ' . |
| 222 | \ 'submatch(7) . submatch(6) . submatch(5) . ' . |
| 223 | \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)', |
| 224 | \ '')) |
| 225 | call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " . |
| 226 | \ "['5'], ['4'], ['3'], ['2'], ['1']]", |
| 227 | \ substitute('A123456789', |
| 228 | \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', |
| 229 | \ '\=string([submatch(0, 1), submatch(9, 1), ' . |
| 230 | \ 'submatch(8, 1), submatch(7, 1), submatch(6, 1), ' . |
| 231 | \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' . |
| 232 | \ 'submatch(2, 1), submatch(1, 1)])', |
| 233 | \ '')) |
| 234 | endfunc |
| 235 | |
| 236 | func Test_sub_replace_6() |
| 237 | set magic& |
| 238 | set cpo+=/ |
| 239 | call assert_equal('a', substitute('A', 'A', 'a', '')) |
| 240 | call assert_equal('%', substitute('B', 'B', '%', '')) |
| 241 | set cpo-=/ |
| 242 | call assert_equal('c', substitute('C', 'C', 'c', '')) |
| 243 | call assert_equal('%', substitute('D', 'D', '%', '')) |
| 244 | endfunc |
| 245 | |
| 246 | func Test_sub_replace_7() |
| 247 | set magic& |
| 248 | set cpo& |
| 249 | call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', '')) |
| 250 | call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', '')) |
| 251 | call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')) |
| 252 | call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g')) |
| 253 | call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g')) |
| 254 | endfunc |
| 255 | |
| 256 | " Test for *:s%* on :substitute. |
| 257 | func Test_sub_replace_8() |
| 258 | new |
| 259 | set magic& |
| 260 | set cpo& |
| 261 | $put =',,X' |
| 262 | s/\(^\|,\)\ze\(,\|X\)/\1N/g |
| 263 | call assert_equal('N,,NX', getline("$")) |
| 264 | $put =',,Y' |
| 265 | let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc' |
| 266 | call feedkeys(cmd . "\<CR>a", "xt") |
| 267 | call assert_equal('N,,NY', getline("$")) |
| 268 | :$put =',,Z' |
| 269 | let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc' |
| 270 | call feedkeys(cmd . "\<CR>yy", "xt") |
| 271 | call assert_equal('N,,NZ', getline("$")) |
| 272 | enew! | close |
| 273 | endfunc |
| 274 | |
| 275 | func Test_sub_replace_9() |
| 276 | new |
| 277 | set magic& |
| 278 | set cpo& |
| 279 | $put ='xxx' |
| 280 | call feedkeys(":s/x/X/gc\<CR>yyq", "xt") |
| 281 | call assert_equal('XXx', getline("$")) |
| 282 | enew! | close |
| 283 | endfunc |
| 284 | |
| 285 | func Test_sub_replace_10() |
| 286 | set magic& |
| 287 | set cpo& |
| 288 | call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g')) |
| 289 | call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g')) |
| 290 | call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g')) |
| 291 | call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g')) |
| 292 | call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g')) |
| 293 | call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g')) |
| 294 | call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g')) |
| 295 | call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g')) |
| 296 | endfunc |