neovim

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

096_location_list_spec.lua (6877B)


      1 -- Test for problems in quickfix/location list:
      2 -- A. incorrectly copying location lists which caused the location list to show a
      3 --    different name than the file that was actually being displayed.
      4 -- B. not reusing the window for which the location list window is opened but
      5 --    instead creating new windows.
      6 -- C. make sure that the location list window is not reused instead of the window
      7 --    it belongs to.
      8 
      9 local n = require('test.functional.testnvim')()
     10 
     11 local source = n.source
     12 local clear, command, expect = n.clear, n.command, n.expect
     13 
     14 describe('location list', function()
     15  local test_file = 'Xtest-096_location_list.out'
     16  setup(clear)
     17  teardown(function()
     18    os.remove(test_file)
     19  end)
     20 
     21  it('is working', function()
     22    -- Set up the test environment.
     23    source(
     24      -- This function serves as a callback which is executed on editing a new
     25      -- buffer. It accepts a "test protocol" file name that looks like
     26      -- "test://foo.txt". It sets some buffer-local settings and populates the
     27      -- buffer with one line consisting of the base name ("foo").
     28      [[
     29      function! ReadTestProtocol(name)
     30        let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
     31        let word = substitute(base, '\v(.*)\..*', '\1', '')
     32 
     33        setl modifiable
     34        setl noreadonly
     35        setl noswapfile
     36        setl bufhidden=delete
     37        %del _
     38        " For problem 2:
     39        " 'buftype' has to be set to reproduce the constant opening of new windows.
     40        setl buftype=nofile
     41 
     42        call setline(1, word)
     43 
     44        setl nomodified
     45        setl nomodifiable
     46        setl readonly
     47        exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
     48      endfunction
     49      ]]
     50        -- Register the above buffer setup function to be executed before
     51        -- starting to edit a new "test protocol" buffer.
     52        .. [[
     53      augroup testgroup
     54        au!
     55        autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
     56      augroup END
     57      ]]
     58        -- Populate the location list of the current window with some test
     59        -- protocol file locations such as "test://foo.txt".
     60        .. [[
     61      let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
     62      let qflist = []
     63      for word in words
     64        call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
     65        " NOTE: problem 1:
     66        " Intentionally not setting 'lnum' so that the quickfix entries are not
     67        " valid.
     68        call setloclist(0, qflist, ' ')
     69      endfor
     70    ]]
     71    )
     72 
     73    -- Set up the result buffer.
     74    command('enew')
     75    command('w! ' .. test_file)
     76    command('b 1')
     77 
     78    -- Test A.
     79 
     80    -- Open a new buffer as the sole window, rewind and open the prepopulated
     81    -- location list and navigate through the entries.
     82    command('lrewind')
     83    command('enew')
     84    command('lopen')
     85    command(('lnext|'):rep(4))
     86 
     87    -- Split the window, copying the location list, then open the copied
     88    -- location list and again navigate forward.
     89    command('vert split')
     90    command('wincmd L')
     91    command('lopen')
     92    command('wincmd p')
     93    command('lnext')
     94 
     95    -- Record the current file name and the file name of the corresponding
     96    -- location list entry, then open the result buffer.
     97    command('let fileName = expand("%")')
     98    command('wincmd p')
     99    command([[let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')]])
    100    command('wincmd n')
    101    command('wincmd K')
    102    command('b ' .. test_file)
    103 
    104    -- Prepare test output and write it to the result buffer.
    105    command([[let fileName = substitute(fileName, '\\', '/', 'g')]])
    106    command([[let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')]])
    107    command([[call append(line('$'), "Test A:")]])
    108    command([[call append(line('$'), "  - file name displayed: " . fileName)]])
    109    command(
    110      [[call append(line('$'), "  - quickfix claims that the file name displayed is: " . locationListFileName)]]
    111    )
    112    command('w')
    113 
    114    -- Clean slate for the next test.
    115    command('wincmd o')
    116    command('b 1')
    117 
    118    -- Test B.
    119 
    120    -- Rewind the location list, then open it and browse through it by running
    121    -- ":{number}" followed by Enter repeatedly in the location list window.
    122    command('lrewind')
    123    command('lopen')
    124    command('2')
    125    command([[exe "normal \\<CR>"]])
    126    command('wincmd p')
    127    command('3')
    128    command([[exe "normal \<CR>"]])
    129    command('wincmd p')
    130    command('4')
    131    command([[exe "normal \<CR>"]])
    132 
    133    -- Record the number of windows open, then go back to the result buffer.
    134    command('let numberOfWindowsOpen = winnr("$")')
    135    command('wincmd n')
    136    command('wincmd K')
    137    command('b ' .. test_file)
    138 
    139    -- Prepare test output and write it to the result buffer.
    140    command('call append(line("$"), "Test B:")')
    141    command('call append(line("$"), "  - number of window open: " . numberOfWindowsOpen)')
    142    command('w')
    143 
    144    -- Clean slate.
    145    command('wincmd o')
    146    command('b 1')
    147 
    148    -- Test C.
    149 
    150    -- Rewind the location list, then open it and again do the ":{number}" plus
    151    -- Enter browsing. But this time, move the location list window to the top
    152    -- to check whether it (the first window found) will be reused when we try
    153    -- to open new windows.
    154    command('lrewind')
    155    command('lopen')
    156    command('wincmd K')
    157    command('2')
    158    command([[exe "normal \<CR>"]])
    159    command('wincmd p')
    160    command('3')
    161    command([[exe "normal \<CR>"]])
    162    command('wincmd p')
    163    command('4')
    164    command([[exe "normal \<CR>"]])
    165 
    166    -- Record the 'buftype' of window 1 (the location list) and the buffer name
    167    -- of window 2 (the current "test protocol" buffer), then go back to the
    168    -- result buffer.
    169    command('1wincmd w')
    170    command('let locationListWindowBufType = &buftype')
    171    command('2wincmd w')
    172    command('let bufferName = expand("%")')
    173    command('wincmd n')
    174    command('wincmd K')
    175    command('b ' .. test_file)
    176 
    177    -- Prepare test output and write it to the result buffer.
    178    command([[let bufferName = substitute(bufferName, '\\', '/', 'g')]])
    179    command([[call append(line("$"), "Test C:")]])
    180    command(
    181      [[call append(line('$'), "  - 'buftype' of the location list window: " . locationListWindowBufType)]]
    182    )
    183    command([[call append(line('$'), "  - buffer displayed in the 2nd window: " . bufferName)]])
    184    command('w')
    185    command('wincmd o')
    186    command('b 1')
    187 
    188    -- Assert buffer contents.
    189    expect([[
    190 
    191      Test A:
    192        - file name displayed: test://bar.txt
    193        - quickfix claims that the file name displayed is: test://bar.txt
    194      Test B:
    195        - number of window open: 2
    196      Test C:
    197        - 'buftype' of the location list window: quickfix
    198        - buffer displayed in the 2nd window: test://quux.txt]])
    199  end)
    200 end)