neovim/test/functional/editor/macro_spec.lua

232 lines
4.2 KiB
Lua
Raw Normal View History

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
2017-04-19 04:01:41 -07:00
local eq = t.eq
local eval = n.eval
local feed = n.feed
local clear = n.clear
local expect = n.expect
local command = n.command
local fn = n.fn
local api = n.api
local insert = n.insert
2017-04-19 04:01:41 -07:00
describe('macros with default mappings', function()
before_each(function()
2024-01-02 18:09:18 -07:00
clear({ args_rm = { '--cmd' } })
end)
2017-04-19 04:01:41 -07:00
it('can be recorded and replayed', function()
feed('qiahello<esc>q')
expect('hello')
eq('ahello', eval('@i'))
2017-04-19 04:01:41 -07:00
feed('@i')
expect('hellohello')
eq('ahello', eval('@i'))
2017-04-19 04:01:41 -07:00
end)
2017-04-19 04:01:41 -07:00
it('applies maps', function()
command('imap x l')
command('nmap l a')
feed('qilxxx<esc>q')
expect('lll')
eq('lxxx', eval('@i'))
2017-04-19 04:01:41 -07:00
feed('@i')
expect('llllll')
eq('lxxx', eval('@i'))
2017-04-19 04:01:41 -07:00
end)
it('can be replayed with Q', function()
insert [[
hello
hello
hello]]
feed [[gg]]
feed [[qqAFOO<esc>q]]
expect [[
helloFOO
hello
hello]]
2024-01-02 18:09:18 -07:00
feed [[Q]]
expect [[
helloFOOFOO
hello
hello]]
2024-01-02 18:09:18 -07:00
feed [[G3Q]]
expect [[
helloFOOFOO
hello
helloFOOFOOFOO]]
2024-01-02 18:09:18 -07:00
feed [[ggV3jQ]]
expect [[
helloFOOFOOFOO
helloFOO
helloFOOFOOFOOFOO]]
end)
it('can be replayed with Q and @@', function()
insert [[
hello
hello
hello]]
feed [[gg]]
feed [[qqAFOO<esc>q]]
expect [[
helloFOO
hello
hello]]
2024-01-02 18:09:18 -07:00
feed [[Q]]
expect [[
helloFOOFOO
hello
hello]]
2024-01-02 18:09:18 -07:00
feed [[G3@@]]
expect [[
helloFOOFOO
hello
helloFOOFOOFOO]]
2024-01-02 18:09:18 -07:00
feed [[ggV2j@@]]
expect [[
helloFOOFOOFOO
helloFOO
helloFOOFOOFOOFOO]]
end)
it('can be replayed with @ in linewise Visual mode', function()
insert [[
hello
hello
hello]]
feed [[gg]]
feed [[qqAFOO<esc>qu]]
expect [[
hello
hello
hello]]
feed [[qwA123<esc>qu]]
expect [[
hello
hello
hello]]
2024-01-02 18:09:18 -07:00
feed [[V3j@q]]
expect [[
helloFOO
helloFOO
helloFOO]]
feed [[ggVj@w]]
expect [[
helloFOO123
helloFOO123
helloFOO]]
end)
it('can be recorded and replayed in Visual mode', function()
insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
feed('0vqifofRq')
eq({ 0, 1, 7, 0 }, fn.getpos('.'))
eq({ 0, 1, 1, 0 }, fn.getpos('v'))
feed('Q')
eq({ 0, 1, 19, 0 }, fn.getpos('.'))
eq({ 0, 1, 1, 0 }, fn.getpos('v'))
feed('Q')
eq({ 0, 1, 27, 0 }, fn.getpos('.'))
eq({ 0, 1, 1, 0 }, fn.getpos('v'))
feed('@i')
eq({ 0, 1, 43, 0 }, fn.getpos('.'))
eq({ 0, 1, 1, 0 }, fn.getpos('v'))
end)
it('can be replayed with @ in blockwise Visual mode', function()
insert [[
hello
hello
hello]]
feed [[gg]]
feed [[qqAFOO<esc>qu]]
expect [[
hello
hello
hello]]
feed [[qwA123<esc>qu]]
expect [[
hello
hello
hello]]
feed [[0<C-v>3jl@q]]
expect [[
heFOOllo
heFOOllo
heFOOllo]]
feed [[gg0<C-v>j@w]]
expect [[
h123eFOOllo
h123eFOOllo
heFOOllo]]
end)
end)
describe('immediately after a macro has finished executing,', function()
before_each(function()
clear()
command([[let @a = 'gg0']])
end)
describe('reg_executing() from RPC returns an empty string', function()
it('if the macro does not end with a <Nop> mapping', function()
feed('@a')
eq('', fn.reg_executing())
end)
it('if the macro ends with a <Nop> mapping', function()
command('nnoremap 0 <Nop>')
feed('@a')
eq('', fn.reg_executing())
end)
end)
describe('characters from a mapping are not treated as a part of the macro #18015', function()
before_each(function()
command('nnoremap s qa')
end)
it('if the macro does not end with a <Nop> mapping', function()
2024-01-02 18:09:18 -07:00
feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
expect('')
eq('', eval('@a'))
end)
it('if the macro ends with a <Nop> mapping', function()
command('nnoremap 0 <Nop>')
2024-01-02 18:09:18 -07:00
feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
expect('')
eq('', eval('@a'))
end)
end)
end)
describe('reg_recorded()', function()
before_each(clear)
it('returns the correct value', function()
feed [[qqyyq]]
eq('q', eval('reg_recorded()'))
end)
2017-04-19 04:01:41 -07:00
end)