xref: /vim-8.2.3635/src/testdir/test_search.vim (revision cb03397a)
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  :1
35  " third match
36  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
37  call assert_equal('  4 their', getline('.'))
38  :1
39  " fourth match
40  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
41  call assert_equal('  5 there', getline('.'))
42  :1
43  " fifth match
44  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
45  call assert_equal('  6 their', getline('.'))
46  :1
47  " sixth match
48  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
49  call assert_equal('  7 the', getline('.'))
50  :1
51  " seventh match
52  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
53  call assert_equal('  8 them', getline('.'))
54  :1
55  " eigth match
56  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
57  call assert_equal('  9 these', getline('.'))
58  :1
59  " no further match
60  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
61  call assert_equal('  9 these', getline('.'))
62
63  " Test 3
64  " Ctrl-G goes from one match to the next
65  " and continues back at the top
66  set incsearch wrapscan
67  :1
68  " first match
69  call feedkeys("/the\<cr>", 'tx')
70  call assert_equal('  2 these', getline('.'))
71  :1
72  " second match
73  call feedkeys("/the\<C-G>\<cr>", 'tx')
74  call assert_equal('  3 the', getline('.'))
75  :1
76  " third match
77  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
78  call assert_equal('  4 their', getline('.'))
79  :1
80  " fourth match
81  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
82  call assert_equal('  5 there', getline('.'))
83  :1
84  " fifth match
85  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
86  call assert_equal('  6 their', getline('.'))
87  :1
88  " sixth match
89  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
90  call assert_equal('  7 the', getline('.'))
91  :1
92  " seventh match
93  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
94  call assert_equal('  8 them', getline('.'))
95  :1
96  " eigth match
97  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
98  call assert_equal('  9 these', getline('.'))
99  :1
100  " back at first match
101  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
102  call assert_equal('  2 these', getline('.'))
103
104  " Test 4
105  " CTRL-T goes to the previous match
106  set incsearch nowrapscan
107  $
108  " first match
109  call feedkeys("?the\<cr>", 'tx')
110  call assert_equal('  9 these', getline('.'))
111  $
112  " first match
113  call feedkeys("?the\<C-G>\<cr>", 'tx')
114  call assert_equal('  9 these', getline('.'))
115  $
116  " second match
117  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
118  call assert_equal('  8 them', getline('.'))
119  $
120  " last match
121  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
122  call assert_equal('  2 these', getline('.'))
123  $
124  " last match
125  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
126  call assert_equal('  2 these', getline('.'))
127
128  " Test 5
129  " CTRL-T goes to the previous match
130  set incsearch wrapscan
131  $
132  " first match
133  call feedkeys("?the\<cr>", 'tx')
134  call assert_equal('  9 these', getline('.'))
135  $
136  " first match at the top
137  call feedkeys("?the\<C-G>\<cr>", 'tx')
138  call assert_equal('  2 these', getline('.'))
139  $
140  " second match
141  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
142  call assert_equal('  8 them', getline('.'))
143  $
144  " last match
145  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
146  call assert_equal('  2 these', getline('.'))
147  $
148  " back at the bottom of the buffer
149  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
150  call assert_equal('  9 these', getline('.'))
151
152  " Test 6
153  " CTRL-L adds to the search pattern
154  set incsearch wrapscan
155  1
156  " first match
157  call feedkeys("/the\<c-l>\<cr>", 'tx')
158  call assert_equal('  2 these', getline('.'))
159  1
160  " go to next match of 'thes'
161  call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
162  call assert_equal('  9 these', getline('.'))
163  1
164  " wrap around
165  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
166  call assert_equal('  2 these', getline('.'))
167  1
168  " wrap around
169  set nowrapscan
170  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
171  call assert_equal('  9 these', getline('.'))
172
173  " Test 7
174  " <bs> remove from match, but stay at current match
175  set incsearch wrapscan
176  1
177  " first match
178  call feedkeys("/thei\<cr>", 'tx')
179  call assert_equal('  4 their', getline('.'))
180  1
181  " delete one char, add another
182  call feedkeys("/thei\<bs>s\<cr>", 'tx')
183  call assert_equal('  9 these', getline('.'))
184  1
185  " delete one char, add another,  go to previous match, add one char
186  call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
187  call assert_equal('  8 them', getline('.'))
188  1
189  " delete all chars, start from the beginning again
190  call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
191  call assert_equal('  3 the', getline('.'))
192
193  " clean up
194  call test_disable_char_avail(0)
195  bw!
196endfunc
197
198func Test_search_cmdline2()
199  if !exists('+incsearch')
200    return
201  endif
202  " need to disable char_avail,
203  " so that expansion of commandline works
204  call test_disable_char_avail(1)
205  new
206  call setline(1, ['  1', '  2 these', '  3 the theother'])
207  " Test 1
208  " Ctrl-T goes correctly back and forth
209  set incsearch
210  1
211  " first match
212  call feedkeys("/the\<cr>", 'tx')
213  call assert_equal('  2 these', getline('.'))
214  1
215  " go to next match (on next line)
216  call feedkeys("/the\<C-G>\<cr>", 'tx')
217  call assert_equal('  3 the theother', getline('.'))
218  1
219  " go to next match (still on line 3)
220  call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
221  call assert_equal('  3 the theother', getline('.'))
222  1
223  " go to next match (still on line 3)
224  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
225  call assert_equal('  3 the theother', getline('.'))
226  1
227  " go to previous match (on line 3)
228  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
229  call assert_equal('  3 the theother', getline('.'))
230  1
231  " go to previous match (on line 3)
232  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
233  call assert_equal('  3 the theother', getline('.'))
234  1
235  " go to previous match (on line 2)
236  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
237  call assert_equal('  2 these', getline('.'))
238
239  " clean up
240  call test_disable_char_avail(0)
241  bw!
242endfunc
243