neovim

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

cfilter_spec.lua (2727B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local clear = n.clear
      5 local command = n.command
      6 local eq = t.eq
      7 local fn = n.fn
      8 
      9 describe('cfilter.lua', function()
     10  before_each(function()
     11    clear()
     12    command('packadd cfilter')
     13  end)
     14 
     15  for _, list in ipairs({
     16    {
     17      name = 'Cfilter',
     18      get = fn.getqflist,
     19      set = fn.setqflist,
     20    },
     21    {
     22      name = 'Lfilter',
     23      get = function()
     24        return fn.getloclist(0)
     25      end,
     26      set = function(items)
     27        return fn.setloclist(0, items)
     28      end,
     29    },
     30  }) do
     31    local filter = function(s, bang)
     32      if not bang then
     33        bang = ''
     34      else
     35        bang = '!'
     36      end
     37 
     38      command(string.format('%s%s %s', list.name, bang, s))
     39    end
     40 
     41    describe((':%s'):format(list.name), function()
     42      it('does not error on empty list', function()
     43        filter('nothing')
     44        eq({}, fn.getqflist())
     45      end)
     46 
     47      it('requires an argument', function()
     48        local ok = pcall(filter, '')
     49        eq(false, ok)
     50      end)
     51 
     52      local test = function(name, s, res, map, bang)
     53        it(('%s (%s)'):format(name, s), function()
     54          list.set({
     55            { filename = 'foo', lnum = 1, text = 'bar' },
     56            { filename = 'foo', lnum = 2, text = 'baz' },
     57            { filename = 'foo', lnum = 3, text = 'zed' },
     58          })
     59 
     60          filter(s, bang)
     61 
     62          local got = list.get()
     63          if map then
     64            got = map(got)
     65          end
     66          eq(res, got)
     67        end)
     68      end
     69 
     70      local toname = function(qflist)
     71        return fn.map(qflist, 'v:val.text')
     72      end
     73 
     74      test('filters with no matches', 'does not match', {})
     75 
     76      test('filters with matches', 'ba', { 'bar', 'baz' }, toname)
     77      test('filters with matches', 'z', { 'baz', 'zed' }, toname)
     78      test('filters with matches', '^z', { 'zed' }, toname)
     79      test('filters with not matches', '^z', { 'bar', 'baz' }, toname, true)
     80 
     81      it('also supports using the / register', function()
     82        list.set({
     83          { filename = 'foo', lnum = 1, text = 'bar' },
     84          { filename = 'foo', lnum = 2, text = 'baz' },
     85          { filename = 'foo', lnum = 3, text = 'zed' },
     86        })
     87 
     88        fn.setreg('/', 'ba')
     89        filter('/')
     90 
     91        eq({ 'bar', 'baz' }, toname(list.get()))
     92      end)
     93 
     94      it('also supports using the / register with bang', function()
     95        list.set({
     96          { filename = 'foo', lnum = 1, text = 'bar' },
     97          { filename = 'foo', lnum = 2, text = 'baz' },
     98          { filename = 'foo', lnum = 3, text = 'zed' },
     99        })
    100 
    101        fn.setreg('/', 'ba')
    102        filter('/', true)
    103 
    104        eq({ 'zed' }, toname(list.get()))
    105      end)
    106    end)
    107  end
    108 end)