mirror of
https://github.com/neovim/neovim.git
synced 2025-11-11 21:08:53 +00:00
test: Add 'eval' functional helper
The eval helper transforms vimL expressions into lua tables, it's useful for verifying function output.
This commit is contained in:
@@ -4,7 +4,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from lupa import LuaRuntime
|
from lupa import LuaRuntime, as_attrgetter
|
||||||
from neovim import Nvim, spawn_session
|
from neovim import Nvim, spawn_session
|
||||||
|
|
||||||
|
|
||||||
@@ -33,6 +33,14 @@ function(d)
|
|||||||
end
|
end
|
||||||
''')
|
''')
|
||||||
|
|
||||||
|
def to_table(obj):
|
||||||
|
if type(obj) in [tuple, list]:
|
||||||
|
return list_to_table(list(to_table(e) for e in obj))
|
||||||
|
if type(obj) is dict:
|
||||||
|
return dict_to_table(as_attrgetter(
|
||||||
|
dict((k, to_table(v)) for k, v in obj.items())))
|
||||||
|
return obj
|
||||||
|
|
||||||
nvim_prog = os.environ.get('NVIM_PROG', 'build/bin/nvim')
|
nvim_prog = os.environ.get('NVIM_PROG', 'build/bin/nvim')
|
||||||
nvim_argv = [nvim_prog, '-u', 'NONE', '--embed']
|
nvim_argv = [nvim_prog, '-u', 'NONE', '--embed']
|
||||||
|
|
||||||
@@ -51,6 +59,9 @@ nvim = Nvim.from_session(session)
|
|||||||
def nvim_command(cmd):
|
def nvim_command(cmd):
|
||||||
nvim.command(cmd)
|
nvim.command(cmd)
|
||||||
|
|
||||||
|
def nvim_eval(expr):
|
||||||
|
return to_table(nvim.eval(expr))
|
||||||
|
|
||||||
def nvim_feed(input, mode=''):
|
def nvim_feed(input, mode=''):
|
||||||
nvim.feedkeys(input)
|
nvim.feedkeys(input)
|
||||||
|
|
||||||
@@ -63,6 +74,7 @@ def nvim_replace_termcodes(input, *opts):
|
|||||||
|
|
||||||
expose = [
|
expose = [
|
||||||
nvim_command,
|
nvim_command,
|
||||||
|
nvim_eval,
|
||||||
nvim_feed,
|
nvim_feed,
|
||||||
nvim_replace_termcodes,
|
nvim_replace_termcodes,
|
||||||
buffer_slice,
|
buffer_slice,
|
||||||
|
|||||||
@@ -31,9 +31,24 @@ local function execute(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function eval(expr)
|
||||||
|
local status, result = pcall(function() return nvim_eval(expr) end)
|
||||||
|
if not status then
|
||||||
|
error('Failed to evaluate expression "' .. expr .. '"')
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
local function eq(expected, actual)
|
||||||
|
return assert.are.same(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function neq(expected, actual)
|
||||||
|
return assert.are_not.same(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
local function expect(contents, first, last, buffer_index)
|
local function expect(contents, first, last, buffer_index)
|
||||||
return assert.are.same(dedent(contents),
|
return eq(dedent(contents), buffer_slice(first, last, buffer_idx))
|
||||||
buffer_slice(first, last, buffer_idx))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
rawfeed([[:function BeforeEachTest()
|
rawfeed([[:function BeforeEachTest()
|
||||||
@@ -80,5 +95,8 @@ return {
|
|||||||
insert = insert,
|
insert = insert,
|
||||||
feed = feed,
|
feed = feed,
|
||||||
execute = execute,
|
execute = execute,
|
||||||
|
eval = eval,
|
||||||
|
eq = eq,
|
||||||
|
neq = neq,
|
||||||
expect = expect
|
expect = expect
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user