blob: 6e5146079ac26e727066a9fa030530b04fc28920 [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('.'))
34 :1
35 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020036 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020037 call assert_equal(' 4 their', getline('.'))
38 :1
39 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020040 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020041 call assert_equal(' 5 there', getline('.'))
42 :1
43 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020044 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020045 call assert_equal(' 6 their', getline('.'))
46 :1
47 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020048 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020049 call assert_equal(' 7 the', getline('.'))
50 :1
51 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020052 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020053 call assert_equal(' 8 them', getline('.'))
54 :1
55 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020056 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020057 call assert_equal(' 9 these', getline('.'))
58 :1
59 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020060 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020061 call assert_equal(' 9 these', getline('.'))
62
63 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020064 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020065 " 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
Bram Moolenaar11956692016-08-27 16:26:56 +020073 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020074 call assert_equal(' 3 the', getline('.'))
75 :1
76 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020077 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020078 call assert_equal(' 4 their', getline('.'))
79 :1
80 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020081 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020082 call assert_equal(' 5 there', getline('.'))
83 :1
84 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020085 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020086 call assert_equal(' 6 their', getline('.'))
87 :1
88 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020089 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020090 call assert_equal(' 7 the', getline('.'))
91 :1
92 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020093 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020094 call assert_equal(' 8 them', getline('.'))
95 :1
96 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020097 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020098 call assert_equal(' 9 these', getline('.'))
99 :1
100 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200101 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200102 call assert_equal(' 2 these', getline('.'))
103
104 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200105 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200106 set incsearch nowrapscan
107 $
108 " first match
109 call feedkeys("?the\<cr>", 'tx')
110 call assert_equal(' 9 these', getline('.'))
111 $
112 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200113 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200114 call assert_equal(' 9 these', getline('.'))
115 $
116 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200117 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200118 call assert_equal(' 8 them', getline('.'))
119 $
120 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200121 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200122 call assert_equal(' 2 these', getline('.'))
123 $
124 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200125 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200126 call assert_equal(' 2 these', getline('.'))
127
128 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200129 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200130 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
Bram Moolenaar11956692016-08-27 16:26:56 +0200137 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200138 call assert_equal(' 2 these', getline('.'))
139 $
140 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200141 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200142 call assert_equal(' 8 them', getline('.'))
143 $
144 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200145 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200146 call assert_equal(' 2 these', getline('.'))
147 $
148 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200149 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200150 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'
Bram Moolenaar11956692016-08-27 16:26:56 +0200161 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200162 call assert_equal(' 9 these', getline('.'))
163 1
164 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200165 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200166 call assert_equal(' 2 these', getline('.'))
167 1
168 " wrap around
169 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200170 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200171 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
Bram Moolenaar11956692016-08-27 16:26:56 +0200186 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200187 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
Bram Moolenaar11956692016-08-27 16:26:56 +0200208 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200209 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)
Bram Moolenaar11956692016-08-27 16:26:56 +0200216 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200217 call assert_equal(' 3 the theother', getline('.'))
218 1
219 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200220 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200221 call assert_equal(' 3 the theother', getline('.'))
222 1
223 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200224 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200225 call assert_equal(' 3 the theother', getline('.'))
226 1
227 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200228 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200229 call assert_equal(' 3 the theother', getline('.'))
230 1
231 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200232 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200233 call assert_equal(' 3 the theother', getline('.'))
234 1
235 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200236 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200237 call assert_equal(' 2 these', getline('.'))
238
239 " clean up
240 call test_disable_char_avail(0)
241 bw!
242endfunc