blob: 57997289d71e45a124057aa6fbeee03533ccc663 [file] [log] [blame]
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001" Tests for search_stats, when "S" is not in 'shortmess'
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02002
Bram Moolenaar0f63ed32019-09-04 16:32:36 +02003source check.vim
4source screendump.vim
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005
Bram Moolenaar0f63ed32019-09-04 16:32:36 +02006func Test_search_stat()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02007 new
8 set shortmess-=S
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +02009 " Append 50 lines with text to search for, "foobar" appears 20 times
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020010 call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10))
11
Bram Moolenaar984f0312019-05-24 13:11:47 +020012 " match at second line
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020013 call cursor(1, 1)
Bram Moolenaar984f0312019-05-24 13:11:47 +020014 let messages_before = execute('messages')
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020015 let @/ = 'fo*\(bar\?\)\?'
16 let g:a = execute(':unsilent :norm! n')
17 let stat = '\[2/50\]'
18 let pat = escape(@/, '()*?'). '\s\+'
19 call assert_match(pat .. stat, g:a)
Bram Moolenaar984f0312019-05-24 13:11:47 +020020 " didn't get added to message history
21 call assert_equal(messages_before, execute('messages'))
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020022
Bram Moolenaar984f0312019-05-24 13:11:47 +020023 " Match at last line
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020024 call cursor(line('$')-2, 1)
25 let g:a = execute(':unsilent :norm! n')
26 let stat = '\[50/50\]'
27 call assert_match(pat .. stat, g:a)
28
Bram Moolenaar984f0312019-05-24 13:11:47 +020029 " No search stat
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020030 set shortmess+=S
31 call cursor(1, 1)
32 let stat = '\[2/50\]'
33 let g:a = execute(':unsilent :norm! n')
34 call assert_notmatch(pat .. stat, g:a)
35 set shortmess-=S
36
Bram Moolenaar984f0312019-05-24 13:11:47 +020037 " Many matches
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020038 call cursor(line('$')-2, 1)
39 let @/ = '.'
40 let pat = escape(@/, '()*?'). '\s\+'
41 let g:a = execute(':unsilent :norm! n')
42 let stat = '\[>99/>99\]'
43 call assert_match(pat .. stat, g:a)
Bram Moolenaardc6855a2019-05-18 19:26:29 +020044 call cursor(line('$'), 1)
45 let g:a = execute(':unsilent :norm! n')
46 let stat = '\[1/>99\] W'
47 call assert_match(pat .. stat, g:a)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020048
Bram Moolenaar984f0312019-05-24 13:11:47 +020049 " Many matches
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020050 call cursor(1, 1)
51 let g:a = execute(':unsilent :norm! n')
52 let stat = '\[2/>99\]'
53 call assert_match(pat .. stat, g:a)
Bram Moolenaardc6855a2019-05-18 19:26:29 +020054 call cursor(1, 1)
55 let g:a = execute(':unsilent :norm! N')
56 let stat = '\[>99/>99\] W'
57 call assert_match(pat .. stat, g:a)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020058
Bram Moolenaar984f0312019-05-24 13:11:47 +020059 " right-left
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020060 if exists("+rightleft")
61 set rl
62 call cursor(1,1)
63 let @/ = 'foobar'
64 let pat = 'raboof/\s\+'
65 let g:a = execute(':unsilent :norm! n')
66 let stat = '\[20/2\]'
67 call assert_match(pat .. stat, g:a)
68 set norl
69 endif
70
Bram Moolenaar984f0312019-05-24 13:11:47 +020071 " right-left bottom
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020072 if exists("+rightleft")
73 set rl
74 call cursor('$',1)
75 let pat = 'raboof?\s\+'
76 let g:a = execute(':unsilent :norm! N')
77 let stat = '\[20/20\]'
78 call assert_match(pat .. stat, g:a)
79 set norl
80 endif
81
Bram Moolenaar984f0312019-05-24 13:11:47 +020082 " right-left back at top
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020083 if exists("+rightleft")
84 set rl
85 call cursor('$',1)
86 let pat = 'raboof/\s\+'
87 let g:a = execute(':unsilent :norm! n')
88 let stat = '\[20/1\]'
89 call assert_match(pat .. stat, g:a)
90 call assert_match('search hit BOTTOM, continuing at TOP', g:a)
91 set norl
92 endif
93
Bram Moolenaar984f0312019-05-24 13:11:47 +020094 " normal, back at bottom
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020095 call cursor(1,1)
96 let @/ = 'foobar'
97 let pat = '?foobar\s\+'
98 let g:a = execute(':unsilent :norm! N')
99 let stat = '\[20/20\]'
100 call assert_match(pat .. stat, g:a)
101 call assert_match('search hit TOP, continuing at BOTTOM', g:a)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +0200102 call assert_match('\[20/20\] W', Screenline(&lines))
Bram Moolenaar9dfa3132019-05-04 21:08:40 +0200103
Bram Moolenaar984f0312019-05-24 13:11:47 +0200104 " normal, no match
Bram Moolenaar9dfa3132019-05-04 21:08:40 +0200105 call cursor(1,1)
106 let @/ = 'zzzzzz'
107 let g:a = ''
108 try
109 let g:a = execute(':unsilent :norm! n')
110 catch /^Vim\%((\a\+)\)\=:E486/
111 let stat = ''
112 " error message is not redir'ed to g:a, it is empty
113 call assert_true(empty(g:a))
114 catch
115 call assert_false(1)
116 endtry
117
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +0200118 " with count
119 call cursor(1, 1)
120 let @/ = 'fo*\(bar\?\)\?'
121 let g:a = execute(':unsilent :norm! 2n')
122 let stat = '\[3/50\]'
123 let pat = escape(@/, '()*?'). '\s\+'
124 call assert_match(pat .. stat, g:a)
125 let g:a = execute(':unsilent :norm! 2n')
126 let stat = '\[5/50\]'
127 call assert_match(pat .. stat, g:a)
128
129 " with offset
130 call cursor(1, 1)
131 call feedkeys("/fo*\\(bar\\?\\)\\?/+1\<cr>", 'tx')
132 let g:a = execute(':unsilent :norm! n')
133 let stat = '\[5/50\]'
134 let pat = escape(@/ .. '/+1', '()*?'). '\s\+'
135 call assert_match(pat .. stat, g:a)
136
Bram Moolenaar984f0312019-05-24 13:11:47 +0200137 " normal, n comes from a mapping
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +0200138 " Need to move over more than 64 lines to trigger char_avail(.
139 nnoremap n nzv
140 call cursor(1,1)
141 call append(50, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10))
142 call setline(2, 'find this')
143 call setline(70, 'find this')
144 let @/ = 'find this'
145 let pat = '/find this\s\+'
146 let g:a = execute(':unsilent :norm n')
147 " g:a will contain several lines
148 let g:b = split(g:a, "\n")[-1]
149 let stat = '\[1/2\]'
150 call assert_match(pat .. stat, g:b)
151 unmap n
152
Bram Moolenaar984f0312019-05-24 13:11:47 +0200153 " normal, but silent
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +0200154 call cursor(1,1)
155 let @/ = 'find this'
156 let pat = '/find this\s\+'
157 let g:a = execute(':norm! n')
158 let stat = '\[1/2\]'
159 call assert_notmatch(pat .. stat, g:a)
160
Bram Moolenaar19e8ac72019-09-03 22:23:38 +0200161 " normal, n comes from a silent mapping
162 " First test a normal mapping, then a silent mapping
163 call cursor(1,1)
164 nnoremap n n
165 let @/ = 'find this'
166 let pat = '/find this\s\+'
167 let g:a = execute(':unsilent :norm n')
168 let g:b = split(g:a, "\n")[-1]
169 let stat = '\[1/2\]'
170 call assert_match(pat .. stat, g:b)
171 nnoremap <silent> n n
172 call cursor(1,1)
173 let g:a = execute(':unsilent :norm n')
174 let g:b = split(g:a, "\n")[-1]
175 let stat = '\[1/2\]'
176 call assert_notmatch(pat .. stat, g:b)
177 call assert_match(stat, g:b)
Bram Moolenaar0f63ed32019-09-04 16:32:36 +0200178 " Test that the message is not truncated
179 " it would insert '...' into the output.
180 call assert_match('^\s\+' .. stat, g:b)
Bram Moolenaar19e8ac72019-09-03 22:23:38 +0200181 unmap n
182
183 " Clean up
Bram Moolenaar9dfa3132019-05-04 21:08:40 +0200184 set shortmess+=S
Bram Moolenaar19e8ac72019-09-03 22:23:38 +0200185 " close the window
Bram Moolenaar9dfa3132019-05-04 21:08:40 +0200186 bwipe!
187endfunc
Bram Moolenaar0f63ed32019-09-04 16:32:36 +0200188
189func! Test_search_stat_screendump()
190 CheckScreendump
191
192 let lines =<< trim END
193 set shortmess-=S
194 " Append 50 lines with text to search for, "foobar" appears 20 times
195 call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 20))
196 call setline(2, 'find this')
197 call setline(70, 'find this')
198 nnoremap n n
199 let @/ = 'find this'
200 call cursor(1,1)
201 norm n
202 END
203 call writefile(lines, 'Xsearchstat')
204 let buf = RunVimInTerminal('-S Xsearchstat', #{rows: 10})
205 call term_wait(buf)
206 call VerifyScreenDump(buf, 'Test_searchstat_1', {})
207
208 call term_sendkeys(buf, ":nnoremap <silent> n n\<cr>")
209 call term_sendkeys(buf, "gg0n")
210 call term_wait(buf)
211 call VerifyScreenDump(buf, 'Test_searchstat_2', {})
212
213 call StopVimInTerminal(buf)
214 call delete('Xsearchstat')
215endfunc