robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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