1" Tests for search_stats, when "S" is not in 'shortmess'
2
3source check.vim
4source screendump.vim
5
6func Test_search_stat()
7  new
8  set shortmess-=S
9  " Append 50 lines with text to search for, "foobar" appears 20 times
10  call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10))
11
12  " match at second line
13  call cursor(1, 1)
14  let messages_before = execute('messages')
15  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)
20  " didn't get added to message history
21  call assert_equal(messages_before, execute('messages'))
22
23  " Match at last line
24  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
29  " No search stat
30  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
37  " Many matches
38  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)
44  call cursor(line('$'), 1)
45  let g:a = execute(':unsilent :norm! n')
46  let stat = 'W \[1/>99\]'
47  call assert_match(pat .. stat, g:a)
48
49  " Many matches
50  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)
54  call cursor(1, 1)
55  let g:a = execute(':unsilent :norm! N')
56  let stat = 'W \[>99/>99\]'
57  call assert_match(pat .. stat, g:a)
58
59  " right-left
60  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
71  " right-left bottom
72  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
82  " right-left back at top
83  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 = 'W \[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
94  " normal, back at bottom
95  call cursor(1,1)
96  let @/ = 'foobar'
97  let pat = '?foobar\s\+'
98  let g:a = execute(':unsilent :norm! N')
99  let stat = 'W \[20/20\]'
100  call assert_match(pat .. stat, g:a)
101  call assert_match('search hit TOP, continuing at BOTTOM', g:a)
102  call assert_match('W \[20/20\]', Screenline(&lines))
103
104  " normal, no match
105  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
118  " 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
137  " normal, n comes from a mapping
138  "     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
153  " normal, but silent
154  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
161  " 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)
178  " Test that the message is not truncated
179  " it would insert '...' into the output.
180  call assert_match('^\s\+' .. stat, g:b)
181  unmap n
182
183  " Clean up
184  set shortmess+=S
185  " close the window
186  bwipe!
187endfunc
188
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 TermWait(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 TermWait(buf)
211  call VerifyScreenDump(buf, 'Test_searchstat_2', {})
212
213  call StopVimInTerminal(buf)
214  call delete('Xsearchstat')
215endfunc
216