blob: 746b3268b2a1e0382d59a44772e1eb666efaa5d5 [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
204 set quoteescape&
Bram Moolenaar53a70282022-05-09 13:15:07 +0100205
206 " this was going beyond the end of the line
207 %del
208 sil! norm i"\
209 sil! norm i"\
210 sil! norm i"\
211 call assert_equal('"\', getline(1))
212
213 bwipe!
Dominique Pelleaf631f62021-09-03 16:50:16 +0200214 endfor
215
216 set enc=utf-8
Bram Moolenaardb510072017-09-28 21:52:17 +0200217endfunc
218
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200219func Test_empty_html_tag()
220 new
221 call setline(1, '<div></div>')
222 normal 0citxxx
223 call assert_equal('<div>xxx</div>', getline(1))
224
225 call setline(1, '<div></div>')
226 normal 0f<cityyy
227 call assert_equal('<div>yyy</div>', getline(1))
228
229 call setline(1, '<div></div>')
230 normal 0f<vitsaaa
231 call assert_equal('aaa', getline(1))
232
Dominique Pelle923dce22021-11-21 11:36:04 +0000233 " selecting a tag block in a non-empty blank line should fail
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200234 call setline(1, ' ')
235 call assert_beeps('normal $vaty')
236
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200237 bwipe!
238endfunc
239
Bram Moolenaardb510072017-09-28 21:52:17 +0200240" Tests for match() and matchstr()
241func Test_match()
242 call assert_equal("b", matchstr("abcd", ".", 0, 2))
243 call assert_equal("bc", matchstr("abcd", "..", 0, 2))
244 call assert_equal("c", matchstr("abcd", ".", 2, 0))
245 call assert_equal("a", matchstr("abcd", ".", 0, -1))
246 call assert_equal(-1, match("abcd", ".", 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200247 call assert_equal(0, match("abcd", ".", 0, -1))
248 call assert_equal(0, match('abc', '.', 0, 1))
249 call assert_equal(1, match('abc', '.', 0, 2))
250 call assert_equal(2, match('abc', '.', 0, 3))
Bram Moolenaardb510072017-09-28 21:52:17 +0200251 call assert_equal(-1, match('abc', '.', 0, 4))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200252 call assert_equal(1, match('abc', '.', 1, 1))
253 call assert_equal(2, match('abc', '.', 2, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200254 call assert_equal(-1, match('abc', '.', 3, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200255 call assert_equal(3, match('abc', '$', 0, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200256 call assert_equal(-1, match('abc', '$', 0, 2))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200257 call assert_equal(3, match('abc', '$', 1, 1))
258 call assert_equal(3, match('abc', '$', 2, 1))
259 call assert_equal(3, match('abc', '$', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200260 call assert_equal(-1, match('abc', '$', 4, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200261 call assert_equal(0, match('abc', '\zs', 0, 1))
262 call assert_equal(1, match('abc', '\zs', 0, 2))
263 call assert_equal(2, match('abc', '\zs', 0, 3))
264 call assert_equal(3, match('abc', '\zs', 0, 4))
Bram Moolenaardb510072017-09-28 21:52:17 +0200265 call assert_equal(-1, match('abc', '\zs', 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200266 call assert_equal(1, match('abc', '\zs', 1, 1))
267 call assert_equal(2, match('abc', '\zs', 2, 1))
268 call assert_equal(3, match('abc', '\zs', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200269 call assert_equal(-1, match('abc', '\zs', 4, 1))
270endfunc
Bram Moolenaar82846a02018-02-09 18:09:54 +0100271
272" This was causing an illegal memory access
273func Test_inner_tag()
274 new
275 norm ixxx
276 call feedkeys("v", 'xt')
277 insert
278x
279x
280.
281 norm it
282 q!
283endfunc
Bram Moolenaar85160712018-06-19 18:27:41 +0200284
285func Test_sentence()
286 enew!
287 call setline(1, 'A sentence. A sentence? A sentence!')
288
289 normal yis
290 call assert_equal('A sentence.', @")
291 normal yas
292 call assert_equal('A sentence. ', @")
293
294 normal )
295
296 normal yis
297 call assert_equal('A sentence?', @")
298 normal yas
299 call assert_equal('A sentence? ', @")
300
301 normal )
302
303 normal yis
304 call assert_equal('A sentence!', @")
305 normal yas
306 call assert_equal(' A sentence!', @")
307
308 normal 0
309 normal 2yis
310 call assert_equal('A sentence. ', @")
311 normal 3yis
312 call assert_equal('A sentence. A sentence?', @")
313 normal 2yas
314 call assert_equal('A sentence. A sentence? ', @")
315
316 %delete _
317endfunc
318
319func Test_sentence_with_quotes()
320 enew!
321 call setline(1, 'A "sentence." A sentence.')
322
323 normal yis
324 call assert_equal('A "sentence."', @")
325 normal yas
326 call assert_equal('A "sentence." ', @")
327
328 normal )
329
330 normal yis
331 call assert_equal('A sentence.', @")
332 normal yas
333 call assert_equal(' A sentence.', @")
334
335 %delete _
336endfunc
337
Bram Moolenaar1e115362019-01-09 23:01:02 +0100338func Test_sentence_with_cursor_on_delimiter()
Bram Moolenaar85160712018-06-19 18:27:41 +0200339 enew!
340 call setline(1, "A '([sentence.])' A sentence.")
341
342 normal! 15|yis
343 call assert_equal("A '([sentence.])'", @")
344 normal! 15|yas
345 call assert_equal("A '([sentence.])' ", @")
346
347 normal! 16|yis
348 call assert_equal("A '([sentence.])'", @")
349 normal! 16|yas
350 call assert_equal("A '([sentence.])' ", @")
351
352 normal! 17|yis
353 call assert_equal("A '([sentence.])'", @")
354 normal! 17|yas
355 call assert_equal("A '([sentence.])' ", @")
356
Bram Moolenaar2f03e5a2020-06-18 15:33:25 +0200357 " don't get stuck on a quote at the start of a sentence
358 %delete _
359 call setline(1, ['A sentence.', '"A sentence"?', 'A sentence!'])
360 normal gg))
361 call assert_equal(3, getcurpos()[1])
362
363 %delete _
364 call setline(1, ['A sentence.', "'A sentence'?", 'A sentence!'])
365 normal gg))
366 call assert_equal(3, getcurpos()[1])
367
Bram Moolenaar85160712018-06-19 18:27:41 +0200368 %delete _
369endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200370
371" Test for the paragraph (ap) text object
372func Test_paragraph()
373 new
374 call setline(1, ['First line.', 'Second line.', 'Third line.'])
375 call cursor(2, 1)
376 normal vapy
377 call assert_equal("First line.\nSecond line.\nThird line.\n", @")
378
379 call cursor(2, 1)
380 call assert_beeps('normal vapapy')
381
382 call setline(1, ['First line.', 'Second line.', ' ', ''])
383 call cursor(1, 1)
384 normal vapy
385 call assert_equal("First line.\nSecond line.\n \n\n", @")
386
387 call setline(1, ['', '', '', 'First line.', 'Second line.'])
388 call cursor(2, 1)
389 normal yap
390 call assert_equal("\n\n\nFirst line.\nSecond line.\n", @")
391 call assert_beeps('normal 3yap')
392 exe "normal \<C-C>"
393
394 %d
395 call setline(1, [' ', ' ', ' '])
396 call cursor(2, 1)
397 normal Vipy
398 call assert_equal(" \n \n \n", @")
399 call cursor(2, 1)
400 call assert_beeps("normal Vipip")
401 exe "normal \<C-C>"
402
403 close!
404endfunc
405
406" Tests for text object aw
407func Test_textobj_a_word()
408 new
409 call append(0, ['foobar,eins,foobar', 'foo,zwei,foo '])
410 " diw
411 norm! 1gg0diw
412 call assert_equal([',eins,foobar', 'foo,zwei,foo ', ''], getline(1,'$'))
413 " daw
414 norm! 2ggEdaw
415 call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
416 " daw the last word in a line
417 call setline(1, ['foo bar', 'foo bar', ''])
418 call cursor(1, 5)
419 normal daw
420 call assert_equal('foo', getline(1))
421 " aw in visual mode
422 call cursor(2, 5)
423 normal! vawx
424 call assert_equal('foo', getline(2))
425 %d
426 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
427 " diW
428 norm! 2ggwd2iW
429 call assert_equal(['foo eins foobar', 'foo foo ', ''], getline(1,'$'))
430 " daW
431 norm! 1ggd2aW
432 call assert_equal(['foobar', 'foo foo ', ''], getline(1,'$'))
433
434 %d
435 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
436 " aw in visual line mode switches to characterwise mode
437 norm! 2gg$Vawd
438 call assert_equal(['foo eins foobar', 'foo zwei foo'], getline(1,'$'))
439 norm! 1gg$Viwd
440 call assert_equal(['foo eins ', 'foo zwei foo'], getline(1,'$'))
441
442 " visually selecting a tab before a word with 'selection' set to 'exclusive'
443 set selection=exclusive
444 normal gg3lvlawy
445 call assert_equal("\teins", @")
446 " visually selecting a tab before a word with 'selection' set to 'inclusive'
447 set selection=inclusive
448 normal gg3lvlawy
449 call assert_equal("\teins\t", @")
450 set selection&
451
452 " selecting a word with no non-space characters in a buffer fails
453 %d
454 call setline(1, ' ')
455 call assert_beeps('normal 3lyaw')
456
457 " visually selecting words backwards with no more words to select
458 call setline(1, 'one two')
459 call assert_beeps('normal 2lvh2aw')
460 exe "normal \<C-C>"
461 call assert_beeps('normal $vh3aw')
462 exe "normal \<C-C>"
463 call setline(1, ['', 'one two'])
464 call assert_beeps('normal 2G2lvh3aw')
465 exe "normal \<C-C>"
466
467 " selecting words forward with no more words to select
468 %d
469 call setline(1, 'one a')
470 call assert_beeps('normal 0y3aw')
471 call setline(1, 'one two ')
472 call assert_beeps('normal 0y3aw')
473 call assert_beeps('normal 03ly2aw')
474
475 " clean up
476 bw!
477endfunc
478
479" Test for is and as text objects
480func Test_textobj_sentence()
481 new
482 call append(0, ['This is a test. With some sentences!', '',
483 \ 'Even with a question? And one more. And no sentence here'])
484 " Test for dis - does not remove trailing whitespace
485 norm! 1gg0dis
486 call assert_equal([' With some sentences!', '',
487 \ 'Even with a question? And one more. And no sentence here', ''],
488 \ getline(1,'$'))
489 " Test for das - removes leading whitespace
490 norm! 3ggf?ldas
491 call assert_equal([' With some sentences!', '',
492 \ 'Even with a question? And no sentence here', ''], getline(1,'$'))
493 " when used in visual mode, is made characterwise
494 norm! 3gg$Visy
495 call assert_equal('v', visualmode())
496 " reset visualmode()
497 norm! 3ggVy
498 norm! 3gg$Vasy
499 call assert_equal('v', visualmode())
500 " basic testing for textobjects a< and at
501 %d
502 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
503 " a<
504 norm! 1gg0da<
505 call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
506 norm! 1pj
507 call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
508 " at
509 norm! d2at
510 call assert_equal([' '], getline(1,'$'))
511 %d
512 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
513 " i<
514 norm! 1gg0di<
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 norm! d2it
519 call assert_equal(['<div></div>',' '], getline(1,'$'))
520 " basic testing for a[ and i[ text object
521 %d
522 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
523 norm! 3gg0di[
524 call assert_equal([' ', '[', ']'], getline(1,'$'))
525 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
526 norm! 3gg0ftd2a[
527 call assert_equal([' '], getline(1,'$'))
528
529 " clean up
530 bw!
531endfunc
532
533" Test for quote (', " and `) textobjects
534func Test_textobj_quote()
535 new
536
537 " Test for i" when cursor is in front of a quoted object
538 call append(0, 'foo "bar"')
539 norm! 1gg0di"
540 call assert_equal(['foo ""', ''], getline(1,'$'))
541
542 " Test for visually selecting an inner quote
543 %d
544 " extend visual selection from one quote to the next
545 call setline(1, 'color "red" color "blue"')
546 call cursor(1, 7)
547 normal v4li"y
548 call assert_equal('"red" color "blue', @")
549
550 " try to extend visual selection from one quote to a non-existing quote
551 call setline(1, 'color "red" color blue')
552 call cursor(1, 7)
553 call feedkeys('v4li"y', 'xt')
554 call assert_equal('"red"', @")
555
556 " try to extend visual selection from one quote to a next partial quote
557 call setline(1, 'color "red" color "blue')
558 call cursor(1, 7)
559 normal v4li"y
560 call assert_equal('"red" color ', @")
561
562 " select a quote backwards in visual mode
563 call cursor(1, 12)
564 normal vhi"y
565 call assert_equal('red" ', @")
566 call assert_equal(8, col('.'))
567
568 " select a quote backwards in visual mode from outside the quote
569 call cursor(1, 17)
570 normal v2hi"y
571 call assert_equal('red', @")
572 call assert_equal(8, col('.'))
573
574 " visually selecting a quote with 'selection' set to 'exclusive'
575 call setline(1, 'He said "How are you?"')
576 set selection=exclusive
577 normal 012lv2li"y
578 call assert_equal('How are you?', @")
579 set selection&
580
581 " try copy a quote object with a single quote in the line
582 call setline(1, "Smith's car")
583 call cursor(1, 6)
584 call assert_beeps("normal yi'")
585 call assert_beeps("normal 2lyi'")
586
587 " selecting space before and after a quoted string
588 call setline(1, "some 'special' string")
589 normal 0ya'
590 call assert_equal("'special' ", @")
591 call setline(1, "some 'special'string")
592 normal 0ya'
593 call assert_equal(" 'special'", @")
594
Dominique Pelleaf631f62021-09-03 16:50:16 +0200595 " quoted string with odd or even number of backslashes.
596 call setline(1, 'char *s = "foo\"bar"')
597 normal $hhyi"
598 call assert_equal('foo\"bar', @")
599 call setline(1, 'char *s = "foo\\"bar"')
600 normal $hhyi"
601 call assert_equal('bar', @")
602 call setline(1, 'char *s = "foo\\\"bar"')
603 normal $hhyi"
604 call assert_equal('foo\\\"bar', @")
605 call setline(1, 'char *s = "foo\\\\"bar"')
606 normal $hhyi"
607 call assert_equal('bar', @")
608
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200609 close!
610endfunc
611
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200612" Test for i(, i<, etc. when cursor is in front of a block
613func Test_textobj_find_paren_forward()
614 new
615
616 " i< and a> when cursor is in front of a block
617 call setline(1, '#include <foo.h>')
618 normal 0yi<
619 call assert_equal('foo.h', @")
620 normal 0ya>
621 call assert_equal('<foo.h>', @")
622
623 " 2i(, 3i( in front of a block enters second/third nested '('
624 call setline(1, 'foo (bar (baz (quux)))')
625 normal 0yi)
626 call assert_equal('bar (baz (quux))', @")
627 normal 02yi)
628 call assert_equal('baz (quux)', @")
629 normal 03yi)
630 call assert_equal('quux', @")
631
632 " 3i( in front of a block doesn't enter third but un-nested '('
633 call setline(1, 'foo (bar (baz) (quux))')
634 normal 03di)
635 call assert_equal('foo (bar (baz) (quux))', getline(1))
636 normal 02di)
637 call assert_equal('foo (bar () (quux))', getline(1))
638 normal 0di)
639 call assert_equal('foo ()', getline(1))
640
641 close!
642endfunc
643
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200644" vim: shiftwidth=2 sts=2 expandtab