robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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