unittests: Add tv_dict_item_{add,remove} tests

This commit is contained in:
ZyX 2017-03-05 01:26:26 +03:00
parent ffaf7c7521
commit 6c622ed08b
2 changed files with 40 additions and 13 deletions

View File

@ -416,7 +416,10 @@ local alloc_logging_helpers = {
-- lua_…: allocated by this file, not by some Neovim function -- lua_…: allocated by this file, not by some Neovim function
lua_pt = function(pt) return {func='calloc', args={1, ffi.sizeof('partial_T')}, ret=void(pt)} end, lua_pt = function(pt) return {func='calloc', args={1, ffi.sizeof('partial_T')}, ret=void(pt)} end,
lua_tvs = function(argv, argc) return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)} end, lua_tvs = function(argv, argc)
argc = alloc_len(argc)
return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)}
end,
} }
local function int(n) local function int(n)
@ -430,7 +433,7 @@ end
local function dict(d) local function dict(d)
return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free), return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free),
d, {}) d or {}, {})
end end
local callback2tbl_type_tab = nil local callback2tbl_type_tab = nil

View File

@ -8,6 +8,7 @@ local OK = helpers.OK
local eq = helpers.eq local eq = helpers.eq
local neq = helpers.neq local neq = helpers.neq
local ffi = helpers.ffi local ffi = helpers.ffi
local FAIL = helpers.FAIL
local cimport = helpers.cimport local cimport = helpers.cimport
local to_cstr = helpers.to_cstr local to_cstr = helpers.to_cstr
local alloc_log_new = helpers.alloc_log_new local alloc_log_new = helpers.alloc_log_new
@ -17,6 +18,7 @@ local int = eval_helpers.int
local list = eval_helpers.list local list = eval_helpers.list
local dict = eval_helpers.dict local dict = eval_helpers.dict
local lst2tbl = eval_helpers.lst2tbl local lst2tbl = eval_helpers.lst2tbl
local dct2tbl = eval_helpers.dct2tbl
local typvalt = eval_helpers.typvalt local typvalt = eval_helpers.typvalt
local type_key = eval_helpers.type_key local type_key = eval_helpers.type_key
local li_alloc = eval_helpers.li_alloc local li_alloc = eval_helpers.li_alloc
@ -111,6 +113,18 @@ local function ga_alloc(itemsize, growsize)
return ga return ga
end end
local function check_emsg(f, msg)
local saved_last_msg_hist = lib.last_msg_hist
local ret = {f()}
if msg ~= nil then
neq(saved_last_msg_hist, lib.last_msg_hist)
eq(msg, ffi.string(lib.last_msg_hist.msg))
else
eq(saved_last_msg_hist, lib.last_msg_hist)
end
return unpack(ret)
end
describe('typval.c', function() describe('typval.c', function()
describe('list', function() describe('list', function()
describe('item', function() describe('item', function()
@ -1242,17 +1256,6 @@ describe('typval.c', function()
alloc_log:check({}) alloc_log:check({})
end) end)
end) end)
local function check_emsg(f, msg)
local saved_last_msg_hist = lib.last_msg_hist
local ret = {f()}
if msg ~= nil then
neq(saved_last_msg_hist, lib.last_msg_hist)
eq(msg, ffi.string(lib.last_msg_hist.msg))
else
eq(saved_last_msg_hist, lib.last_msg_hist)
end
return unpack(ret)
end
describe('nr()', function() describe('nr()', function()
local function tv_list_find_nr(l, n, msg) local function tv_list_find_nr(l, n, msg)
return check_emsg(function() return check_emsg(function()
@ -1543,6 +1546,27 @@ describe('typval.c', function()
check_tv_dict_item_alloc_len('', 0, tv, {a.freed(tv.vval.v_string)}) check_tv_dict_item_alloc_len('', 0, tv, {a.freed(tv.vval.v_string)})
end) end)
end) end)
describe('add/remove', function()
itp('works', function()
local d = dict()
eq({}, dct2tbl(d))
alloc_log:check({a.dict(d)})
local di = ffi.gc(lib.tv_dict_item_alloc(''), nil)
local tv = lua2typvalt('test')
di.di_tv = tv
alloc_log:check({a.di(di, ''), a.str(tv.vval.v_string, 'test')})
eq(OK, lib.tv_dict_add(d, di))
alloc_log:check({})
eq(FAIL, check_emsg(function() return lib.tv_dict_add(d, di) end,
'E685: Internal error: hash_add()'))
alloc_log:clear()
lib.tv_dict_item_remove(d, di)
alloc_log:check({
a.freed(tv.vval.v_string),
a.freed(di),
})
end)
end)
end) end)
end) end)
end) end)