aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-03-04 23:28:54 +0300
committerZyX <kp-pav@yandex.ru>2017-03-29 10:08:45 +0300
commit4c3be98db9b24b7d5b8dc2cf55574e778b7ad137 (patch)
treea8c84fddd40f6d9cebc5267952cfedb2a23eb797
parent140174669e359ae7147288366dd94952955980cc (diff)
downloadrneovim-4c3be98db9b24b7d5b8dc2cf55574e778b7ad137.tar.gz
rneovim-4c3be98db9b24b7d5b8dc2cf55574e778b7ad137.tar.bz2
rneovim-4c3be98db9b24b7d5b8dc2cf55574e778b7ad137.zip
unittests: Add tv_dict_watcher_{add,remove} tests
-rw-r--r--test/unit/eval/helpers.lua246
-rw-r--r--test/unit/eval/typval_spec.lua115
2 files changed, 282 insertions, 79 deletions
diff --git a/test/unit/eval/helpers.lua b/test/unit/eval/helpers.lua
index cd85b143d9..21ef51ff20 100644
--- a/test/unit/eval/helpers.lua
+++ b/test/unit/eval/helpers.lua
@@ -33,18 +33,62 @@ local function li_alloc(nogc)
return li
end
-local function list(...)
- local ret = ffi.gc(eval.tv_list_alloc(), eval.tv_list_unref)
- eq(0, ret.lv_refcount)
- ret.lv_refcount = 1
- for i = 1, select('#', ...) do
- local val = select(i, ...)
- local li_tv = ffi.gc(lua2typvalt(val), nil)
- local li = li_alloc(true)
- li.li_tv = li_tv
- eval.tv_list_append(ret, li)
+local function populate_list(l, lua_l, processed)
+ processed = processed or {}
+ eq(0, l.lv_refcount)
+ l.lv_refcount = 1
+ processed[lua_l] = l
+ for i = 1, #lua_l do
+ local item_tv = ffi.gc(lua2typvalt(lua_l[i], processed), nil)
+ local item_li = eval.tv_list_item_alloc()
+ item_li.li_tv = item_tv
+ eval.tv_list_append(l, item_li)
end
- return ret
+ return l
+end
+
+local function populate_dict(d, lua_d, processed)
+ processed = processed or {}
+ eq(0, d.dv_refcount)
+ d.dv_refcount = 1
+ processed[lua_d] = d
+ for k, v in pairs(lua_d) do
+ if type(k) == 'string' then
+ local di = eval.tv_dict_item_alloc(to_cstr(k))
+ local val_tv = ffi.gc(lua2typvalt(v, processed), nil)
+ eval.tv_copy(val_tv, di.di_tv)
+ eval.tv_clear(val_tv)
+ eval.tv_dict_add(d, di)
+ end
+ end
+ return d
+end
+
+local function populate_partial(pt, lua_pt, processed)
+ processed = processed or {}
+ eq(0, pt.pt_refcount)
+ processed[lua_pt] = pt
+ local argv = nil
+ if lua_pt.args and #lua_pt.args > 0 then
+ argv = ffi.gc(ffi.cast('typval_T*', eval.xmalloc(ffi.sizeof('typval_T') * #lua_pt.args)), nil)
+ for i, arg in ipairs(lua_pt.args) do
+ local arg_tv = ffi.gc(lua2typvalt(arg, processed), nil)
+ argv[i - 1] = arg_tv
+ end
+ end
+ local dict = nil
+ if lua_pt.dict then
+ local dict_tv = ffi.gc(lua2typvalt(lua_pt.dict, processed), nil)
+ assert(dict_tv.v_type == eval.VAR_DICT)
+ dict = dict_tv.vval.v_dict
+ end
+ pt.pt_refcount = 1
+ pt.pt_name = eval.xmemdupz(to_cstr(lua_pt.value), #lua_pt.value)
+ pt.pt_auto = not not lua_pt.auto
+ pt.pt_argc = lua_pt.args and #lua_pt.args or 0
+ pt.pt_argv = argv
+ pt.pt_dict = dict
+ return pt
end
local ptr2key = function(ptr)
@@ -55,6 +99,30 @@ local lst2tbl
local dct2tbl
local typvalt2lua
+
+local function partial2lua(pt, processed)
+ processed = processed or {}
+ local value, auto, dict, argv = nil, nil, nil, nil
+ if pt ~= nil then
+ value = ffi.string(pt.pt_name)
+ auto = pt.pt_auto and true or nil
+ argv = {}
+ for i = 1, pt.pt_argc do
+ argv[i] = typvalt2lua(pt.pt_argv[i - 1], processed)
+ end
+ if pt.pt_dict ~= nil then
+ dict = dct2tbl(pt.pt_dict)
+ end
+ end
+ return {
+ [type_key]=func_type,
+ value=value,
+ auto=auto,
+ args=argv,
+ dict=dict,
+ }
+end
+
local typvalt2lua_tab = nil
local function typvalt2lua_tab_init()
@@ -97,26 +165,7 @@ local function typvalt2lua_tab_init()
if processed[p_key] then
return processed[p_key]
end
- local pt = t.vval.v_partial
- local value, auto, dict, argv = nil, nil, nil, nil
- if pt ~= nil then
- value = ffi.string(pt.pt_name)
- auto = pt.pt_auto and true or nil
- argv = {}
- for i = 1, pt.pt_argc do
- argv[i] = typvalt2lua(pt.pt_argv[i - 1], processed)
- end
- if pt.pt_dict ~= nil then
- dict = dct2tbl(pt.pt_dict)
- end
- end
- return {
- [type_key]=func_type,
- value=value,
- auto=auto,
- args=argv,
- dict=dict,
- }
+ return partial2lua(t.vval.v_partial, processed)
end,
}
end
@@ -257,36 +306,16 @@ local lua2typvalt_type_tab = {
processed[l].lv_refcount = processed[l].lv_refcount + 1
return typvalt(eval.VAR_LIST, {v_list=processed[l]})
end
- local lst = eval.tv_list_alloc()
- lst.lv_refcount = 1
- processed[l] = lst
- local ret = typvalt(eval.VAR_LIST, {v_list=lst})
- for i = 1, #l do
- local item_tv = ffi.gc(lua2typvalt(l[i], processed), nil)
- eval.tv_list_append_tv(lst, item_tv)
- eval.tv_clear(item_tv)
- end
- return ret
+ local lst = populate_list(eval.tv_list_alloc(), l, processed)
+ return typvalt(eval.VAR_LIST, {v_list=lst})
end,
[dict_type] = function(l, processed)
if processed[l] then
processed[l].dv_refcount = processed[l].dv_refcount + 1
return typvalt(eval.VAR_DICT, {v_dict=processed[l]})
end
- local dct = eval.tv_dict_alloc()
- dct.dv_refcount = 1
- processed[l] = dct
- local ret = typvalt(eval.VAR_DICT, {v_dict=dct})
- for k, v in pairs(l) do
- if type(k) == 'string' then
- local di = eval.tv_dict_item_alloc(to_cstr(k))
- local val_tv = ffi.gc(lua2typvalt(v, processed), nil)
- eval.tv_copy(val_tv, di.di_tv)
- eval.tv_clear(val_tv)
- eval.tv_dict_add(dct, di)
- end
- end
- return ret
+ local dct = populate_dict(eval.tv_dict_alloc(), l, processed)
+ return typvalt(eval.VAR_DICT, {v_dict=dct})
end,
[func_type] = function(l, processed)
if processed[l] then
@@ -294,29 +323,8 @@ local lua2typvalt_type_tab = {
return typvalt(eval.VAR_PARTIAL, {v_partial=processed[l]})
end
if l.args or l.dict then
- local pt = ffi.gc(ffi.cast('partial_T*', eval.xmalloc(ffi.sizeof('partial_T'))), nil)
- processed[l] = pt
- local argv = nil
- if l.args and #l.args > 0 then
- argv = ffi.gc(ffi.cast('typval_T*', eval.xmalloc(ffi.sizeof('typval_T') * #l.args)), nil)
- for i, arg in ipairs(l.args) do
- local arg_tv = ffi.gc(lua2typvalt(arg, processed), nil)
- eval.tv_copy(arg_tv, argv[i - 1])
- eval.tv_clear(arg_tv)
- end
- end
- local dict = nil
- if l.dict then
- local dict_tv = ffi.gc(lua2typvalt(l.dict, processed), nil)
- assert(dict_tv.v_type == eval.VAR_DICT)
- dict = dict_tv.vval.v_dict
- end
- pt.pt_refcount = 1
- pt.pt_name = eval.xmemdupz(to_cstr(l.value), #l.value)
- pt.pt_auto = not not l.auto
- pt.pt_argc = l.args and #l.args or 0
- pt.pt_argv = argv
- pt.pt_dict = dict
+ local pt = populate_partial(ffi.gc(ffi.cast('partial_T*',
+ eval.xcalloc(1, ffi.sizeof('partial_T'))), nil), l, processed)
return typvalt(eval.VAR_PARTIAL, {v_partial=pt})
else
return typvalt(eval.VAR_FUNC, {
@@ -402,13 +410,91 @@ local alloc_logging_helpers = {
return {func='malloc', args={size + 1}, ret=void(s)}
end,
+ dwatcher = function(w) return {func='malloc', args={ffi.sizeof('DictWatcher')}, ret=void(w)} end,
+
freed = function(p) return {func='free', args={type(p) == 'table' and p or void(p)}} end,
+
+ -- 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_tvs = function(argv, argc) return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)} end,
}
local function int(n)
return {[type_key]=int_type, value=n}
end
+local function list(...)
+ return populate_list(ffi.gc(eval.tv_list_alloc(), eval.tv_list_unref),
+ {...}, {})
+end
+
+local function dict(d)
+ return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free),
+ d, {})
+end
+
+local callback2tbl_type_tab = nil
+
+local function init_callback2tbl_type_tab()
+ if callback2tbl_type_tab then
+ return
+ end
+ callback2tbl_type_tab = {
+ [tonumber(eval.kCallbackNone)] = function(_) return {type='none'} end,
+ [tonumber(eval.kCallbackFuncref)] = function(cb)
+ return {type='fref', fref=ffi.string(cb.data.funcref)}
+ end,
+ [tonumber(eval.kCallbackPartial)] = function(cb)
+ local lua_pt = partial2lua(cb.data.partial)
+ return {type='pt', fref=ffi.string(lua_pt.value), pt=lua_pt}
+ end
+ }
+end
+
+local function callback2tbl(cb)
+ init_callback2tbl_type_tab()
+ return callback2tbl_type_tab[tonumber(cb.type)](cb)
+end
+
+local function tbl2callback(tbl)
+ local ret = nil
+ if tbl.type == 'none' then
+ ret = ffi.new('Callback[1]', {{type=eval.kCallbackNone}})
+ elseif tbl.type == 'fref' then
+ ret = ffi.new('Callback[1]', {{type=eval.kCallbackFuncref,
+ data={funcref=eval.xstrdup(tbl.fref)}}})
+ elseif tbl.type == 'pt' then
+ local pt = ffi.gc(ffi.cast('partial_T*',
+ eval.xcalloc(1, ffi.sizeof('partial_T'))), eval.partial_unref)
+ ret = ffi.new('Callback[1]', {{type=eval.kCallbackPartial,
+ data={partial=populate_partial(pt, tbl.pt, {})}}})
+ else
+ assert(false)
+ end
+ return ffi.gc(ffi.cast('Callback*', ret), helpers.callback_free)
+end
+
+local function dict_watchers(d)
+ local ret = {}
+ local h = d.watchers
+ local q = h.next
+ local qs = {}
+ local key_patterns = {}
+ while q ~= h do
+ local qitem = ffi.cast('DictWatcher *',
+ ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
+ ret[#ret + 1] = {
+ cb=callback2tbl(qitem.callback),
+ pat=ffi.string(qitem.key_pattern, qitem.key_pattern_len),
+ busy=qitem.busy,
+ }
+ qs[#qs + 1] = qitem
+ key_patterns[#key_patterns + 1] = {qitem.key_pattern, qitem.key_pattern_len}
+ q = q.next
+ end
+ return ret, qs, key_patterns
+end
+
return {
int=int,
@@ -427,6 +513,7 @@ return {
locks_key=locks_key,
list=list,
+ dict=dict,
lst2tbl=lst2tbl,
dct2tbl=dct2tbl,
@@ -446,5 +533,8 @@ return {
list_items=list_items,
dict_items=dict_items,
+ dict_watchers=dict_watchers,
+ tbl2callback=tbl2callback,
+
empty_list = {[type_key]=list_type},
}
diff --git a/test/unit/eval/typval_spec.lua b/test/unit/eval/typval_spec.lua
index b0a7e9109a..4efde9c6c1 100644
--- a/test/unit/eval/typval_spec.lua
+++ b/test/unit/eval/typval_spec.lua
@@ -14,21 +14,26 @@ local alloc_log_new = helpers.alloc_log_new
local a = eval_helpers.alloc_logging_helpers
local int = eval_helpers.int
local list = eval_helpers.list
+local dict = eval_helpers.dict
local lst2tbl = eval_helpers.lst2tbl
local typvalt = eval_helpers.typvalt
local type_key = eval_helpers.type_key
local li_alloc = eval_helpers.li_alloc
local int_type = eval_helpers.int_type
local first_di = eval_helpers.first_di
+local func_type = eval_helpers.func_type
local null_list = eval_helpers.null_list
local null_dict = eval_helpers.null_dict
local empty_list = eval_helpers.empty_list
local lua2typvalt = eval_helpers.lua2typvalt
local typvalt2lua = eval_helpers.typvalt2lua
local null_string = eval_helpers.null_string
+local tbl2callback = eval_helpers.tbl2callback
+local dict_watchers = eval_helpers.dict_watchers
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')
local function list_items(l)
local lis = {}
@@ -1387,4 +1392,112 @@ describe('typval.c', function()
end)
end)
end)
+ describe('dict', function()
+ describe('watcher', function()
+ describe('add/remove', function()
+ itp('works with an empty key', function()
+ local d = dict({})
+ eq({}, dict_watchers(d))
+ local cb = ffi.gc(tbl2callback({type='none'}), nil)
+ alloc_log:clear()
+ lib.tv_dict_watcher_add(d, '*', 0, cb[0])
+ local ws, qs = dict_watchers(d)
+ local key_p = qs[1].key_pattern
+ alloc_log:check({
+ a.dwatcher(qs[1]),
+ a.str(key_p, 0),
+ })
+ eq({{busy=false, cb={type='none'}, pat=''}}, ws)
+ eq(true, lib.tv_dict_watcher_remove(d, 'x', 0, cb[0]))
+ alloc_log:check({
+ a.freed(key_p),
+ a.freed(qs[1]),
+ })
+ eq({}, dict_watchers(d))
+ end)
+ itp('works with multiple callbacks', function()
+ local d = dict({})
+ eq({}, dict_watchers(d))
+ alloc_log:check({a.dict(d)})
+ local cbs = {}
+ cbs[1] = {'te', ffi.gc(tbl2callback({type='none'}), nil)}
+ alloc_log:check({})
+ cbs[2] = {'foo', ffi.gc(tbl2callback({type='fref', fref='tr'}), nil)}
+ alloc_log:check({
+ a.str(cbs[2][2].data.funcref, #('tr')),
+ })
+ cbs[3] = {'te', ffi.gc(tbl2callback({type='pt', fref='tr', pt={
+ value='tr',
+ args={'test'},
+ dict={},
+ }}), nil)}
+ local pt3 = cbs[3][2].data.partial
+ local pt3_argv = pt3.pt_argv
+ local pt3_dict = pt3.pt_dict
+ local pt3_name = pt3.pt_name
+ local pt3_str_arg = pt3.pt_argv[0].vval.v_string
+ alloc_log:check({
+ a.lua_pt(pt3),
+ a.lua_tvs(pt3_argv, pt3.pt_argc),
+ a.str(pt3_str_arg, #('test')),
+ a.dict(pt3_dict),
+ a.str(pt3_name, #('tr')),
+ })
+ for _, v in ipairs(cbs) do
+ lib.tv_dict_watcher_add(d, v[1], #(v[1]), v[2][0])
+ end
+ local ws, qs, kps = dict_watchers(d)
+ eq({{busy=false, pat=cbs[1][1], cb={type='none'}},
+ {busy=false, pat=cbs[2][1], cb={type='fref', fref='tr'}},
+ {busy=false, pat=cbs[3][1], cb={type='pt', fref='tr', pt={
+ [type_key]=func_type,
+ value='tr',
+ args={'test'},
+ dict={},
+ }}}}, ws)
+ alloc_log:check({
+ a.dwatcher(qs[1]),
+ a.str(kps[1][1], kps[1][2]),
+ a.dwatcher(qs[2]),
+ a.str(kps[2][1], kps[2][2]),
+ a.dwatcher(qs[3]),
+ a.str(kps[3][1], kps[3][2]),
+ })
+ eq(true, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
+ alloc_log:check({
+ a.freed(cbs[2][2].data.funcref),
+ a.freed(kps[2][1]),
+ a.freed(qs[2]),
+ })
+ eq(false, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
+ eq({{busy=false, pat=cbs[1][1], cb={type='none'}},
+ {busy=false, pat=cbs[3][1], cb={type='pt', fref='tr', pt={
+ [type_key]=func_type,
+ value='tr',
+ args={'test'},
+ dict={},
+ }}}}, dict_watchers(d))
+ eq(true, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
+ alloc_log:check({
+ a.freed(pt3_str_arg),
+ a.freed(pt3_argv),
+ a.freed(pt3_dict),
+ a.freed(pt3_name),
+ a.freed(pt3),
+ a.freed(kps[3][1]),
+ a.freed(qs[3]),
+ })
+ eq(false, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
+ eq({{busy=false, pat=cbs[1][1], cb={type='none'}}}, dict_watchers(d))
+ eq(true, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
+ alloc_log:check({
+ a.freed(kps[1][1]),
+ a.freed(qs[1]),
+ })
+ eq(false, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
+ eq({}, dict_watchers(d))
+ end)
+ end)
+ end)
+ end)
end)