oldfiles_spec.lua 3.9 KB

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