test_substitute.vim 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. " Tests for the substitute (:s) command
  2. source shared.vim
  3. source check.vim
  4. source screendump.vim
  5. " NOTE: This needs to be the first test to be
  6. " run in the file, since it depends on
  7. " that the previous substitution atom
  8. " was not yet set.
  9. "
  10. " recursive call of :s and sub-replace special
  11. " (did cause heap-use-after free in < v9.0.2121)
  12. func Test_aaaa_substitute_expr_recursive_special()
  13. func R()
  14. " FIXME: leaving out the 'n' flag leaks memory, why?
  15. %s/./\='.'/gn
  16. endfunc
  17. new Xfoobar_UAF
  18. put ='abcdef'
  19. let bufnr = bufnr('%')
  20. try
  21. silent! :s/./~\=R()/0
  22. "call assert_fails(':s/./~\=R()/0', 'E939:')
  23. let @/='.'
  24. ~g
  25. catch /^Vim\%((\a\+)\)\=:E565:/
  26. endtry
  27. delfunc R
  28. exe bufnr .. "bw!"
  29. endfunc
  30. func Test_multiline_subst()
  31. enew!
  32. call append(0, ["1 aa",
  33. \ "bb",
  34. \ "cc",
  35. \ "2 dd",
  36. \ "ee",
  37. \ "3 ef",
  38. \ "gh",
  39. \ "4 ij",
  40. \ "5 a8",
  41. \ "8b c9",
  42. \ "9d",
  43. \ "6 e7",
  44. \ "77f",
  45. \ "xxxxx"])
  46. 1
  47. " test if replacing a line break works with a back reference
  48. /^1/,/^2/s/\n\(.\)/ \1/
  49. " test if inserting a line break works with a back reference
  50. /^3/,/^4/s/\(.\)$/\r\1/
  51. " test if replacing a line break with another line break works
  52. /^5/,/^6/s/\(\_d\{3}\)/x\1x/
  53. call assert_equal('1 aa bb cc 2 dd ee', getline(1))
  54. call assert_equal('3 e', getline(2))
  55. call assert_equal('f', getline(3))
  56. call assert_equal('g', getline(4))
  57. call assert_equal('h', getline(5))
  58. call assert_equal('4 i', getline(6))
  59. call assert_equal('j', getline(7))
  60. call assert_equal('5 ax8', getline(8))
  61. call assert_equal('8xb cx9', getline(9))
  62. call assert_equal('9xd', getline(10))
  63. call assert_equal('6 ex7', getline(11))
  64. call assert_equal('7x7f', getline(12))
  65. call assert_equal('xxxxx', getline(13))
  66. enew!
  67. endfunc
  68. func Test_substitute_variants()
  69. " Validate that all the 2-/3-letter variants which embed the flags into the
  70. " command name actually work.
  71. enew!
  72. let ln = 'Testing string'
  73. let variants = [
  74. \ { 'cmd': ':s/Test/test/c', 'exp': 'testing string', 'prompt': 'y' },
  75. \ { 'cmd': ':s/foo/bar/ce', 'exp': ln },
  76. \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' },
  77. \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' },
  78. \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' },
  79. \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' },
  80. \ { 'cmd': ':s/t/r/cn', 'exp': ln },
  81. \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' },
  82. \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' },
  83. \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' },
  84. \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' },
  85. \ { 'cmd': ':s/foo/bar/ge', 'exp': ln },
  86. \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' },
  87. \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' },
  88. \ { 'cmd': ':s/t/r/gI', 'exp': 'Tesring srring' },
  89. \ { 'cmd': ':s/t/r/gn', 'exp': ln },
  90. \ { 'cmd': ':s/t/r/gp', 'exp': 'Tesring srring' },
  91. \ { 'cmd': ':s/t/r/gl', 'exp': 'Tesring srring' },
  92. \ { 'cmd': ':s//r/gr', 'exp': 'Testr strr' },
  93. \ { 'cmd': ':s/t/r/ic', 'exp': 'resting string', 'prompt': 'y' },
  94. \ { 'cmd': ':s/foo/bar/ie', 'exp': ln },
  95. \ { 'cmd': ':s/t/r/i', 'exp': 'resting string' },
  96. \ { 'cmd': ':s/t/r/iI', 'exp': 'Tesring string' },
  97. \ { 'cmd': ':s/t/r/in', 'exp': ln },
  98. \ { 'cmd': ':s/t/r/ip', 'exp': 'resting string' },
  99. \ { 'cmd': ':s//r/ir', 'exp': 'Testr string' },
  100. \ { 'cmd': ':s/t/r/Ic', 'exp': 'Tesring string', 'prompt': 'y' },
  101. \ { 'cmd': ':s/foo/bar/Ie', 'exp': ln },
  102. \ { 'cmd': ':s/t/r/Ig', 'exp': 'Tesring srring' },
  103. \ { 'cmd': ':s/t/r/Ii', 'exp': 'resting string' },
  104. \ { 'cmd': ':s/t/r/I', 'exp': 'Tesring string' },
  105. \ { 'cmd': ':s/t/r/Ip', 'exp': 'Tesring string' },
  106. \ { 'cmd': ':s/t/r/Il', 'exp': 'Tesring string' },
  107. \ { 'cmd': ':s//r/Ir', 'exp': 'Testr string' },
  108. \ { 'cmd': ':s//r/rc', 'exp': 'Testr string', 'prompt': 'y' },
  109. \ { 'cmd': ':s//r/rg', 'exp': 'Testr strr' },
  110. \ { 'cmd': ':s//r/ri', 'exp': 'Testr string' },
  111. \ { 'cmd': ':s//r/rI', 'exp': 'Testr string' },
  112. \ { 'cmd': ':s//r/rn', 'exp': 'Testing string' },
  113. \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' },
  114. \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' },
  115. \ { 'cmd': ':s//r/r', 'exp': 'Testr string' },
  116. \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' },
  117. \]
  118. for var in variants
  119. for run in [1, 2]
  120. let cmd = var.cmd
  121. if run == 2 && cmd =~ "/.*/.*/."
  122. " Change :s/from/to/{flags} to :s{flags}
  123. let cmd = substitute(cmd, '/.*/', '', '')
  124. endif
  125. call setline(1, [ln])
  126. let msg = printf('using "%s"', cmd)
  127. let @/='ing'
  128. let v:errmsg = ''
  129. call feedkeys(cmd . "\<CR>" . get(var, 'prompt', ''), 'ntx')
  130. " No error should exist (matters for testing e flag)
  131. call assert_equal('', v:errmsg, msg)
  132. call assert_equal(var.exp, getline('.'), msg)
  133. endfor
  134. endfor
  135. endfunc
  136. " Test the l, p, # flags.
  137. func Test_substitute_flags_lp()
  138. new
  139. call setline(1, "abc\tdef\<C-h>ghi")
  140. let a = execute('s/a/a/p')
  141. call assert_equal("\nabc def^Hghi", a)
  142. let a = execute('s/a/a/l')
  143. call assert_equal("\nabc^Idef^Hghi$", a)
  144. let a = execute('s/a/a/#')
  145. call assert_equal("\n 1 abc def^Hghi", a)
  146. let a = execute('s/a/a/p#')
  147. call assert_equal("\n 1 abc def^Hghi", a)
  148. let a = execute('s/a/a/l#')
  149. call assert_equal("\n 1 abc^Idef^Hghi$", a)
  150. let a = execute('s/a/a/')
  151. call assert_equal("", a)
  152. bwipe!
  153. endfunc
  154. func Test_substitute_repeat()
  155. " This caused an invalid memory access.
  156. split Xfile
  157. s/^/x
  158. call feedkeys("Qsc\<CR>y", 'tx')
  159. bwipe!
  160. endfunc
  161. " Test :s with ? as delimiter.
  162. func Test_substitute_question_delimiter()
  163. new
  164. call setline(1, '??:??')
  165. %s?\?\??!!?g
  166. call assert_equal('!!:!!', getline(1))
  167. bwipe!
  168. endfunc
  169. " Test %s/\n// which is implemented as a special case to use a
  170. " more efficient join rather than doing a regular substitution.
  171. func Test_substitute_join()
  172. new
  173. call setline(1, ["foo\tbar", "bar\<C-H>foo"])
  174. let a = execute('%s/\n//')
  175. call assert_equal("", a)
  176. call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
  177. call assert_equal('\n', histget("search", -1))
  178. call setline(1, ["foo\tbar", "bar\<C-H>foo"])
  179. let a = execute('%s/\n//g')
  180. call assert_equal("", a)
  181. call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
  182. call assert_equal('\n', histget("search", -1))
  183. call setline(1, ["foo\tbar", "bar\<C-H>foo"])
  184. let a = execute('%s/\n//p')
  185. call assert_equal("\nfoo barbar^Hfoo", a)
  186. call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
  187. call assert_equal('\n', histget("search", -1))
  188. call setline(1, ["foo\tbar", "bar\<C-H>foo"])
  189. let a = execute('%s/\n//l')
  190. call assert_equal("\nfoo^Ibarbar^Hfoo$", a)
  191. call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
  192. call assert_equal('\n', histget("search", -1))
  193. call setline(1, ["foo\tbar", "bar\<C-H>foo"])
  194. let a = execute('%s/\n//#')
  195. call assert_equal("\n 1 foo barbar^Hfoo", a)
  196. call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$'))
  197. call assert_equal('\n', histget("search", -1))
  198. call setline(1, ['foo', 'bar', 'baz', 'qux'])
  199. call execute('1,2s/\n//')
  200. call assert_equal(['foobarbaz', 'qux'], getline(1, '$'))
  201. bwipe!
  202. endfunc
  203. func Test_substitute_count()
  204. new
  205. call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
  206. 2
  207. s/foo/bar/3
  208. call assert_equal(['foo foo', 'bar foo', 'bar foo', 'bar foo', 'foo foo'],
  209. \ getline(1, '$'))
  210. call assert_fails('s/foo/bar/0', 'E939:')
  211. call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo'])
  212. 2,4s/foo/bar/ 10
  213. call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'],
  214. \ getline(1, '$'))
  215. call assert_fails('s/./b/2147483647', 'E1510:')
  216. bwipe!
  217. endfunc
  218. " Test substitute 'n' flag (report number of matches, do not substitute).
  219. func Test_substitute_flag_n()
  220. new
  221. let lines = ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']
  222. call setline(1, lines)
  223. call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/n'))
  224. call assert_equal("\n6 matches on 3 lines", execute('2,4s/foo/bar/gn'))
  225. " c flag (confirm) should be ignored when using n flag.
  226. call assert_equal("\n3 matches on 3 lines", execute('2,4s/foo/bar/nc'))
  227. " No substitution should have been done.
  228. call assert_equal(lines, getline(1, '$'))
  229. %delete _
  230. call setline(1, ['A', 'Bar', 'Baz'])
  231. call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn'))
  232. bwipe!
  233. endfunc
  234. func Test_substitute_errors()
  235. new
  236. call setline(1, 'foobar')
  237. call assert_fails('s/FOO/bar/', 'E486:')
  238. call assert_fails('s/foo/bar/@', 'E488:')
  239. call assert_fails('s/\(/bar/', 'E54:')
  240. call assert_fails('s afooabara', 'E146:')
  241. call assert_fails('s\\a', 'E10:')
  242. setl nomodifiable
  243. call assert_fails('s/foo/bar/', 'E21:')
  244. call assert_fails("let s=substitute([], 'a', 'A', 'g')", 'E730:')
  245. call assert_fails("let s=substitute('abcda', [], 'A', 'g')", 'E730:')
  246. call assert_fails("let s=substitute('abcda', 'a', [], 'g')", 'E730:')
  247. call assert_fails("let s=substitute('abcda', 'a', 'A', [])", 'E730:')
  248. call assert_fails("let s=substitute('abc', '\\%(', 'A', 'g')", 'E53:')
  249. bwipe!
  250. endfunc
  251. " Test for *sub-replace-special* and *sub-replace-expression* on substitute().
  252. func Test_sub_replace_1()
  253. " Run the tests with 'magic' on
  254. set magic
  255. set cpo&
  256. call assert_equal('AA', substitute('A', 'A', '&&', ''))
  257. call assert_equal('&', substitute('B', 'B', '\&', ''))
  258. call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
  259. call assert_equal('d', substitute('D', 'D', 'd', ''))
  260. call assert_equal('~', substitute('E', 'E', '~', ''))
  261. call assert_equal('~', substitute('F', 'F', '\~', ''))
  262. call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
  263. call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
  264. call assert_equal('iI', substitute('I', 'I', '\lII', ''))
  265. call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
  266. call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
  267. call assert_equal("l\<C-V>\<C-M>l",
  268. \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
  269. call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
  270. call assert_equal("n\<C-V>\<C-M>n",
  271. \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
  272. call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
  273. call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
  274. call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
  275. call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
  276. call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
  277. call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
  278. call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
  279. call assert_equal("w\\w", substitute('wWw', 'W', "\\", ''))
  280. call assert_equal("x\<C-M>x", substitute('xXx', 'X', "\r", ''))
  281. call assert_equal("YyyY", substitute('Y', 'Y', '\L\uyYy\l\EY', ''))
  282. call assert_equal("zZZz", substitute('Z', 'Z', '\U\lZzZ\u\Ez', ''))
  283. " \v or \V after $
  284. call assert_equal('abxx', substitute('abcd', 'xy$\v|cd$', 'xx', ''))
  285. call assert_equal('abxx', substitute('abcd', 'xy$\V\|cd\$', 'xx', ''))
  286. endfunc
  287. func Test_sub_replace_2()
  288. " Run the tests with 'magic' off
  289. set nomagic
  290. set cpo&
  291. call assert_equal('AA', substitute('A', 'A', '&&', ''))
  292. call assert_equal('&', substitute('B', 'B', '\&', ''))
  293. call assert_equal('C123456789987654321', substitute('C123456789', 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', '\0\9\8\7\6\5\4\3\2\1', ''))
  294. call assert_equal('d', substitute('D', 'D', 'd', ''))
  295. call assert_equal('~', substitute('E', 'E', '~', ''))
  296. call assert_equal('~', substitute('F', 'F', '\~', ''))
  297. call assert_equal('Gg', substitute('G', 'G', '\ugg', ''))
  298. call assert_equal('Hh', substitute('H', 'H', '\Uh\Eh', ''))
  299. call assert_equal('iI', substitute('I', 'I', '\lII', ''))
  300. call assert_equal('jJ', substitute('J', 'J', '\LJ\EJ', ''))
  301. call assert_equal('Kk', substitute('K', 'K', '\Uk\ek', ''))
  302. call assert_equal("l\<C-V>\<C-M>l",
  303. \ substitute('lLl', 'L', "\<C-V>\<C-M>", ''))
  304. call assert_equal("m\<C-M>m", substitute('mMm', 'M', '\r', ''))
  305. call assert_equal("n\<C-V>\<C-M>n",
  306. \ substitute('nNn', 'N', "\\\<C-V>\<C-M>", ''))
  307. call assert_equal("o\no", substitute('oOo', 'O', '\n', ''))
  308. call assert_equal("p\<C-H>p", substitute('pPp', 'P', '\b', ''))
  309. call assert_equal("q\tq", substitute('qQq', 'Q', '\t', ''))
  310. call assert_equal('r\r', substitute('rRr', 'R', '\\', ''))
  311. call assert_equal('scs', substitute('sSs', 'S', '\c', ''))
  312. call assert_equal("t\<C-M>t", substitute('tTt', 'T', "\r", ''))
  313. call assert_equal("u\nu", substitute('uUu', 'U', "\n", ''))
  314. call assert_equal("v\<C-H>v", substitute('vVv', 'V', "\b", ''))
  315. call assert_equal('w\w', substitute('wWw', 'W', "\\", ''))
  316. call assert_equal('XxxX', substitute('X', 'X', '\L\uxXx\l\EX', ''))
  317. call assert_equal('yYYy', substitute('Y', 'Y', '\U\lYyY\u\Ey', ''))
  318. endfunc
  319. func Test_sub_replace_3()
  320. set magic&
  321. set cpo&
  322. call assert_equal('a\a', substitute('aAa', 'A', '\="\\"', ''))
  323. call assert_equal('b\\b', substitute('bBb', 'B', '\="\\\\"', ''))
  324. call assert_equal("c\rc", substitute('cCc', 'C', "\\=\"\r\"", ''))
  325. call assert_equal("d\\\rd", substitute('dDd', 'D', "\\=\"\\\\\r\"", ''))
  326. call assert_equal("e\\\\\re", substitute('eEe', 'E', "\\=\"\\\\\\\\\r\"", ''))
  327. call assert_equal('f\rf', substitute('fFf', 'F', '\="\\r"', ''))
  328. call assert_equal('j\nj', substitute('jJj', 'J', '\="\\n"', ''))
  329. call assert_equal("k\<C-M>k", substitute('kKk', 'K', '\="\r"', ''))
  330. call assert_equal("l\nl", substitute('lLl', 'L', '\="\n"', ''))
  331. endfunc
  332. " Test for submatch() on substitute().
  333. func Test_sub_replace_4()
  334. set magic&
  335. set cpo&
  336. call assert_equal('a\a', substitute('aAa', 'A',
  337. \ '\=substitute(submatch(0), ".", "\\", "")', ''))
  338. call assert_equal('b\b', substitute('bBb', 'B',
  339. \ '\=substitute(submatch(0), ".", "\\\\", "")', ''))
  340. call assert_equal("c\<C-V>\<C-M>c", substitute('cCc', 'C', '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")', ''))
  341. call assert_equal("d\<C-V>\<C-M>d", substitute('dDd', 'D', '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")', ''))
  342. call assert_equal("e\\\<C-V>\<C-M>e", substitute('eEe', 'E', '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")', ''))
  343. call assert_equal("f\<C-M>f", substitute('fFf', 'F', '\=substitute(submatch(0), ".", "\\r", "")', ''))
  344. call assert_equal("j\nj", substitute('jJj', 'J', '\=substitute(submatch(0), ".", "\\n", "")', ''))
  345. call assert_equal("k\rk", substitute('kKk', 'K', '\=substitute(submatch(0), ".", "\r", "")', ''))
  346. call assert_equal("l\nl", substitute('lLl', 'L', '\=substitute(submatch(0), ".", "\n", "")', ''))
  347. endfunc
  348. func Test_sub_replace_5()
  349. set magic&
  350. set cpo&
  351. call assert_equal('A123456789987654321', substitute('A123456789',
  352. \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
  353. \ '\=submatch(0) . submatch(9) . submatch(8) . ' .
  354. \ 'submatch(7) . submatch(6) . submatch(5) . ' .
  355. \ 'submatch(4) . submatch(3) . submatch(2) . submatch(1)',
  356. \ ''))
  357. call assert_equal("[['A123456789'], ['9'], ['8'], ['7'], ['6'], " .
  358. \ "['5'], ['4'], ['3'], ['2'], ['1']]",
  359. \ substitute('A123456789',
  360. \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
  361. \ '\=string([submatch(0, 1), submatch(9, 1), ' .
  362. \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
  363. \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
  364. \ 'submatch(2, 1), submatch(1, 1)])',
  365. \ ''))
  366. endfunc
  367. func Test_sub_replace_6()
  368. set magic&
  369. " Nvim: no "/" flag in 'cpoptions'.
  370. " set cpo+=/
  371. call assert_equal('a', substitute('A', 'A', 'a', ''))
  372. call assert_equal('%', substitute('B', 'B', '%', ''))
  373. set cpo-=/
  374. call assert_equal('c', substitute('C', 'C', 'c', ''))
  375. call assert_equal('%', substitute('D', 'D', '%', ''))
  376. endfunc
  377. func Test_sub_replace_7()
  378. set magic&
  379. set cpo&
  380. call assert_equal('AA', substitute('AA', 'A.', '\=submatch(0)', ''))
  381. call assert_equal("B\nB", substitute("B\nB", 'B.', '\=submatch(0)', ''))
  382. call assert_equal("['B\n']B", substitute("B\nB", 'B.', '\=string(submatch(0, 1))', ''))
  383. call assert_equal('-abab', substitute('-bb', '\zeb', 'a', 'g'))
  384. call assert_equal('c-cbcbc', substitute('-bb', '\ze', 'c', 'g'))
  385. endfunc
  386. " Test for *:s%* on :substitute.
  387. func Test_sub_replace_8()
  388. new
  389. set magic&
  390. set cpo&
  391. $put =',,X'
  392. s/\(^\|,\)\ze\(,\|X\)/\1N/g
  393. call assert_equal('N,,NX', getline("$"))
  394. $put =',,Y'
  395. let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc'
  396. call feedkeys(cmd . "\<CR>a", "xt")
  397. call assert_equal('N,,NY', getline("$"))
  398. :$put =',,Z'
  399. let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc'
  400. call feedkeys(cmd . "\<CR>yy", "xt")
  401. call assert_equal('N,,NZ', getline("$"))
  402. enew! | close
  403. endfunc
  404. func Test_sub_replace_9()
  405. new
  406. set magic&
  407. set cpo&
  408. $put ='xxx'
  409. call feedkeys(":s/x/X/gc\<CR>yyq", "xt")
  410. call assert_equal('XXx', getline("$"))
  411. enew! | close
  412. endfunc
  413. func Test_sub_replace_10()
  414. set magic&
  415. set cpo&
  416. call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
  417. call assert_equal('aaa', substitute('123', '\zs.', 'a', 'g'))
  418. call assert_equal('1a2a3a', substitute('123', '.\zs', 'a', 'g'))
  419. call assert_equal('a1a2a3a', substitute('123', '\ze', 'a', 'g'))
  420. call assert_equal('a1a2a3', substitute('123', '\ze.', 'a', 'g'))
  421. call assert_equal('aaa', substitute('123', '.\ze', 'a', 'g'))
  422. call assert_equal('aa2a3a', substitute('123', '1\|\ze', 'a', 'g'))
  423. call assert_equal('1aaa', substitute('123', '1\zs\|[23]', 'a', 'g'))
  424. endfunc
  425. func SubReplacer(text, submatches)
  426. return a:text .. a:submatches[0] .. a:text
  427. endfunc
  428. func SubReplacerVar(text, ...)
  429. return a:text .. a:1[0] .. a:text
  430. endfunc
  431. func SubReplacer20(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, submatches)
  432. return a:t3 .. a:submatches[0] .. a:t11
  433. endfunc
  434. func Test_substitute_partial()
  435. call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacer', ['foo']), 'g'))
  436. call assert_equal('1foo2foo3', substitute('123', '2', function('SubReplacerVar', ['foo']), 'g'))
  437. " 19 arguments plus one is just OK
  438. let Replacer = function('SubReplacer20', repeat(['foo'], 19))
  439. call assert_equal('1foo2foo3', substitute('123', '2', Replacer, 'g'))
  440. " 20 arguments plus one is too many
  441. let Replacer = function('SubReplacer20', repeat(['foo'], 20))
  442. call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:')
  443. endfunc
  444. func Test_substitute_float()
  445. CheckFeature float
  446. call assert_equal('number 1.23', substitute('number ', '$', { -> 1.23 }, ''))
  447. " vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, ''))
  448. endfunc
  449. " Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
  450. " Execute a list of :substitute command tests
  451. func Run_SubCmd_Tests(tests)
  452. enew!
  453. for t in a:tests
  454. let start = line('.') + 1
  455. let end = start + len(t[2]) - 1
  456. exe "normal o" . t[0]
  457. call cursor(start, 1)
  458. exe t[1]
  459. call assert_equal(t[2], getline(start, end), t[1])
  460. endfor
  461. enew!
  462. endfunc
  463. func Test_sub_cmd_1()
  464. set magic
  465. set cpo&
  466. " List entry format: [input, cmd, output]
  467. let tests = [['A', 's/A/&&/', ['AA']],
  468. \ ['B', 's/B/\&/', ['&']],
  469. \ ['C123456789', 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
  470. \ ['D', 's/D/d/', ['d']],
  471. \ ['E', 's/E/~/', ['d']],
  472. \ ['F', 's/F/\~/', ['~']],
  473. \ ['G', 's/G/\ugg/', ['Gg']],
  474. \ ['H', 's/H/\Uh\Eh/', ['Hh']],
  475. \ ['I', 's/I/\lII/', ['iI']],
  476. \ ['J', 's/J/\LJ\EJ/', ['jJ']],
  477. \ ['K', 's/K/\Uk\ek/', ['Kk']],
  478. \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
  479. \ ['mMm', 's/M/\r/', ['m', 'm']],
  480. \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
  481. \ ['oOo', 's/O/\n/', ["o\no"]],
  482. \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
  483. \ ['qQq', 's/Q/\t/', ["q\tq"]],
  484. \ ['rRr', 's/R/\\/', ['r\r']],
  485. \ ['sSs', 's/S/\c/', ['scs']],
  486. \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
  487. \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
  488. \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
  489. \ ['\', 's/\\/\\\\/', ['\\']]
  490. \ ]
  491. call Run_SubCmd_Tests(tests)
  492. endfunc
  493. func Test_sub_cmd_2()
  494. set nomagic
  495. set cpo&
  496. " List entry format: [input, cmd, output]
  497. let tests = [['A', 's/A/&&/', ['&&']],
  498. \ ['B', 's/B/\&/', ['B']],
  499. \ ['C123456789', 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/', ['C123456789987654321']],
  500. \ ['D', 's/D/d/', ['d']],
  501. \ ['E', 's/E/~/', ['~']],
  502. \ ['F', 's/F/\~/', ['~']],
  503. \ ['G', 's/G/\ugg/', ['Gg']],
  504. \ ['H', 's/H/\Uh\Eh/', ['Hh']],
  505. \ ['I', 's/I/\lII/', ['iI']],
  506. \ ['J', 's/J/\LJ\EJ/', ['jJ']],
  507. \ ['K', 's/K/\Uk\ek/', ['Kk']],
  508. \ ['lLl', "s/L/\<C-V>\<C-M>/", ["l\<C-V>", 'l']],
  509. \ ['mMm', 's/M/\r/', ['m', 'm']],
  510. \ ['nNn', "s/N/\\\<C-V>\<C-M>/", ["n\<C-V>", 'n']],
  511. \ ['oOo', 's/O/\n/', ["o\no"]],
  512. \ ['pPp', 's/P/\b/', ["p\<C-H>p"]],
  513. \ ['qQq', 's/Q/\t/', ["q\tq"]],
  514. \ ['rRr', 's/R/\\/', ['r\r']],
  515. \ ['sSs', 's/S/\c/', ['scs']],
  516. \ ['tTt', "s/T/\<C-V>\<C-J>/", ["t\<C-V>\<C-J>t"]],
  517. \ ['U', 's/U/\L\uuUu\l\EU/', ['UuuU']],
  518. \ ['V', 's/V/\U\lVvV\u\Ev/', ['vVVv']],
  519. \ ['\', 's/\\/\\\\/', ['\\']]
  520. \ ]
  521. call Run_SubCmd_Tests(tests)
  522. endfunc
  523. func Test_sub_cmd_3()
  524. set nomagic
  525. set cpo&
  526. " List entry format: [input, cmd, output]
  527. let tests = [['aAa', "s/A/\\='\\'/", ['a\a']],
  528. \ ['bBb', "s/B/\\='\\\\'/", ['b\\b']],
  529. \ ['cCc', "s/C/\\='\<C-V>\<C-M>'/", ["c\<C-V>", 'c']],
  530. \ ['dDd', "s/D/\\='\\\<C-V>\<C-M>'/", ["d\\\<C-V>", 'd']],
  531. \ ['eEe', "s/E/\\='\\\\\<C-V>\<C-M>'/", ["e\\\\\<C-V>", 'e']],
  532. \ ['fFf', "s/F/\\='\r'/", ['f', 'f']],
  533. \ ['gGg', "s/G/\\='\<C-V>\<C-J>'/", ["g\<C-V>", 'g']],
  534. \ ['hHh', "s/H/\\='\\\<C-V>\<C-J>'/", ["h\\\<C-V>", 'h']],
  535. \ ['iIi', "s/I/\\='\\\\\<C-V>\<C-J>'/", ["i\\\\\<C-V>", 'i']],
  536. \ ['jJj', "s/J/\\='\n'/", ['j', 'j']],
  537. \ ['kKk', 's/K/\="\r"/', ['k', 'k']],
  538. \ ['lLl', 's/L/\="\n"/', ['l', 'l']]
  539. \ ]
  540. call Run_SubCmd_Tests(tests)
  541. endfunc
  542. " Test for submatch() on :substitute.
  543. func Test_sub_cmd_4()
  544. set magic&
  545. set cpo&
  546. " List entry format: [input, cmd, output]
  547. let tests = [ ['aAa', "s/A/\\=substitute(submatch(0), '.', '\\', '')/",
  548. \ ['a\a']],
  549. \ ['bBb', "s/B/\\=substitute(submatch(0), '.', '\\', '')/",
  550. \ ['b\b']],
  551. \ ['cCc', "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/",
  552. \ ["c\<C-V>", 'c']],
  553. \ ['dDd', "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/",
  554. \ ["d\<C-V>", 'd']],
  555. \ ['eEe', "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/",
  556. \ ["e\\\<C-V>", 'e']],
  557. \ ['fFf', "s/F/\\=substitute(submatch(0), '.', '\\r', '')/",
  558. \ ['f', 'f']],
  559. \ ['gGg', 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/',
  560. \ ["g\<C-V>", 'g']],
  561. \ ['hHh', 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/',
  562. \ ["h\<C-V>", 'h']],
  563. \ ['iIi', 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/',
  564. \ ["i\\\<C-V>", 'i']],
  565. \ ['jJj', "s/J/\\=substitute(submatch(0), '.', '\\n', '')/",
  566. \ ['j', 'j']],
  567. \ ['kKk', "s/K/\\=substitute(submatch(0), '.', '\\r', '')/",
  568. \ ['k', 'k']],
  569. \ ['lLl', "s/L/\\=substitute(submatch(0), '.', '\\n', '')/",
  570. \ ['l', 'l']],
  571. \ ]
  572. call Run_SubCmd_Tests(tests)
  573. endfunc
  574. func Test_sub_cmd_5()
  575. set magic&
  576. set cpo&
  577. " List entry format: [input, cmd, output]
  578. 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']],
  579. \ ['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']]"]],
  580. \ ]
  581. call Run_SubCmd_Tests(tests)
  582. endfunc
  583. " Test for *:s%* on :substitute.
  584. func Test_sub_cmd_6()
  585. set magic&
  586. " Nvim: no "/" flag in 'cpoptions'.
  587. " set cpo+=/
  588. " List entry format: [input, cmd, output]
  589. let tests = [ ['A', 's/A/a/', ['a']],
  590. \ ['B', 's/B/%/', ['a']],
  591. \ ]
  592. " call Run_SubCmd_Tests(tests)
  593. set cpo-=/
  594. let tests = [ ['C', 's/C/c/', ['c']],
  595. \ ['D', 's/D/%/', ['%']],
  596. \ ]
  597. call Run_SubCmd_Tests(tests)
  598. set cpo&
  599. endfunc
  600. " Test for :s replacing \n with line break.
  601. func Test_sub_cmd_7()
  602. set magic&
  603. set cpo&
  604. " List entry format: [input, cmd, output]
  605. let tests = [ ["A\<C-V>\<C-M>A", 's/A./\=submatch(0)/', ['A', 'A']],
  606. \ ["B\<C-V>\<C-J>B", 's/B./\=submatch(0)/', ['B', 'B']],
  607. \ ["C\<C-V>\<C-J>C", 's/C./\=strtrans(string(submatch(0, 1)))/', [strtrans("['C\<C-J>']C")]],
  608. \ ["D\<C-V>\<C-J>\nD", 's/D.\nD/\=strtrans(string(submatch(0, 1)))/', [strtrans("['D\<C-J>', 'D']")]],
  609. \ ["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']")]],
  610. \ ]
  611. call Run_SubCmd_Tests(tests)
  612. exe "normal oQ\nQ\<Esc>k"
  613. call assert_fails('s/Q[^\n]Q/\=submatch(0)."foobar"/', 'E486')
  614. enew!
  615. endfunc
  616. func TitleString()
  617. let check = 'foo' =~ 'bar'
  618. return ""
  619. endfunc
  620. func Test_sub_cmd_8()
  621. set titlestring=%{TitleString()}
  622. enew!
  623. call append(0, ['', 'test_one', 'test_two'])
  624. call cursor(1,1)
  625. /^test_one/s/.*/\="foo\nbar"/
  626. call assert_equal('foo', getline(2))
  627. call assert_equal('bar', getline(3))
  628. call feedkeys(':/^test_two/s/.*/\="foo\nbar"/c', "t")
  629. call feedkeys("\<CR>y", "xt")
  630. call assert_equal('foo', getline(4))
  631. call assert_equal('bar', getline(5))
  632. enew!
  633. set titlestring&
  634. endfunc
  635. func Test_sub_cmd_9()
  636. new
  637. let input = ['1 aaa', '2 aaa', '3 aaa']
  638. call setline(1, input)
  639. func Foo()
  640. return submatch(0)
  641. endfunc
  642. %s/aaa/\=Foo()/gn
  643. call assert_equal(input, getline(1, '$'))
  644. call assert_equal(1, &modifiable)
  645. delfunc Foo
  646. bw!
  647. endfunc
  648. func Test_sub_highlight_zero_match()
  649. CheckRunVimInTerminal
  650. let lines =<< trim END
  651. call setline(1, ['one', 'two', 'three'])
  652. END
  653. call writefile(lines, 'XscriptSubHighlight', 'D')
  654. let buf = RunVimInTerminal('-S XscriptSubHighlight', #{rows: 8, cols: 60})
  655. call term_sendkeys(buf, ":%s/^/ /c\<CR>")
  656. call VerifyScreenDump(buf, 'Test_sub_highlight_zer_match_1', {})
  657. call term_sendkeys(buf, "\<Esc>")
  658. call StopVimInTerminal(buf)
  659. endfunc
  660. func Test_nocatch_sub_failure_handling()
  661. " normal error results in all replacements
  662. func Foo()
  663. foobar
  664. endfunc
  665. new
  666. call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
  667. " need silent! to avoid a delay when entering Insert mode
  668. silent! %s/aaa/\=Foo()/g
  669. call assert_equal(['1 0', '2 0', '3 0'], getline(1, 3))
  670. " Throw without try-catch causes abort after the first line.
  671. " We cannot test this, since it would stop executing the test script.
  672. " try/catch does not result in any changes
  673. func! Foo()
  674. throw 'error'
  675. endfunc
  676. call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
  677. let error_caught = 0
  678. try
  679. %s/aaa/\=Foo()/g
  680. catch
  681. let error_caught = 1
  682. endtry
  683. call assert_equal(1, error_caught)
  684. call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
  685. " Same, but using "n" flag so that "sandbox" gets set
  686. call setline(1, ['1 aaa', '2 aaa', '3 aaa'])
  687. let error_caught = 0
  688. try
  689. %s/aaa/\=Foo()/gn
  690. catch
  691. let error_caught = 1
  692. endtry
  693. call assert_equal(1, error_caught)
  694. call assert_equal(['1 aaa', '2 aaa', '3 aaa'], getline(1, 3))
  695. delfunc Foo
  696. bwipe!
  697. endfunc
  698. " Test ":s/pat/sub/" with different ~s in sub.
  699. func Test_replace_with_tilde()
  700. new
  701. " Set the last replace string to empty
  702. s/^$//
  703. call append(0, ['- Bug in "vPPPP" on this text:'])
  704. normal gg
  705. s/u/~u~/
  706. call assert_equal('- Bug in "vPPPP" on this text:', getline(1))
  707. s/i/~u~/
  708. call assert_equal('- Bug uuun "vPPPP" on this text:', getline(1))
  709. s/o/~~~/
  710. call assert_equal('- Bug uuun "vPPPP" uuuuuuuuun this text:', getline(1))
  711. close!
  712. endfunc
  713. func Test_replace_keeppatterns()
  714. new
  715. a
  716. foobar
  717. substitute foo asdf foo
  718. one two
  719. .
  720. normal gg
  721. /^substitute
  722. s/foo/bar/
  723. call assert_equal('foo', @/)
  724. call assert_equal('substitute bar asdf foo', getline('.'))
  725. /^substitute
  726. keeppatterns s/asdf/xyz/
  727. call assert_equal('^substitute', @/)
  728. call assert_equal('substitute bar xyz foo', getline('.'))
  729. /^substitute
  730. &
  731. call assert_equal('^substitute', @/)
  732. call assert_equal('substitute bar xyz bar', getline('.'))
  733. exe "normal /bar /e\<CR>"
  734. call assert_equal(15, col('.'))
  735. normal -
  736. keeppatterns /xyz
  737. call assert_equal('bar ', @/)
  738. call assert_equal('substitute bar xyz bar', getline('.'))
  739. exe "normal 0dn"
  740. call assert_equal('xyz bar', getline('.'))
  741. close!
  742. endfunc
  743. func Test_sub_beyond_end()
  744. new
  745. call setline(1, '#')
  746. let @/ = '^#\n\zs'
  747. s///e
  748. call assert_equal('#', getline(1))
  749. bwipe!
  750. endfunc
  751. " Test for repeating last substitution using :~ and :&r
  752. func Test_repeat_last_sub()
  753. new
  754. call setline(1, ['blue green yellow orange white'])
  755. s/blue/red/
  756. let @/ = 'yellow'
  757. ~
  758. let @/ = 'white'
  759. :&r
  760. let @/ = 'green'
  761. s//gray
  762. call assert_equal('red gray red orange red', getline(1))
  763. close!
  764. endfunc
  765. " Test for Vi compatible substitution:
  766. " \/{string}/, \?{string}? and \&{string}&
  767. func Test_sub_vi_compatibility()
  768. new
  769. call setline(1, ['blue green yellow orange blue'])
  770. let @/ = 'orange'
  771. s\/white/
  772. let @/ = 'blue'
  773. s\?amber?
  774. let @/ = 'white'
  775. s\&green&
  776. call assert_equal('amber green yellow white green', getline(1))
  777. close!
  778. endfunc
  779. " Test for substitute with the new text longer than the original text
  780. func Test_sub_expand_text()
  781. new
  782. call setline(1, 'abcabcabcabcabcabcabcabc')
  783. s/b/\=repeat('B', 10)/g
  784. call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1))
  785. close!
  786. endfunc
  787. " Test for command failures when the last substitute pattern is not set.
  788. func Test_sub_with_no_last_pat()
  789. let lines =<< trim [SCRIPT]
  790. call assert_fails('~', 'E33:')
  791. call assert_fails('s//abc/g', 'E35:')
  792. call assert_fails('s\/bar', 'E35:')
  793. call assert_fails('s\&bar&', 'E33:')
  794. call writefile(v:errors, 'Xresult')
  795. qall!
  796. [SCRIPT]
  797. call writefile(lines, 'Xscript', 'D')
  798. if RunVim([], [], '--clean -S Xscript')
  799. call assert_equal([], readfile('Xresult'))
  800. endif
  801. let lines =<< trim [SCRIPT]
  802. set cpo+=/
  803. call assert_fails('s/abc/%/', 'E33:')
  804. call writefile(v:errors, 'Xresult')
  805. qall!
  806. [SCRIPT]
  807. " Nvim: no "/" flag in 'cpoptions'.
  808. " call writefile(lines, 'Xscript')
  809. " if RunVim([], [], '--clean -S Xscript')
  810. " call assert_equal([], readfile('Xresult'))
  811. " endif
  812. call delete('Xresult')
  813. endfunc
  814. func Test_substitute()
  815. call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
  816. " Substitute with special keys
  817. call assert_equal("a\<End>c", substitute('abc', "a.c", "a\<End>c", ''))
  818. endfunc
  819. func Test_substitute_expr()
  820. let g:val = 'XXX'
  821. call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', ''))
  822. call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, ''))
  823. call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
  824. \ '\=nr2char("0x" . submatch(1))', 'g'))
  825. call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
  826. \ {-> nr2char("0x" . submatch(1))}, 'g'))
  827. call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)',
  828. \ {-> submatch(2) . submatch(3) . submatch(1)}, ''))
  829. func Recurse()
  830. return substitute('yyy', 'y\(.\)y', {-> submatch(1)}, '')
  831. endfunc
  832. " recursive call works
  833. call assert_equal('-y-x-', substitute('xxx', 'x\(.\)x', {-> '-' . Recurse() . '-' . submatch(1) . '-'}, ''))
  834. call assert_fails("let s=submatch([])", 'E745:')
  835. call assert_fails("let s=submatch(2, [])", 'E745:')
  836. endfunc
  837. func Test_invalid_submatch()
  838. " This was causing invalid memory access in Vim-7.4.2232 and older
  839. call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:')
  840. call assert_fails('eval submatch(-1)', 'E935:')
  841. call assert_equal('', submatch(0))
  842. call assert_equal('', submatch(1))
  843. call assert_equal([], submatch(0, 1))
  844. call assert_equal([], submatch(1, 1))
  845. endfunc
  846. func Test_submatch_list_concatenate()
  847. let pat = 'A\(.\)'
  848. let Rep = {-> string([submatch(0, 1)] + [[submatch(1)]])}
  849. call substitute('A1', pat, Rep, '')->assert_equal("[['A1'], ['1']]")
  850. endfunc
  851. func Test_substitute_expr_arg()
  852. call assert_equal('123456789-123456789=', substitute('123456789',
  853. \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
  854. \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
  855. call assert_equal('123456-123456=789', substitute('123456789',
  856. \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)',
  857. \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
  858. call assert_equal('123456789-123456789x=', substitute('123456789',
  859. \ '\(.\)\(.\)\(.*\)',
  860. \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
  861. call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:')
  862. call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:')
  863. call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:')
  864. call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:')
  865. endfunc
  866. " Test for using a function to supply the substitute string
  867. func Test_substitute_using_func()
  868. func Xfunc()
  869. return '1234'
  870. endfunc
  871. call assert_equal('a1234f', substitute('abcdef', 'b..e',
  872. \ function("Xfunc"), ''))
  873. delfunc Xfunc
  874. endfunc
  875. " Test for using submatch() with a multiline match
  876. func Test_substitute_multiline_submatch()
  877. new
  878. call setline(1, ['line1', 'line2', 'line3', 'line4'])
  879. %s/^line1\(\_.\+\)line4$/\=submatch(1)/
  880. call assert_equal(['', 'line2', 'line3', ''], getline(1, '$'))
  881. close!
  882. endfunc
  883. func Test_substitute_skipped_range()
  884. new
  885. if 0
  886. /1/5/2/2/\n
  887. endif
  888. call assert_equal([0, 1, 1, 0, 1], getcurpos())
  889. bwipe!
  890. endfunc
  891. " Test using the 'gdefault' option (when on, flag 'g' is default on).
  892. func Test_substitute_gdefault()
  893. new
  894. " First check without 'gdefault'
  895. call setline(1, 'foo bar foo')
  896. s/foo/FOO/
  897. call assert_equal('FOO bar foo', getline(1))
  898. call setline(1, 'foo bar foo')
  899. s/foo/FOO/g
  900. call assert_equal('FOO bar FOO', getline(1))
  901. call setline(1, 'foo bar foo')
  902. s/foo/FOO/gg
  903. call assert_equal('FOO bar foo', getline(1))
  904. " Then check with 'gdefault'
  905. set gdefault
  906. call setline(1, 'foo bar foo')
  907. s/foo/FOO/
  908. call assert_equal('FOO bar FOO', getline(1))
  909. call setline(1, 'foo bar foo')
  910. s/foo/FOO/g
  911. call assert_equal('FOO bar foo', getline(1))
  912. call setline(1, 'foo bar foo')
  913. s/foo/FOO/gg
  914. call assert_equal('FOO bar FOO', getline(1))
  915. " Setting 'compatible' should reset 'gdefault'
  916. call assert_equal(1, &gdefault)
  917. " set compatible
  918. set nogdefault
  919. call assert_equal(0, &gdefault)
  920. set nocompatible
  921. call assert_equal(0, &gdefault)
  922. bw!
  923. endfunc
  924. " This was using "old_sub" after it was freed.
  925. func Test_using_old_sub()
  926. " set compatible maxfuncdepth=10
  927. set maxfuncdepth=10
  928. new
  929. call setline(1, 'some text.')
  930. func Repl()
  931. ~
  932. s/
  933. endfunc
  934. silent! s/\%')/\=Repl()
  935. delfunc Repl
  936. bwipe!
  937. set nocompatible
  938. endfunc
  939. " This was switching windows in between computing the length and using it.
  940. func Test_sub_change_window()
  941. silent! lfile
  942. sil! norm o0000000000000000000000000000000000000000000000000000
  943. func Repl()
  944. lopen
  945. endfunc
  946. silent! s/\%')/\=Repl()
  947. bwipe!
  948. bwipe!
  949. delfunc Repl
  950. endfunc
  951. " This was undoign a change in between computing the length and using it.
  952. func Do_Test_sub_undo_change()
  953. new
  954. norm o0000000000000000000000000000000000000000000000000000
  955. silent! s/\%')/\=Repl()
  956. bwipe!
  957. endfunc
  958. func Test_sub_undo_change()
  959. func Repl()
  960. silent! norm g-
  961. endfunc
  962. call Do_Test_sub_undo_change()
  963. func! Repl()
  964. silent earlier
  965. endfunc
  966. call Do_Test_sub_undo_change()
  967. delfunc Repl
  968. endfunc
  969. " This was opening a command line window from the expression
  970. func Test_sub_open_cmdline_win()
  971. " the error only happens in a very specific setup, run a new Vim instance to
  972. " get a clean starting point.
  973. let lines =<< trim [SCRIPT]
  974. set vb t_vb=
  975. norm o0000000000000000000000000000000000000000000000000000
  976. func Replace()
  977. norm q/
  978. endfunc
  979. s/\%')/\=Replace()
  980. redir >Xresult
  981. messages
  982. redir END
  983. qall!
  984. [SCRIPT]
  985. call writefile(lines, 'Xscript', 'D')
  986. if RunVim([], [], '-u NONE -S Xscript')
  987. call assert_match('E565: Not allowed to change text or change window',
  988. \ readfile('Xresult')->join('XX'))
  989. endif
  990. call delete('Xresult')
  991. endfunc
  992. " This was editing a script file from the expression
  993. func Test_sub_edit_scriptfile()
  994. new
  995. norm o0000000000000000000000000000000000000000000000000000
  996. func EditScript()
  997. silent! scr! Xfile
  998. endfunc
  999. s/\%')/\=EditScript()
  1000. delfunc EditScript
  1001. bwipe!
  1002. endfunc
  1003. " This was editing another file from the expression.
  1004. func Test_sub_expr_goto_other_file()
  1005. call writefile([''], 'Xfileone', 'D')
  1006. enew!
  1007. call setline(1, ['a', 'b', 'c', 'd',
  1008. \ 'Xfileone zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'])
  1009. func g:SplitGotoFile()
  1010. exe "sil! norm 0\<C-W>gf"
  1011. return ''
  1012. endfunc
  1013. $
  1014. s/\%')/\=g:SplitGotoFile()
  1015. delfunc g:SplitGotoFile
  1016. bwipe!
  1017. endfunc
  1018. func Test_recursive_expr_substitute()
  1019. " this was reading invalid memory
  1020. let lines =<< trim END
  1021. func Repl(g, n)
  1022. s
  1023. r%:s000
  1024. endfunc
  1025. next 0
  1026. let caught = 0
  1027. s/\%')/\=Repl(0, 0)
  1028. qall!
  1029. END
  1030. call writefile(lines, 'XexprSubst', 'D')
  1031. call RunVim([], [], '--clean -S XexprSubst')
  1032. endfunc
  1033. " Test for the 2-letter and 3-letter :substitute commands
  1034. func Test_substitute_short_cmd()
  1035. new
  1036. call setline(1, ['one', 'one one one'])
  1037. s/one/two
  1038. call cursor(2, 1)
  1039. " :sc
  1040. call feedkeys(":sc\<CR>y", 'xt')
  1041. call assert_equal('two one one', getline(2))
  1042. " :scg
  1043. call setline(2, 'one one one')
  1044. call feedkeys(":scg\<CR>nyq", 'xt')
  1045. call assert_equal('one two one', getline(2))
  1046. " :sci
  1047. call setline(2, 'ONE One onE')
  1048. call feedkeys(":sci\<CR>y", 'xt')
  1049. call assert_equal('two One onE', getline(2))
  1050. " :scI
  1051. set ignorecase
  1052. call setline(2, 'ONE One one')
  1053. call feedkeys(":scI\<CR>y", 'xt')
  1054. call assert_equal('ONE One two', getline(2))
  1055. set ignorecase&
  1056. " :scn
  1057. call setline(2, 'one one one')
  1058. let t = execute('scn')->split("\n")
  1059. call assert_equal(['1 match on 1 line'], t)
  1060. call assert_equal('one one one', getline(2))
  1061. " :scp
  1062. call setline(2, "\tone one one")
  1063. redir => output
  1064. call feedkeys(":scp\<CR>y", 'xt')
  1065. redir END
  1066. call assert_equal(' two one one', output->split("\n")[-1])
  1067. call assert_equal("\ttwo one one", getline(2))
  1068. " :scl
  1069. call setline(2, "\tone one one")
  1070. redir => output
  1071. call feedkeys(":scl\<CR>y", 'xt')
  1072. redir END
  1073. call assert_equal("^Itwo one one$", output->split("\n")[-1])
  1074. call assert_equal("\ttwo one one", getline(2))
  1075. " :sgc
  1076. call setline(2, 'one one one one one')
  1077. call feedkeys(":sgc\<CR>nyyq", 'xt')
  1078. call assert_equal('one two two one one', getline(2))
  1079. " :sg
  1080. call setline(2, 'one one one')
  1081. sg
  1082. call assert_equal('two two two', getline(2))
  1083. " :sgi
  1084. call setline(2, 'ONE One onE')
  1085. sgi
  1086. call assert_equal('two two two', getline(2))
  1087. " :sgI
  1088. set ignorecase
  1089. call setline(2, 'ONE One one')
  1090. sgI
  1091. call assert_equal('ONE One two', getline(2))
  1092. set ignorecase&
  1093. " :sgn
  1094. call setline(2, 'one one one')
  1095. let t = execute('sgn')->split("\n")
  1096. call assert_equal(['3 matches on 1 line'], t)
  1097. call assert_equal('one one one', getline(2))
  1098. " :sgp
  1099. call setline(2, "\tone one one")
  1100. redir => output
  1101. sgp
  1102. redir END
  1103. call assert_equal(' two two two', output->split("\n")[-1])
  1104. call assert_equal("\ttwo two two", getline(2))
  1105. " :sgl
  1106. call setline(2, "\tone one one")
  1107. redir => output
  1108. sgl
  1109. redir END
  1110. call assert_equal("^Itwo two two$", output->split("\n")[-1])
  1111. call assert_equal("\ttwo two two", getline(2))
  1112. " :sgr
  1113. call setline(2, "one one one")
  1114. call cursor(2, 1)
  1115. s/abc/xyz/e
  1116. let @/ = 'one'
  1117. sgr
  1118. call assert_equal('xyz xyz xyz', getline(2))
  1119. " :sic
  1120. call cursor(1, 1)
  1121. s/one/two/e
  1122. call setline(2, "ONE One one")
  1123. call cursor(2, 1)
  1124. call feedkeys(":sic\<CR>y", 'xt')
  1125. call assert_equal('two One one', getline(2))
  1126. " :si
  1127. call setline(2, "ONE One one")
  1128. si
  1129. call assert_equal('two One one', getline(2))
  1130. " :siI
  1131. call setline(2, "ONE One one")
  1132. siI
  1133. call assert_equal('ONE One two', getline(2))
  1134. " :sin
  1135. call setline(2, 'ONE One onE')
  1136. let t = execute('sin')->split("\n")
  1137. call assert_equal(['1 match on 1 line'], t)
  1138. call assert_equal('ONE One onE', getline(2))
  1139. " :sip
  1140. call setline(2, "\tONE One onE")
  1141. redir => output
  1142. sip
  1143. redir END
  1144. call assert_equal(' two One onE', output->split("\n")[-1])
  1145. call assert_equal("\ttwo One onE", getline(2))
  1146. " :sir
  1147. call setline(2, "ONE One onE")
  1148. call cursor(2, 1)
  1149. s/abc/xyz/e
  1150. let @/ = 'one'
  1151. sir
  1152. call assert_equal('xyz One onE', getline(2))
  1153. " :sIc
  1154. call cursor(1, 1)
  1155. s/one/two/e
  1156. call setline(2, "ONE One one")
  1157. call cursor(2, 1)
  1158. call feedkeys(":sIc\<CR>y", 'xt')
  1159. call assert_equal('ONE One two', getline(2))
  1160. " :sIg
  1161. call setline(2, "ONE one onE one")
  1162. sIg
  1163. call assert_equal('ONE two onE two', getline(2))
  1164. " :sIi
  1165. call setline(2, "ONE One one")
  1166. sIi
  1167. call assert_equal('two One one', getline(2))
  1168. " :sI
  1169. call setline(2, "ONE One one")
  1170. sI
  1171. call assert_equal('ONE One two', getline(2))
  1172. " :sIn
  1173. call setline(2, 'ONE One one')
  1174. let t = execute('sIn')->split("\n")
  1175. call assert_equal(['1 match on 1 line'], t)
  1176. call assert_equal('ONE One one', getline(2))
  1177. " :sIp
  1178. call setline(2, "\tONE One one")
  1179. redir => output
  1180. sIp
  1181. redir END
  1182. call assert_equal(' ONE One two', output->split("\n")[-1])
  1183. call assert_equal("\tONE One two", getline(2))
  1184. " :sIl
  1185. call setline(2, "\tONE onE one")
  1186. redir => output
  1187. sIl
  1188. redir END
  1189. call assert_equal("^IONE onE two$", output->split("\n")[-1])
  1190. call assert_equal("\tONE onE two", getline(2))
  1191. " :sIr
  1192. call setline(2, "ONE one onE")
  1193. call cursor(2, 1)
  1194. s/abc/xyz/e
  1195. let @/ = 'one'
  1196. sIr
  1197. call assert_equal('ONE xyz onE', getline(2))
  1198. " :src
  1199. call setline(2, "ONE one one")
  1200. call cursor(2, 1)
  1201. s/abc/xyz/e
  1202. let @/ = 'one'
  1203. call feedkeys(":src\<CR>y", 'xt')
  1204. call assert_equal('ONE xyz one', getline(2))
  1205. " :srg
  1206. call setline(2, "one one one")
  1207. call cursor(2, 1)
  1208. s/abc/xyz/e
  1209. let @/ = 'one'
  1210. srg
  1211. call assert_equal('xyz xyz xyz', getline(2))
  1212. " :sri
  1213. call setline(2, "ONE one onE")
  1214. call cursor(2, 1)
  1215. s/abc/xyz/e
  1216. let @/ = 'one'
  1217. sri
  1218. call assert_equal('xyz one onE', getline(2))
  1219. " :srI
  1220. call setline(2, "ONE one onE")
  1221. call cursor(2, 1)
  1222. s/abc/xyz/e
  1223. let @/ = 'one'
  1224. srI
  1225. call assert_equal('ONE xyz onE', getline(2))
  1226. " :srn
  1227. call setline(2, "ONE one onE")
  1228. call cursor(2, 1)
  1229. s/abc/xyz/e
  1230. let @/ = 'one'
  1231. let t = execute('srn')->split("\n")
  1232. call assert_equal(['1 match on 1 line'], t)
  1233. call assert_equal('ONE one onE', getline(2))
  1234. " :srp
  1235. call setline(2, "\tONE one onE")
  1236. call cursor(2, 1)
  1237. s/abc/xyz/e
  1238. let @/ = 'one'
  1239. redir => output
  1240. srp
  1241. redir END
  1242. call assert_equal(' ONE xyz onE', output->split("\n")[-1])
  1243. call assert_equal("\tONE xyz onE", getline(2))
  1244. " :srl
  1245. call setline(2, "\tONE one onE")
  1246. call cursor(2, 1)
  1247. s/abc/xyz/e
  1248. let @/ = 'one'
  1249. redir => output
  1250. srl
  1251. redir END
  1252. call assert_equal("^IONE xyz onE$", output->split("\n")[-1])
  1253. call assert_equal("\tONE xyz onE", getline(2))
  1254. " :sr
  1255. call setline(2, "ONE one onE")
  1256. call cursor(2, 1)
  1257. s/abc/xyz/e
  1258. let @/ = 'one'
  1259. sr
  1260. call assert_equal('ONE xyz onE', getline(2))
  1261. " :sce
  1262. s/abc/xyz/e
  1263. call assert_fails("sc", 'E486:')
  1264. sce
  1265. " :sge
  1266. call assert_fails("sg", 'E486:')
  1267. sge
  1268. " :sie
  1269. call assert_fails("si", 'E486:')
  1270. sie
  1271. " :sIe
  1272. call assert_fails("sI", 'E486:')
  1273. sIe
  1274. bw!
  1275. endfunc
  1276. " Check handling expanding "~" resulting in extremely long text.
  1277. " FIXME: disabled, it takes too long to run on CI
  1278. "func Test_substitute_tilde_too_long()
  1279. " enew!
  1280. "
  1281. " s/.*/ixxx
  1282. " s//~~~~~~~~~AAAAAAA@(
  1283. "
  1284. " " Either fails with "out of memory" or "text too long".
  1285. " " This can take a long time.
  1286. " call assert_fails('sil! norm &&&&&&&&&', ['E1240:\|E342:'])
  1287. "
  1288. " bwipe!
  1289. "endfunc
  1290. " This should be done last to reveal a memory leak when vim_regsub_both() is
  1291. " called to evaluate an expression but it is not used in a second call.
  1292. func Test_z_substitute_expr_leak()
  1293. func SubExpr()
  1294. ~n
  1295. endfunc
  1296. silent! s/\%')/\=SubExpr()
  1297. delfunc SubExpr
  1298. endfunc
  1299. func Test_substitute_expr_switch_win()
  1300. func R()
  1301. wincmd x
  1302. return 'XXXX'
  1303. endfunc
  1304. new Xfoobar
  1305. let bufnr = bufnr('%')
  1306. put ='abcdef'
  1307. silent! s/\%')/\=R()
  1308. call assert_fails(':%s/./\=R()/g', 'E565:')
  1309. delfunc R
  1310. exe bufnr .. "bw!"
  1311. endfunc
  1312. " recursive call of :s using test-replace special
  1313. func Test_substitute_expr_recursive()
  1314. func Q()
  1315. %s/./\='foobar'/gn
  1316. return "foobar"
  1317. endfunc
  1318. func R()
  1319. %s/./\=Q()/g
  1320. endfunc
  1321. new Xfoobar_UAF
  1322. let bufnr = bufnr('%')
  1323. put ='abcdef'
  1324. silent! s/./\=R()/g
  1325. call assert_fails(':%s/./\=R()/g', 'E565:')
  1326. delfunc R
  1327. delfunc Q
  1328. exe bufnr .. "bw!"
  1329. endfunc
  1330. " Test for changing 'cpo' in a substitute expression
  1331. func Test_substitute_expr_cpo()
  1332. func XSubExpr()
  1333. set cpo=
  1334. return 'x'
  1335. endfunc
  1336. let save_cpo = &cpo
  1337. call assert_equal('xxx', substitute('abc', '.', '\=XSubExpr()', 'g'))
  1338. call assert_equal(save_cpo, &cpo)
  1339. delfunc XSubExpr
  1340. endfunc
  1341. " vim: shiftwidth=2 sts=2 expandtab