blob: 9c2d6105130f7f2c9646afdce0fa2cbfb96d2cc6 [file] [log] [blame]
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001" 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', @/)
Bram Moolenaar11956692016-08-27 16:26:56 +020019 call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020020 call assert_equal('foobar', @/)
21
22 " Test 2
Bram Moolenaar11956692016-08-27 16:26:56 +020023 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020024 " 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
Bram Moolenaar11956692016-08-27 16:26:56 +020032 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020033 call assert_equal(' 3 the', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020034 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020035 :1
36 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020037 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020038 call assert_equal(' 4 their', getline('.'))
39 :1
40 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020041 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020042 call assert_equal(' 5 there', getline('.'))
43 :1
44 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020045 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020046 call assert_equal(' 6 their', getline('.'))
47 :1
48 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020049 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020050 call assert_equal(' 7 the', getline('.'))
51 :1
52 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020053 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020054 call assert_equal(' 8 them', getline('.'))
55 :1
56 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020057 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020058 call assert_equal(' 9 these', getline('.'))
59 :1
60 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020061 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020062 call assert_equal(' 9 these', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020063 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020064
65 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020066 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020067 " 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
Bram Moolenaar11956692016-08-27 16:26:56 +020075 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020076 call assert_equal(' 3 the', getline('.'))
77 :1
78 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020079 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020080 call assert_equal(' 4 their', getline('.'))
81 :1
82 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020083 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020084 call assert_equal(' 5 there', getline('.'))
85 :1
86 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020087 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020088 call assert_equal(' 6 their', getline('.'))
89 :1
90 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020091 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020092 call assert_equal(' 7 the', getline('.'))
93 :1
94 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020095 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020096 call assert_equal(' 8 them', getline('.'))
97 :1
98 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020099 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200100 call assert_equal(' 9 these', getline('.'))
101 :1
102 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200103 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200104 call assert_equal(' 2 these', getline('.'))
105
106 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200107 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200108 set incsearch nowrapscan
109 $
110 " first match
111 call feedkeys("?the\<cr>", 'tx')
112 call assert_equal(' 9 these', getline('.'))
113 $
114 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200115 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200116 call assert_equal(' 9 these', getline('.'))
117 $
118 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200119 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200120 call assert_equal(' 8 them', getline('.'))
121 $
122 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200123 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200124 call assert_equal(' 2 these', getline('.'))
125 $
126 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200127 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200128 call assert_equal(' 2 these', getline('.'))
129
130 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200131 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200132 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
Bram Moolenaar11956692016-08-27 16:26:56 +0200139 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200140 call assert_equal(' 2 these', getline('.'))
141 $
142 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200143 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144 call assert_equal(' 8 them', getline('.'))
145 $
146 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200147 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200148 call assert_equal(' 2 these', getline('.'))
149 $
150 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200151 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200152 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'
Bram Moolenaar11956692016-08-27 16:26:56 +0200163 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200164 call assert_equal(' 9 these', getline('.'))
165 1
166 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200167 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200168 call assert_equal(' 2 these', getline('.'))
169 1
170 " wrap around
171 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200172 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200173 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')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200185 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200186 1
187 " delete one char, add another, go to previous match, add one char
Bram Moolenaar11956692016-08-27 16:26:56 +0200188 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200189 call assert_equal(' 9 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200190 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
Bram Moolenaar11956692016-08-27 16:26:56 +0200210 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200211 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)
Bram Moolenaar11956692016-08-27 16:26:56 +0200218 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200219 call assert_equal(' 3 the theother', getline('.'))
220 1
221 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200222 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200223 call assert_equal(' 3 the theother', getline('.'))
224 1
225 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200226 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200227 call assert_equal(' 3 the theother', getline('.'))
228 1
229 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200230 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200231 call assert_equal(' 3 the theother', getline('.'))
232 1
233 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200234 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200235 call assert_equal(' 3 the theother', getline('.'))
236 1
237 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200238 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200239 call assert_equal(' 2 these', getline('.'))
240
Bram Moolenaardda933d2016-09-03 21:04:58 +0200241 " 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
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200266 " clean up
Bram Moolenaardda933d2016-09-03 21:04:58 +0200267 set noincsearch
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200268 call test_disable_char_avail(0)
269 bw!
270endfunc