1" Test the :filter command modifier
2
3source check.vim
4
5func Test_filter()
6  edit Xdoesnotmatch
7  edit Xwillmatch
8  call assert_equal('"Xwillmatch"', substitute(execute('filter willma ls'), '[^"]*\(".*"\)[^"]*', '\1', ''))
9  bwipe Xdoesnotmatch
10  bwipe Xwillmatch
11
12  new
13  call setline(1, ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'])
14  call assert_equal("\nfoo2\nfoo4", execute('filter /foo[24]/ 1,$print'))
15  call assert_equal("\n  2 foo2\n  4 foo4", execute('filter /foo[24]/ 1,$number'))
16  call assert_equal("\nfoo2$\nfoo4$", execute('filter /foo[24]/ 1,$list'))
17
18  call assert_equal("\nfoo1$\nfoo3$\nfoo5$", execute('filter! /foo[24]/ 1,$list'))
19  bwipe!
20
21  command XTryThis echo 'this'
22  command XTryThat echo 'that'
23  command XDoThat echo 'that'
24  let lines = split(execute('filter XTry command'), "\n")
25  call assert_equal(3, len(lines))
26  call assert_match("XTryThat", lines[1])
27  call assert_match("XTryThis", lines[2])
28  delcommand XTryThis
29  delcommand XTryThat
30  delcommand XDoThat
31
32  map f1 the first key
33  map f2 the second key
34  map f3 not a key
35  let lines = split(execute('filter the map f'), "\n")
36  call assert_equal(2, len(lines))
37  call assert_match("f2", lines[0])
38  call assert_match("f1", lines[1])
39  unmap f1
40  unmap f2
41  unmap f3
42endfunc
43
44func Test_filter_fails()
45  call assert_fails('filter', 'E471:')
46  call assert_fails('filter pat', 'E476:')
47  call assert_fails('filter /pat', 'E476:')
48  call assert_fails('filter /pat/', 'E476:')
49  call assert_fails('filter /pat/ asdf', 'E492:')
50  " Using assert_fails() causes E476 instead of E866. So use a try-catch.
51  let caught_e866 = 0
52  try
53    filter /\@>b/ ls
54  catch /E866:/
55    let caught_e866 = 1
56  endtry
57  call assert_equal(1, caught_e866)
58
59  call assert_fails('filter!', 'E471:')
60  call assert_fails('filter! pat', 'E476:')
61  call assert_fails('filter! /pat', 'E476:')
62  call assert_fails('filter! /pat/', 'E476:')
63  call assert_fails('filter! /pat/ asdf', 'E492:')
64endfunc
65
66function s:complete_filter_cmd(filtcmd)
67  let keystroke = "\<TAB>\<C-R>=execute('let cmdline = getcmdline()')\<CR>\<C-C>"
68  let cmdline = ''
69  call feedkeys(':' . a:filtcmd . keystroke, 'ntx')
70  return cmdline
71endfunction
72
73func Test_filter_cmd_completion()
74  " Do not complete pattern
75  call assert_equal("filter \t", s:complete_filter_cmd('filter '))
76  call assert_equal("filter pat\t", s:complete_filter_cmd('filter pat'))
77  call assert_equal("filter /pat\t", s:complete_filter_cmd('filter /pat'))
78  call assert_equal("filter /pat/\t", s:complete_filter_cmd('filter /pat/'))
79
80  " Complete after string pattern
81  call assert_equal('filter pat print', s:complete_filter_cmd('filter pat pri'))
82
83  " Complete after regexp pattern
84  call assert_equal('filter /pat/ print', s:complete_filter_cmd('filter /pat/ pri'))
85  call assert_equal('filter #pat# print', s:complete_filter_cmd('filter #pat# pri'))
86endfunc
87
88func Test_filter_cmd_with_filter()
89  new
90  set shelltemp
91  %!echo "a|b"
92  let out = getline(1)
93  bw!
94  if has('win32')
95    let out = trim(out, '" ')
96  endif
97  call assert_equal('a|b', out)
98  set shelltemp&
99endfunction
100
101func Test_filter_commands()
102  CheckFeature quickfix
103
104  let g:test_filter_a = 1
105  let b:test_filter_b = 2
106  let test_filter_c = 3
107
108  " Test filtering :let command
109  let res = split(execute("filter /^test_filter/ let"), "\n")
110  call assert_equal(["test_filter_a         #1"], res)
111
112  let res = split(execute("filter /\\v^(b:)?test_filter/ let"), "\n")
113  call assert_equal(["test_filter_a         #1", "b:test_filter_b       #2"], res)
114
115  unlet g:test_filter_a
116  unlet b:test_filter_b
117  unlet test_filter_c
118
119  " Test filtering :set command
120  let helplang=&helplang
121  set helplang=en
122  let res = join(split(execute("filter /^help/ set"), "\n")[1:], " ")
123  call assert_match('^\s*helplang=\w*$', res)
124  let &helplang=helplang
125
126  " Test filtering :llist command
127  call setloclist(0, [{"filename": "/path/vim.c"}, {"filename": "/path/vim.h"}, {"module": "Main.Test"}])
128  let res = split(execute("filter /\\.c$/ llist"), "\n")
129  call assert_equal([" 1 /path/vim.c:  "], res)
130
131  let res = split(execute("filter /\\.Test$/ llist"), "\n")
132  call assert_equal([" 3 Main.Test:  "], res)
133
134  " Test filtering :jump command
135  e file.c
136  e file.h
137  e file.hs
138  let res = split(execute("filter /\.c$/ jumps"), "\n")[1:]
139  call assert_equal(["   2     1    0 file.c", ">"], res)
140
141  " Test filtering :marks command
142  b file.c
143  mark A
144  b file.h
145  mark B
146  let res = split(execute("filter /\.c$/ marks"), "\n")[1:]
147  call assert_equal([" A      1    0 file.c"], res)
148
149  " Test filtering :highlight command
150  highlight MyHlGroup ctermfg=10
151  let res = split(execute("filter /MyHlGroup/ highlight"), "\n")
152  call assert_equal(["MyHlGroup      xxx ctermfg=10"], res)
153
154  call setline(1, ['one', 'two', 'three'])
155  1mark a
156  2mark b
157  3mark c
158  let res = split(execute("filter /two/ marks abc"), "\n")[1:]
159  call assert_equal([" b      2    0 two"], res)
160
161  bwipe! file.c
162  bwipe! file.h
163  bwipe! file.hs
164endfunc
165
166func Test_filter_display()
167  edit Xdoesnotmatch
168  let @a = '!!willmatch'
169  let @b = '!!doesnotmatch'
170  let @c = "oneline\ntwoline\nwillmatch\n"
171  let @/ = '!!doesnotmatch'
172  call feedkeys(":echo '!!doesnotmatch:'\<CR>", 'ntx')
173  let lines = map(split(execute('filter /willmatch/ display'), "\n"), 'v:val[5:6]')
174
175  call assert_true(index(lines, '"a') >= 0)
176  call assert_false(index(lines, '"b') >= 0)
177  call assert_true(index(lines, '"c') >= 0)
178  call assert_false(index(lines, '"/') >= 0)
179  call assert_false(index(lines, '":') >= 0)
180  call assert_false(index(lines, '"%') >= 0)
181
182  let lines = map(split(execute('filter /doesnotmatch/ display'), "\n"), 'v:val[5:6]')
183  call assert_true(index(lines, '"a') < 0)
184  call assert_false(index(lines, '"b') < 0)
185  call assert_true(index(lines, '"c') < 0)
186  call assert_false(index(lines, '"/') < 0)
187  call assert_false(index(lines, '":') < 0)
188  call assert_false(index(lines, '"%') < 0)
189
190  bwipe!
191endfunc
192
193" vim: shiftwidth=2 sts=2 expandtab
194