test: Refactor functional helpers to use vim_input

The vim_input function accepts raw terminal input and so is better to emulate
real user, especially because it is not deferred as vim_feedkeys.

Using this function required a number of changes:

- expect() was refactored to use curbuf_contents()
- The vim_eval function in request() was moved to curbuf_contents(). For most
  cases this is enough(we only care for synchronizing api calls with user input
  when verifying buffer contents).
- <C-@>(NUL) is preprocessed before being passed to replace_termcodes.
- Legacy test 4 had a bug that only became visible when using vim_input, it is
  fixed now.
- An extra blank line deletion was required for test 101

The last two items show that vim_feedkeys because it is not 100% equivalent to
receiving terminal input.
This commit is contained in:
Thiago de Arruda
2014-11-21 13:06:03 -03:00
parent e15485c5d6
commit 179c51319d
4 changed files with 29 additions and 29 deletions

View File

@@ -42,7 +42,6 @@ local function request(method, ...)
if not loop_stopped then if not loop_stopped then
-- Except when the loop has been stopped by a notification triggered -- Except when the loop has been stopped by a notification triggered
-- by the initial request, for example. -- by the initial request, for example.
session:request('vim_eval', '1')
end end
return rv return rv
end end
@@ -99,25 +98,20 @@ local function nvim_eval(expr)
return request('vim_eval', expr) return request('vim_eval', expr)
end end
local function nvim_feed(input, mode) local function nvim_feed(input)
mode = mode or '' while #input > 0 do
request('vim_feedkeys', input, mode) local written = request('vim_input', input)
end input = input:sub(written + 1)
local function buffer_slice(start, stop, buffer_idx)
local include_end = false
if not stop then
stop = -1
include_end = true
end end
local buffer = request('vim_get_buffers')[buffer_idx or 1]
local slice = request('buffer_get_line_slice', buffer, start or 0, stop,
true, include_end)
return table.concat(slice, '\n')
end end
local function nvim_replace_termcodes(input) local function nvim_replace_termcodes(input)
return request('vim_replace_termcodes', input, false, true, true) -- small hack to stop <C-@> from being replaced by the internal
-- representation(which is different and won't work for vim_input)
local temp_replacement = 'CCCCCCCCC@@@@@@@@@@'
input = input:gsub('<[Cc][-]@>', temp_replacement)
local rv = request('vim_replace_termcodes', input, false, true, true)
return rv:gsub(temp_replacement, '\000')
end end
local function dedent(str) local function dedent(str)
@@ -150,7 +144,7 @@ end
local function rawfeed(...) local function rawfeed(...)
for _, v in ipairs({...}) do for _, v in ipairs({...}) do
nvim_feed(dedent(v), 'nt') nvim_feed(dedent(v))
end end
end end
@@ -166,19 +160,19 @@ local function clear()
end end
local function insert(...) local function insert(...)
nvim_feed('i', 'nt') nvim_feed('i')
rawfeed(...) rawfeed(...)
nvim_feed(nvim_replace_termcodes('<ESC>'), 'nt') nvim_feed(nvim_replace_termcodes('<ESC>'))
end end
local function execute(...) local function execute(...)
for _, v in ipairs({...}) do for _, v in ipairs({...}) do
if v:sub(1, 1) ~= '/' then if v:sub(1, 1) ~= '/' then
-- not a search command, prefix with colon -- not a search command, prefix with colon
nvim_feed(':', 'nt') nvim_feed(':')
end end
nvim_feed(v, 'nt') nvim_feed(v)
nvim_feed(nvim_replace_termcodes('<CR>'), 'nt') nvim_feed(nvim_replace_termcodes('<CR>'))
end end
end end
@@ -200,10 +194,6 @@ local function neq(expected, actual)
return assert.are_not.same(expected, actual) return assert.are_not.same(expected, actual)
end end
local function expect(contents, first, last, buffer_index)
return eq(dedent(contents), buffer_slice(first, last, buffer_index))
end
local function ok(expr) local function ok(expr)
assert.is_true(expr) assert.is_true(expr)
end end
@@ -233,6 +223,10 @@ local function curbuf(method, ...)
end end
local function curbuf_contents() local function curbuf_contents()
-- Before inspecting the buffer, execute 'vim_eval' to wait until all
-- previously sent keys are processed(vim_eval is a deferred function, and
-- only processed after all input)
session:request('vim_eval', '1')
return table.concat(curbuf('get_line_slice', 0, -1, true, true), '\n') return table.concat(curbuf('get_line_slice', 0, -1, true, true), '\n')
end end
@@ -252,6 +246,10 @@ local function curtab(method, ...)
return tabpage(method, tab, ...) return tabpage(method, tab, ...)
end end
local function expect(contents)
return eq(dedent(contents), curbuf_contents())
end
clear() clear()
return { return {

View File

@@ -31,7 +31,7 @@ describe('BufEnter with modelines', function()
execute('sp Xxx') execute('sp Xxx')
-- Append text with autoindent to this file -- Append text with autoindent to this file
feed('G?this is a<Esc>') feed('G?this is a<CR>')
feed('othis should be auto-indented<Esc>') feed('othis should be auto-indented<Esc>')
-- Go to Xxx, no autocmd anymore -- Go to Xxx, no autocmd anymore
@@ -39,7 +39,7 @@ describe('BufEnter with modelines', function()
execute('buf Xxx') execute('buf Xxx')
-- Append text without autoindent to Xxx -- Append text without autoindent to Xxx
feed('G?this is a<Esc>') feed('G?this is a<CR>')
feed('othis should be in column 1<Esc>') feed('othis should be in column 1<Esc>')
execute('wq') execute('wq')

View File

@@ -3,6 +3,7 @@
local helpers = require('test.functional.helpers') local helpers = require('test.functional.helpers')
local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
local execute, expect = helpers.execute, helpers.expect local execute, expect = helpers.execute, helpers.expect
local eval = helpers.eval
describe('v:hlsearch', function() describe('v:hlsearch', function()
setup(clear) setup(clear)
@@ -44,7 +45,7 @@ describe('v:hlsearch', function()
execute('$put=r') execute('$put=r')
execute('call garbagecollect(1)') execute('call garbagecollect(1)')
execute('call getchar()') execute('call getchar()')
execute('1d') execute('1d', '1d')
-- Assert buffer contents. -- Assert buffer contents.
expect([[ expect([[

View File

@@ -10,6 +10,7 @@ local eq, clear, eval, feed, nvim =
local function create_file_with_nuls(name) local function create_file_with_nuls(name)
return function() return function()
feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>') feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>')
eval('1') -- wait for the file to be created
end end
end end