robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.7 KiB119 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4local t_eval = require('test.unit.eval.testutil')
5local api_t = require('test.unit.api.testutil')
6
7local cimport = t.cimport
8local NULL = t.NULL
9local eq = t.eq
10
11local lua2typvalt = t_eval.lua2typvalt
12local typvalt2lua = t_eval.typvalt2lua
13local typvalt = t_eval.typvalt
14
15local nil_value = api_t.nil_value
16local list_type = api_t.list_type
17local int_type = api_t.int_type
18local type_key = api_t.type_key
19local obj2lua = api_t.obj2lua
20local func_type = api_t.func_type
21
22local api = cimport('./src/nvim/api/private/helpers.h', './src/nvim/api/private/converter.h')
23
24describe('vim_to_object', function()
25  local vim_to_object = function(l)
26    return obj2lua(api.vim_to_object(lua2typvalt(l), nil, false))
27  end
28
29  local different_output_test = function(name, input, output)
30    itp(name, function()
31      eq(output, vim_to_object(input))
32    end)
33  end
34
35  local simple_test = function(name, l)
36    different_output_test(name, l, l)
37  end
38
39  simple_test('converts true', true)
40  simple_test('converts false', false)
41  simple_test('converts nil', nil_value)
42  simple_test('converts 1', 1)
43  simple_test('converts -1.5', -1.5)
44  simple_test('converts empty string', '')
45  simple_test('converts non-empty string', 'foobar')
46  simple_test('converts integer 10', { [type_key] = int_type, value = 10 })
47  simple_test('converts empty dict', {})
48  simple_test('converts dict with scalar values', { test = 10, test2 = true, test3 = 'test' })
49  simple_test('converts dict with containers inside', { test = {}, test2 = { 1, 2 } })
50  simple_test('converts empty list', { [type_key] = list_type })
51  simple_test('converts list with scalar values', { 1, 2, 'test', 'foo' })
52  simple_test(
53    'converts list with containers inside',
54    { {}, { test = {}, test3 = { test4 = true } } }
55  )
56
57  local dct = {}
58  dct.dct = dct
59  different_output_test('outputs nil for nested dictionaries (1 level)', dct, { dct = nil_value })
60
61  local lst = {}
62  lst[1] = lst
63  different_output_test('outputs nil for nested lists (1 level)', lst, { nil_value })
64
65  local dct2 = { test = true, dict = nil_value }
66  dct2.dct = { dct2 }
67  different_output_test(
68    'outputs nil for nested dictionaries (2 level, in list)',
69    dct2,
70    { dct = { nil_value }, test = true, dict = nil_value }
71  )
72
73  local dct3 = { test = true, dict = nil_value }
74  dct3.dct = { dctin = dct3 }
75  different_output_test(
76    'outputs nil for nested dictionaries (2 level, in dict)',
77    dct3,
78    { dct = { dctin = nil_value }, test = true, dict = nil_value }
79  )
80
81  local lst2 = {}
82  lst2[1] = { lst2 }
83  different_output_test('outputs nil for nested lists (2 level, in list)', lst2, { { nil_value } })
84
85  local lst3 = { nil, true, false, 'ttest' }
86  lst3[1] = { lst = lst3 }
87  different_output_test(
88    'outputs nil for nested lists (2 level, in dict)',
89    lst3,
90    { { lst = nil_value }, true, false, 'ttest' }
91  )
92
93  itp('outputs empty list for NULL list', function()
94    local tt = typvalt('VAR_LIST', { v_list = NULL })
95    eq(nil, tt.vval.v_list)
96    eq({ [type_key] = list_type }, obj2lua(api.vim_to_object(tt, nil, false)))
97  end)
98
99  itp('outputs empty dict for NULL dict', function()
100    local tt = typvalt('VAR_DICT', { v_dict = NULL })
101    eq(nil, tt.vval.v_dict)
102    eq({}, obj2lua(api.vim_to_object(tt, nil, false)))
103  end)
104
105  itp('regression: partials in a list', function()
106    local llist = {
107      {
108        [type_key] = func_type,
109        value = 'printf',
110        args = { '%s' },
111        dict = { v = 1 },
112      },
113      {},
114    }
115    local list = lua2typvalt(llist)
116    eq(llist, typvalt2lua(list))
117    eq({ nil_value, {} }, obj2lua(api.vim_to_object(list, nil, false)))
118  end)
119end)