unittests: Add tv_dict_item_{alloc,free} tests

This commit is contained in:
ZyX 2017-03-05 01:09:55 +03:00
parent cdb1aa3e47
commit ffaf7c7521
2 changed files with 59 additions and 0 deletions

View File

@ -225,6 +225,19 @@ local function which(exe)
end end
end end
local function concat_tables(...)
local ret = {}
for i = 1, select('#', ...) do
local tbl = select(i, ...)
if tbl then
for _, v in ipairs(tbl) do
ret[#ret + 1] = v
end
end
end
return ret
end
return { return {
eq = eq, eq = eq,
neq = neq, neq = neq,
@ -238,4 +251,5 @@ return {
check_cores = check_cores, check_cores = check_cores,
hasenv = hasenv, hasenv = hasenv,
which = which, which = which,
concat_tables = concat_tables,
} }

View File

@ -1,4 +1,5 @@
local helpers = require('test.unit.helpers')(after_each) local helpers = require('test.unit.helpers')(after_each)
local global_helpers = require('test.helpers')
local eval_helpers = require('test.unit.eval.helpers') local eval_helpers = require('test.unit.eval.helpers')
local itp = helpers.gen_itp(it) local itp = helpers.gen_itp(it)
@ -31,6 +32,8 @@ local null_string = eval_helpers.null_string
local tbl2callback = eval_helpers.tbl2callback local tbl2callback = eval_helpers.tbl2callback
local dict_watchers = eval_helpers.dict_watchers local dict_watchers = eval_helpers.dict_watchers
local concat_tables = global_helpers.concat_tables
local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h', local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h',
'./src/nvim/mbyte.h', './src/nvim/garray.h', './src/nvim/mbyte.h', './src/nvim/garray.h',
'./src/nvim/eval.h') './src/nvim/eval.h')
@ -1498,6 +1501,48 @@ describe('typval.c', function()
eq({}, dict_watchers(d)) eq({}, dict_watchers(d))
end) end)
end) end)
describe('notify', function()
-- Way too hard to test it here, functional tests in
-- dict_notifications_spec.lua.
end)
end)
describe('item', function()
describe('alloc/free', function()
local function check_tv_dict_item_alloc_len(s, len, tv, more_frees)
local di
if len == nil then
di = ffi.gc(lib.tv_dict_item_alloc(s), nil)
len = #s
else
di = ffi.gc(lib.tv_dict_item_alloc_len(s, len or #s), nil)
end
eq(s:sub(1, len), ffi.string(di.di_key))
alloc_log:check({a.di(di, len)})
if tv then
di.di_tv = tv
else
di.di_tv.v_type = lib.VAR_UNKNOWN
end
lib.tv_dict_item_free(di)
alloc_log:check(concat_tables(more_frees, {a.freed(di)}))
end
local function check_tv_dict_item_alloc(s, tv, more_frees)
return check_tv_dict_item_alloc_len(s, nil, tv, more_frees)
end
itp('works', function()
check_tv_dict_item_alloc('')
check_tv_dict_item_alloc('t')
check_tv_dict_item_alloc('TEST')
check_tv_dict_item_alloc_len('', 0)
check_tv_dict_item_alloc_len('TEST', 2)
local tv = lua2typvalt('test')
alloc_log:check({a.str(tv.vval.v_string, #('test'))})
check_tv_dict_item_alloc('', tv, {a.freed(tv.vval.v_string)})
tv = lua2typvalt('test')
alloc_log:check({a.str(tv.vval.v_string, #('test'))})
check_tv_dict_item_alloc_len('', 0, tv, {a.freed(tv.vval.v_string)})
end)
end)
end) end)
end) end)
end) end)