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