blob: 320cd169b639f77ea32d0b8e90a2aeddec901c45 [file] [log] [blame]
Bram Moolenaar00b24be2016-07-23 22:04:47 +02001" Test for textobjects
2
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02003func CpoM(line, useM, expected)
Bram Moolenaar00b24be2016-07-23 22:04:47 +02004 new
5
6 if a:useM
7 set cpoptions+=M
8 else
9 set cpoptions-=M
10 endif
11
12 call setline(1, a:line)
13
14 call setreg('"', '')
15 normal! ggfrmavi)y
16 call assert_equal(getreg('"'), a:expected[0])
17
18 call setreg('"', '')
19 normal! `afbmavi)y
20 call assert_equal(getreg('"'), a:expected[1])
21
22 call setreg('"', '')
23 normal! `afgmavi)y
24 call assert_equal(getreg('"'), a:expected[2])
25
26 q!
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020027endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020028
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020029func Test_inner_block_without_cpo_M()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020030 call CpoM('(red \(blue) green)', 0, ['red \(blue', 'red \(blue', ''])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020031endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020032
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020033func Test_inner_block_with_cpo_M_left_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020034 call CpoM('(red \(blue) green)', 1, ['red \(blue) green', 'blue', 'red \(blue) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020035endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020036
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020037func Test_inner_block_with_cpo_M_right_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020038 call CpoM('(red (blue\) green)', 1, ['red (blue\) green', 'blue\', 'red (blue\) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020039endfunc
40
LemonBoy53737b52022-05-24 11:49:31 +010041func Test_inner_block_single_char()
42 new
43 call setline(1, "(a)")
44
45 set selection=inclusive
46 let @" = ''
47 call assert_nobeep('norm! 0faviby')
48 call assert_equal('a', @")
49
50 set selection=exclusive
51 let @" = ''
52 call assert_nobeep('norm! 0faviby')
53 call assert_equal('a', @")
54
55 set selection&
56 bwipe!
57endfunc
58
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020059func Test_quote_selection_selection_exclusive()
60 new
61 call setline(1, "a 'bcde' f")
62 set selection=exclusive
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010063
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020064 exe "norm! fdvhi'y"
65 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010066
LemonBoy53737b52022-05-24 11:49:31 +010067 let @" = 'dummy'
Bram Moolenaar7170b292019-11-17 17:32:28 +010068 exe "norm! $gevi'y"
69 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010070
LemonBoy53737b52022-05-24 11:49:31 +010071 let @" = 'dummy'
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010072 exe "norm! 0fbhvi'y"
73 call assert_equal('bcde', @")
74
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020075 set selection&vim
76 bw!
77endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +020078
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +010079func Test_quote_selection_selection_exclusive_abort()
80 new
81 set selection=exclusive
82 call setline(1, "'abzzc'")
83 let exp_curs = [0, 1, 6, 0]
84 call cursor(1,1)
85 exe 'norm! fcdvi"'
86 " make sure to end visual mode to have a clear state
87 exe "norm! \<esc>"
88 call assert_equal(exp_curs, getpos('.'))
89 call cursor(1,1)
90 exe 'norm! fcvi"'
91 exe "norm! \<esc>"
92 call assert_equal(exp_curs, getpos('.'))
93 call cursor(1,2)
94 exe 'norm! vfcoi"'
95 exe "norm! \<esc>"
96 let exp_curs = [0, 1, 2, 0]
97 let exp_visu = [0, 1, 7, 0]
98 call assert_equal(exp_curs, getpos('.'))
99 call assert_equal(exp_visu, getpos("'>"))
100 set selection&vim
101 bw!
102endfunc
103
Bram Moolenaardb510072017-09-28 21:52:17 +0200104" Tests for string and html text objects
105func Test_string_html_objects()
Bram Moolenaardb510072017-09-28 21:52:17 +0200106
Dominique Pelleaf631f62021-09-03 16:50:16 +0200107 for e in ['utf-8', 'latin1', 'cp932']
108 enew!
109 exe 'set enc=' .. e
Bram Moolenaardb510072017-09-28 21:52:17 +0200110
Dominique Pelleaf631f62021-09-03 16:50:16 +0200111 let t = '"wo\"rd\\" foo'
112 put =t
113 normal! da"
114 call assert_equal('foo', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200115
Dominique Pelleaf631f62021-09-03 16:50:16 +0200116 let t = "'foo' 'bar' 'piep'"
117 put =t
118 normal! 0va'a'rx
119 call assert_equal("xxxxxxxxxxxx'piep'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200120
Dominique Pelleaf631f62021-09-03 16:50:16 +0200121 let t = "bla bla `quote` blah"
122 put =t
123 normal! 02f`da`
124 call assert_equal("bla bla blah", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200125
Dominique Pelleaf631f62021-09-03 16:50:16 +0200126 let t = 'out " in "noXno"'
127 put =t
128 normal! 0fXdi"
129 call assert_equal('out " in ""', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200130
Dominique Pelleaf631f62021-09-03 16:50:16 +0200131 let t = "\"'\" 'blah' rep 'buh'"
132 put =t
133 normal! 03f'vi'ry
134 call assert_equal("\"'\" 'blah'yyyyy'buh'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200135
Dominique Pelleaf631f62021-09-03 16:50:16 +0200136 set quoteescape=+*-
137 let t = "bla `s*`d-`+++`l**` b`la"
138 put =t
139 normal! di`
140 call assert_equal("bla `` b`la", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200141
Dominique Pelleaf631f62021-09-03 16:50:16 +0200142 let t = 'voo "nah" sdf " asdf" sdf " sdf" sd'
143 put =t
144 normal! $F"va"oha"i"rz
145 call assert_equal('voo "zzzzzzzzzzzzzzzzzzzzzzzzzzzzsd', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200146
Dominique Pelleaf631f62021-09-03 16:50:16 +0200147 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
148 put =t
149 normal! fXdit
150 call assert_equal('-<b>asdf<i></i>asdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200151
Dominique Pelleaf631f62021-09-03 16:50:16 +0200152 let t = "-<b>asdX<i>a<i />sdf</i>asdf</b>-"
153 put =t
154 normal! 0fXdit
155 call assert_equal('-<b></b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200156
Dominique Pelleaf631f62021-09-03 16:50:16 +0200157 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
158 put =t
159 normal! fXdat
160 call assert_equal('-<b>asdfasdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200161
Dominique Pelleaf631f62021-09-03 16:50:16 +0200162 let t = "-<b>asdX<i>as<b />df</i>asdf</b>-"
163 put =t
164 normal! 0fXdat
165 call assert_equal('--', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200166
Dominique Pelleaf631f62021-09-03 16:50:16 +0200167 let t = "-<b>\ninnertext object\n</b>"
168 put =t
169 normal! dit
170 call assert_equal('-<b></b>', getline('.'), e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200171
Dominique Pelleaf631f62021-09-03 16:50:16 +0200172 " copy the tag block from leading indentation before the start tag
173 let t = " <b>\ntext\n</b>"
174 $put =t
175 normal! 2kvaty
176 call assert_equal("<b>\ntext\n</b>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200177
Dominique Pelleaf631f62021-09-03 16:50:16 +0200178 " copy the tag block from the end tag
179 let t = "<title>\nwelcome\n</title>"
180 $put =t
181 normal! $vaty
182 call assert_equal("<title>\nwelcome\n</title>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200183
Dominique Pelleaf631f62021-09-03 16:50:16 +0200184 " copy the outer tag block from a tag without an end tag
185 let t = "<html>\n<title>welcome\n</html>"
186 $put =t
187 normal! k$vaty
188 call assert_equal("<html>\n<title>welcome\n</html>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200189
Dominique Pelleaf631f62021-09-03 16:50:16 +0200190 " nested tag that has < in a different line from >
191 let t = "<div><div\n></div></div>"
192 $put =t
193 normal! k0vaty
194 call assert_equal("<div><div\n></div></div>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200195
Dominique Pelleaf631f62021-09-03 16:50:16 +0200196 " nested tag with attribute that has < in a different line from >
197 let t = "<div><div\nattr=\"attr\"\n></div></div>"
198 $put =t
199 normal! 2k0vaty
200 call assert_equal("<div><div\nattr=\"attr\"\n></div></div>", @", e)
201
Christian Brabandtca7f93e2024-06-19 20:26:51 +0200202 " tag, that includes a > in some attribute
203 let t = "<div attr=\"attr >> foo >> bar \">Hello</div>"
204 $put =t
205 normal! fHyit
206 call assert_equal("Hello", @", e)
207
208 " tag, that includes a > in some attribute
209 let t = "<div attr='attr >> foo >> bar '>Hello 123</div>"
210 $put =t
211 normal! fHyit
212 call assert_equal("Hello 123", @", e)
213
Dominique Pelleaf631f62021-09-03 16:50:16 +0200214 set quoteescape&
Bram Moolenaar53a70282022-05-09 13:15:07 +0100215
216 " this was going beyond the end of the line
217 %del
218 sil! norm i"\
219 sil! norm i"\
220 sil! norm i"\
221 call assert_equal('"\', getline(1))
222
223 bwipe!
Dominique Pelleaf631f62021-09-03 16:50:16 +0200224 endfor
225
226 set enc=utf-8
Bram Moolenaardb510072017-09-28 21:52:17 +0200227endfunc
228
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200229func Test_empty_html_tag()
230 new
231 call setline(1, '<div></div>')
232 normal 0citxxx
233 call assert_equal('<div>xxx</div>', getline(1))
234
235 call setline(1, '<div></div>')
236 normal 0f<cityyy
237 call assert_equal('<div>yyy</div>', getline(1))
238
239 call setline(1, '<div></div>')
240 normal 0f<vitsaaa
241 call assert_equal('aaa', getline(1))
242
Dominique Pelle923dce22021-11-21 11:36:04 +0000243 " selecting a tag block in a non-empty blank line should fail
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200244 call setline(1, ' ')
245 call assert_beeps('normal $vaty')
246
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200247 bwipe!
248endfunc
249
Bram Moolenaardb510072017-09-28 21:52:17 +0200250" Tests for match() and matchstr()
251func Test_match()
252 call assert_equal("b", matchstr("abcd", ".", 0, 2))
253 call assert_equal("bc", matchstr("abcd", "..", 0, 2))
254 call assert_equal("c", matchstr("abcd", ".", 2, 0))
255 call assert_equal("a", matchstr("abcd", ".", 0, -1))
256 call assert_equal(-1, match("abcd", ".", 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200257 call assert_equal(0, match("abcd", ".", 0, -1))
258 call assert_equal(0, match('abc', '.', 0, 1))
259 call assert_equal(1, match('abc', '.', 0, 2))
260 call assert_equal(2, match('abc', '.', 0, 3))
Bram Moolenaardb510072017-09-28 21:52:17 +0200261 call assert_equal(-1, match('abc', '.', 0, 4))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200262 call assert_equal(1, match('abc', '.', 1, 1))
263 call assert_equal(2, match('abc', '.', 2, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200264 call assert_equal(-1, match('abc', '.', 3, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200265 call assert_equal(3, match('abc', '$', 0, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200266 call assert_equal(-1, match('abc', '$', 0, 2))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200267 call assert_equal(3, match('abc', '$', 1, 1))
268 call assert_equal(3, match('abc', '$', 2, 1))
269 call assert_equal(3, match('abc', '$', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200270 call assert_equal(-1, match('abc', '$', 4, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200271 call assert_equal(0, match('abc', '\zs', 0, 1))
272 call assert_equal(1, match('abc', '\zs', 0, 2))
273 call assert_equal(2, match('abc', '\zs', 0, 3))
274 call assert_equal(3, match('abc', '\zs', 0, 4))
Bram Moolenaardb510072017-09-28 21:52:17 +0200275 call assert_equal(-1, match('abc', '\zs', 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200276 call assert_equal(1, match('abc', '\zs', 1, 1))
277 call assert_equal(2, match('abc', '\zs', 2, 1))
278 call assert_equal(3, match('abc', '\zs', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200279 call assert_equal(-1, match('abc', '\zs', 4, 1))
280endfunc
Bram Moolenaar82846a02018-02-09 18:09:54 +0100281
282" This was causing an illegal memory access
283func Test_inner_tag()
284 new
285 norm ixxx
286 call feedkeys("v", 'xt')
287 insert
288x
289x
290.
291 norm it
292 q!
293endfunc
Bram Moolenaar85160712018-06-19 18:27:41 +0200294
295func Test_sentence()
296 enew!
297 call setline(1, 'A sentence. A sentence? A sentence!')
298
299 normal yis
300 call assert_equal('A sentence.', @")
301 normal yas
302 call assert_equal('A sentence. ', @")
303
304 normal )
305
306 normal yis
307 call assert_equal('A sentence?', @")
308 normal yas
309 call assert_equal('A sentence? ', @")
310
311 normal )
312
313 normal yis
314 call assert_equal('A sentence!', @")
315 normal yas
316 call assert_equal(' A sentence!', @")
317
318 normal 0
319 normal 2yis
320 call assert_equal('A sentence. ', @")
321 normal 3yis
322 call assert_equal('A sentence. A sentence?', @")
323 normal 2yas
324 call assert_equal('A sentence. A sentence? ', @")
325
326 %delete _
327endfunc
328
329func Test_sentence_with_quotes()
330 enew!
331 call setline(1, 'A "sentence." A sentence.')
332
333 normal yis
334 call assert_equal('A "sentence."', @")
335 normal yas
336 call assert_equal('A "sentence." ', @")
337
338 normal )
339
340 normal yis
341 call assert_equal('A sentence.', @")
342 normal yas
343 call assert_equal(' A sentence.', @")
344
345 %delete _
346endfunc
347
Bram Moolenaar1e115362019-01-09 23:01:02 +0100348func Test_sentence_with_cursor_on_delimiter()
Bram Moolenaar85160712018-06-19 18:27:41 +0200349 enew!
350 call setline(1, "A '([sentence.])' A sentence.")
351
352 normal! 15|yis
353 call assert_equal("A '([sentence.])'", @")
354 normal! 15|yas
355 call assert_equal("A '([sentence.])' ", @")
356
357 normal! 16|yis
358 call assert_equal("A '([sentence.])'", @")
359 normal! 16|yas
360 call assert_equal("A '([sentence.])' ", @")
361
362 normal! 17|yis
363 call assert_equal("A '([sentence.])'", @")
364 normal! 17|yas
365 call assert_equal("A '([sentence.])' ", @")
366
Bram Moolenaar2f03e5a2020-06-18 15:33:25 +0200367 " don't get stuck on a quote at the start of a sentence
368 %delete _
369 call setline(1, ['A sentence.', '"A sentence"?', 'A sentence!'])
370 normal gg))
371 call assert_equal(3, getcurpos()[1])
372
373 %delete _
374 call setline(1, ['A sentence.', "'A sentence'?", 'A sentence!'])
375 normal gg))
376 call assert_equal(3, getcurpos()[1])
377
Bram Moolenaar85160712018-06-19 18:27:41 +0200378 %delete _
379endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200380
381" Test for the paragraph (ap) text object
382func Test_paragraph()
383 new
384 call setline(1, ['First line.', 'Second line.', 'Third line.'])
385 call cursor(2, 1)
386 normal vapy
387 call assert_equal("First line.\nSecond line.\nThird line.\n", @")
388
389 call cursor(2, 1)
390 call assert_beeps('normal vapapy')
391
392 call setline(1, ['First line.', 'Second line.', ' ', ''])
393 call cursor(1, 1)
394 normal vapy
395 call assert_equal("First line.\nSecond line.\n \n\n", @")
396
397 call setline(1, ['', '', '', 'First line.', 'Second line.'])
398 call cursor(2, 1)
399 normal yap
400 call assert_equal("\n\n\nFirst line.\nSecond line.\n", @")
401 call assert_beeps('normal 3yap')
402 exe "normal \<C-C>"
403
404 %d
405 call setline(1, [' ', ' ', ' '])
406 call cursor(2, 1)
407 normal Vipy
408 call assert_equal(" \n \n \n", @")
409 call cursor(2, 1)
410 call assert_beeps("normal Vipip")
411 exe "normal \<C-C>"
412
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100413 bw!
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200414endfunc
415
416" Tests for text object aw
417func Test_textobj_a_word()
418 new
419 call append(0, ['foobar,eins,foobar', 'foo,zwei,foo '])
420 " diw
421 norm! 1gg0diw
422 call assert_equal([',eins,foobar', 'foo,zwei,foo ', ''], getline(1,'$'))
423 " daw
424 norm! 2ggEdaw
425 call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
426 " daw the last word in a line
427 call setline(1, ['foo bar', 'foo bar', ''])
428 call cursor(1, 5)
429 normal daw
430 call assert_equal('foo', getline(1))
431 " aw in visual mode
432 call cursor(2, 5)
433 normal! vawx
434 call assert_equal('foo', getline(2))
435 %d
436 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
437 " diW
438 norm! 2ggwd2iW
439 call assert_equal(['foo eins foobar', 'foo foo ', ''], getline(1,'$'))
440 " daW
441 norm! 1ggd2aW
442 call assert_equal(['foobar', 'foo foo ', ''], getline(1,'$'))
443
444 %d
445 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
446 " aw in visual line mode switches to characterwise mode
447 norm! 2gg$Vawd
448 call assert_equal(['foo eins foobar', 'foo zwei foo'], getline(1,'$'))
449 norm! 1gg$Viwd
450 call assert_equal(['foo eins ', 'foo zwei foo'], getline(1,'$'))
451
452 " visually selecting a tab before a word with 'selection' set to 'exclusive'
453 set selection=exclusive
454 normal gg3lvlawy
455 call assert_equal("\teins", @")
456 " visually selecting a tab before a word with 'selection' set to 'inclusive'
457 set selection=inclusive
458 normal gg3lvlawy
459 call assert_equal("\teins\t", @")
460 set selection&
461
462 " selecting a word with no non-space characters in a buffer fails
463 %d
464 call setline(1, ' ')
465 call assert_beeps('normal 3lyaw')
466
467 " visually selecting words backwards with no more words to select
468 call setline(1, 'one two')
469 call assert_beeps('normal 2lvh2aw')
470 exe "normal \<C-C>"
471 call assert_beeps('normal $vh3aw')
472 exe "normal \<C-C>"
473 call setline(1, ['', 'one two'])
474 call assert_beeps('normal 2G2lvh3aw')
475 exe "normal \<C-C>"
476
477 " selecting words forward with no more words to select
478 %d
479 call setline(1, 'one a')
480 call assert_beeps('normal 0y3aw')
481 call setline(1, 'one two ')
482 call assert_beeps('normal 0y3aw')
483 call assert_beeps('normal 03ly2aw')
484
485 " clean up
486 bw!
487endfunc
488
489" Test for is and as text objects
490func Test_textobj_sentence()
491 new
492 call append(0, ['This is a test. With some sentences!', '',
493 \ 'Even with a question? And one more. And no sentence here'])
494 " Test for dis - does not remove trailing whitespace
495 norm! 1gg0dis
496 call assert_equal([' With some sentences!', '',
497 \ 'Even with a question? And one more. And no sentence here', ''],
498 \ getline(1,'$'))
499 " Test for das - removes leading whitespace
500 norm! 3ggf?ldas
501 call assert_equal([' With some sentences!', '',
502 \ 'Even with a question? And no sentence here', ''], getline(1,'$'))
503 " when used in visual mode, is made characterwise
504 norm! 3gg$Visy
505 call assert_equal('v', visualmode())
506 " reset visualmode()
507 norm! 3ggVy
508 norm! 3gg$Vasy
509 call assert_equal('v', visualmode())
510 " basic testing for textobjects a< and at
511 %d
512 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
513 " a<
514 norm! 1gg0da<
515 call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
516 norm! 1pj
517 call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
518 " at
519 norm! d2at
520 call assert_equal([' '], getline(1,'$'))
521 %d
522 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
523 " i<
524 norm! 1gg0di<
525 call assert_equal(['<> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
526 norm! 1Pj
527 call assert_equal(['<div> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
528 norm! d2it
529 call assert_equal(['<div></div>',' '], getline(1,'$'))
530 " basic testing for a[ and i[ text object
531 %d
532 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
533 norm! 3gg0di[
534 call assert_equal([' ', '[', ']'], getline(1,'$'))
535 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
536 norm! 3gg0ftd2a[
537 call assert_equal([' '], getline(1,'$'))
538
539 " clean up
540 bw!
541endfunc
542
543" Test for quote (', " and `) textobjects
544func Test_textobj_quote()
545 new
546
547 " Test for i" when cursor is in front of a quoted object
548 call append(0, 'foo "bar"')
549 norm! 1gg0di"
550 call assert_equal(['foo ""', ''], getline(1,'$'))
551
552 " Test for visually selecting an inner quote
553 %d
554 " extend visual selection from one quote to the next
555 call setline(1, 'color "red" color "blue"')
556 call cursor(1, 7)
557 normal v4li"y
558 call assert_equal('"red" color "blue', @")
559
560 " try to extend visual selection from one quote to a non-existing quote
561 call setline(1, 'color "red" color blue')
562 call cursor(1, 7)
563 call feedkeys('v4li"y', 'xt')
564 call assert_equal('"red"', @")
565
566 " try to extend visual selection from one quote to a next partial quote
567 call setline(1, 'color "red" color "blue')
568 call cursor(1, 7)
569 normal v4li"y
570 call assert_equal('"red" color ', @")
571
572 " select a quote backwards in visual mode
573 call cursor(1, 12)
574 normal vhi"y
575 call assert_equal('red" ', @")
576 call assert_equal(8, col('.'))
577
578 " select a quote backwards in visual mode from outside the quote
579 call cursor(1, 17)
580 normal v2hi"y
581 call assert_equal('red', @")
582 call assert_equal(8, col('.'))
583
584 " visually selecting a quote with 'selection' set to 'exclusive'
585 call setline(1, 'He said "How are you?"')
586 set selection=exclusive
587 normal 012lv2li"y
588 call assert_equal('How are you?', @")
589 set selection&
590
591 " try copy a quote object with a single quote in the line
592 call setline(1, "Smith's car")
593 call cursor(1, 6)
594 call assert_beeps("normal yi'")
595 call assert_beeps("normal 2lyi'")
596
597 " selecting space before and after a quoted string
598 call setline(1, "some 'special' string")
599 normal 0ya'
600 call assert_equal("'special' ", @")
601 call setline(1, "some 'special'string")
602 normal 0ya'
603 call assert_equal(" 'special'", @")
604
Dominique Pelleaf631f62021-09-03 16:50:16 +0200605 " quoted string with odd or even number of backslashes.
606 call setline(1, 'char *s = "foo\"bar"')
607 normal $hhyi"
608 call assert_equal('foo\"bar', @")
609 call setline(1, 'char *s = "foo\\"bar"')
610 normal $hhyi"
611 call assert_equal('bar', @")
612 call setline(1, 'char *s = "foo\\\"bar"')
613 normal $hhyi"
614 call assert_equal('foo\\\"bar', @")
615 call setline(1, 'char *s = "foo\\\\"bar"')
616 normal $hhyi"
617 call assert_equal('bar', @")
618
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100619 bw!
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200620endfunc
621
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200622" Test for i(, i<, etc. when cursor is in front of a block
623func Test_textobj_find_paren_forward()
624 new
625
626 " i< and a> when cursor is in front of a block
627 call setline(1, '#include <foo.h>')
628 normal 0yi<
629 call assert_equal('foo.h', @")
630 normal 0ya>
631 call assert_equal('<foo.h>', @")
632
633 " 2i(, 3i( in front of a block enters second/third nested '('
634 call setline(1, 'foo (bar (baz (quux)))')
635 normal 0yi)
636 call assert_equal('bar (baz (quux))', @")
637 normal 02yi)
638 call assert_equal('baz (quux)', @")
639 normal 03yi)
640 call assert_equal('quux', @")
641
642 " 3i( in front of a block doesn't enter third but un-nested '('
643 call setline(1, 'foo (bar (baz) (quux))')
644 normal 03di)
645 call assert_equal('foo (bar (baz) (quux))', getline(1))
646 normal 02di)
647 call assert_equal('foo (bar () (quux))', getline(1))
648 normal 0di)
649 call assert_equal('foo ()', getline(1))
650
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100651 bw!
652endfunc
653
654func Test_inner_block_empty_paren()
655 new
Maxim Kim37795162024-01-05 17:52:49 +0100656 call setline(1, ["(text)()", "", "(text)(", ")", "", "()()", "", "text()"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100657
658 " Example 1
659 call cursor(1, 1)
660 let @" = ''
661 call assert_beeps(':call feedkeys("0f(viby","xt")')
662 call assert_equal(7, getpos('.')[2])
663 call assert_equal('(', @")
664
665 " Example 2
666 call cursor(3, 1)
667 let @" = ''
668 call assert_beeps('call feedkeys("0f(viby", "xt")')
669 call assert_equal(7, getpos('.')[2])
670 call assert_equal('(', @")
671
672 " Example 3
673 call cursor(6, 1)
674 let @" = ''
675 call assert_beeps('call feedkeys("0f(viby", "xt")')
676 call assert_equal(3, getpos('.')[2])
677 call assert_equal('(', @")
Maxim Kim37795162024-01-05 17:52:49 +0100678
679 " Change empty inner block
680 call cursor(8, 1)
681 call feedkeys("0cibtext", "xt")
682 call assert_equal("text(text)", getline('.'))
683
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100684 bwipe!
685endfunc
686
687func Test_inner_block_empty_bracket()
688 new
Maxim Kim37795162024-01-05 17:52:49 +0100689 call setline(1, ["[text][]", "", "[text][", "]", "", "[][]", "", "text[]"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100690
691 " Example 1
692 call cursor(1, 1)
693 let @" = ''
694 call assert_beeps(':call feedkeys("0f[viby","xt")')
695 call assert_equal(7, getpos('.')[2])
696 call assert_equal('[', @")
697
698 " Example 2
699 call cursor(3, 1)
700 let @" = ''
701 call assert_beeps('call feedkeys("0f[viby", "xt")')
702 call assert_equal(7, getpos('.')[2])
703 call assert_equal('[', @")
704
705 " Example 3
706 call cursor(6, 1)
707 let @" = ''
708 call assert_beeps('call feedkeys("0f[viby", "xt")')
709 call assert_equal(3, getpos('.')[2])
710 call assert_equal('[', @")
Maxim Kim37795162024-01-05 17:52:49 +0100711
712 " Change empty inner block
713 call cursor(8, 1)
714 call feedkeys("0ci[text", "xt")
715 call assert_equal("text[text]", getline('.'))
716
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100717 bwipe!
718endfunc
719
720func Test_inner_block_empty_brace()
721 new
Maxim Kim37795162024-01-05 17:52:49 +0100722 call setline(1, ["{text}{}", "", "{text}{", "}", "", "{}{}", "", "text{}"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100723
724 " Example 1
725 call cursor(1, 1)
726 let @" = ''
727 call assert_beeps(':call feedkeys("0f{viby","xt")')
728 call assert_equal(7, getpos('.')[2])
729 call assert_equal('{', @")
730
731 " Example 2
732 call cursor(3, 1)
733 let @" = ''
734 call assert_beeps('call feedkeys("0f{viby", "xt")')
735 call assert_equal(7, getpos('.')[2])
736 call assert_equal('{', @")
737
738 " Example 3
739 call cursor(6, 1)
740 let @" = ''
741 call assert_beeps('call feedkeys("0f{viby", "xt")')
742 call assert_equal(3, getpos('.')[2])
743 call assert_equal('{', @")
Maxim Kim37795162024-01-05 17:52:49 +0100744
745 " Change empty inner block
746 call cursor(8, 1)
747 call feedkeys("0ciBtext", "xt")
748 call assert_equal("text{text}", getline('.'))
749
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100750 bwipe!
751endfunc
752
753func Test_inner_block_empty_lessthan()
754 new
755 call setline(1, ["<text><>", "", "<text><", ">", "", "<><>"])
756
757 " Example 1
758 call cursor(1, 1)
759 let @" = ''
760 call assert_beeps(':call feedkeys("0f<viby","xt")')
761 call assert_equal(7, getpos('.')[2])
762 call assert_equal('<', @")
763
764 " Example 2
765 call cursor(3, 1)
766 let @" = ''
767 call assert_beeps('call feedkeys("0f<viby", "xt")')
768 call assert_equal(7, getpos('.')[2])
769 call assert_equal('<', @")
770
771 " Example 3
772 call cursor(6, 1)
773 let @" = ''
774 call assert_beeps('call feedkeys("0f<viby", "xt")')
775 call assert_equal(3, getpos('.')[2])
776 call assert_equal('<', @")
777 bwipe!
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200778endfunc
779
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200780" vim: shiftwidth=2 sts=2 expandtab