mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 20:08:17 +00:00
eval/encode: Make sure that encoder can encode NULL variables
Adds two undocumented v: variables: _null_list and _null_dict because I do not know a reproducible way to get such lists (though I think I heard about this) and dictionaries (do not remember hearing about them). NULL strings are obtained using $XXX_UNEXISTENT_VAR_XXX. Fixes crash in json_encode($XXX_UNEXISTENT_VAR_XXX). Other added tests worked fine before this commit.
This commit is contained in:
@@ -379,6 +379,8 @@ static struct vimvar {
|
||||
VV(VV_FALSE, "false", VAR_SPECIAL, VV_RO),
|
||||
VV(VV_TRUE, "true", VAR_SPECIAL, VV_RO),
|
||||
VV(VV_NULL, "null", VAR_SPECIAL, VV_RO),
|
||||
VV(VV__NULL_LIST, "_null_list", VAR_LIST, VV_RO),
|
||||
VV(VV__NULL_DICT, "_null_dict", VAR_DICT, VV_RO),
|
||||
};
|
||||
#undef VV
|
||||
|
||||
|
@@ -122,6 +122,8 @@ typedef enum {
|
||||
VV_FALSE,
|
||||
VV_TRUE,
|
||||
VV_NULL,
|
||||
VV__NULL_LIST, // List with NULL value. For test purposes only.
|
||||
VV__NULL_DICT, // Dictionary with NULL value. For test purposes only.
|
||||
} VimVarIndex;
|
||||
|
||||
/// All recognized msgpack types
|
||||
|
@@ -287,6 +287,9 @@ int encode_read_from_list(ListReaderState *const state, char *const buf,
|
||||
(val)->copyID_attr = copyID; \
|
||||
} while (0)
|
||||
|
||||
#define TV_STRLEN(tv) \
|
||||
(tv->vval.v_string == NULL ? 0 : STRLEN(tv->vval.v_string))
|
||||
|
||||
/// Define functions which convert VimL value to something else
|
||||
///
|
||||
/// Creates function `vim_to_{name}(firstargtype firstargname, typval_T *const
|
||||
@@ -306,7 +309,7 @@ static int name##_convert_one_value(firstargtype firstargname, \
|
||||
{ \
|
||||
switch (tv->v_type) { \
|
||||
case VAR_STRING: { \
|
||||
CONV_STRING(tv->vval.v_string, STRLEN(tv->vval.v_string)); \
|
||||
CONV_STRING(tv->vval.v_string, TV_STRLEN(tv)); \
|
||||
break; \
|
||||
} \
|
||||
case VAR_NUMBER: { \
|
||||
|
@@ -784,4 +784,16 @@ describe('json_encode() function', function()
|
||||
eq([["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013"]],
|
||||
eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\n\1\2\3\4\5\6\7\8\9", "\11\12\13\14\15\16\17\18\19"]})'))
|
||||
end)
|
||||
|
||||
it('can dump NULL string', function()
|
||||
eq('""', eval('json_encode($XXX_UNEXISTENT_VAR_XXX)'))
|
||||
end)
|
||||
|
||||
it('can dump NULL list', function()
|
||||
eq('[]', eval('json_encode(v:_null_list)'))
|
||||
end)
|
||||
|
||||
it('can dump NULL dictionary', function()
|
||||
eq('{}', eval('json_encode(v:_null_dict)'))
|
||||
end)
|
||||
end)
|
||||
|
@@ -686,4 +686,18 @@ describe('msgpackdump() function', function()
|
||||
exc_exec('call msgpackdump(' .. val .. ')'))
|
||||
end
|
||||
end)
|
||||
|
||||
it('can dump NULL string', function()
|
||||
eq({'\196\n'}, eval('msgpackdump([$XXX_UNEXISTENT_VAR_XXX])'))
|
||||
eq({'\196\n'}, eval('msgpackdump([{"_TYPE": v:msgpack_types.binary, "_VAL": [$XXX_UNEXISTENT_VAR_XXX]}])'))
|
||||
eq({'\160'}, eval('msgpackdump([{"_TYPE": v:msgpack_types.string, "_VAL": [$XXX_UNEXISTENT_VAR_XXX]}])'))
|
||||
end)
|
||||
|
||||
it('can dump NULL list', function()
|
||||
eq({'\144'}, eval('msgpackdump([v:_null_list])'))
|
||||
end)
|
||||
|
||||
it('can dump NULL dictionary', function()
|
||||
eq({'\128'}, eval('msgpackdump([v:_null_dict])'))
|
||||
end)
|
||||
end)
|
||||
|
@@ -84,6 +84,18 @@ describe('string() function', function()
|
||||
eq('\'\'\'b\'\'\'\'d\'', funcs.string('\'b\'\'d'))
|
||||
eq('\'a\'\'b\'\'c\'\'d\'', funcs.string('a\'b\'c\'d'))
|
||||
end)
|
||||
|
||||
it('dumps NULL strings', function()
|
||||
eq('\'\'', eval('string($XXX_UNEXISTENT_VAR_XXX)'))
|
||||
end)
|
||||
|
||||
it('dumps NULL lists', function()
|
||||
eq('[]', eval('string(v:_null_list)'))
|
||||
end)
|
||||
|
||||
it('dumps NULL dictionaries', function()
|
||||
eq('{}', eval('string(v:_null_dict)'))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('used to represent funcrefs', function()
|
||||
|
43
test/unit/eval/tricks_spec.lua
Normal file
43
test/unit/eval/tricks_spec.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
local helpers = require('test.unit.helpers')
|
||||
|
||||
local cimport = helpers.cimport
|
||||
local to_cstr = helpers.to_cstr
|
||||
local ffi = helpers.ffi
|
||||
local eq = helpers.eq
|
||||
|
||||
local eval = cimport('./src/nvim/eval.h', './src/nvim/memory.h')
|
||||
|
||||
local eval_expr = function(expr)
|
||||
return ffi.gc(eval.eval_expr(to_cstr(expr), nil), function(tv)
|
||||
eval.clear_tv(tv)
|
||||
eval.xfree(tv)
|
||||
end)
|
||||
end
|
||||
|
||||
describe('NULL typval_T', function()
|
||||
it('is produced by $XXX_UNEXISTENT_VAR_XXX', function()
|
||||
-- Required for various tests which need to check whether typval_T with NULL
|
||||
-- string works correctly. This test checks that unexistent environment
|
||||
-- variable produces NULL string, not that some specific environment
|
||||
-- variable does not exist. Last bit is left for the test writers.
|
||||
local unexistent_env = 'XXX_UNEXISTENT_VAR_XXX'
|
||||
while os.getenv(unexistent_env) ~= nil do
|
||||
unexistent_env = unexistent_env .. '_XXX'
|
||||
end
|
||||
local rettv = eval_expr('$' .. unexistent_env)
|
||||
eq(eval.VAR_STRING, rettv.v_type)
|
||||
eq(nil, rettv.vval.v_string)
|
||||
end)
|
||||
|
||||
it('is produced by v:_null_list', function()
|
||||
local rettv = eval_expr('v:_null_list')
|
||||
eq(eval.VAR_LIST, rettv.v_type)
|
||||
eq(nil, rettv.vval.v_list)
|
||||
end)
|
||||
|
||||
it('is produced by v:_null_dict', function()
|
||||
local rettv = eval_expr('v:_null_dict')
|
||||
eq(eval.VAR_DICT, rettv.v_type)
|
||||
eq(nil, rettv.vval.v_dict)
|
||||
end)
|
||||
end)
|
Reference in New Issue
Block a user