robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.2 KiB293 linesraw
1-- ShaDa history saving/reading support
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local t_shada = require('test.functional.shada.testutil')
5
6local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
7local nvim_command, fn, api, nvim_feed, eq = n.command, n.fn, n.api, n.feed, t.eq
8local assert_alive = n.assert_alive
9local expect_exit = n.expect_exit
10local pcall_err = t.pcall_err
11
12local reset, clear = t_shada.reset, t_shada.clear
13
14describe('ShaDa support code', function()
15  before_each(reset)
16  after_each(clear)
17
18  it('is able to dump and read back command-line history', function()
19    nvim_command("set shada='0")
20    nvim_feed(':" Test\n')
21    nvim_command('wshada')
22    reset()
23    nvim_command("set shada='0")
24    nvim_command('rshada')
25    eq('" Test', fn.histget(':', -1))
26  end)
27
28  it('is able to dump and read back 2 items in command-line history', function()
29    nvim_command("set shada='0 history=2")
30    nvim_feed(':" Test\n')
31    nvim_feed(':" Test 2\n')
32    expect_exit(nvim_command, 'qall')
33    reset()
34    nvim_command("set shada='0 history=2")
35    nvim_command('rshada')
36    eq('" Test 2', fn.histget(':', -1))
37    eq('" Test', fn.histget(':', -2))
38  end)
39
40  it('respects &history when dumping', function()
41    nvim_command("set shada='0 history=1")
42    nvim_feed(':" Test\n')
43    nvim_feed(':" Test 2\n')
44    nvim_command('wshada')
45    reset()
46    nvim_command("set shada='0 history=2")
47    nvim_command('rshada')
48    eq('" Test 2', fn.histget(':', -1))
49    eq('', fn.histget(':', -2))
50  end)
51
52  it('respects &history when loading', function()
53    nvim_command("set shada='0 history=2")
54    nvim_feed(':" Test\n')
55    nvim_feed(':" Test 2\n')
56    nvim_command('wshada')
57    reset()
58    nvim_command("set shada='0 history=1")
59    nvim_command('rshada')
60    eq('" Test 2', fn.histget(':', -1))
61    eq('', fn.histget(':', -2))
62  end)
63
64  it('dumps only requested amount of command-line history items', function()
65    nvim_command("set shada='0,:1")
66    nvim_feed(':" Test\n')
67    nvim_feed(':" Test 2\n')
68    nvim_command('wshada')
69    -- Regression test: :wshada should not alter or free history.
70    eq('" Test 2', fn.histget(':', -1))
71    eq('" Test', fn.histget(':', -2))
72    reset()
73    nvim_command("set shada='0")
74    nvim_command('rshada')
75    eq('" Test 2', fn.histget(':', -1))
76    eq('', fn.histget(':', -2))
77  end)
78
79  it('does not respect number in &shada when loading history', function()
80    nvim_command("set shada='0")
81    nvim_feed(':" Test\n')
82    nvim_feed(':" Test 2\n')
83    nvim_command('wshada')
84    reset()
85    nvim_command("set shada='0,:1")
86    nvim_command('rshada')
87    eq('" Test 2', fn.histget(':', -1))
88    eq('" Test', fn.histget(':', -2))
89  end)
90
91  it('dumps and loads all kinds of histories', function()
92    nvim_command('debuggreedy')
93    nvim_feed(':debug echo "Test"\n" Test 2\nc\n') -- Debug history.
94    nvim_feed(':call input("")\nTest 2\n') -- Input history.
95    nvim_feed('"="Test"\nyy') -- Expression history.
96    nvim_feed('/Test\n') -- Search history
97    nvim_feed(':" Test\n') -- Command-line history
98    nvim_command('0debuggreedy')
99    nvim_command('wshada')
100    reset()
101    nvim_command('rshada')
102    eq('" Test', fn.histget(':', -1))
103    eq('Test', fn.histget('/', -1))
104    eq('"Test"', fn.histget('=', -1))
105    eq('Test 2', fn.histget('@', -1))
106    eq('c', fn.histget('>', -1))
107  end)
108
109  it('does not dump last search pattern if /0 in shada', function()
110    nvim_command('set shada+=/0')
111    nvim_command('silent! /test/')
112    expect_exit(nvim_command, 'qall!')
113    reset()
114    nvim_command('language C')
115    local err = pcall_err(n.exec_capture, 'normal! n')
116    eq('nvim_exec2(), line 1: Vim(normal):E35: No previous regular expression', err)
117  end)
118
119  it('dumps and loads last search pattern with offset', function()
120    api.nvim_set_option_value('wrapscan', false, {})
121    fn.setline('.', { 'foo', 'bar--' })
122    nvim_feed('gg0/a/e+1\n')
123    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
124    nvim_command('wshada')
125    reset()
126    api.nvim_set_option_value('wrapscan', false, {})
127    fn.setline('.', { 'foo', 'bar--' })
128    nvim_feed('gg0n')
129    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
130    eq(1, api.nvim_get_vvar('searchforward'))
131    -- Autocommands shouldn't cause search pattern to change
132    nvim_command('autocmd User * :')
133    nvim_command('doautocmd User')
134    nvim_feed('gg0n')
135    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
136    eq(1, api.nvim_get_vvar('searchforward'))
137  end)
138
139  it('dumps and loads last search pattern with offset and backward direction', function()
140    api.nvim_set_option_value('wrapscan', false, {})
141    fn.setline('.', { 'foo', 'bar--' })
142    nvim_feed('G$?a?e+1\n')
143    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
144    nvim_command('wshada')
145    reset()
146    api.nvim_set_option_value('wrapscan', false, {})
147    fn.setline('.', { 'foo', 'bar--' })
148    nvim_feed('G$n')
149    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
150    eq(0, api.nvim_get_vvar('searchforward'))
151    -- Autocommands shouldn't cause search pattern to change
152    nvim_command('autocmd User * :')
153    nvim_command('doautocmd User')
154    nvim_feed('G$n')
155    eq({ 0, 2, 3, 0 }, fn.getpos('.'))
156    eq(0, api.nvim_get_vvar('searchforward'))
157  end)
158
159  it('saves v:hlsearch=1', function()
160    nvim_command('set hlsearch shada-=h')
161    nvim_feed('/test\n')
162    eq(1, api.nvim_get_vvar('hlsearch'))
163    expect_exit(nvim_command, 'qall')
164    reset()
165    eq(1, api.nvim_get_vvar('hlsearch'))
166  end)
167
168  it('saves v:hlsearch=0 with :nohl', function()
169    nvim_command('set hlsearch shada-=h')
170    nvim_feed('/test\n')
171    nvim_command('nohlsearch')
172    expect_exit(nvim_command, 'qall')
173    reset()
174    eq(0, api.nvim_get_vvar('hlsearch'))
175  end)
176
177  it('saves v:hlsearch=0 with default &shada', function()
178    nvim_command('set hlsearch')
179    nvim_feed('/test\n')
180    eq(1, api.nvim_get_vvar('hlsearch'))
181    expect_exit(nvim_command, 'qall')
182    reset()
183    eq(0, api.nvim_get_vvar('hlsearch'))
184  end)
185
186  it('dumps and loads last substitute pattern and replacement string', function()
187    fn.setline('.', { 'foo', 'bar' })
188    nvim_command('%s/f/g/g')
189    eq('goo', fn.getline(1))
190    nvim_command('wshada')
191    reset()
192    fn.setline('.', { 'foo', 'bar' })
193    nvim_command('&')
194    eq('goo', fn.getline(1))
195  end)
196
197  it('does not dump last substitute pattern or replacement string if /0 in shada', function()
198    nvim_command('set shada+=/0')
199    fn.setline('.', { 'foo', 'bar' })
200    nvim_command('%s/f/g/g')
201    eq('goo', fn.getline(1))
202    expect_exit(nvim_command, 'qall!')
203    reset()
204    fn.setline('.', { 'foo', 'bar' })
205    nvim_command('language C')
206    local err = pcall_err(nvim_command, '&')
207    eq('Vim(&):E33: No previous substitute regular expression', err)
208    nvim_feed('/f\n')
209    err = pcall_err(nvim_command, '~&')
210    eq('Vim(~):E33: No previous substitute regular expression', err)
211  end)
212
213  it('dumps and loads history with UTF-8 characters', function()
214    reset()
215    nvim_feed(':echo "«"\n')
216    expect_exit(nvim_command, 'qall')
217    reset()
218    eq('echo "«"', fn.histget(':', -1))
219  end)
220
221  it('dumps and loads replacement with UTF-8 characters', function()
222    nvim_command('substitute/./«/ge')
223    expect_exit(nvim_command, 'qall!')
224    reset()
225    fn.setline('.', { '.' })
226    nvim_command('&')
227    eq('«', fn.getline('.'))
228  end)
229
230  it('dumps and loads substitute pattern with UTF-8 characters', function()
231    nvim_command('substitute/«/./ge')
232    expect_exit(nvim_command, 'qall!')
233    reset()
234    fn.setline('.', { '«\171' })
235    nvim_command('&')
236    eq('.\171', fn.getline('.'))
237  end)
238
239  it('dumps and loads search pattern with UTF-8 characters', function()
240    nvim_command('silent! /«/')
241    expect_exit(nvim_command, 'qall!')
242    reset()
243    fn.setline('.', { 'foo', '\171«' })
244    nvim_feed('gg0n')
245    eq({ 0, 2, 2, 0 }, fn.getpos('.'))
246    eq('\171«', fn.getline('.'))
247    eq('', fn.histget('/', -1))
248  end)
249
250  it('dumps and loads search pattern with 8-bit single-byte', function()
251    -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
252    nvim_command('silent! /\171/')
253    expect_exit(nvim_command, 'qall!')
254    reset()
255    fn.setline('.', { 'foo', '\171«' })
256    nvim_feed('gg0n')
257    eq({ 0, 2, 1, 0 }, fn.getpos('.'))
258    eq('\171«', fn.getline('.'))
259    eq('', fn.histget('/', -1))
260  end)
261
262  it('does not crash when dumping last search pattern (#10945)', function()
263    nvim_command('edit Xtest-functional-shada-history_spec')
264    -- Save jump list
265    nvim_command('wshada')
266    -- Wipe out buffer list (jump list entry gets removed)
267    nvim_command('%bwipeout')
268    -- Restore jump list
269    nvim_command('rshada')
270    nvim_command('silent! /pat/')
271    nvim_command('au BufNew * echo')
272    nvim_command('wshada')
273  end)
274
275  it('does not crash when number of history save to zero (#11497)', function()
276    nvim_command("set shada='10")
277    nvim_feed(':" Test\n')
278    nvim_command('wshada')
279    nvim_command("set shada='10,:0")
280    nvim_command('wshada')
281    assert_alive()
282  end)
283
284  it('does not dump empty replacement string', function()
285    expect_exit(nvim_command, 'qall!')
286    reset()
287    fn.setline('.', { 'foo', 'bar' })
288    nvim_command('language C')
289    nvim_feed('/f\n')
290    local err = pcall_err(nvim_command, '~&')
291    eq('Vim(~):E33: No previous substitute regular expression', err)
292  end)
293end)