1" Tests for multi-line regexps with ":s".
2
3function! 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!
41endfunction
42
43function! 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
108endfunction
109
110func 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!
116endfunc
117