Merge #6112 from ZyX-I/split-eval'/buf_get_changedtick

Better b:changedtick support
This commit is contained in:
Justin M. Keyes
2017-02-27 10:29:46 +01:00
committed by GitHub
27 changed files with 688 additions and 363 deletions

View File

@@ -2,8 +2,11 @@ local helpers = require('test.functional.helpers')(after_each)
local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer
local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq
local curbufmeths, ok = helpers.curbufmeths, helpers.ok
local funcs, request = helpers.funcs, helpers.request
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api/buf', function()
before_each(clear)
@@ -249,6 +252,24 @@ describe('api/buf', function()
eq(1, funcs.exists('b:lua'))
curbufmeths.del_var('lua')
eq(0, funcs.exists('b:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curbufmeths.del_var, 'lua'))
curbufmeths.set_var('lua', 1)
command('lockvar b:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.set_var, 'lua', 1))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.del_var, 'changedtick'))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.set_var, 'changedtick', 1))
end)
end)
describe('get_changedtick', function()
it('works', function()
eq(2, curbufmeths.get_changedtick())
curbufmeths.set_lines(0, 1, false, {'abc\0', '\0def', 'ghi'})
eq(3, curbufmeths.get_changedtick())
eq(3, curbufmeths.get_var('changedtick'))
end)
it('buffer_set_var returns the old value', function()

View File

@@ -6,6 +6,8 @@ local curtabmeths = helpers.curtabmeths
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api/tabpage', function()
before_each(clear)
@@ -32,6 +34,11 @@ describe('api/tabpage', function()
eq(1, funcs.exists('t:lua'))
curtabmeths.del_var('lua')
eq(0, funcs.exists('t:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curtabmeths.del_var, 'lua'))
curtabmeths.set_var('lua', 1)
command('lockvar t:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.set_var, 'lua', 1))
end)
it('tabpage_set_var returns the old value', function()

View File

@@ -7,6 +7,8 @@ local os_name = helpers.os_name
local meths = helpers.meths
local funcs = helpers.funcs
local request = helpers.request
local meth_pcall = helpers.meth_pcall
local command = helpers.command
describe('api', function()
before_each(clear)
@@ -43,7 +45,7 @@ describe('api', function()
it('works', function()
nvim('command', 'let g:v1 = "a"')
nvim('command', 'let g:v2 = [1, 2, {"v3": 3}]')
eq({v1 = 'a', v2 = {1, 2, {v3 = 3}}}, nvim('eval', 'g:'))
eq({v1 = 'a', v2 = { 1, 2, { v3 = 3 } } }, nvim('eval', 'g:'))
end)
it('handles NULL-initialized strings correctly', function()
@@ -65,7 +67,7 @@ describe('api', function()
describe('nvim_call_function', function()
it('works', function()
nvim('call_function', 'setqflist', {{{ filename = 'something', lnum = 17}}, 'r'})
nvim('call_function', 'setqflist', { { { filename = 'something', lnum = 17 } }, 'r' })
eq(17, nvim('call_function', 'getqflist', {})[1].lnum)
eq(17, nvim('call_function', 'eval', {17}))
eq('foo', nvim('call_function', 'simplify', {'this/./is//redundant/../../../foo'}))
@@ -117,6 +119,11 @@ describe('api', function()
eq(1, funcs.exists('g:lua'))
meths.del_var('lua')
eq(0, funcs.exists('g:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(meths.del_var, 'lua'))
meths.set_var('lua', 1)
command('lockvar lua')
eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1))
end)
it('vim_set_var returns the old value', function()
@@ -396,7 +403,7 @@ describe('api', function()
eq(1, meths.get_var('avar'))
req = {
{'nvim_set_var', {'bvar', {2,3}}},
{ 'nvim_set_var', { 'bvar', { 2, 3 } } },
12,
}
status, err = pcall(meths.call_atomic, req)

View File

@@ -8,6 +8,8 @@ local curwinmeths = helpers.curwinmeths
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command
-- check if str is visible at the beginning of some line
local function is_visible(str)
@@ -137,6 +139,11 @@ describe('api/win', function()
eq(1, funcs.exists('w:lua'))
curwinmeths.del_var('lua')
eq(0, funcs.exists('w:lua'))
eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curwinmeths.del_var, 'lua'))
curwinmeths.set_var('lua', 1)
command('lockvar w:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.set_var, 'lua', 1))
end)
it('window_set_var returns the old value', function()

View File

@@ -0,0 +1,142 @@
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local eval = helpers.eval
local feed = helpers.feed
local clear = helpers.clear
local funcs = helpers.funcs
local meths = helpers.meths
local command = helpers.command
local exc_exec = helpers.exc_exec
local redir_exec = helpers.redir_exec
local meth_pcall = helpers.meth_pcall
local curbufmeths = helpers.curbufmeths
before_each(clear)
local function changedtick()
local ct = curbufmeths.get_changedtick()
eq(ct, curbufmeths.get_var('changedtick'))
eq(ct, curbufmeths.get_var('changedtick'))
eq(ct, eval('b:changedtick'))
eq(ct, eval('b:["changedtick"]'))
eq(ct, eval('b:.changedtick'))
eq(ct, funcs.getbufvar('%', 'changedtick'))
eq(ct, funcs.getbufvar('%', '').changedtick)
eq(ct, eval('b:').changedtick)
return ct
end
describe('b:changedtick', function()
-- Ported tests from Vim-8.0.333
it('increments', function() -- Test_changedtick_increments
-- New buffer has an empty line, tick starts at 2
eq(2, changedtick())
funcs.setline(1, 'hello')
eq(3, changedtick())
eq(0, exc_exec('undo'))
-- Somehow undo counts as two changes
eq(5, changedtick())
end)
it('is present in b: dictionary', function()
eq(2, changedtick())
command('let d = b:')
eq(2, meths.get_var('d').changedtick)
end)
it('increments at bdel', function()
command('new')
eq(2, changedtick())
local bnr = curbufmeths.get_number()
eq(2, bnr)
command('bdel')
eq(3, funcs.getbufvar(bnr, 'changedtick'))
eq(1, curbufmeths.get_number())
end)
it('fails to be changed by user', function()
local ct = changedtick()
local ctn = ct + 100500
eq(0, exc_exec('let d = b:'))
eq('\nE46: Cannot change read-only variable "b:changedtick"',
redir_exec('let b:changedtick = ' .. ctn))
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
redir_exec('let b:["changedtick"] = ' .. ctn))
eq('\nE46: Cannot change read-only variable "b:.changedtick"',
redir_exec('let b:.changedtick = ' .. ctn))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('let d.changedtick = ' .. ctn))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.set_var, 'changedtick', ctn))
eq('\nE795: Cannot delete variable b:changedtick',
redir_exec('unlet b:changedtick'))
eq('\nE46: Cannot change read-only variable "b:.changedtick"',
redir_exec('unlet b:.changedtick'))
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
redir_exec('unlet b:["changedtick"]'))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('unlet d.changedtick'))
eq({false, 'Key is read-only: changedtick'},
meth_pcall(curbufmeths.del_var, 'changedtick'))
eq(ct, changedtick())
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
redir_exec('let b:["changedtick"] += ' .. ctn))
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
redir_exec('let b:["changedtick"] -= ' .. ctn))
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
redir_exec('let b:["changedtick"] .= ' .. ctn))
eq(ct, changedtick())
funcs.setline(1, 'hello')
eq(ct + 1, changedtick())
end)
it('is listed in :let output', function()
eq('\nb:changedtick #2',
redir_exec(':let b:'))
end)
it('fails to unlock b:changedtick', function()
eq(0, exc_exec('let d = b:'))
eq(0, funcs.islocked('b:changedtick'))
eq(0, funcs.islocked('d.changedtick'))
eq('\nE940: Cannot lock or unlock variable b:changedtick',
redir_exec('unlockvar b:changedtick'))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('unlockvar d.changedtick'))
eq(0, funcs.islocked('b:changedtick'))
eq(0, funcs.islocked('d.changedtick'))
eq('\nE940: Cannot lock or unlock variable b:changedtick',
redir_exec('lockvar b:changedtick'))
eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('lockvar d.changedtick'))
eq(0, funcs.islocked('b:changedtick'))
eq(0, funcs.islocked('d.changedtick'))
end)
it('is being completed', function()
feed(':echo b:<Tab><Home>let cmdline="<End>"<CR>')
eq('echo b:changedtick', meths.get_var('cmdline'))
end)
it('cannot be changed by filter() or map()', function()
eq(2, changedtick())
eq('\nE795: Cannot delete variable filter() argument',
redir_exec('call filter(b:, 0)'))
eq('\nE742: Cannot change value of map() argument',
redir_exec('call map(b:, 0)'))
eq('\nE742: Cannot change value of map() argument',
redir_exec('call map(b:, "v:val")'))
eq(2, changedtick())
end)
it('cannot be remove()d', function()
eq(2, changedtick())
eq('\nE795: Cannot delete variable remove() argument',
redir_exec('call remove(b:, "changedtick")'))
eq(2, changedtick())
end)
it('does not inherit VAR_FIXED when copying dictionary over', function()
eq(2, changedtick())
eq('', redir_exec('let d1 = copy(b:)|let d1.changedtick = 42'))
eq('', redir_exec('let d2 = copy(b:)|unlet d2.changedtick'))
eq(2, changedtick())
end)
end)

View File

@@ -544,6 +544,14 @@ local function skip_fragile(pending_fn, cond)
return false
end
local function meth_pcall(...)
local ret = {pcall(...)}
if type(ret[2]) == 'string' then
ret[2] = ret[2]:gsub('^[^:]+:%d+: ', '')
end
return ret
end
local funcs = create_callindex(nvim_call)
local meths = create_callindex(nvim)
local uimeths = create_callindex(ui)
@@ -615,6 +623,7 @@ local M = {
skip_fragile = skip_fragile,
set_shell_powershell = set_shell_powershell,
tmpname = tmpname,
meth_pcall = meth_pcall,
NIL = mpack.NIL,
}

View File

@@ -13,6 +13,14 @@ describe('context variables', function()
-- Test for getbufvar().
-- Use strings to test for memory leaks.
source([[
function Getbufscope(buf, ...)
let ret = call('getbufvar', [a:buf, ''] + a:000)
if type(ret) == type({})
return filter(copy(ret), 'v:key isnot# "changedtick"')
else
return ret
endif
endfunction
let t:testvar='abcd'
$put =string(gettabvar(1, 'testvar'))
$put =string(gettabvar(1, 'testvar'))
@@ -20,14 +28,14 @@ describe('context variables', function()
let def_num = '5678'
$put =string(getbufvar(1, 'var_num'))
$put =string(getbufvar(1, 'var_num', def_num))
$put =string(getbufvar(1, ''))
$put =string(getbufvar(1, '', def_num))
$put =string(Getbufscope(1))
$put =string(Getbufscope(1, def_num))
unlet b:var_num
$put =string(getbufvar(1, 'var_num', def_num))
$put =string(getbufvar(1, ''))
$put =string(getbufvar(1, '', def_num))
$put =string(getbufvar(9, ''))
$put =string(getbufvar(9, '', def_num))
$put =string(Getbufscope(1))
$put =string(Getbufscope(1, def_num))
$put =string(Getbufscope(9))
$put =string(Getbufscope(9, def_num))
unlet def_num
$put =string(getbufvar(1, '&autoindent'))
$put =string(getbufvar(1, '&autoindent', 1))

View File

@@ -79,6 +79,13 @@ local function cimport(...)
-- format it (so that the lines are "unique" statements), also filter out
-- Objective-C blocks
if os.getenv('NVIM_TEST_PRINT_I') == '1' then
local lnum = 0
for line in body:gmatch('[^\n]+') do
lnum = lnum + 1
print(lnum, line)
end
end
body = formatc(body)
body = filter_complex_blocks(body)

View File

@@ -124,6 +124,7 @@ function Gcc:init_defines()
self:define('_GNU_SOURCE')
self:define('INCLUDE_GENERATED_DECLARATIONS')
self:define('UNIT_TESTING')
self:define('UNIT_TESTING_LUA_PREPROCESSING')
-- Needed for FreeBSD
self:define('_Thread_local', nil, '')
-- Needed for macOS Sierra