private_helpers_spec.lua 3.6 KB

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