neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_filter_cmd.vim (5948B)


      1 " Test the :filter command modifier
      2 
      3 source check.vim
      4 
      5 func 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
     42 endfunc
     43 
     44 func 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:')
     64 endfunc
     65 
     66 function 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
     71 endfunction
     72 
     73 func 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'))
     86 endfunc
     87 
     88 func 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&
     99 endfunction
    100 
    101 func 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  call setline(1, ['one', 'two', 'three'])
    150  1mark a
    151  2mark b
    152  3mark c
    153  let res = split(execute("filter /two/ marks abc"), "\n")[1:]
    154  call assert_equal([" b      2    0 two"], res)
    155 
    156  bwipe! file.c
    157  bwipe! file.h
    158  bwipe! file.hs
    159 endfunc
    160 
    161 func Test_filter_display()
    162  edit Xdoesnotmatch
    163  let @a = '!!willmatch'
    164  let @b = '!!doesnotmatch'
    165  let @c = "oneline\ntwoline\nwillmatch\n"
    166  let @/ = '!!doesnotmatch'
    167  call feedkeys(":echo '!!doesnotmatch:'\<CR>", 'ntx')
    168  let lines = map(split(execute('filter /willmatch/ display'), "\n"), 'v:val[5:6]')
    169 
    170  call assert_true(index(lines, '"a') >= 0)
    171  call assert_false(index(lines, '"b') >= 0)
    172  call assert_true(index(lines, '"c') >= 0)
    173  call assert_false(index(lines, '"/') >= 0)
    174  call assert_false(index(lines, '":') >= 0)
    175  call assert_false(index(lines, '"%') >= 0)
    176 
    177  let lines = map(split(execute('filter /doesnotmatch/ display'), "\n"), 'v:val[5:6]')
    178  call assert_true(index(lines, '"a') < 0)
    179  call assert_false(index(lines, '"b') < 0)
    180  call assert_true(index(lines, '"c') < 0)
    181  call assert_false(index(lines, '"/') < 0)
    182  call assert_false(index(lines, '":') < 0)
    183  call assert_false(index(lines, '"%') < 0)
    184 
    185  bwipe!
    186 endfunc
    187 
    188 func Test_filter_scriptnames()
    189  let lines = split(execute('filter /test_filter_cmd/ scriptnames'), "\n")
    190  call assert_equal(1, len(lines))
    191  call assert_match('filter_cmd', lines[0])
    192 endfunc
    193 
    194 " vim: shiftwidth=2 sts=2 expandtab