diff options
Diffstat (limited to 'test/functional/lua')
-rw-r--r-- | test/functional/lua/buffer_updates_spec.lua | 162 | ||||
-rw-r--r-- | test/functional/lua/utility_functions_spec.lua | 55 |
2 files changed, 178 insertions, 39 deletions
diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index c419d89be3..990cb97fec 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -5,28 +5,31 @@ local command = helpers.command local meths = helpers.meths local clear = helpers.clear local eq = helpers.eq +local exec_lua = helpers.exec_lua +local feed = helpers.feed local origlines = {"original line 1", "original line 2", "original line 3", "original line 4", "original line 5", - "original line 6"} + "original line 6", + " indented line"} describe('lua: buffer event callbacks', function() before_each(function() clear() - meths.execute_lua([[ + exec_lua([[ local events = {} - function test_register(bufnr, id, changedtick) + function test_register(bufnr, id, changedtick, utf_sizes) local function callback(...) table.insert(events, {id, ...}) if test_unreg == id then return true end end - local opts = {on_lines=callback, on_detach=callback} + local opts = {on_lines=callback, on_detach=callback, utf_sizes=utf_sizes} if changedtick then opts.on_changedtick = callback end @@ -38,55 +41,166 @@ describe('lua: buffer event callbacks', function() events = {} return ret_events end - ]], {}) + ]]) end) - it('works', function() + + -- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot + -- assert the wrong thing), but masks errors with unflushed lines (as + -- nvim_buf_get_offset forces a flush of the memline). To be safe run the + -- test both ways. + local function check(verify,utf_sizes) + local lastsize meths.buf_set_lines(0, 0, -1, true, origlines) - meths.execute_lua("return test_register(...)", {0, "test1"}) + if verify then + lastsize = meths.buf_get_offset(0, meths.buf_line_count(0)) + end + exec_lua("return test_register(...)", 0, "test1",false,utf_sizes) local tick = meths.buf_get_changedtick(0) + local verify_name = "test1" + local function check_events(expected) + local events = exec_lua("return get_events(...)" ) + if utf_sizes then + -- this test case uses ASCII only, so sizes sshould be the same. + -- Unicode is tested below. + for _, event in ipairs(expected) do + event[9] = event[8] + event[10] = event[8] + end + end + eq(expected, events) + if verify then + for _, event in ipairs(events) do + if event[1] == verify_name and event[2] == "lines" then + local startline, endline = event[5], event[7] + local newrange = meths.buf_get_offset(0, endline) - meths.buf_get_offset(0, startline) + local newsize = meths.buf_get_offset(0, meths.buf_line_count(0)) + local oldrange = newrange + lastsize - newsize + eq(oldrange, event[8]) + lastsize = newsize + end + end + end + end + + command('set autoindent') command('normal! GyyggP') tick = tick + 1 - eq({{ "test1", "lines", 1, tick, 0, 0, 1 }}, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test1", "lines", 1, tick, 0, 0, 1, 0}}) meths.buf_set_lines(0, 3, 5, true, {"changed line"}) tick = tick + 1 - eq({{ "test1", "lines", 1, tick, 3, 5, 4 }}, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test1", "lines", 1, tick, 3, 5, 4, 32 }}) - meths.execute_lua("return test_register(...)", {0, "test2", true}) + exec_lua("return test_register(...)", 0, "test2", true, utf_sizes) tick = tick + 1 command('undo') -- plugins can opt in to receive changedtick events, or choose -- to only recieve actual changes. - eq({{ "test1", "lines", 1, tick, 3, 4, 5 }, - { "test2", "lines", 1, tick, 3, 4, 5 }, - { "test2", "changedtick", 1, tick+1 } }, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test1", "lines", 1, tick, 3, 4, 5, 13 }, + { "test2", "lines", 1, tick, 3, 4, 5, 13 }, + { "test2", "changedtick", 1, tick+1 } }) tick = tick + 1 -- simulate next callback returning true - meths.execute_lua("test_unreg = 'test1'", {}) + exec_lua("test_unreg = 'test1'") meths.buf_set_lines(0, 6, 7, true, {"x1","x2","x3"}) tick = tick + 1 -- plugins can opt in to receive changedtick events, or choose -- to only recieve actual changes. - eq({{ "test1", "lines", 1, tick, 6, 7, 9 }, - { "test2", "lines", 1, tick, 6, 7, 9 }}, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test1", "lines", 1, tick, 6, 7, 9, 16 }, + { "test2", "lines", 1, tick, 6, 7, 9, 16 }}) + + verify_name = "test2" meths.buf_set_lines(0, 1, 1, true, {"added"}) tick = tick + 1 - eq({{ "test2", "lines", 1, tick, 1, 1, 2 }}, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test2", "lines", 1, tick, 1, 1, 2, 0 }}) + + feed('wix') + tick = tick + 1 + check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 16 }}) + + -- check hot path for multiple insert + feed('yz') + tick = tick + 1 + check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 17 }}) + + feed('<bs>') + tick = tick + 1 + check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 19 }}) + + feed('<esc>Go') + tick = tick + 1 + check_events({{ "test2", "lines", 1, tick, 11, 11, 12, 0 }}) + + feed('x') + tick = tick + 1 + check_events({{ "test2", "lines", 1, tick, 11, 12, 12, 5 }}) command('bwipe!') - eq({{ "test2", "detach", 1 }}, - meths.execute_lua("return get_events(...)", {})) + check_events({{ "test2", "detach", 1 }}) + end + + it('works', function() + check(false) end) + + it('works with verify', function() + check(true) + end) + + it('works with utf_sizes and ASCII text', function() + check(false,true) + end) + + it('works with utf_sizes and unicode text', function() + local unicode_text = {"ascii text", + "latin text åäö", + "BMP text ɧ αλφά", + "BMP text 汉语 ↥↧", + "SMP 🤦 🦄🦃", + "combining å بِيَّة"} + meths.buf_set_lines(0, 0, -1, true, unicode_text) + feed('gg') + exec_lua("return test_register(...)", 0, "test1", false, true) + local tick = meths.buf_get_changedtick(0) + + feed('dd') + tick = tick + 1 + eq({{ "test1", "lines", 1, tick, 0, 1, 0, 11, 11, 11 }}, exec_lua("return get_events(...)" )) + + feed('A<bs>') + tick = tick + 1 + eq({{ "test1", "lines", 1, tick, 0, 1, 1, 18, 15, 15 }}, exec_lua("return get_events(...)" )) + + feed('<esc>jylp') + tick = tick + 1 + eq({{ "test1", "lines", 1, tick, 1, 2, 2, 21, 16, 16 }}, exec_lua("return get_events(...)" )) + + feed('+eea<cr>') + tick = tick + 1 + eq({{ "test1", "lines", 1, tick, 2, 3, 4, 23, 15, 15 }}, exec_lua("return get_events(...)" )) + + feed('<esc>jdw') + tick = tick + 1 + -- non-BMP chars count as 2 UTF-2 codeunits + eq({{ "test1", "lines", 1, tick, 4, 5, 5, 18, 9, 12 }}, exec_lua("return get_events(...)" )) + + feed('+rx') + tick = tick + 1 + -- count the individual codepoints of a composed character. + eq({{ "test1", "lines", 1, tick, 5, 6, 6, 27, 20, 20 }}, exec_lua("return get_events(...)" )) + + feed('kJ') + tick = tick + 1 + -- NB: this is inefficient (but not really wrong). + eq({{ "test1", "lines", 1, tick, 4, 5, 5, 14, 5, 8 }, + { "test1", "lines", 1, tick+1, 5, 6, 5, 27, 20, 20 }}, exec_lua("return get_events(...)" )) + end) + end) diff --git a/test/functional/lua/utility_functions_spec.lua b/test/functional/lua/utility_functions_spec.lua index 780d3a1565..0d93914119 100644 --- a/test/functional/lua/utility_functions_spec.lua +++ b/test/functional/lua/utility_functions_spec.lua @@ -2,12 +2,12 @@ local helpers = require('test.functional.helpers')(after_each) local funcs = helpers.funcs -local meths = helpers.meths local clear = helpers.clear local eq = helpers.eq local eval = helpers.eval local feed = helpers.feed local meth_pcall = helpers.meth_pcall +local exec_lua = helpers.exec_lua before_each(clear) @@ -110,28 +110,53 @@ describe('lua function', function() eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")')) end) + it("vim.str_utfindex/str_byteindex", function() + exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]]) + local indicies32 = {[0]=0,1,2,3,5,7,9,10,12,13,16,19,20,23,24,28,29,33,34,35,37,38,40,42,44,46,48} + local indicies16 = {[0]=0,1,2,3,5,7,9,10,12,13,16,19,20,23,24,28,28,29,33,33,34,35,37,38,40,42,44,46,48} + for i,k in pairs(indicies32) do + eq(k, exec_lua("return vim.str_byteindex(_G.test_text, ...)", i), i) + end + for i,k in pairs(indicies16) do + eq(k, exec_lua("return vim.str_byteindex(_G.test_text, ..., true)", i), i) + end + local i32, i16 = 0, 0 + for k = 0,48 do + if indicies32[i32] < k then + i32 = i32 + 1 + end + if indicies16[i16] < k then + i16 = i16 + 1 + if indicies16[i16+1] == indicies16[i16] then + i16 = i16 + 1 + end + end + eq({i32, i16}, exec_lua("return {vim.str_utfindex(_G.test_text, ...)}", k), k) + end + end) + it("vim.schedule", function() - meths.execute_lua([[ + exec_lua([[ test_table = {} vim.schedule(function() table.insert(test_table, "xx") end) table.insert(test_table, "yy") - ]], {}) - eq({"yy","xx"}, meths.execute_lua("return test_table", {})) + ]]) + eq({"yy","xx"}, exec_lua("return test_table")) -- type checked args eq({false, 'Error executing lua: vim.schedule: expected function'}, - meth_pcall(meths.execute_lua, "vim.schedule('stringly')", {})) + meth_pcall(exec_lua, "vim.schedule('stringly')")) eq({false, 'Error executing lua: vim.schedule: expected function'}, - meth_pcall(meths.execute_lua, "vim.schedule()", {})) + meth_pcall(exec_lua, "vim.schedule()")) - meths.execute_lua([[ + exec_lua([[ vim.schedule(function() error("big failure\nvery async") end) - ]], {}) + ]]) feed("<cr>") eq('Error executing vim.schedule lua callback: [string "<nvim>"]:2: big failure\nvery async', eval("v:errmsg")) @@ -139,7 +164,7 @@ describe('lua function', function() it("vim.split", function() local split = function(str, sep) - return meths.execute_lua('return vim.split(...)', {str, sep}) + return exec_lua('return vim.split(...)', str, sep) end local tests = { @@ -172,7 +197,7 @@ describe('lua function', function() it('vim.trim', function() local trim = function(s) - return meths.execute_lua('return vim.trim(...)', { s }) + return exec_lua('return vim.trim(...)', s) end local trims = { @@ -194,7 +219,7 @@ describe('lua function', function() it('vim.inspect', function() -- just make sure it basically works, it has its own test suite local inspect = function(t, opts) - return meths.execute_lua('return vim.inspect(...)', { t, opts }) + return exec_lua('return vim.inspect(...)', t, opts) end eq('2', inspect(2)) @@ -202,18 +227,18 @@ describe('lua function', function() inspect({ a = { b = 1 } }, { newline = '+', indent = '' })) -- special value vim.inspect.KEY works - eq('{ KEY_a = "x", KEY_b = "y"}', meths.execute_lua([[ + eq('{ KEY_a = "x", KEY_b = "y"}', exec_lua([[ return vim.inspect({a="x", b="y"}, {newline = '', process = function(item, path) if path[#path] == vim.inspect.KEY then return 'KEY_'..item end return item end}) - ]], {})) + ]])) end) it("vim.deepcopy", function() - local is_dc = meths.execute_lua([[ + local is_dc = exec_lua([[ local a = { x = { 1, 2 }, y = 5} local b = vim.deepcopy(a) @@ -222,7 +247,7 @@ describe('lua function', function() return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and count == 2 and tostring(a) ~= tostring(b) - ]], {}) + ]]) assert(is_dc) end) |