2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.unit.testutil')
|
2024-04-08 02:03:20 -07:00
|
|
|
local itp = t.gen_itp(it)
|
|
|
|
local t_eval = require('test.unit.eval.testutil')
|
|
|
|
local api_t = require('test.unit.api.testutil')
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local cimport = t.cimport
|
|
|
|
local NULL = t.NULL
|
|
|
|
local eq = t.eq
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local lua2typvalt = t_eval.lua2typvalt
|
|
|
|
local typvalt2lua = t_eval.typvalt2lua
|
|
|
|
local typvalt = t_eval.typvalt
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local nil_value = api_t.nil_value
|
|
|
|
local list_type = api_t.list_type
|
|
|
|
local int_type = api_t.int_type
|
|
|
|
local type_key = api_t.type_key
|
|
|
|
local obj2lua = api_t.obj2lua
|
|
|
|
local func_type = api_t.func_type
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local api = cimport('./src/nvim/api/private/t.h', './src/nvim/api/private/converter.h')
|
2016-04-19 09:17:09 -07:00
|
|
|
|
|
|
|
describe('vim_to_object', function()
|
|
|
|
local vim_to_object = function(l)
|
2024-02-12 12:40:27 -07:00
|
|
|
return obj2lua(api.vim_to_object(lua2typvalt(l), nil, false))
|
2016-04-19 09:17:09 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local different_output_test = function(name, input, output)
|
2017-03-04 18:02:45 -07:00
|
|
|
itp(name, function()
|
2016-04-19 09:17:09 -07:00
|
|
|
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)
|
2016-06-11 10:44:27 -07:00
|
|
|
simple_test('converts nil', nil_value)
|
2016-04-19 09:17:09 -07:00
|
|
|
simple_test('converts 1', 1)
|
|
|
|
simple_test('converts -1.5', -1.5)
|
|
|
|
simple_test('converts empty string', '')
|
|
|
|
simple_test('converts non-empty string', 'foobar')
|
2023-12-04 15:32:39 -07:00
|
|
|
simple_test('converts integer 10', { [type_key] = int_type, value = 10 })
|
refactor(api)!: rename Dictionary => Dict
In the api_info() output:
:new|put =map(filter(api_info().functions, '!has_key(v:val,''deprecated_since'')'), 'v:val')
...
{'return_type': 'ArrayOf(Integer, 2)', 'name': 'nvim_win_get_position', 'method': v:true, 'parameters': [['Window', 'window']], 'since': 1}
The `ArrayOf(Integer, 2)` return type didn't break clients when we added
it, which is evidence that clients don't use the `return_type` field,
thus renaming Dictionary => Dict in api_info() is not (in practice)
a breaking change.
2024-09-19 22:34:50 -07:00
|
|
|
simple_test('converts empty dict', {})
|
|
|
|
simple_test('converts dict with scalar values', { test = 10, test2 = true, test3 = 'test' })
|
|
|
|
simple_test('converts dict with containers inside', { test = {}, test2 = { 1, 2 } })
|
2023-12-04 15:32:39 -07:00
|
|
|
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 } } }
|
|
|
|
)
|
2016-04-19 09:17:09 -07:00
|
|
|
|
|
|
|
local dct = {}
|
|
|
|
dct.dct = dct
|
2023-12-04 15:32:39 -07:00
|
|
|
different_output_test('outputs nil for nested dictionaries (1 level)', dct, { dct = nil_value })
|
2016-04-19 09:17:09 -07:00
|
|
|
|
|
|
|
local lst = {}
|
|
|
|
lst[1] = lst
|
2023-12-04 15:32:39 -07:00
|
|
|
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 }
|
|
|
|
)
|
2016-04-19 09:17:09 -07:00
|
|
|
|
|
|
|
local lst2 = {}
|
2023-12-04 15:32:39 -07:00
|
|
|
lst2[1] = { lst2 }
|
|
|
|
different_output_test('outputs nil for nested lists (2 level, in list)', lst2, { { nil_value } })
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
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' }
|
|
|
|
)
|
2016-04-19 09:17:09 -07:00
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('outputs empty list for NULL list', function()
|
2023-12-04 15:32:39 -07:00
|
|
|
local tt = typvalt('VAR_LIST', { v_list = NULL })
|
2016-04-19 09:17:09 -07:00
|
|
|
eq(nil, tt.vval.v_list)
|
2024-02-12 12:40:27 -07:00
|
|
|
eq({ [type_key] = list_type }, obj2lua(api.vim_to_object(tt, nil, false)))
|
2016-04-19 09:17:09 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('outputs empty dict for NULL dict', function()
|
2023-12-04 15:32:39 -07:00
|
|
|
local tt = typvalt('VAR_DICT', { v_dict = NULL })
|
2016-04-19 09:17:09 -07:00
|
|
|
eq(nil, tt.vval.v_dict)
|
2024-02-12 12:40:27 -07:00
|
|
|
eq({}, obj2lua(api.vim_to_object(tt, nil, false)))
|
2016-04-19 09:17:09 -07:00
|
|
|
end)
|
2017-01-03 12:51:29 -07:00
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('regression: partials in a list', function()
|
2017-01-03 12:51:29 -07:00
|
|
|
local llist = {
|
|
|
|
{
|
2023-12-04 15:32:39 -07:00
|
|
|
[type_key] = func_type,
|
|
|
|
value = 'printf',
|
|
|
|
args = { '%s' },
|
|
|
|
dict = { v = 1 },
|
2017-01-03 12:51:29 -07:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
}
|
|
|
|
local list = lua2typvalt(llist)
|
|
|
|
eq(llist, typvalt2lua(list))
|
2024-02-12 12:40:27 -07:00
|
|
|
eq({ nil_value, {} }, obj2lua(api.vim_to_object(list, nil, false)))
|
2017-01-03 12:51:29 -07:00
|
|
|
end)
|
2016-04-19 09:17:09 -07:00
|
|
|
end)
|