neovim

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

oldfiles_spec.lua (4052B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 local Screen = require('test.functional.ui.screen')
      4 
      5 local clear = n.clear
      6 local command = n.command
      7 local expect_exit = n.expect_exit
      8 local api, eq, feed_command = n.api, t.eq, n.feed_command
      9 local feed, poke_eventloop = n.feed, n.poke_eventloop
     10 local ok = t.ok
     11 local eval = n.eval
     12 
     13 local shada_file = 'Xtest.shada'
     14 
     15 local function _clear()
     16  clear {
     17    args = {
     18      '-i',
     19      shada_file, -- Need shada for these tests.
     20      '--cmd',
     21      "set noswapfile undodir=. directory=. viewdir=. backupdir=. belloff= noshowcmd noruler shada=!,'100,<50,s10,h",
     22    },
     23    args_rm = { '-i', '--cmd' },
     24  }
     25 end
     26 
     27 describe(':oldfiles', function()
     28  before_each(_clear)
     29 
     30  after_each(function()
     31    expect_exit(command, 'qall!')
     32    os.remove(shada_file)
     33  end)
     34 
     35  local function add_padding(s)
     36    return s .. string.rep(' ', 96 - string.len(s))
     37  end
     38 
     39  it('shows most recently used files', function()
     40    local screen = Screen.new(100, 5)
     41    screen._default_attr_ids = nil
     42    feed_command('edit testfile1')
     43    feed_command('edit testfile2')
     44    feed_command('wshada')
     45    feed_command('rshada!')
     46    local oldfiles = api.nvim_get_vvar('oldfiles')
     47    feed_command('oldfiles')
     48    screen:expect([[
     49                                                                                                          |
     50      1: ]] .. add_padding(oldfiles[1]) .. [[ |
     51      2: ]] .. add_padding(oldfiles[2]) .. [[ |
     52                                                                                                          |
     53      Press ENTER or type command to continue^                                                             |
     54    ]])
     55    feed('<CR>')
     56  end)
     57 
     58  it('can be filtered with :filter', function()
     59    feed_command('edit file_one.txt')
     60    local file1 = api.nvim_buf_get_name(0)
     61    feed_command('edit file_two.txt')
     62    local file2 = api.nvim_buf_get_name(0)
     63    feed_command('edit another.txt')
     64    local another = api.nvim_buf_get_name(0)
     65    feed_command('wshada')
     66    feed_command('rshada!')
     67 
     68    local function get_oldfiles(cmd)
     69      local q = eval([[split(execute(']] .. cmd .. [['), "\n")]])
     70      for i, _ in ipairs(q) do
     71        q[i] = q[i]:gsub('^%d+:%s+', '')
     72      end
     73      table.sort(q)
     74      return q
     75    end
     76 
     77    local oldfiles = get_oldfiles('oldfiles')
     78    eq({ another, file1, file2 }, oldfiles)
     79 
     80    oldfiles = get_oldfiles('filter file_ oldfiles')
     81    eq({ file1, file2 }, oldfiles)
     82 
     83    oldfiles = get_oldfiles('filter /another/ oldfiles')
     84    eq({ another }, oldfiles)
     85 
     86    oldfiles = get_oldfiles('filter! file_ oldfiles')
     87    eq({ another }, oldfiles)
     88  end)
     89 end)
     90 
     91 describe(':browse oldfiles', function()
     92  local filename
     93  local filename2
     94  local oldfiles
     95 
     96  before_each(function()
     97    _clear()
     98    feed_command('edit testfile1')
     99    filename = api.nvim_buf_get_name(0)
    100    feed_command('edit testfile2')
    101    filename2 = api.nvim_buf_get_name(0)
    102    feed_command('wshada')
    103    poke_eventloop()
    104    _clear()
    105 
    106    -- Ensure nvim is out of "Press ENTER..." prompt.
    107    feed('<cr>')
    108 
    109    -- Ensure v:oldfiles isn't busted.  Since things happen so fast,
    110    -- the ordering of v:oldfiles is unstable (it uses qsort() under-the-hood).
    111    -- Let's verify the contents and the length of v:oldfiles before moving on.
    112    oldfiles = n.api.nvim_get_vvar('oldfiles')
    113    eq(2, #oldfiles)
    114    ok(filename == oldfiles[1] or filename == oldfiles[2])
    115    ok(filename2 == oldfiles[1] or filename2 == oldfiles[2])
    116 
    117    feed_command('browse oldfiles')
    118  end)
    119 
    120  after_each(function()
    121    expect_exit(command, 'qall!')
    122    os.remove(shada_file)
    123  end)
    124 
    125  it('provides a prompt and edits the chosen file', function()
    126    feed('2<cr>')
    127    eq(oldfiles[2], api.nvim_buf_get_name(0))
    128  end)
    129 
    130  it('provides a prompt and does nothing on <cr>', function()
    131    feed('<cr>')
    132    eq('', api.nvim_buf_get_name(0))
    133  end)
    134 
    135  it('provides a prompt and does nothing if choice is out-of-bounds', function()
    136    feed('3<cr>')
    137    eq('', api.nvim_buf_get_name(0))
    138  end)
    139 end)