aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/eval/typval.c5
-rw-r--r--test/unit/eval/typval_spec.lua73
2 files changed, 75 insertions, 3 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 620da0032e..c245b9222e 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -1840,10 +1840,11 @@ static inline void _nothing_conv_dict_end(typval_T *const tv,
/// Free memory for a variable value and set the value to NULL or 0
///
/// @param[in,out] tv Value to free.
-void tv_clear(typval_T *tv)
+void tv_clear(typval_T *const tv)
{
if (tv != NULL && tv->v_type != VAR_UNKNOWN) {
- const int evn_ret = encode_vim_to_nothing(NULL, tv, "tv_clear argument");
+ const int evn_ret = encode_vim_to_nothing(NULL, tv,
+ _("tv_clear() argument"));
(void)evn_ret;
assert(evn_ret == OK);
}
diff --git a/test/unit/eval/typval_spec.lua b/test/unit/eval/typval_spec.lua
index 0d894ff8b0..8b0470d3b7 100644
--- a/test/unit/eval/typval_spec.lua
+++ b/test/unit/eval/typval_spec.lua
@@ -55,12 +55,16 @@ local function list_watch(l, li)
end
local function get_alloc_rets(exp_log, res)
+ setmetatable(res, {
+ __index={
+ freed=function(r, n) return {func='free', args={r[n]}} end
+ }
+ })
for i = 1,#exp_log do
if ({malloc=true, calloc=true})[exp_log[i].func] then
res[#res + 1] = exp_log[i].ret
end
end
- res.freed = function(r, n) return {func='free', args={r[n]}} end
return exp_log
end
@@ -2201,4 +2205,71 @@ describe('typval.c', function()
end)
end)
end)
+ describe('tv', function()
+ describe('alloc', function()
+ describe('list ret()', function()
+ itp('works', function()
+ local rettv = typvalt(lib.VAR_UNKNOWN)
+ local l = lib.tv_list_alloc_ret(rettv)
+ eq(empty_list, typvalt2lua(rettv))
+ eq(rettv.vval.v_list, l)
+ end)
+ end)
+ describe('dict ret()', function()
+ itp('works', function()
+ local rettv = typvalt(lib.VAR_UNKNOWN)
+ lib.tv_dict_alloc_ret(rettv)
+ eq({}, typvalt2lua(rettv))
+ end)
+ end)
+ end)
+ describe('clear()', function()
+ itp('works', function()
+ local function deffrees(alloc_rets)
+ local ret = {}
+ for i = #alloc_rets, 1, -1 do
+ ret[#alloc_rets - i + 1] = alloc_rets:freed(i)
+ end
+ return ret
+ end
+ local function defalloc()
+ return {}
+ end
+ alloc_log:check({})
+ lib.tv_clear(nil)
+ alloc_log:check({})
+ local ll = {}
+ local ll_l = nil
+ ll[1] = ll
+ local dd = {}
+ dd.dd = dd
+ for _, v in ipairs({
+ {nil},
+ {null_string, nil, function() return {a.freed(alloc_log.null)} end},
+ {0},
+ {int(0)},
+ {true},
+ {false},
+ {{}, function(tv) return {a.dict(tv.vval.v_dict)} end},
+ {empty_list, function(tv) return {a.list(tv.vval.v_list)} end},
+ {ll, function(tv)
+ ll_l = tv.vval.v_list
+ return {a.list(tv.vval.v_list), a.li(tv.vval.v_list.lv_first)}
+ end, defalloc},
+ {dd, function(tv)
+ dd_d = tv.vval.v_dict
+ return {a.dict(tv.vval.v_dict), a.di(first_di(tv.vval.v_dict))}
+ end, defalloc},
+ }) do
+ local tv = lua2typvalt(v[1])
+ local alloc_rets = {}
+ alloc_log:check(get_alloc_rets((v[2] or defalloc)(tv), alloc_rets))
+ lib.tv_clear(tv)
+ alloc_log:check((v[3] or deffrees)(alloc_rets))
+ end
+ eq(1, ll_l.lv_refcount)
+ eq(1, dd_d.dv_refcount)
+ end)
+ end)
+ end)
end)