xref: /vim-8.2.3635/src/testdir/test_search.vim (revision e16b00a1)
1" Test for the search command
2
3set belloff=all
4func Test_search_cmdline()
5  if !exists('+incsearch')
6    return
7  endif
8  " need to disable char_avail,
9  " so that expansion of commandline works
10  call test_override("char_avail", 1)
11  new
12  call setline(1, ['  1', '  2 these', '  3 the', '  4 their', '  5 there', '  6 their', '  7 the', '  8 them', '  9 these', ' 10 foobar'])
13  " Test 1
14  " CTRL-N / CTRL-P skips through the previous search history
15  set noincsearch
16  :1
17  call feedkeys("/foobar\<cr>", 'tx')
18  call feedkeys("/the\<cr>",'tx')
19  call assert_equal('the', @/)
20  call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
21  call assert_equal('foobar', @/)
22
23  " Test 2
24  " Ctrl-G goes from one match to the next
25  " until the end of the buffer
26  set incsearch nowrapscan
27  :1
28  " first match
29  call feedkeys("/the\<cr>", 'tx')
30  call assert_equal('  2 these', getline('.'))
31  :1
32  " second match
33  call feedkeys("/the\<C-G>\<cr>", 'tx')
34  call assert_equal('  3 the', getline('.'))
35  call assert_equal([0, 0, 0, 0], getpos('"'))
36  :1
37  " third match
38  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
39  call assert_equal('  4 their', getline('.'))
40  :1
41  " fourth match
42  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
43  call assert_equal('  5 there', getline('.'))
44  :1
45  " fifth match
46  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
47  call assert_equal('  6 their', getline('.'))
48  :1
49  " sixth match
50  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
51  call assert_equal('  7 the', getline('.'))
52  :1
53  " seventh match
54  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
55  call assert_equal('  8 them', getline('.'))
56  :1
57  " eigth match
58  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
59  call assert_equal('  9 these', getline('.'))
60  :1
61  " no further match
62  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
63  call assert_equal('  9 these', getline('.'))
64  call assert_equal([0, 0, 0, 0], getpos('"'))
65
66  " Test 3
67  " Ctrl-G goes from one match to the next
68  " and continues back at the top
69  set incsearch wrapscan
70  :1
71  " first match
72  call feedkeys("/the\<cr>", 'tx')
73  call assert_equal('  2 these', getline('.'))
74  :1
75  " second match
76  call feedkeys("/the\<C-G>\<cr>", 'tx')
77  call assert_equal('  3 the', getline('.'))
78  :1
79  " third match
80  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
81  call assert_equal('  4 their', getline('.'))
82  :1
83  " fourth match
84  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
85  call assert_equal('  5 there', getline('.'))
86  :1
87  " fifth match
88  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
89  call assert_equal('  6 their', getline('.'))
90  :1
91  " sixth match
92  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
93  call assert_equal('  7 the', getline('.'))
94  :1
95  " seventh match
96  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
97  call assert_equal('  8 them', getline('.'))
98  :1
99  " eigth match
100  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
101  call assert_equal('  9 these', getline('.'))
102  :1
103  " back at first match
104  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
105  call assert_equal('  2 these', getline('.'))
106
107  " Test 4
108  " CTRL-T goes to the previous match
109  set incsearch nowrapscan
110  $
111  " first match
112  call feedkeys("?the\<cr>", 'tx')
113  call assert_equal('  9 these', getline('.'))
114  $
115  " first match
116  call feedkeys("?the\<C-G>\<cr>", 'tx')
117  call assert_equal('  9 these', getline('.'))
118  $
119  " second match
120  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
121  call assert_equal('  8 them', getline('.'))
122  $
123  " last match
124  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
125  call assert_equal('  2 these', getline('.'))
126  $
127  " last match
128  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
129  call assert_equal('  2 these', getline('.'))
130
131  " Test 5
132  " CTRL-T goes to the previous match
133  set incsearch wrapscan
134  $
135  " first match
136  call feedkeys("?the\<cr>", 'tx')
137  call assert_equal('  9 these', getline('.'))
138  $
139  " first match at the top
140  call feedkeys("?the\<C-G>\<cr>", 'tx')
141  call assert_equal('  2 these', getline('.'))
142  $
143  " second match
144  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
145  call assert_equal('  8 them', getline('.'))
146  $
147  " last match
148  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
149  call assert_equal('  2 these', getline('.'))
150  $
151  " back at the bottom of the buffer
152  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
153  call assert_equal('  9 these', getline('.'))
154
155  " Test 6
156  " CTRL-L adds to the search pattern
157  set incsearch wrapscan
158  1
159  " first match
160  call feedkeys("/the\<c-l>\<cr>", 'tx')
161  call assert_equal('  2 these', getline('.'))
162  1
163  " go to next match of 'thes'
164  call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
165  call assert_equal('  9 these', getline('.'))
166  1
167  " wrap around
168  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
169  call assert_equal('  2 these', getline('.'))
170  1
171  " wrap around
172  set nowrapscan
173  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
174  call assert_equal('  9 these', getline('.'))
175
176  " Test 7
177  " <bs> remove from match, but stay at current match
178  set incsearch wrapscan
179  1
180  " first match
181  call feedkeys("/thei\<cr>", 'tx')
182  call assert_equal('  4 their', getline('.'))
183  1
184  " delete one char, add another
185  call feedkeys("/thei\<bs>s\<cr>", 'tx')
186  call assert_equal('  2 these', getline('.'))
187  1
188  " delete one char, add another,  go to previous match, add one char
189  call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
190  call assert_equal('  9 these', getline('.'))
191  1
192  " delete all chars, start from the beginning again
193  call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
194  call assert_equal('  3 the', getline('.'))
195
196  " clean up
197  call test_override("char_avail", 0)
198  bw!
199endfunc
200
201func Test_search_cmdline2()
202  if !exists('+incsearch')
203    return
204  endif
205  " need to disable char_avail,
206  " so that expansion of commandline works
207  call test_override("char_avail", 1)
208  new
209  call setline(1, ['  1', '  2 these', '  3 the theother'])
210  " Test 1
211  " Ctrl-T goes correctly back and forth
212  set incsearch
213  1
214  " first match
215  call feedkeys("/the\<cr>", 'tx')
216  call assert_equal('  2 these', getline('.'))
217  1
218  " go to next match (on next line)
219  call feedkeys("/the\<C-G>\<cr>", 'tx')
220  call assert_equal('  3 the theother', getline('.'))
221  1
222  " go to next match (still on line 3)
223  call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
224  call assert_equal('  3 the theother', getline('.'))
225  1
226  " go to next match (still on line 3)
227  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
228  call assert_equal('  3 the theother', getline('.'))
229  1
230  " go to previous match (on line 3)
231  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
232  call assert_equal('  3 the theother', getline('.'))
233  1
234  " go to previous match (on line 3)
235  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
236  call assert_equal('  3 the theother', getline('.'))
237  1
238  " go to previous match (on line 2)
239  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
240  call assert_equal('  2 these', getline('.'))
241
242  " Test 2: keep the view,
243  " after deleting a character from the search cmd
244  call setline(1, ['  1', '  2 these', '  3 the', '  4 their', '  5 there', '  6 their', '  7 the', '  8 them', '  9 these', ' 10 foobar'])
245  resize 5
246  1
247  call feedkeys("/foo\<bs>\<cr>", 'tx')
248  redraw
249  call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
250
251  " remove all history entries
252  for i in range(10)
253      call histdel('/')
254  endfor
255
256  " Test 3: reset the view,
257  " after deleting all characters from the search cmd
258  norm! 1gg0
259  " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
260  " nor "/foo\<c-u>\<cr>" works to delete the commandline.
261  " In that case Vim should return "E35 no previous regular expression",
262  " but it looks like Vim still sees /foo and therefore the test fails.
263  " Therefore, disableing this test
264  "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
265  "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
266
267  " clean up
268  set noincsearch
269  call test_override("char_avail", 0)
270  bw!
271endfunc
272
273func Test_use_sub_pat()
274  split
275  let @/ = ''
276  func X()
277    s/^/a/
278    /
279  endfunc
280  call X()
281  bwipe!
282endfunc
283
284func Test_searchpair()
285  new
286  call setline(1, ['other code here', '', '[', '" cursor here', ']'])
287  4
288  let a=searchpair('\[','',']','bW')
289  call assert_equal(3, a)
290  set nomagic
291  4
292  let a=searchpair('\[','',']','bW')
293  call assert_equal(3, a)
294  set magic
295  q!
296endfunc
297
298func Test_searchc()
299  " These commands used to cause memory overflow in searchc().
300  new
301  norm ixx
302  exe "norm 0t\u93cf"
303  bw!
304endfunc
305
306func Test_search_cmdline3()
307  if !exists('+incsearch')
308    return
309  endif
310  " need to disable char_avail,
311  " so that expansion of commandline works
312  call test_override("char_avail", 1)
313  new
314  call setline(1, ['  1', '  2 the~e', '  3 the theother'])
315  set incsearch
316  1
317  " first match
318  call feedkeys("/the\<c-l>\<cr>", 'tx')
319  call assert_equal('  2 the~e', getline('.'))
320  " clean up
321  set noincsearch
322  call test_override("char_avail", 0)
323  bw!
324endfunc
325
326func Test_search_cmdline4()
327  if !exists('+incsearch')
328    return
329  endif
330  " need to disable char_avail,
331  " so that expansion of commandline works
332  call test_override("char_avail", 1)
333  new
334  call setline(1, ['  1 the first', '  2 the second', '  3 the third'])
335  set incsearch
336  $
337  call feedkeys("?the\<c-g>\<cr>", 'tx')
338  call assert_equal('  3 the third', getline('.'))
339  $
340  call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
341  call assert_equal('  1 the first', getline('.'))
342  $
343  call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
344  call assert_equal('  2 the second', getline('.'))
345  $
346  call feedkeys("?the\<c-t>\<cr>", 'tx')
347  call assert_equal('  1 the first', getline('.'))
348  $
349  call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
350  call assert_equal('  3 the third', getline('.'))
351  $
352  call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
353  call assert_equal('  2 the second', getline('.'))
354  " clean up
355  set noincsearch
356  call test_override("char_avail", 0)
357  bw!
358endfunc
359