blob: 2622b06a4a6800487dd97ec99c5a9bf821158f8e [file] [log] [blame]
Bram Moolenaar00b24be2016-07-23 22:04:47 +02001" Test for textobjects
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar00b24be2016-07-23 22:04:47 +02004
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02005func CpoM(line, useM, expected)
Bram Moolenaar00b24be2016-07-23 22:04:47 +02006 new
7
8 if a:useM
9 set cpoptions+=M
10 else
11 set cpoptions-=M
12 endif
13
14 call setline(1, a:line)
15
16 call setreg('"', '')
17 normal! ggfrmavi)y
18 call assert_equal(getreg('"'), a:expected[0])
19
20 call setreg('"', '')
21 normal! `afbmavi)y
22 call assert_equal(getreg('"'), a:expected[1])
23
24 call setreg('"', '')
25 normal! `afgmavi)y
26 call assert_equal(getreg('"'), a:expected[2])
27
28 q!
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020029endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020030
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020031func Test_inner_block_without_cpo_M()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020032 call CpoM('(red \(blue) green)', 0, ['red \(blue', 'red \(blue', ''])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020033endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020034
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020035func Test_inner_block_with_cpo_M_left_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020036 call CpoM('(red \(blue) green)', 1, ['red \(blue) green', 'blue', 'red \(blue) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020037endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020038
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020039func Test_inner_block_with_cpo_M_right_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020040 call CpoM('(red (blue\) green)', 1, ['red (blue\) green', 'blue\', 'red (blue\) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020041endfunc
42
LemonBoy53737b52022-05-24 11:49:31 +010043func Test_inner_block_single_char()
44 new
45 call setline(1, "(a)")
46
47 set selection=inclusive
48 let @" = ''
49 call assert_nobeep('norm! 0faviby')
50 call assert_equal('a', @")
51
52 set selection=exclusive
53 let @" = ''
54 call assert_nobeep('norm! 0faviby')
55 call assert_equal('a', @")
56
57 set selection&
58 bwipe!
59endfunc
60
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020061func Test_quote_selection_selection_exclusive()
62 new
63 call setline(1, "a 'bcde' f")
64 set selection=exclusive
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010065
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020066 exe "norm! fdvhi'y"
67 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010068
LemonBoy53737b52022-05-24 11:49:31 +010069 let @" = 'dummy'
Bram Moolenaar7170b292019-11-17 17:32:28 +010070 exe "norm! $gevi'y"
71 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010072
LemonBoy53737b52022-05-24 11:49:31 +010073 let @" = 'dummy'
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010074 exe "norm! 0fbhvi'y"
75 call assert_equal('bcde', @")
76
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020077 set selection&vim
78 bw!
79endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +020080
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +010081func Test_quote_selection_selection_exclusive_abort()
82 new
83 set selection=exclusive
84 call setline(1, "'abzzc'")
85 let exp_curs = [0, 1, 6, 0]
86 call cursor(1,1)
87 exe 'norm! fcdvi"'
88 " make sure to end visual mode to have a clear state
89 exe "norm! \<esc>"
90 call assert_equal(exp_curs, getpos('.'))
91 call cursor(1,1)
92 exe 'norm! fcvi"'
93 exe "norm! \<esc>"
94 call assert_equal(exp_curs, getpos('.'))
95 call cursor(1,2)
96 exe 'norm! vfcoi"'
97 exe "norm! \<esc>"
98 let exp_curs = [0, 1, 2, 0]
99 let exp_visu = [0, 1, 7, 0]
100 call assert_equal(exp_curs, getpos('.'))
101 call assert_equal(exp_visu, getpos("'>"))
102 set selection&vim
103 bw!
104endfunc
105
Bram Moolenaardb510072017-09-28 21:52:17 +0200106" Tests for string and html text objects
107func Test_string_html_objects()
Bram Moolenaardb510072017-09-28 21:52:17 +0200108
Dominique Pelleaf631f62021-09-03 16:50:16 +0200109 for e in ['utf-8', 'latin1', 'cp932']
110 enew!
111 exe 'set enc=' .. e
Bram Moolenaardb510072017-09-28 21:52:17 +0200112
Dominique Pelleaf631f62021-09-03 16:50:16 +0200113 let t = '"wo\"rd\\" foo'
114 put =t
115 normal! da"
116 call assert_equal('foo', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200117
Dominique Pelleaf631f62021-09-03 16:50:16 +0200118 let t = "'foo' 'bar' 'piep'"
119 put =t
120 normal! 0va'a'rx
121 call assert_equal("xxxxxxxxxxxx'piep'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200122
Dominique Pelleaf631f62021-09-03 16:50:16 +0200123 let t = "bla bla `quote` blah"
124 put =t
125 normal! 02f`da`
126 call assert_equal("bla bla blah", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200127
Dominique Pelleaf631f62021-09-03 16:50:16 +0200128 let t = 'out " in "noXno"'
129 put =t
130 normal! 0fXdi"
131 call assert_equal('out " in ""', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200132
Dominique Pelleaf631f62021-09-03 16:50:16 +0200133 let t = "\"'\" 'blah' rep 'buh'"
134 put =t
135 normal! 03f'vi'ry
136 call assert_equal("\"'\" 'blah'yyyyy'buh'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200137
Dominique Pelleaf631f62021-09-03 16:50:16 +0200138 set quoteescape=+*-
139 let t = "bla `s*`d-`+++`l**` b`la"
140 put =t
141 normal! di`
142 call assert_equal("bla `` b`la", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200143
Dominique Pelleaf631f62021-09-03 16:50:16 +0200144 let t = 'voo "nah" sdf " asdf" sdf " sdf" sd'
145 put =t
146 normal! $F"va"oha"i"rz
147 call assert_equal('voo "zzzzzzzzzzzzzzzzzzzzzzzzzzzzsd', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200148
Dominique Pelleaf631f62021-09-03 16:50:16 +0200149 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
150 put =t
151 normal! fXdit
152 call assert_equal('-<b>asdf<i></i>asdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200153
Dominique Pelleaf631f62021-09-03 16:50:16 +0200154 let t = "-<b>asdX<i>a<i />sdf</i>asdf</b>-"
155 put =t
156 normal! 0fXdit
157 call assert_equal('-<b></b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200158
Dominique Pelleaf631f62021-09-03 16:50:16 +0200159 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
160 put =t
161 normal! fXdat
162 call assert_equal('-<b>asdfasdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200163
Dominique Pelleaf631f62021-09-03 16:50:16 +0200164 let t = "-<b>asdX<i>as<b />df</i>asdf</b>-"
165 put =t
166 normal! 0fXdat
167 call assert_equal('--', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200168
Dominique Pelleaf631f62021-09-03 16:50:16 +0200169 let t = "-<b>\ninnertext object\n</b>"
170 put =t
171 normal! dit
172 call assert_equal('-<b></b>', getline('.'), e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200173
Dominique Pelleaf631f62021-09-03 16:50:16 +0200174 " copy the tag block from leading indentation before the start tag
175 let t = " <b>\ntext\n</b>"
176 $put =t
177 normal! 2kvaty
178 call assert_equal("<b>\ntext\n</b>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200179
Dominique Pelleaf631f62021-09-03 16:50:16 +0200180 " copy the tag block from the end tag
181 let t = "<title>\nwelcome\n</title>"
182 $put =t
183 normal! $vaty
184 call assert_equal("<title>\nwelcome\n</title>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200185
Dominique Pelleaf631f62021-09-03 16:50:16 +0200186 " copy the outer tag block from a tag without an end tag
187 let t = "<html>\n<title>welcome\n</html>"
188 $put =t
189 normal! k$vaty
190 call assert_equal("<html>\n<title>welcome\n</html>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200191
Dominique Pelleaf631f62021-09-03 16:50:16 +0200192 " nested tag that has < in a different line from >
193 let t = "<div><div\n></div></div>"
194 $put =t
195 normal! k0vaty
196 call assert_equal("<div><div\n></div></div>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200197
Dominique Pelleaf631f62021-09-03 16:50:16 +0200198 " nested tag with attribute that has < in a different line from >
199 let t = "<div><div\nattr=\"attr\"\n></div></div>"
200 $put =t
201 normal! 2k0vaty
202 call assert_equal("<div><div\nattr=\"attr\"\n></div></div>", @", e)
203
Christian Brabandtca7f93e2024-06-19 20:26:51 +0200204 " tag, that includes a > in some attribute
205 let t = "<div attr=\"attr >> foo >> bar \">Hello</div>"
206 $put =t
207 normal! fHyit
208 call assert_equal("Hello", @", e)
209
210 " tag, that includes a > in some attribute
211 let t = "<div attr='attr >> foo >> bar '>Hello 123</div>"
212 $put =t
213 normal! fHyit
214 call assert_equal("Hello 123", @", e)
215
Dominique Pelleaf631f62021-09-03 16:50:16 +0200216 set quoteescape&
Bram Moolenaar53a70282022-05-09 13:15:07 +0100217
218 " this was going beyond the end of the line
219 %del
220 sil! norm i"\
221 sil! norm i"\
222 sil! norm i"\
223 call assert_equal('"\', getline(1))
224
225 bwipe!
Dominique Pelleaf631f62021-09-03 16:50:16 +0200226 endfor
227
228 set enc=utf-8
Bram Moolenaardb510072017-09-28 21:52:17 +0200229endfunc
230
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200231func Test_empty_html_tag()
232 new
233 call setline(1, '<div></div>')
234 normal 0citxxx
235 call assert_equal('<div>xxx</div>', getline(1))
236
237 call setline(1, '<div></div>')
238 normal 0f<cityyy
239 call assert_equal('<div>yyy</div>', getline(1))
240
241 call setline(1, '<div></div>')
242 normal 0f<vitsaaa
243 call assert_equal('aaa', getline(1))
244
Dominique Pelle923dce22021-11-21 11:36:04 +0000245 " selecting a tag block in a non-empty blank line should fail
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200246 call setline(1, ' ')
247 call assert_beeps('normal $vaty')
248
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200249 bwipe!
250endfunc
251
Bram Moolenaardb510072017-09-28 21:52:17 +0200252" Tests for match() and matchstr()
253func Test_match()
254 call assert_equal("b", matchstr("abcd", ".", 0, 2))
255 call assert_equal("bc", matchstr("abcd", "..", 0, 2))
256 call assert_equal("c", matchstr("abcd", ".", 2, 0))
257 call assert_equal("a", matchstr("abcd", ".", 0, -1))
258 call assert_equal(-1, match("abcd", ".", 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200259 call assert_equal(0, match("abcd", ".", 0, -1))
260 call assert_equal(0, match('abc', '.', 0, 1))
261 call assert_equal(1, match('abc', '.', 0, 2))
262 call assert_equal(2, match('abc', '.', 0, 3))
Bram Moolenaardb510072017-09-28 21:52:17 +0200263 call assert_equal(-1, match('abc', '.', 0, 4))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200264 call assert_equal(1, match('abc', '.', 1, 1))
265 call assert_equal(2, match('abc', '.', 2, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200266 call assert_equal(-1, match('abc', '.', 3, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200267 call assert_equal(3, match('abc', '$', 0, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200268 call assert_equal(-1, match('abc', '$', 0, 2))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200269 call assert_equal(3, match('abc', '$', 1, 1))
270 call assert_equal(3, match('abc', '$', 2, 1))
271 call assert_equal(3, match('abc', '$', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200272 call assert_equal(-1, match('abc', '$', 4, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200273 call assert_equal(0, match('abc', '\zs', 0, 1))
274 call assert_equal(1, match('abc', '\zs', 0, 2))
275 call assert_equal(2, match('abc', '\zs', 0, 3))
276 call assert_equal(3, match('abc', '\zs', 0, 4))
Bram Moolenaardb510072017-09-28 21:52:17 +0200277 call assert_equal(-1, match('abc', '\zs', 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200278 call assert_equal(1, match('abc', '\zs', 1, 1))
279 call assert_equal(2, match('abc', '\zs', 2, 1))
280 call assert_equal(3, match('abc', '\zs', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200281 call assert_equal(-1, match('abc', '\zs', 4, 1))
282endfunc
Bram Moolenaar82846a02018-02-09 18:09:54 +0100283
284" This was causing an illegal memory access
285func Test_inner_tag()
286 new
287 norm ixxx
288 call feedkeys("v", 'xt')
289 insert
290x
291x
292.
293 norm it
294 q!
295endfunc
Bram Moolenaar85160712018-06-19 18:27:41 +0200296
297func Test_sentence()
298 enew!
299 call setline(1, 'A sentence. A sentence? A sentence!')
300
301 normal yis
302 call assert_equal('A sentence.', @")
303 normal yas
304 call assert_equal('A sentence. ', @")
305
306 normal )
307
308 normal yis
309 call assert_equal('A sentence?', @")
310 normal yas
311 call assert_equal('A sentence? ', @")
312
313 normal )
314
315 normal yis
316 call assert_equal('A sentence!', @")
317 normal yas
318 call assert_equal(' A sentence!', @")
319
320 normal 0
321 normal 2yis
322 call assert_equal('A sentence. ', @")
323 normal 3yis
324 call assert_equal('A sentence. A sentence?', @")
325 normal 2yas
326 call assert_equal('A sentence. A sentence? ', @")
327
328 %delete _
329endfunc
330
331func Test_sentence_with_quotes()
332 enew!
333 call setline(1, 'A "sentence." A sentence.')
334
335 normal yis
336 call assert_equal('A "sentence."', @")
337 normal yas
338 call assert_equal('A "sentence." ', @")
339
340 normal )
341
342 normal yis
343 call assert_equal('A sentence.', @")
344 normal yas
345 call assert_equal(' A sentence.', @")
346
347 %delete _
348endfunc
349
Bram Moolenaar1e115362019-01-09 23:01:02 +0100350func Test_sentence_with_cursor_on_delimiter()
Bram Moolenaar85160712018-06-19 18:27:41 +0200351 enew!
352 call setline(1, "A '([sentence.])' A sentence.")
353
354 normal! 15|yis
355 call assert_equal("A '([sentence.])'", @")
356 normal! 15|yas
357 call assert_equal("A '([sentence.])' ", @")
358
359 normal! 16|yis
360 call assert_equal("A '([sentence.])'", @")
361 normal! 16|yas
362 call assert_equal("A '([sentence.])' ", @")
363
364 normal! 17|yis
365 call assert_equal("A '([sentence.])'", @")
366 normal! 17|yas
367 call assert_equal("A '([sentence.])' ", @")
368
Bram Moolenaar2f03e5a2020-06-18 15:33:25 +0200369 " don't get stuck on a quote at the start of a sentence
370 %delete _
371 call setline(1, ['A sentence.', '"A sentence"?', 'A sentence!'])
372 normal gg))
373 call assert_equal(3, getcurpos()[1])
374
375 %delete _
376 call setline(1, ['A sentence.', "'A sentence'?", 'A sentence!'])
377 normal gg))
378 call assert_equal(3, getcurpos()[1])
379
Bram Moolenaar85160712018-06-19 18:27:41 +0200380 %delete _
381endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200382
383" Test for the paragraph (ap) text object
384func Test_paragraph()
385 new
386 call setline(1, ['First line.', 'Second line.', 'Third line.'])
387 call cursor(2, 1)
388 normal vapy
389 call assert_equal("First line.\nSecond line.\nThird line.\n", @")
390
391 call cursor(2, 1)
392 call assert_beeps('normal vapapy')
393
394 call setline(1, ['First line.', 'Second line.', ' ', ''])
395 call cursor(1, 1)
396 normal vapy
397 call assert_equal("First line.\nSecond line.\n \n\n", @")
398
399 call setline(1, ['', '', '', 'First line.', 'Second line.'])
400 call cursor(2, 1)
401 normal yap
402 call assert_equal("\n\n\nFirst line.\nSecond line.\n", @")
403 call assert_beeps('normal 3yap')
404 exe "normal \<C-C>"
405
406 %d
407 call setline(1, [' ', ' ', ' '])
408 call cursor(2, 1)
409 normal Vipy
410 call assert_equal(" \n \n \n", @")
411 call cursor(2, 1)
412 call assert_beeps("normal Vipip")
413 exe "normal \<C-C>"
414
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100415 bw!
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200416endfunc
417
418" Tests for text object aw
419func Test_textobj_a_word()
420 new
421 call append(0, ['foobar,eins,foobar', 'foo,zwei,foo '])
422 " diw
423 norm! 1gg0diw
424 call assert_equal([',eins,foobar', 'foo,zwei,foo ', ''], getline(1,'$'))
425 " daw
426 norm! 2ggEdaw
427 call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
428 " daw the last word in a line
429 call setline(1, ['foo bar', 'foo bar', ''])
430 call cursor(1, 5)
431 normal daw
432 call assert_equal('foo', getline(1))
433 " aw in visual mode
434 call cursor(2, 5)
435 normal! vawx
436 call assert_equal('foo', getline(2))
437 %d
438 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
439 " diW
440 norm! 2ggwd2iW
441 call assert_equal(['foo eins foobar', 'foo foo ', ''], getline(1,'$'))
442 " daW
443 norm! 1ggd2aW
444 call assert_equal(['foobar', 'foo foo ', ''], getline(1,'$'))
445
446 %d
447 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
448 " aw in visual line mode switches to characterwise mode
449 norm! 2gg$Vawd
450 call assert_equal(['foo eins foobar', 'foo zwei foo'], getline(1,'$'))
451 norm! 1gg$Viwd
452 call assert_equal(['foo eins ', 'foo zwei foo'], getline(1,'$'))
453
454 " visually selecting a tab before a word with 'selection' set to 'exclusive'
455 set selection=exclusive
456 normal gg3lvlawy
457 call assert_equal("\teins", @")
458 " visually selecting a tab before a word with 'selection' set to 'inclusive'
459 set selection=inclusive
460 normal gg3lvlawy
461 call assert_equal("\teins\t", @")
462 set selection&
463
464 " selecting a word with no non-space characters in a buffer fails
465 %d
466 call setline(1, ' ')
467 call assert_beeps('normal 3lyaw')
468
469 " visually selecting words backwards with no more words to select
470 call setline(1, 'one two')
471 call assert_beeps('normal 2lvh2aw')
472 exe "normal \<C-C>"
473 call assert_beeps('normal $vh3aw')
474 exe "normal \<C-C>"
475 call setline(1, ['', 'one two'])
476 call assert_beeps('normal 2G2lvh3aw')
477 exe "normal \<C-C>"
478
479 " selecting words forward with no more words to select
480 %d
481 call setline(1, 'one a')
482 call assert_beeps('normal 0y3aw')
483 call setline(1, 'one two ')
484 call assert_beeps('normal 0y3aw')
485 call assert_beeps('normal 03ly2aw')
486
487 " clean up
488 bw!
489endfunc
490
491" Test for is and as text objects
492func Test_textobj_sentence()
493 new
494 call append(0, ['This is a test. With some sentences!', '',
495 \ 'Even with a question? And one more. And no sentence here'])
496 " Test for dis - does not remove trailing whitespace
497 norm! 1gg0dis
498 call assert_equal([' With some sentences!', '',
499 \ 'Even with a question? And one more. And no sentence here', ''],
500 \ getline(1,'$'))
501 " Test for das - removes leading whitespace
502 norm! 3ggf?ldas
503 call assert_equal([' With some sentences!', '',
504 \ 'Even with a question? And no sentence here', ''], getline(1,'$'))
505 " when used in visual mode, is made characterwise
506 norm! 3gg$Visy
507 call assert_equal('v', visualmode())
508 " reset visualmode()
509 norm! 3ggVy
510 norm! 3gg$Vasy
511 call assert_equal('v', visualmode())
512 " basic testing for textobjects a< and at
513 %d
514 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
515 " a<
516 norm! 1gg0da<
517 call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
518 norm! 1pj
519 call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
520 " at
521 norm! d2at
522 call assert_equal([' '], getline(1,'$'))
523 %d
524 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
525 " i<
526 norm! 1gg0di<
527 call assert_equal(['<> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
528 norm! 1Pj
529 call assert_equal(['<div> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
530 norm! d2it
531 call assert_equal(['<div></div>',' '], getline(1,'$'))
532 " basic testing for a[ and i[ text object
533 %d
534 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
535 norm! 3gg0di[
536 call assert_equal([' ', '[', ']'], getline(1,'$'))
537 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
538 norm! 3gg0ftd2a[
539 call assert_equal([' '], getline(1,'$'))
540
541 " clean up
542 bw!
543endfunc
544
545" Test for quote (', " and `) textobjects
546func Test_textobj_quote()
547 new
548
549 " Test for i" when cursor is in front of a quoted object
550 call append(0, 'foo "bar"')
551 norm! 1gg0di"
552 call assert_equal(['foo ""', ''], getline(1,'$'))
553
554 " Test for visually selecting an inner quote
555 %d
556 " extend visual selection from one quote to the next
557 call setline(1, 'color "red" color "blue"')
558 call cursor(1, 7)
559 normal v4li"y
560 call assert_equal('"red" color "blue', @")
561
562 " try to extend visual selection from one quote to a non-existing quote
563 call setline(1, 'color "red" color blue')
564 call cursor(1, 7)
565 call feedkeys('v4li"y', 'xt')
566 call assert_equal('"red"', @")
567
568 " try to extend visual selection from one quote to a next partial quote
569 call setline(1, 'color "red" color "blue')
570 call cursor(1, 7)
571 normal v4li"y
572 call assert_equal('"red" color ', @")
573
574 " select a quote backwards in visual mode
575 call cursor(1, 12)
576 normal vhi"y
577 call assert_equal('red" ', @")
578 call assert_equal(8, col('.'))
579
580 " select a quote backwards in visual mode from outside the quote
581 call cursor(1, 17)
582 normal v2hi"y
583 call assert_equal('red', @")
584 call assert_equal(8, col('.'))
585
586 " visually selecting a quote with 'selection' set to 'exclusive'
587 call setline(1, 'He said "How are you?"')
588 set selection=exclusive
589 normal 012lv2li"y
590 call assert_equal('How are you?', @")
591 set selection&
592
593 " try copy a quote object with a single quote in the line
594 call setline(1, "Smith's car")
595 call cursor(1, 6)
596 call assert_beeps("normal yi'")
597 call assert_beeps("normal 2lyi'")
598
599 " selecting space before and after a quoted string
600 call setline(1, "some 'special' string")
601 normal 0ya'
602 call assert_equal("'special' ", @")
603 call setline(1, "some 'special'string")
604 normal 0ya'
605 call assert_equal(" 'special'", @")
606
Dominique Pelleaf631f62021-09-03 16:50:16 +0200607 " quoted string with odd or even number of backslashes.
608 call setline(1, 'char *s = "foo\"bar"')
609 normal $hhyi"
610 call assert_equal('foo\"bar', @")
611 call setline(1, 'char *s = "foo\\"bar"')
612 normal $hhyi"
613 call assert_equal('bar', @")
614 call setline(1, 'char *s = "foo\\\"bar"')
615 normal $hhyi"
616 call assert_equal('foo\\\"bar', @")
617 call setline(1, 'char *s = "foo\\\\"bar"')
618 normal $hhyi"
619 call assert_equal('bar', @")
620
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100621 bw!
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200622endfunc
623
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200624" Test for i(, i<, etc. when cursor is in front of a block
625func Test_textobj_find_paren_forward()
626 new
627
628 " i< and a> when cursor is in front of a block
629 call setline(1, '#include <foo.h>')
630 normal 0yi<
631 call assert_equal('foo.h', @")
632 normal 0ya>
633 call assert_equal('<foo.h>', @")
634
635 " 2i(, 3i( in front of a block enters second/third nested '('
636 call setline(1, 'foo (bar (baz (quux)))')
637 normal 0yi)
638 call assert_equal('bar (baz (quux))', @")
639 normal 02yi)
640 call assert_equal('baz (quux)', @")
641 normal 03yi)
642 call assert_equal('quux', @")
643
644 " 3i( in front of a block doesn't enter third but un-nested '('
645 call setline(1, 'foo (bar (baz) (quux))')
646 normal 03di)
647 call assert_equal('foo (bar (baz) (quux))', getline(1))
648 normal 02di)
649 call assert_equal('foo (bar () (quux))', getline(1))
650 normal 0di)
651 call assert_equal('foo ()', getline(1))
652
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100653 bw!
654endfunc
655
656func Test_inner_block_empty_paren()
657 new
Maxim Kim37795162024-01-05 17:52:49 +0100658 call setline(1, ["(text)()", "", "(text)(", ")", "", "()()", "", "text()"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100659
660 " Example 1
661 call cursor(1, 1)
662 let @" = ''
663 call assert_beeps(':call feedkeys("0f(viby","xt")')
664 call assert_equal(7, getpos('.')[2])
665 call assert_equal('(', @")
666
667 " Example 2
668 call cursor(3, 1)
669 let @" = ''
670 call assert_beeps('call feedkeys("0f(viby", "xt")')
671 call assert_equal(7, getpos('.')[2])
672 call assert_equal('(', @")
673
674 " Example 3
675 call cursor(6, 1)
676 let @" = ''
677 call assert_beeps('call feedkeys("0f(viby", "xt")')
678 call assert_equal(3, getpos('.')[2])
679 call assert_equal('(', @")
Maxim Kim37795162024-01-05 17:52:49 +0100680
681 " Change empty inner block
682 call cursor(8, 1)
683 call feedkeys("0cibtext", "xt")
684 call assert_equal("text(text)", getline('.'))
685
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100686 bwipe!
687endfunc
688
689func Test_inner_block_empty_bracket()
690 new
Maxim Kim37795162024-01-05 17:52:49 +0100691 call setline(1, ["[text][]", "", "[text][", "]", "", "[][]", "", "text[]"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100692
693 " Example 1
694 call cursor(1, 1)
695 let @" = ''
696 call assert_beeps(':call feedkeys("0f[viby","xt")')
697 call assert_equal(7, getpos('.')[2])
698 call assert_equal('[', @")
699
700 " Example 2
701 call cursor(3, 1)
702 let @" = ''
703 call assert_beeps('call feedkeys("0f[viby", "xt")')
704 call assert_equal(7, getpos('.')[2])
705 call assert_equal('[', @")
706
707 " Example 3
708 call cursor(6, 1)
709 let @" = ''
710 call assert_beeps('call feedkeys("0f[viby", "xt")')
711 call assert_equal(3, getpos('.')[2])
712 call assert_equal('[', @")
Maxim Kim37795162024-01-05 17:52:49 +0100713
714 " Change empty inner block
715 call cursor(8, 1)
716 call feedkeys("0ci[text", "xt")
717 call assert_equal("text[text]", getline('.'))
718
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100719 bwipe!
720endfunc
721
722func Test_inner_block_empty_brace()
723 new
Maxim Kim37795162024-01-05 17:52:49 +0100724 call setline(1, ["{text}{}", "", "{text}{", "}", "", "{}{}", "", "text{}"])
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100725
726 " Example 1
727 call cursor(1, 1)
728 let @" = ''
729 call assert_beeps(':call feedkeys("0f{viby","xt")')
730 call assert_equal(7, getpos('.')[2])
731 call assert_equal('{', @")
732
733 " Example 2
734 call cursor(3, 1)
735 let @" = ''
736 call assert_beeps('call feedkeys("0f{viby", "xt")')
737 call assert_equal(7, getpos('.')[2])
738 call assert_equal('{', @")
739
740 " Example 3
741 call cursor(6, 1)
742 let @" = ''
743 call assert_beeps('call feedkeys("0f{viby", "xt")')
744 call assert_equal(3, getpos('.')[2])
745 call assert_equal('{', @")
Maxim Kim37795162024-01-05 17:52:49 +0100746
747 " Change empty inner block
748 call cursor(8, 1)
749 call feedkeys("0ciBtext", "xt")
750 call assert_equal("text{text}", getline('.'))
751
Christian Brabandtad4d7f42024-01-04 21:43:36 +0100752 bwipe!
753endfunc
754
755func Test_inner_block_empty_lessthan()
756 new
757 call setline(1, ["<text><>", "", "<text><", ">", "", "<><>"])
758
759 " Example 1
760 call cursor(1, 1)
761 let @" = ''
762 call assert_beeps(':call feedkeys("0f<viby","xt")')
763 call assert_equal(7, getpos('.')[2])
764 call assert_equal('<', @")
765
766 " Example 2
767 call cursor(3, 1)
768 let @" = ''
769 call assert_beeps('call feedkeys("0f<viby", "xt")')
770 call assert_equal(7, getpos('.')[2])
771 call assert_equal('<', @")
772
773 " Example 3
774 call cursor(6, 1)
775 let @" = ''
776 call assert_beeps('call feedkeys("0f<viby", "xt")')
777 call assert_equal(3, getpos('.')[2])
778 call assert_equal('<', @")
779 bwipe!
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200780endfunc
781
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200782" vim: shiftwidth=2 sts=2 expandtab