unittests: Add tests for vim_to_object function

This commit is contained in:
ZyX 2016-04-19 19:17:09 +03:00
parent da15b5c1f3
commit 6b06bdafa2
4 changed files with 401 additions and 29 deletions

150
test/unit/api/helpers.lua Normal file
View File

@ -0,0 +1,150 @@
local helpers = require('test.unit.helpers')
local eval_helpers = require('test.unit.eval.helpers')
local cimport = helpers.cimport
local to_cstr = helpers.to_cstr
local ffi = helpers.ffi
local list_type = eval_helpers.list_type
local dict_type = eval_helpers.dict_type
local func_type = eval_helpers.func_type
local int_type = eval_helpers.int_type
local flt_type = eval_helpers.flt_type
local type_key = eval_helpers.type_key
local api = cimport('./src/nvim/api/private/defs.h',
'./src/nvim/api/private/helpers.h',
'./src/nvim/memory.h')
local obj2lua
local obj2lua_tab = {
[tonumber(api.kObjectTypeArray)] = function(obj)
local ret = {[type_key]=list_type}
for i = 1,tonumber(obj.data.array.size) do
ret[i] = obj2lua(obj.data.array.items[i - 1])
end
if ret[1] then
ret[type_key] = nil
end
return ret
end,
[tonumber(api.kObjectTypeDictionary)] = function(obj)
local ret = {}
for i = 1,tonumber(obj.data.dictionary.size) do
local kv_pair = obj.data.dictionary.items[i - 1]
ret[ffi.string(kv_pair.key.data, kv_pair.key.size)] = obj2lua(kv_pair.value)
end
return ret
end,
[tonumber(api.kObjectTypeBoolean)] = function(obj)
if obj.data.boolean == false then
return false
else
return true
end
end,
[tonumber(api.kObjectTypeNil)] = function(obj)
return NIL
end,
[tonumber(api.kObjectTypeFloat)] = function(obj)
return tonumber(obj.data.floating)
end,
[tonumber(api.kObjectTypeInteger)] = function(obj)
return {[type_key]=int_type, value=tonumber(obj.data.integer)}
end,
[tonumber(api.kObjectTypeString)] = function(obj)
return ffi.string(obj.data.string.data, obj.data.string.size)
end,
}
obj2lua = function(obj)
return ((obj2lua_tab[tonumber(obj['type'])] or function(obj)
assert(false, 'Converting ' .. tostring(tonumber(obj['type'])) .. ' is not implementing yet')
end)(obj))
end
local obj = function(typ, data)
return ffi.gc(ffi.new('Object', {['type']=typ, data=data}),
api.api_free_object)
end
local lua2obj_type_tab = {
[int_type] = function(l)
return obj(api.kObjectTypeInteger, {integer=l.value})
end,
[flt_type] = function(l)
return obj(api.kObjectTypeFloat, {floating=l})
end,
[list_type] = function(l)
local len = #l
local arr = obj(api.kObjectTypeArray, {array={
size=len,
capacity=len,
items=ffi.cast('Object *', api.xmalloc(len * ffi.sizeof('Object'))),
}})
for i = 1, len do
arr.data.array.items[i - 1] = ffi.gc(lua2obj(l[i]), nil)
end
return arr
end,
[dict_type] = function(l)
local kvs = {}
for k, v in pairs(l) do
if type(k) == 'string' then
kvs[#kvs + 1] = {k, v}
end
end
local len = #kvs
local dct = obj(api.kObjectTypeDictionary, {dictionary={
size=len,
capacity=len,
items=ffi.cast('KeyValuePair *',
api.xmalloc(len * ffi.sizeof('KeyValuePair'))),
}})
for i = 1, len do
local key, value = table.unpack(kvs[i])
dct.data.dictionary.items[i - 1] = ffi.new(
'KeyValuePair', {key=ffi.gc(lua2obj(key), nil).data.string,
value=ffi.gc(lua2obj(val), nil)})
end
return dct
end,
}
lua2obj = function(l)
if type(l) == 'table' then
if l[type_key] then
return lua2obj_type_tab[l[type_key]](l)
else
if l[1] then
return lua2obj_type_tab[list_type](l)
else
return lua2obj_type_tab[dict_type](l)
end
end
elseif type(l) == 'number' then
return lua2obj_type_tab[flt_type](l)
elseif type(l) == 'boolean' then
return obj(api.kObjectTypeBoolean, {boolean=l})
elseif type(l) == 'string' then
return obj(api.kObjectTypeString, {string={
size=#l,
data=eval.xmemdupz(to_cstr(l), #l),
}})
elseif l == nil or l == NIL then
return obj(api.kObjectTypeNil, {integer=0})
end
end
return {
list_type=list_type,
dict_type=dict_type,
func_type=func_type,
int_type=int_type,
flt_type=flt_type,
type_key=type_key,
obj2lua=obj2lua,
lua2obj=lua2obj,
}

View File

@ -0,0 +1,87 @@
local helpers = require('test.unit.helpers')
local eval_helpers = require('test.unit.eval.helpers')
local api_helpers = require('test.unit.api.helpers')
local cimport = helpers.cimport
local NULL = helpers.NULL
local eq = helpers.eq
local lua2typvalt = eval_helpers.lua2typvalt
local typvalt = eval_helpers.typvalt
local list_type = api_helpers.list_type
local int_type = api_helpers.int_type
local type_key = api_helpers.type_key
local obj2lua = api_helpers.obj2lua
local api = cimport('./src/nvim/api/private/helpers.h')
describe('vim_to_object', function()
local vim_to_object = function(l)
return obj2lua(api.vim_to_object(lua2typvalt(l)))
end
local different_output_test = function(name, input, output)
it(name, function()
eq(output, vim_to_object(input))
end)
end
local simple_test = function(name, l)
different_output_test(name, l, l)
end
simple_test('converts true', true)
simple_test('converts false', false)
simple_test('converts nil', NIL)
simple_test('converts 1', 1)
simple_test('converts -1.5', -1.5)
simple_test('converts empty string', '')
simple_test('converts non-empty string', 'foobar')
simple_test('converts integer 10', {[type_key]=int_type, value=10})
simple_test('converts empty dictionary', {})
simple_test('converts dictionary with scalar values', {test=10, test2=true, test3='test'})
simple_test('converts dictionary with containers inside', {test={}, test2={1, 2}})
simple_test('converts empty list', {[type_key]=list_type})
simple_test('converts list with scalar values', {1, 2, 'test', 'foo'})
simple_test('converts list with containers inside', {{}, {test={}, test3={test4=true}}})
local dct = {}
dct.dct = dct
different_output_test('outputs nil for nested dictionaries (1 level)', dct, {dct=NIL})
local lst = {}
lst[1] = lst
different_output_test('outputs nil for nested lists (1 level)', lst, {NIL})
local dct2 = {test=true, dict=NIL}
dct2.dct = {dct2}
different_output_test('outputs nil for nested dictionaries (2 level, in list)',
dct2, {dct={NIL}, test=true, dict=NIL})
local dct3 = {test=true, dict=NIL}
dct3.dct = {dctin=dct3}
different_output_test('outputs nil for nested dictionaries (2 level, in dict)',
dct3, {dct={dctin=NIL}, test=true, dict=NIL})
local lst2 = {}
lst2[1] = {lst2}
different_output_test('outputs nil for nested lists (2 level, in list)', lst2, {{NIL}})
local lst3 = {nil, true, false, 'ttest'}
lst3[1] = {lst=lst3}
different_output_test('outputs nil for nested lists (2 level, in dict)',
lst3, {{lst=NIL}, true, false, 'ttest'})
it('outputs empty list for NULL list', function()
local tt = typvalt('VAR_LIST', {v_list=NULL})
eq(nil, tt.vval.v_list)
eq({[type_key]=list_type}, obj2lua(api.vim_to_object(tt)))
end)
it('outputs empty dict for NULL dict', function()
local tt = typvalt('VAR_DICT', {v_dict=NULL})
eq(nil, tt.vval.v_dict)
eq({}, obj2lua(api.vim_to_object(tt)))
end)
end)

View File

@ -27,74 +27,74 @@ describe('encode_list_write()', function()
it('writes ASCII string literal with printable characters', function()
local l = list()
eq(0, encode_list_write(l, 'abc'))
eq({[type_key]=list_type, 'abc'}, lst2tbl(l))
eq({'abc'}, lst2tbl(l))
end)
it('writes string starting with NL', function()
local l = list()
eq(0, encode_list_write(l, '\nabc'))
eq({[type_key]=list_type, null_string, 'abc'}, lst2tbl(l))
eq({null_string, 'abc'}, lst2tbl(l))
end)
it('writes string starting with NL twice', function()
local l = list()
eq(0, encode_list_write(l, '\nabc'))
eq({[type_key]=list_type, null_string, 'abc'}, lst2tbl(l))
eq({null_string, 'abc'}, lst2tbl(l))
eq(0, encode_list_write(l, '\nabc'))
eq({[type_key]=list_type, null_string, 'abc', 'abc'}, lst2tbl(l))
eq({null_string, 'abc', 'abc'}, lst2tbl(l))
end)
it('writes string ending with NL', function()
local l = list()
eq(0, encode_list_write(l, 'abc\n'))
eq({[type_key]=list_type, 'abc', null_string}, lst2tbl(l))
eq({'abc', null_string}, lst2tbl(l))
end)
it('writes string ending with NL twice', function()
local l = list()
eq(0, encode_list_write(l, 'abc\n'))
eq({[type_key]=list_type, 'abc', null_string}, lst2tbl(l))
eq({'abc', null_string}, lst2tbl(l))
eq(0, encode_list_write(l, 'abc\n'))
eq({[type_key]=list_type, 'abc', 'abc', null_string}, lst2tbl(l))
eq({'abc', 'abc', null_string}, lst2tbl(l))
end)
it('writes string starting, ending and containing NL twice', function()
local l = list()
eq(0, encode_list_write(l, '\na\nb\n'))
eq({[type_key]=list_type, null_string, 'a', 'b', null_string}, lst2tbl(l))
eq({null_string, 'a', 'b', null_string}, lst2tbl(l))
eq(0, encode_list_write(l, '\na\nb\n'))
eq({[type_key]=list_type, null_string, 'a', 'b', null_string, 'a', 'b', null_string}, lst2tbl(l))
eq({null_string, 'a', 'b', null_string, 'a', 'b', null_string}, lst2tbl(l))
end)
it('writes string starting, ending and containing NUL with NL between twice', function()
local l = list()
eq(0, encode_list_write(l, '\0\n\0\n\0'))
eq({[type_key]=list_type, '\n', '\n', '\n'}, lst2tbl(l))
eq({'\n', '\n', '\n'}, lst2tbl(l))
eq(0, encode_list_write(l, '\0\n\0\n\0'))
eq({[type_key]=list_type, '\n', '\n', '\n\n', '\n', '\n'}, lst2tbl(l))
eq({'\n', '\n', '\n\n', '\n', '\n'}, lst2tbl(l))
end)
it('writes string starting, ending and containing NL with NUL between twice', function()
local l = list()
eq(0, encode_list_write(l, '\n\0\n\0\n'))
eq({[type_key]=list_type, null_string, '\n', '\n', null_string}, lst2tbl(l))
eq({null_string, '\n', '\n', null_string}, lst2tbl(l))
eq(0, encode_list_write(l, '\n\0\n\0\n'))
eq({[type_key]=list_type, null_string, '\n', '\n', null_string, '\n', '\n', null_string}, lst2tbl(l))
eq({null_string, '\n', '\n', null_string, '\n', '\n', null_string}, lst2tbl(l))
end)
it('writes string containing a single NL twice', function()
local l = list()
eq(0, encode_list_write(l, '\n'))
eq({[type_key]=list_type, null_string, null_string}, lst2tbl(l))
eq({null_string, null_string}, lst2tbl(l))
eq(0, encode_list_write(l, '\n'))
eq({[type_key]=list_type, null_string, null_string, null_string}, lst2tbl(l))
eq({null_string, null_string, null_string}, lst2tbl(l))
end)
it('writes string containing a few NLs twice', function()
local l = list()
eq(0, encode_list_write(l, '\n\n\n'))
eq({[type_key]=list_type, null_string, null_string, null_string, null_string}, lst2tbl(l))
eq({null_string, null_string, null_string, null_string}, lst2tbl(l))
eq(0, encode_list_write(l, '\n\n\n'))
eq({[type_key]=list_type, null_string, null_string, null_string, null_string, null_string, null_string, null_string}, lst2tbl(l))
eq({null_string, null_string, null_string, null_string, null_string, null_string, null_string}, lst2tbl(l))
end)
end)

View File

@ -11,6 +11,10 @@ local null_string = {[true]='NULL string'}
local null_list = {[true]='NULL list'}
local type_key = {[true]='type key'}
local list_type = {[true]='list type'}
local dict_type = {[true]='dict type'}
local func_type = {[true]='func type'}
local int_type = {[true]='int type'}
local flt_type = {[true]='flt type'}
local function list(...)
local ret = ffi.gc(eval.list_alloc(), eval.list_unref)
@ -37,7 +41,51 @@ local function list(...)
return ret
end
local lst2tbl = function(l)
local special_tab = {
[eval.kSpecialVarFalse] = false,
[eval.kSpecialVarNull] = NIL,
[eval.kSpecialVarTrue] = true,
}
local lst2tbl
local dct2tbl
local typvalt2lua_tab = {
[tonumber(eval.VAR_SPECIAL)] = function(t)
return special_tab[t.vval.v_special]
end,
[tonumber(eval.VAR_NUMBER)] = function(t)
return {[type_key]=int_type, value=tonumber(t.vval.v_number)}
end,
[tonumber(eval.VAR_FLOAT)] = function(t)
return tonumber(t.vval.v_float)
end,
[tonumber(eval.VAR_STRING)] = function(t)
str = t.vval.v_string
if str == nil then
return null_string
else
return ffi.string(str)
end
end,
[tonumber(eval.VAR_LIST)] = function(t)
return lst2tbl(t.vval.v_list)
end,
[tonumber(eval.VAR_DICT)] = function(t)
return dct2tbl(t.vval.v_dict)
end,
[tonumber(eval.VAR_FUNC)] = function(t)
return {[type_key]=func_type, value=typvalt2lua_tab[eval.VAR_STRING](t)}
end,
}
local typvalt2lua = function(t)
return ((typvalt2lua_tab[tonumber(t.v_type)] or function(t)
assert(false, 'Converting ' .. tonumber(t.v_type) .. ' was not implemented yet')
end)(t))
end
lst2tbl = function(l)
local ret = {[type_key]=list_type}
if l == nil then
return ret
@ -45,28 +93,115 @@ local lst2tbl = function(l)
local li = l.lv_first
-- (listitem_T *) NULL is equal to nil, but yet it is not false.
while li ~= nil do
local typ = li.li_tv.v_type
if typ == eval.VAR_STRING then
local str = li.li_tv.vval.v_string
if str == nil then
ret[#ret + 1] = null_string
else
ret[#ret + 1] = ffi.string(str)
end
else
assert(false, 'Not implemented yet')
end
ret[#ret + 1] = typvalt2lua(li.li_tv)
li = li.li_next
end
if ret[1] then
ret[type_key] = nil
end
return ret
end
dct2tbl = function(d)
local ret = {}
assert(false, 'Converting dictionaries is not implemented yet')
end
local lua2typvalt
local typvalt = function(typ, vval)
if type(typ) == 'string' then
typ = eval[typ]
end
return ffi.gc(ffi.new('typval_T', {v_type=typ, vval=vval}), eval.clear_tv)
end
local lua2typvalt_type_tab = {
[int_type] = function(l, processed)
return typvalt(eval.VAR_NUMBER, {v_number=l.value})
end,
[flt_type] = function(l, processed)
return lua2typvalt(l.value, processed)
end,
[list_type] = function(l, processed)
if processed[l] then
processed[l].lv_refcount = processed[l].lv_refcount + 1
return typvalt(eval.VAR_LIST, {v_list=processed[l]})
end
local lst = eval.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.list_append_tv(lst, item_tv)
eval.clear_tv(item_tv)
end
return ret
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.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.dictitem_alloc(to_cstr(k))
local val_tv = ffi.gc(lua2typvalt(v, processed), nil)
eval.copy_tv(val_tv, di.di_tv)
eval.clear_tv(val_tv)
eval.dict_add(dct, di)
end
end
return ret
end,
}
lua2typvalt = function(l, processed)
processed = processed or {}
if type(l) == 'table' then
if l[type_key] then
return lua2typvalt_type_tab[l[type_key]](l, processed)
else
if l[1] then
return lua2typvalt_type_tab[list_type](l, processed)
else
return lua2typvalt_type_tab[dict_type](l, processed)
end
end
elseif type(l) == 'number' then
return typvalt(eval.VAR_FLOAT, {v_float=l})
elseif type(l) == 'boolean' then
return typvalt(eval.VAR_SPECIAL, {
v_special=(l and eval.kSpecialVarTrue or eval.kSpecialVarFalse)
})
elseif type(l) == 'string' then
return typvalt(eval.VAR_STRING, {v_string=eval.xmemdupz(to_cstr(l), #l)})
elseif l == nil or l == NIL then
return typvalt(eval.VAR_SPECIAL, {v_special=eval.kSpecialVarNull})
end
end
return {
null_string=null_string,
null_list=null_list,
list_type=list_type,
dict_type=dict_type,
func_type=func_type,
int_type=int_type,
flt_type=flt_type,
type_key=type_key,
list=list,
lst2tbl=lst2tbl,
dct2tbl=dct2tbl,
lua2typvalt=lua2typvalt,
typvalt2lua=typvalt2lua,
typvalt=typvalt,
}