diff options
Diffstat (limited to 'test/unit/api')
-rw-r--r-- | test/unit/api/helpers.lua | 99 | ||||
-rw-r--r-- | test/unit/api/private_helpers_spec.lua | 80 |
2 files changed, 102 insertions, 77 deletions
diff --git a/test/unit/api/helpers.lua b/test/unit/api/helpers.lua index 3d306d2b1b..23c5db43f7 100644 --- a/test/unit/api/helpers.lua +++ b/test/unit/api/helpers.lua @@ -13,9 +13,11 @@ 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 api = cimport( + './src/nvim/api/private/defs.h', + './src/nvim/api/private/helpers.h', + './src/nvim/memory.h' +) local obj2lua @@ -27,8 +29,8 @@ local function init_obj2lua_tab() end obj2lua_tab = { [tonumber(api.kObjectTypeArray)] = function(obj) - local ret = {[type_key]=list_type} - for i = 1,tonumber(obj.data.array.size) do + 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 @@ -38,7 +40,7 @@ local function init_obj2lua_tab() end, [tonumber(api.kObjectTypeDictionary)] = function(obj) local ret = {} - for i = 1,tonumber(obj.data.dictionary.size) do + 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 @@ -58,7 +60,7 @@ local function init_obj2lua_tab() return tonumber(obj.data.floating) end, [tonumber(api.kObjectTypeInteger)] = function(obj) - return {[type_key]=int_type, value=tonumber(obj.data.integer)} + 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) @@ -68,32 +70,38 @@ end obj2lua = function(obj) init_obj2lua_tab() - return ((obj2lua_tab[tonumber(obj['type'])] or function(obj_inner) - assert(false, 'Converting ' .. tostring(tonumber(obj_inner['type'])) .. ' is not implementing yet') - end)(obj)) + return ( + (obj2lua_tab[tonumber(obj['type'])] or function(obj_inner) + assert( + false, + 'Converting ' .. tostring(tonumber(obj_inner['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) + return ffi.gc(ffi.new('Object', { ['type'] = typ, data = data }), api.api_free_object) end local lua2obj local lua2obj_type_tab = { [int_type] = function(l) - return obj(api.kObjectTypeInteger, {integer=l.value}) + return obj(api.kObjectTypeInteger, { integer = l.value }) end, [flt_type] = function(l) - return obj(api.kObjectTypeFloat, {floating=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'))), - }}) + 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 @@ -103,21 +111,23 @@ local lua2obj_type_tab = { local kvs = {} for k, v in pairs(l) do if type(k) == 'string' then - kvs[#kvs + 1] = {k, v} + 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'))), - }}) + 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, val = 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)}) + 'KeyValuePair', + { key = ffi.gc(lua2obj(key), nil).data.string, value = ffi.gc(lua2obj(val), nil) } + ) end return dct end, @@ -137,28 +147,31 @@ lua2obj = function(l) elseif type(l) == 'number' then return lua2obj_type_tab[flt_type](l) elseif type(l) == 'boolean' then - return obj(api.kObjectTypeBoolean, {boolean=l}) + return obj(api.kObjectTypeBoolean, { boolean = l }) elseif type(l) == 'string' then - return obj(api.kObjectTypeString, {string={ - size=#l, - data=api.xmemdupz(to_cstr(l), #l), - }}) + return obj( + api.kObjectTypeString, + { string = { + size = #l, + data = api.xmemdupz(to_cstr(l), #l), + } } + ) elseif l == nil or l == nil_value then - return obj(api.kObjectTypeNil, {integer=0}) + 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, + list_type = list_type, + dict_type = dict_type, + func_type = func_type, + int_type = int_type, + flt_type = flt_type, - nil_value=nil_value, + nil_value = nil_value, - type_key=type_key, + type_key = type_key, - obj2lua=obj2lua, - lua2obj=lua2obj, + obj2lua = obj2lua, + lua2obj = lua2obj, } diff --git a/test/unit/api/private_helpers_spec.lua b/test/unit/api/private_helpers_spec.lua index dbb4b3ae5a..9843bd5c9e 100644 --- a/test/unit/api/private_helpers_spec.lua +++ b/test/unit/api/private_helpers_spec.lua @@ -22,7 +22,7 @@ local api = cimport('./src/nvim/api/private/helpers.h', './src/nvim/api/private/ describe('vim_to_object', function() local vim_to_object = function(l) - return obj2lua(api.vim_to_object(lua2typvalt(l))) + return obj2lua(api.vim_to_object(lua2typvalt(l), nil, false)) end local different_output_test = function(name, input, output) @@ -42,65 +42,77 @@ describe('vim_to_object', function() 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 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}}}) + 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_value}) + different_output_test('outputs nil for nested dictionaries (1 level)', dct, { dct = nil_value }) local lst = {} lst[1] = lst - different_output_test('outputs nil for nested lists (1 level)', lst, {nil_value}) - - local dct2 = {test=true, dict=nil_value} - dct2.dct = {dct2} - different_output_test('outputs nil for nested dictionaries (2 level, in list)', - dct2, {dct={nil_value}, test=true, dict=nil_value}) - - local dct3 = {test=true, dict=nil_value} - dct3.dct = {dctin=dct3} - different_output_test('outputs nil for nested dictionaries (2 level, in dict)', - dct3, {dct={dctin=nil_value}, test=true, dict=nil_value}) + different_output_test('outputs nil for nested lists (1 level)', lst, { nil_value }) + + local dct2 = { test = true, dict = nil_value } + dct2.dct = { dct2 } + different_output_test( + 'outputs nil for nested dictionaries (2 level, in list)', + dct2, + { dct = { nil_value }, test = true, dict = nil_value } + ) + + local dct3 = { test = true, dict = nil_value } + dct3.dct = { dctin = dct3 } + different_output_test( + 'outputs nil for nested dictionaries (2 level, in dict)', + dct3, + { dct = { dctin = nil_value }, test = true, dict = nil_value } + ) local lst2 = {} - lst2[1] = {lst2} - different_output_test('outputs nil for nested lists (2 level, in list)', lst2, {{nil_value}}) + lst2[1] = { lst2 } + different_output_test('outputs nil for nested lists (2 level, in list)', lst2, { { nil_value } }) - 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_value}, true, false, 'ttest'}) + 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_value }, true, false, 'ttest' } + ) itp('outputs empty list for NULL list', function() - local tt = typvalt('VAR_LIST', {v_list=NULL}) + 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))) + eq({ [type_key] = list_type }, obj2lua(api.vim_to_object(tt, nil, false))) end) itp('outputs empty dict for NULL dict', function() - local tt = typvalt('VAR_DICT', {v_dict=NULL}) + local tt = typvalt('VAR_DICT', { v_dict = NULL }) eq(nil, tt.vval.v_dict) - eq({}, obj2lua(api.vim_to_object(tt))) + eq({}, obj2lua(api.vim_to_object(tt, nil, false))) end) itp('regression: partials in a list', function() local llist = { { - [type_key]=func_type, - value='printf', - args={'%s'}, - dict={v=1}, + [type_key] = func_type, + value = 'printf', + args = { '%s' }, + dict = { v = 1 }, }, {}, } local list = lua2typvalt(llist) eq(llist, typvalt2lua(list)) - eq({nil_value, {}}, obj2lua(api.vim_to_object(list))) + eq({ nil_value, {} }, obj2lua(api.vim_to_object(list, nil, false))) end) end) |