functests: Add tests

This commit is contained in:
ZyX
2017-06-20 18:17:19 +03:00
parent 60c0252672
commit 607dc3e0f9
2 changed files with 180 additions and 3 deletions

View File

@@ -0,0 +1,176 @@
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local clear = helpers.clear
local funcs = helpers.funcs
local dedent = helpers.dedent
local redir_exec = helpers.redir_exec
before_each(clear)
local function check_nofunc(fname)
eq(0, funcs.exists('*' .. fname))
end
local function check_func(fname, body)
if type(body) == 'number' then
body = ('return %i'):format(body)
end
eq(dedent(([[
function %s()%s
endfunction]]
), 3):format(fname, body and ('\n1 ' .. body) or ''),
redir_exec('function ' .. fname))
end
describe(':endfunction', function()
it('accepts bang', function()
eq('', redir_exec([[
function F()
endfunction!
]]))
check_func('F')
eq('', redir_exec([[
function! F()
return 1
endfunction!
]]))
check_func('F', 1)
end)
it('accepts comments', function()
eq('', redir_exec([[
function F1()
endfunction " Comment
]]))
check_func('F1')
eq('', redir_exec([[
function F2()
endfunction " }}}
]]))
check_func('F2')
eq('', redir_exec([[
function F3()
endfunction " F3
]]))
check_func('F3')
eq('', redir_exec([[
function F4()
endfunction! " F4
]]))
check_func('F4')
eq('', redir_exec([[
function! F4()
return 2
endfunction! " F4
]]))
check_func('F4', 2)
end)
it('accepts function name', function()
eq('', redir_exec([[
function F0()
endfunction F0
]]))
check_func('F0')
eq('', redir_exec([[
function F1()
endfunction! F1
]]))
check_func('F1')
eq('', redir_exec([[
function! F2()
endfunction! F2
]]))
check_func('F2')
eq('', redir_exec([[
function! F2()
return 3
endfunction! F2
]]))
check_func('F2', 3)
end)
it('accepts weird characters', function()
eq('', redir_exec([[
function F1()
endfunction: }}}
]]))
check_func('F1')
-- From accurev
eq('', redir_exec([[
function F2()
endfunction :}}}
]]))
check_func('F2')
-- From cream-vimabbrev
eq('', redir_exec([[
function F3()
endfunction 1}}}
]]))
check_func('F3')
-- From pyunit
eq('', redir_exec([[
function F4()
endfunction # }}}
]]))
check_func('F4')
-- From vim-lldb
eq('', redir_exec([[
function F5()
endfunction()
]]))
check_func('F5')
-- From vim-mail
eq('', redir_exec([[
function F6()
endfunction;
]]))
check_func('F6')
end)
it('accepts commented bar', function()
eq('', redir_exec([[
function F1()
endfunction " F1 | echo 42
]]))
check_func('F1')
eq('', redir_exec([[
function! F1()
return 42
endfunction! " F1 | echo 42
]]))
check_func('F1', 42)
end)
it('errors out on an uncommented bar', function()
eq('\nE488: Trailing characters: | echo 42', redir_exec([[
function F1()
endfunction | echo 42
]]))
check_nofunc('F1')
end)
it('allows running multiple commands', function()
eq('\n2', redir_exec([[
function F1()
echo 2
endfunction
call F1()
]]))
check_func('F1', 'echo 2')
eq('\n2\n3\n4', redir_exec([[
function F2()
echo 2
endfunction F2
function F3()
echo 3
endfunction " F3
function! F4()
echo 4
endfunction!
call F2()
call F3()
call F4()
]]))
check_func('F2', 'echo 2')
check_func('F3', 'echo 3')
check_func('F4', 'echo 4')
end)
end)
-- vim: foldmarker=▶,▲

View File

@@ -284,7 +284,7 @@ local function concat_tables(...)
return ret return ret
end end
local function dedent(str) local function dedent(str, leave_indent)
-- find minimum common indent across lines -- find minimum common indent across lines
local indent = nil local indent = nil
for line in str:gmatch('[^\n]+') do for line in str:gmatch('[^\n]+') do
@@ -297,12 +297,13 @@ local function dedent(str)
-- no minimum common indent -- no minimum common indent
return str return str
end end
local left_indent = (' '):rep(leave_indent or 0)
-- create a pattern for the indent -- create a pattern for the indent
indent = indent:gsub('%s', '[ \t]') indent = indent:gsub('%s', '[ \t]')
-- strip it from the first line -- strip it from the first line
str = str:gsub('^'..indent, '') str = str:gsub('^'..indent, left_indent)
-- strip it from the remaining lines -- strip it from the remaining lines
str = str:gsub('[\n]'..indent, '\n') str = str:gsub('[\n]'..indent, '\n' .. left_indent)
return str return str
end end