diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2024-01-03 02:09:18 +0100 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2024-01-03 02:09:29 +0100 |
commit | 04f2f864e270e772c6326cefdf24947f0130e492 (patch) | |
tree | 46f83f909b888a66c741032ab955afc6eab84292 /test/functional/editor/put_spec.lua | |
parent | 59d117ec99b6037cb9fad5bbfb6d0b18f5012927 (diff) | |
download | rneovim-04f2f864e270e772c6326cefdf24947f0130e492.tar.gz rneovim-04f2f864e270e772c6326cefdf24947f0130e492.tar.bz2 rneovim-04f2f864e270e772c6326cefdf24947f0130e492.zip |
refactor: format test/*
Diffstat (limited to 'test/functional/editor/put_spec.lua')
-rw-r--r-- | test/functional/editor/put_spec.lua | 305 |
1 files changed, 158 insertions, 147 deletions
diff --git a/test/functional/editor/put_spec.lua b/test/functional/editor/put_spec.lua index 47068470bb..8e06794093 100644 --- a/test/functional/editor/put_spec.lua +++ b/test/functional/editor/put_spec.lua @@ -35,12 +35,12 @@ describe('put command', function() before_each(reset) local function visual_marks_zero() - for _,v in pairs(funcs.getpos("'<")) do + for _, v in pairs(funcs.getpos("'<")) do if v ~= 0 then return false end end - for _,v in pairs(funcs.getpos("'>")) do + for _, v in pairs(funcs.getpos("'>")) do if v ~= 0 then return false end @@ -51,10 +51,12 @@ describe('put command', function() -- {{{ Where test definitions are run local function run_test_variations(test_variations, extra_setup) reset() - if extra_setup then extra_setup() end + if extra_setup then + extra_setup() + end local init_contents = curbuf_contents() local init_cursorpos = funcs.getcurpos() - local assert_no_change = function (exception_table, after_undo) + local assert_no_change = function(exception_table, after_undo) expect(init_contents) -- When putting the ". register forwards, undo doesn't move -- the cursor back to where it was before. @@ -69,7 +71,9 @@ describe('put command', function() for _, test in pairs(test_variations) do it(test.description, function() - if extra_setup then extra_setup() end + if extra_setup then + extra_setup() + end local orig_dotstr = funcs.getreg('.') helpers.ok(visual_marks_zero()) -- Make sure every test starts from the same conditions @@ -115,8 +119,13 @@ describe('put command', function() end -- run_test_variations() -- }}} - local function create_test_defs(test_defs, command_base, command_creator, -- {{{ - expect_base, expect_creator) + local function create_test_defs( + test_defs, + command_base, + command_creator, -- {{{ + expect_base, + expect_creator + ) local rettab = {} local exceptions for _, v in pairs(test_defs) do @@ -125,8 +134,7 @@ describe('put command', function() else exceptions = {} end - table.insert(rettab, - { + table.insert(rettab, { test_action = command_creator(command_base, v[1]), test_assertions = expect_creator(expect_base, v[2]), description = v[3], @@ -146,7 +154,7 @@ describe('put command', function() for linenum, line in pairs(funcs.split(expect_string, '\n', 1)) do local column = line:find('x') if column then - return {linenum, column}, expect_string:gsub('x', '') + return { linenum, column }, expect_string:gsub('x', '') end end end -- find_cursor_position() }}} @@ -186,7 +194,7 @@ describe('put command', function() -- '.' command. if not (exception_table.redo_position and after_redo) then local actual_position = funcs.getcurpos() - eq(cursor_position, {actual_position[2], actual_position[5]}) + eq(cursor_position, { actual_position[2], actual_position[5] }) end end end -- expect_creator() }}} @@ -195,13 +203,13 @@ describe('put command', function() local function copy_def(def) local rettab = { '', {}, '', nil } rettab[1] = def[1] - for k,v in pairs(def[2]) do + for k, v in pairs(def[2]) do rettab[2][k] = v end rettab[3] = def[3] if def[4] then rettab[4] = {} - for k,v in pairs(def[4]) do + for k, v in pairs(def[4]) do rettab[4][k] = v end end @@ -211,52 +219,52 @@ describe('put command', function() local normal_command_defs = { { 'p', - {cursor_after = false, put_backwards = false, dot_register = false}, + { cursor_after = false, put_backwards = false, dot_register = false }, 'pastes after cursor with p', }, { 'gp', - {cursor_after = true, put_backwards = false, dot_register = false}, + { cursor_after = true, put_backwards = false, dot_register = false }, 'leaves cursor after text with gp', }, { '".p', - {cursor_after = false, put_backwards = false, dot_register = true}, + { cursor_after = false, put_backwards = false, dot_register = true }, 'works with the ". register', }, { '".gp', - {cursor_after = true, put_backwards = false, dot_register = true}, + { cursor_after = true, put_backwards = false, dot_register = true }, 'gp works with the ". register', - {redo_position = true}, + { redo_position = true }, }, { 'P', - {cursor_after = false, put_backwards = true, dot_register = false}, + { cursor_after = false, put_backwards = true, dot_register = false }, 'pastes before cursor with P', }, { 'gP', - {cursor_after = true, put_backwards = true, dot_register = false}, + { cursor_after = true, put_backwards = true, dot_register = false }, 'gP pastes before cursor and leaves cursor after text', }, { '".P', - {cursor_after = false, put_backwards = true, dot_register = true}, + { cursor_after = false, put_backwards = true, dot_register = true }, 'P works with ". register', }, { '".gP', - {cursor_after = true, put_backwards = true, dot_register = true}, + { cursor_after = true, put_backwards = true, dot_register = true }, 'gP works with ". register', - {redo_position = true}, + { redo_position = true }, }, } -- Add a definition applying a count for each definition above. -- Could do this for each transformation (p -> P, p -> gp etc), but I think -- it's neater this way (balance between being explicit and too verbose). - for i = 1,#normal_command_defs do + for i = 1, #normal_command_defs do local cur = normal_command_defs[i] -- Make modified copy of current definition that includes a count. @@ -279,35 +287,36 @@ describe('put command', function() local ex_command_defs = { { 'put', - {put_backwards = false, dot_register = false}, + { put_backwards = false, dot_register = false }, 'pastes linewise forwards with :put', }, { 'put!', - {put_backwards = true, dot_register = false}, + { put_backwards = true, dot_register = false }, 'pastes linewise backwards with :put!', }, { 'put .', - {put_backwards = false, dot_register = true}, + { put_backwards = false, dot_register = true }, 'pastes linewise with the dot register', }, { 'put! .', - {put_backwards = true, dot_register = true}, + { put_backwards = true, dot_register = true }, 'pastes linewise backwards with the dot register', }, } local function non_dotdefs(def_table) - return filter(function(d) return not d[2].dot_register end, def_table) + return filter(function(d) + return not d[2].dot_register + end, def_table) end -- }}} -- Conversion functions {{{ - local function convert_charwise(expect_base, conversion_table, - virtualedit_end, visual_put) + local function convert_charwise(expect_base, conversion_table, virtualedit_end, visual_put) expect_base = dedent(expect_base) -- There is no difference between 'P' and 'p' when VIsual_active if not visual_put then @@ -324,7 +333,7 @@ describe('put command', function() end if conversion_table.count > 1 then local rep_string = 'test_string"' - local extra_puts = rep_string:rep(conversion_table.count - 1) + local extra_puts = rep_string:rep(conversion_table.count - 1) expect_base = expect_base:gsub('test_stringx"', extra_puts .. 'test_stringx"') end if conversion_table.cursor_after then @@ -395,7 +404,7 @@ describe('put command', function() indent = '' end local rep_string = indent .. p_str .. '\n' - local extra_puts = rep_string:rep(conversion_table.count - 1) + local extra_puts = rep_string:rep(conversion_table.count - 1) local orig_string, new_string if conversion_table.cursor_after then orig_string = indent .. p_str .. '\nx' @@ -420,8 +429,13 @@ describe('put command', function() return orig_line:sub(1, prev_end - 1) .. 'x' .. orig_line:sub(prev_end) end - local function convert_blockwise(expect_base, conversion_table, visual, - use_b, trailing_whitespace) + local function convert_blockwise( + expect_base, + conversion_table, + visual, + use_b, + trailing_whitespace + ) expect_base = dedent(expect_base) local p_str = 'test_string"' if use_b then @@ -452,11 +466,9 @@ describe('put command', function() if conversion_table.count and conversion_table.count > 1 then local p_pattern = p_str:gsub('%.', '%%.') - expect_base = expect_base:gsub(p_pattern, - p_str:rep(conversion_table.count)) - expect_base = expect_base:gsub('test_stringx([b".])', - p_str:rep(conversion_table.count - 1) - .. '%0') + expect_base = expect_base:gsub(p_pattern, p_str:rep(conversion_table.count)) + expect_base = + expect_base:gsub('test_stringx([b".])', p_str:rep(conversion_table.count - 1) .. '%0') end if conversion_table.cursor_after then @@ -496,8 +508,13 @@ describe('put command', function() -- }}} -- Convenience functions {{{ - local function run_normal_mode_tests(test_string, base_map, extra_setup, - virtualedit_end, selection_string) + local function run_normal_mode_tests( + test_string, + base_map, + extra_setup, + virtualedit_end, + selection_string + ) local function convert_closure(e, c) return convert_charwise(e, c, virtualedit_end, selection_string) end @@ -532,8 +549,12 @@ describe('put command', function() local function run_linewise_tests(expect_base, base_command, extra_setup) local linewise_test_defs = create_test_defs( - ex_command_defs, base_command, - create_put_action, expect_base, convert_linewiseer) + ex_command_defs, + base_command, + create_put_action, + expect_base, + convert_linewiseer + ) run_test_variations(linewise_test_defs, extra_setup) end -- run_linewise_tests() -- }}} @@ -545,7 +566,8 @@ describe('put command', function() Line of words 2]] run_normal_mode_tests(expect_string, 'p') - run_linewise_tests([[ + run_linewise_tests( + [[ Line of words 1 xtest_string" Line of words 2]], @@ -585,11 +607,12 @@ describe('put command', function() run_test_variations( create_test_defs( linewise_put_defs, - 'put a', create_put_action, - base_expect_string, convert_linewiseer + 'put a', + create_put_action, + base_expect_string, + convert_linewiseer ) ) - end) describe('blockwise register', function() @@ -600,18 +623,13 @@ describe('put command', function() test_stringb]] local function expect_block_creator(expect_base, conversion_table) - return expect_creator(function(e,c) return convert_blockwise(e,c,nil,true) end, - expect_base, conversion_table) + return expect_creator(function(e, c) + return convert_blockwise(e, c, nil, true) + end, expect_base, conversion_table) end run_test_variations( - create_test_defs( - blockwise_put_defs, - '"bp', - create_p_action, - test_base, - expect_block_creator - ) + create_test_defs(blockwise_put_defs, '"bp', create_p_action, test_base, expect_block_creator) ) end) @@ -632,17 +650,17 @@ describe('put command', function() describe('linewise paste with autoindent', function() -- luacheck: ignore - run_linewise_tests([[ + run_linewise_tests( + [[ Line of words 1 Line of words 2 xtest_string"]], - 'put' - , + 'put', function() funcs.setline('$', ' Line of words 2') -- Set curswant to '8' to be at the end of the tab character -- This is where the cursor is put back after the 'u' command. - funcs.setpos('.', {0, 2, 1, 0, 8}) + funcs.setpos('.', { 0, 2, 1, 0, 8 }) command('set autoindent') end ) @@ -655,7 +673,7 @@ describe('put command', function() run_normal_mode_tests(test_string, 'p', function() funcs.setline('$', ' Line of words 2') command('setlocal virtualedit=all') - funcs.setpos('.', {0, 2, 1, 2, 3}) + funcs.setpos('.', { 0, 2, 1, 2, 3 }) end) end) @@ -667,7 +685,7 @@ describe('put command', function() run_normal_mode_tests(test_string, 'p', function() funcs.setline('$', ' Line of words 2') command('setlocal virtualedit=all') - funcs.setpos('.', {0, 1, 16, 1, 17}) + funcs.setpos('.', { 0, 1, 16, 1, 17 }) end, true) end) @@ -679,12 +697,10 @@ describe('put command', function() run_normal_mode_tests(test_string, 'v2ep', nil, nil, 'Line of') end) describe('over trailing newline', function() - local test_string = 'Line of test_stringx"Line of words 2' + local test_string = 'Line of test_stringx"Line of words 2' run_normal_mode_tests(test_string, 'v$p', function() - funcs.setpos('.', {0, 1, 9, 0, 9}) - end, - nil, - 'words 1\n') + funcs.setpos('.', { 0, 1, 9, 0, 9 }) + end, nil, 'words 1\n') end) describe('linewise mode', function() local test_string = [[ @@ -693,8 +709,7 @@ describe('put command', function() local function expect_vis_linewise(expect_base, conversion_table) return expect_creator(function(e, c) return convert_linewise(e, c, nil, nil) - end, - expect_base, conversion_table) + end, expect_base, conversion_table) end run_test_variations( create_test_defs( @@ -704,15 +719,16 @@ describe('put command', function() test_string, expect_vis_linewise ), - function() funcs.setpos('.', {0, 1, 1, 0, 1}) end + function() + funcs.setpos('.', { 0, 1, 1, 0, 1 }) + end ) describe('with whitespace at bol', function() local function expect_vis_lineindented(expect_base, conversion_table) local test_expect = expect_creator(function(e, c) - return convert_linewise(e, c, nil, nil, ' ') - end, - expect_base, conversion_table) + return convert_linewise(e, c, nil, nil, ' ') + end, expect_base, conversion_table) return function(exception_table, after_redo) test_expect(exception_table, after_redo) if not conversion_table.put_backwards then @@ -737,7 +753,6 @@ describe('put command', function() end ) end) - end) describe('blockwise visual mode', function() @@ -747,10 +762,10 @@ describe('put command', function() local function expect_block_creator(expect_base, conversion_table) local test_expect = expect_creator(function(e, c) - return convert_blockwise(e, c, true) - end, expect_base, conversion_table) - return function(e,c) - test_expect(e,c) + return convert_blockwise(e, c, true) + end, expect_base, conversion_table) + return function(e, c) + test_expect(e, c) if not conversion_table.put_backwards then eq('Lin\nLin', funcs.getreg('"')) end @@ -758,28 +773,26 @@ describe('put command', function() end local select_down_test_defs = create_test_defs( - normal_command_defs, - '<C-v>jllp', - create_p_action, - test_base, - expect_block_creator + normal_command_defs, + '<C-v>jllp', + create_p_action, + test_base, + expect_block_creator ) run_test_variations(select_down_test_defs) - -- Undo and redo of a visual block put leave the cursor in the top -- left of the visual block area no matter where the cursor was -- when it started. local undo_redo_no = map(function(table) - local rettab = copy_def(table) - if not rettab[4] then - rettab[4] = {} - end - rettab[4].undo_position = true - rettab[4].redo_position = true - return rettab - end, - normal_command_defs) + local rettab = copy_def(table) + if not rettab[4] then + rettab[4] = {} + end + rettab[4].undo_position = true + rettab[4].redo_position = true + return rettab + end, normal_command_defs) -- Selection direction doesn't matter run_test_variations( @@ -790,7 +803,9 @@ describe('put command', function() test_base, expect_block_creator ), - function() funcs.setpos('.', {0, 2, 1, 0, 1}) end + function() + funcs.setpos('.', { 0, 2, 1, 0, 1 }) + end ) describe('blockwise cursor after undo', function() @@ -800,62 +815,45 @@ describe('put command', function() -- the same pattern as everything else. -- Here we fix this by directly checking the undo/redo position -- in the test_assertions of our test definitions. - local function assertion_creator(_,_) - return function(_,_) + local function assertion_creator(_, _) + return function(_, _) feed('u') -- Have to use feed('u') here to set curswant, because -- ex_undo() doesn't do that. - eq({0, 1, 1, 0, 1}, funcs.getcurpos()) + eq({ 0, 1, 1, 0, 1 }, funcs.getcurpos()) feed('<C-r>') - eq({0, 1, 1, 0, 1}, funcs.getcurpos()) + eq({ 0, 1, 1, 0, 1 }, funcs.getcurpos()) end end run_test_variations( - create_test_defs( - undo_redo_no, - '<C-v>kllp', - create_p_action, - test_base, - assertion_creator - ), - function() funcs.setpos('.', {0, 2, 1, 0, 1}) end + create_test_defs(undo_redo_no, '<C-v>kllp', create_p_action, test_base, assertion_creator), + function() + funcs.setpos('.', { 0, 2, 1, 0, 1 }) + end ) end) end) - describe("with 'virtualedit'", function() describe('splitting a tab character', function() local base_expect_string = [[ Line of words 1 test_stringx" Line of words 2]] - run_normal_mode_tests( - base_expect_string, - 'vp', - function() - funcs.setline('$', ' Line of words 2') - command('setlocal virtualedit=all') - funcs.setpos('.', {0, 2, 1, 2, 3}) - end, - nil, - ' ' - ) + run_normal_mode_tests(base_expect_string, 'vp', function() + funcs.setline('$', ' Line of words 2') + command('setlocal virtualedit=all') + funcs.setpos('.', { 0, 2, 1, 2, 3 }) + end, nil, ' ') end) describe('after end of line', function() local base_expect_string = [[ Line of words 1 test_stringx" Line of words 2]] - run_normal_mode_tests( - base_expect_string, - 'vp', - function() - command('setlocal virtualedit=all') - funcs.setpos('.', {0, 1, 16, 2, 18}) - end, - true, - ' ' - ) + run_normal_mode_tests(base_expect_string, 'vp', function() + command('setlocal virtualedit=all') + funcs.setpos('.', { 0, 1, 16, 2, 18 }) + end, true, ' ') end) end) end) @@ -873,9 +871,12 @@ describe('put command', function() Line of words 1 Line of words 2]]) feed('u1go<C-v>j".p') - eq([[ + eq( + [[ ine of words 1 - ine of words 2]], curbuf_contents()) + ine of words 2]], + curbuf_contents() + ) end) local screen @@ -891,33 +892,42 @@ describe('put command', function() end helpers.ok(not screen.bell and not screen.visualbell) actions() - screen:expect{condition=function() - if should_ring then - if not screen.bell and not screen.visualbell then - error('Bell was not rung after action') - end - else - if screen.bell or screen.visualbell then - error('Bell was rung after action') + screen:expect { + condition = function() + if should_ring then + if not screen.bell and not screen.visualbell then + error('Bell was not rung after action') + end + else + if screen.bell or screen.visualbell then + error('Bell was rung after action') + end end - end - end, unchanged=(not should_ring)} + end, + unchanged = not should_ring, + } screen.bell = false screen.visualbell = false end it('should not ring the bell with gp at end of line', function() - bell_test(function() feed('$".gp') end) + bell_test(function() + feed('$".gp') + end) -- Even if the last character is a multibyte character. reset() funcs.setline(1, 'helloม') - bell_test(function() feed('$".gp') end) + bell_test(function() + feed('$".gp') + end) end) it('should not ring the bell with gp and end of file', function() - funcs.setpos('.', {0, 2, 1, 0}) - bell_test(function() feed('$vl".gp') end) + funcs.setpos('.', { 0, 2, 1, 0 }) + bell_test(function() + feed('$vl".gp') + end) end) it('should ring the bell when deleting if not appropriate', function() @@ -926,7 +936,9 @@ describe('put command', function() expect([[ ine of words 1 Line of words 2]]) - bell_test(function() feed('".P') end, true) + bell_test(function() + feed('".P') + end, true) end) it('should restore cursor position after undo of ".p', function() @@ -946,4 +958,3 @@ describe('put command', function() end) end) end) - |