neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

private_helpers_spec.lua (3714B)


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