HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/ubuntu/neovim/test/functional/plugin/cfilter_spec.lua
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local clear = n.clear
local command = n.command
local eq = t.eq
local fn = n.fn

describe('cfilter.lua', function()
  before_each(function()
    clear()
    command('packadd cfilter')
  end)

  for _, list in ipairs({
    {
      name = 'Cfilter',
      get = fn.getqflist,
      set = fn.setqflist,
    },
    {
      name = 'Lfilter',
      get = function()
        return fn.getloclist(0)
      end,
      set = function(items)
        return fn.setloclist(0, items)
      end,
    },
  }) do
    local filter = function(s, bang)
      if not bang then
        bang = ''
      else
        bang = '!'
      end

      command(string.format('%s%s %s', list.name, bang, s))
    end

    describe((':%s'):format(list.name), function()
      it('does not error on empty list', function()
        filter('nothing')
        eq({}, fn.getqflist())
      end)

      it('requires an argument', function()
        local ok = pcall(filter, '')
        eq(false, ok)
      end)

      local test = function(name, s, res, map, bang)
        it(('%s (%s)'):format(name, s), function()
          list.set({
            { filename = 'foo', lnum = 1, text = 'bar' },
            { filename = 'foo', lnum = 2, text = 'baz' },
            { filename = 'foo', lnum = 3, text = 'zed' },
          })

          filter(s, bang)

          local got = list.get()
          if map then
            got = map(got)
          end
          eq(res, got)
        end)
      end

      local toname = function(qflist)
        return fn.map(qflist, 'v:val.text')
      end

      test('filters with no matches', 'does not match', {})

      test('filters with matches', 'ba', { 'bar', 'baz' }, toname)
      test('filters with matches', 'z', { 'baz', 'zed' }, toname)
      test('filters with matches', '^z', { 'zed' }, toname)
      test('filters with not matches', '^z', { 'bar', 'baz' }, toname, true)

      it('also supports using the / register', function()
        list.set({
          { filename = 'foo', lnum = 1, text = 'bar' },
          { filename = 'foo', lnum = 2, text = 'baz' },
          { filename = 'foo', lnum = 3, text = 'zed' },
        })

        fn.setreg('/', 'ba')
        filter('/')

        eq({ 'bar', 'baz' }, toname(list.get()))
      end)

      it('also supports using the / register with bang', function()
        list.set({
          { filename = 'foo', lnum = 1, text = 'bar' },
          { filename = 'foo', lnum = 2, text = 'baz' },
          { filename = 'foo', lnum = 3, text = 'zed' },
        })

        fn.setreg('/', 'ba')
        filter('/', true)

        eq({ 'zed' }, toname(list.get()))
      end)
    end)
  end
end)