diff options
Diffstat (limited to 'test/functional/api/buffer_spec.lua')
-rw-r--r-- | test/functional/api/buffer_spec.lua | 91 |
1 files changed, 71 insertions, 20 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 93599c04f1..9d6cfb99ab 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -24,8 +24,8 @@ describe('api/buf', function() end - describe('line_count, insert and del_line', function() - it('works', function() + describe('nvim_buf_set_lines, nvim_buf_line_count', function() + it('deprecated forms', function() eq(1, curbuf_depr('line_count')) curbuf_depr('insert', -1, {'line'}) eq(2, curbuf_depr('line_count')) @@ -39,6 +39,41 @@ describe('api/buf', function() eq(1, curbuf_depr('line_count')) end) + it('cursor position is maintained after lines are inserted #9961', function() + -- replace the buffer contents with these three lines. + request('nvim_buf_set_lines', 0, 0, -1, 1, {"line1", "line2", "line3", "line4"}) + -- Set the current cursor to {3, 2}. + curwin('set_cursor', {3, 2}) + + -- add 2 lines and delete 1 line above the current cursor position. + request('nvim_buf_set_lines', 0, 1, 2, 1, {"line5", "line6"}) + -- check the current set of lines in the buffer. + eq({"line1", "line5", "line6", "line3", "line4"}, buffer('get_lines', 0, 0, -1, 1)) + -- cursor should be moved below by 1 line. + eq({4, 2}, curwin('get_cursor')) + + -- add a line after the current cursor position. + request('nvim_buf_set_lines', 0, 5, 5, 1, {"line7"}) + -- check the current set of lines in the buffer. + eq({"line1", "line5", "line6", "line3", "line4", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + -- cursor position is unchanged. + eq({4, 2}, curwin('get_cursor')) + + -- overwrite current cursor line. + request('nvim_buf_set_lines', 0, 3, 5, 1, {"line8", "line9"}) + -- check the current set of lines in the buffer. + eq({"line1", "line5", "line6", "line8", "line9", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + -- cursor position is unchanged. + eq({4, 2}, curwin('get_cursor')) + + -- delete current cursor line. + request('nvim_buf_set_lines', 0, 3, 5, 1, {}) + -- check the current set of lines in the buffer. + eq({"line1", "line5", "line6", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + -- cursor position is unchanged. + eq({4, 2}, curwin('get_cursor')) + end) + it('line_count has defined behaviour for unloaded buffers', function() -- we'll need to know our bufnr for when it gets unloaded local bufnr = curbuf('get_number') @@ -70,7 +105,7 @@ describe('api/buf', function() end) end) - describe('{get,set,del}_line', function() + describe('deprecated: {get,set,del}_line', function() it('works', function() eq('', curbuf_depr('get_line', 0)) curbuf_depr('set_line', 0, 'line1') @@ -102,7 +137,7 @@ describe('api/buf', function() end) end) - describe('{get,set}_line_slice', function() + describe('deprecated: {get,set}_line_slice', function() it('get_line_slice: out-of-bounds returns empty array', function() curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'}) eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity @@ -149,7 +184,7 @@ describe('api/buf', function() end) end) - describe('{get,set}_lines', function() + describe('nvim_buf_get_lines, nvim_buf_set_lines', function() local get_lines, set_lines = curbufmeths.get_lines, curbufmeths.set_lines local line_count = curbufmeths.line_count @@ -272,7 +307,7 @@ describe('api/buf', function() eq({}, get_lines(-3, -4, true)) end) - it('set_line_slice: out-of-bounds can extend past end', function() + it('set_lines: out-of-bounds can extend past end', function() set_lines(0, -1, true, {'a', 'b', 'c'}) eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity @@ -286,7 +321,7 @@ describe('api/buf', function() eq({'e', 'a', 'b', 'c', 'd'}, get_lines(0, -1, true)) end) - it("set_line on alternate buffer does not access invalid line (E315)", function() + it("set_lines on alternate buffer does not access invalid line (E315)", function() feed_command('set hidden') insert('Initial file') command('enew') @@ -334,9 +369,27 @@ describe('api/buf', function() {2:-- INSERT --} | ]]) end) + + it('set_lines on hidden buffer preserves "previous window" #9741', function() + insert([[ + visible buffer line 1 + line 2 + ]]) + local hiddenbuf = meths.create_buf(false,true) + command('vsplit') + command('vsplit') + feed('<c-w>l<c-w>l<c-w>l') + eq(3, funcs.winnr()) + feed('<c-w>h') + eq(2, funcs.winnr()) + meths.buf_set_lines(hiddenbuf, 0, -1, true, + {'hidden buffer line 1', 'line 2'}) + feed('<c-w>p') + eq(3, funcs.winnr()) + end) end) - describe('get_offset', function() + describe('nvim_buf_get_offset', function() local get_offset = curbufmeths.get_offset it('works', function() curbufmeths.set_lines(0,-1,true,{'Some\r','exa\000mple', '', 'buf\rfer', 'text'}) @@ -373,7 +426,7 @@ describe('api/buf', function() end) end) - describe('{get,set,del}_var', function() + describe('nvim_buf_get_var, nvim_buf_set_var, nvim_buf_del_var', function() it('works', function() curbuf('set_var', 'lua', {1, 2, {['3'] = 1}}) eq({1, 2, {['3'] = 1}}, curbuf('get_var', 'lua')) @@ -393,7 +446,7 @@ describe('api/buf', function() end) end) - describe('get_changedtick', function() + describe('nvim_buf_get_changedtick', function() it('works', function() eq(2, curbufmeths.get_changedtick()) curbufmeths.set_lines(0, 1, false, {'abc\0', '\0def', 'ghi'}) @@ -417,7 +470,7 @@ describe('api/buf', function() end) end) - describe('{get,set}_option', function() + describe('nvim_buf_get_option, nvim_buf_set_option', function() it('works', function() eq(8, curbuf('get_option', 'shiftwidth')) curbuf('set_option', 'shiftwidth', 4) @@ -430,7 +483,7 @@ describe('api/buf', function() end) end) - describe('{get,set}_name', function() + describe('nvim_buf_get_name, nvim_buf_set_name', function() it('works', function() nvim('command', 'new') eq('', curbuf('get_name')) @@ -438,14 +491,12 @@ describe('api/buf', function() curbuf('set_name', new_name) eq(new_name, curbuf('get_name')) nvim('command', 'w!') - local f = io.open(new_name) - ok(f ~= nil) - f:close() + eq(1, funcs.filereadable(new_name)) os.remove(new_name) end) end) - describe('is_loaded', function() + describe('nvim_buf_is_loaded', function() it('works', function() -- record our buffer number for when we unload it local bufnr = curbuf('get_number') @@ -470,7 +521,7 @@ describe('api/buf', function() end) end) - describe('is_valid', function() + describe('nvim_buf_is_valid', function() it('works', function() nvim('command', 'new') local b = nvim('get_current_buf') @@ -480,12 +531,12 @@ describe('api/buf', function() end) end) - describe('get_mark', function() + describe('nvim_buf_get_mark', function() it('works', function() curbuf('set_lines', -1, -1, true, {'a', 'bit of', 'text'}) curwin('set_cursor', {3, 4}) - nvim('command', 'mark V') - eq({3, 0}, curbuf('get_mark', 'V')) + nvim('command', 'mark v') + eq({3, 0}, curbuf('get_mark', 'v')) end) end) end) |