diff options
-rw-r--r-- | src/nvim/eval/typval.c | 7 | ||||
-rw-r--r-- | test/functional/legacy/glob2regpat_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/vimscript/execute_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/vimscript/writefile_spec.lua | 2 | ||||
-rw-r--r-- | test/old/testdir/test_eval_stuff.vim | 6 | ||||
-rw-r--r-- | test/old/testdir/test_execute_func.vim | 9 | ||||
-rw-r--r-- | test/old/testdir/test_float_func.vim | 5 | ||||
-rw-r--r-- | test/old/testdir/test_functions.vim | 15 | ||||
-rw-r--r-- | test/old/testdir/test_glob2regpat.vim | 5 | ||||
-rw-r--r-- | test/old/testdir/test_listdict.vim | 6 | ||||
-rw-r--r-- | test/old/testdir/test_substitute.vim | 9 | ||||
-rw-r--r-- | test/old/testdir/vim9.vim | 4 | ||||
-rw-r--r-- | test/unit/eval/typval_spec.lua | 233 |
13 files changed, 144 insertions, 172 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 42e9dc8f03..a392d441cf 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -3876,7 +3876,6 @@ static const char *const str_errors[] = { [VAR_FUNC]= N_(FUNC_ERROR), [VAR_LIST]= N_("E730: Using a List as a String"), [VAR_DICT]= N_("E731: Using a Dictionary as a String"), - [VAR_FLOAT]= e_using_float_as_string, [VAR_BLOB]= N_("E976: Using a Blob as a String"), [VAR_UNKNOWN]= e_using_invalid_value_as_string, }; @@ -3899,12 +3898,12 @@ bool tv_check_str(const typval_T *const tv) case VAR_BOOL: case VAR_SPECIAL: case VAR_STRING: + case VAR_FLOAT: return true; case VAR_PARTIAL: case VAR_FUNC: case VAR_LIST: case VAR_DICT: - case VAR_FLOAT: case VAR_BLOB: case VAR_UNKNOWN: emsg(_(str_errors[tv->v_type])); @@ -4275,6 +4274,9 @@ const char *tv_get_string_buf_chk(const typval_T *const tv, char *const buf) case VAR_NUMBER: snprintf(buf, NUMBUFLEN, "%" PRIdVARNUMBER, tv->vval.v_number); // -V576 return buf; + case VAR_FLOAT: + vim_snprintf(buf, NUMBUFLEN, "%g", tv->vval.v_float); + return buf; case VAR_STRING: if (tv->vval.v_string != NULL) { return tv->vval.v_string; @@ -4290,7 +4292,6 @@ const char *tv_get_string_buf_chk(const typval_T *const tv, char *const buf) case VAR_FUNC: case VAR_LIST: case VAR_DICT: - case VAR_FLOAT: case VAR_BLOB: case VAR_UNKNOWN: emsg(_(str_errors[tv->v_type])); diff --git a/test/functional/legacy/glob2regpat_spec.lua b/test/functional/legacy/glob2regpat_spec.lua index 1771f12f85..de304f3e4b 100644 --- a/test/functional/legacy/glob2regpat_spec.lua +++ b/test/functional/legacy/glob2regpat_spec.lua @@ -1,16 +1,10 @@ --- Tests for signs - local helpers = require('test.functional.helpers')(after_each) -local clear, exc_exec = helpers.clear, helpers.exc_exec +local clear = helpers.clear local eq, eval = helpers.eq, helpers.eval describe('glob2regpat()', function() before_each(clear) - it('handles invalid input', function() - eq('Vim(call):E806: Using a Float as a String', - exc_exec('call glob2regpat(1.33)')) - end) it('returns ^$ for empty input', function() eq('^$', eval("glob2regpat('')")) end) diff --git a/test/functional/vimscript/execute_spec.lua b/test/functional/vimscript/execute_spec.lua index a9a4ad4811..bb28938708 100644 --- a/test/functional/vimscript/execute_spec.lua +++ b/test/functional/vimscript/execute_spec.lua @@ -93,14 +93,10 @@ describe('execute()', function() it('captures errors', function() local ret - ret = exc_exec('call execute(0.0)') - eq('Vim(call):E806: Using a Float as a String', ret) ret = exc_exec('call execute(v:_null_dict)') eq('Vim(call):E731: Using a Dictionary as a String', ret) ret = exc_exec('call execute(function("tr"))') eq('Vim(call):E729: Using a Funcref as a String', ret) - ret = exc_exec('call execute(["echo 42", 0.0, "echo 44"])') - eq('Vim:E806: Using a Float as a String', ret) ret = exc_exec('call execute(["echo 42", v:_null_dict, "echo 44"])') eq('Vim:E731: Using a Dictionary as a String', ret) ret = exc_exec('call execute(["echo 42", function("tr"), "echo 44"])') @@ -330,9 +326,6 @@ describe('execute()', function() it('propagates errors for "" and "silent"', function() local ret - ret = exc_exec('call execute(0.0, "")') - eq('Vim(call):E806: Using a Float as a String', ret) - ret = exc_exec('call execute(v:_null_dict, "silent")') eq('Vim(call):E731: Using a Dictionary as a String', ret) diff --git a/test/functional/vimscript/writefile_spec.lua b/test/functional/vimscript/writefile_spec.lua index 521a4eb2b1..88c19bd839 100644 --- a/test/functional/vimscript/writefile_spec.lua +++ b/test/functional/vimscript/writefile_spec.lua @@ -145,8 +145,6 @@ describe('writefile()', function() pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname))) end for _, args in ipairs({'[], %s, "b"', '[], "' .. fname .. '", %s'}) do - eq('Vim(call):E806: Using a Float as a String', - pcall_err(command, ('call writefile(%s)'):format(args:format('0.0')))) eq('Vim(call):E730: Using a List as a String', pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))) eq('Vim(call):E731: Using a Dictionary as a String', diff --git a/test/old/testdir/test_eval_stuff.vim b/test/old/testdir/test_eval_stuff.vim index 20eb873326..45dfd57842 100644 --- a/test/old/testdir/test_eval_stuff.vim +++ b/test/old/testdir/test_eval_stuff.vim @@ -230,6 +230,12 @@ func Test_string_concatenation() let a = 'a' let a..=b call assert_equal('ab', a) + + if has('float') + let a = 'A' + let b = 1.234 + call assert_equal('A1.234', a .. b) + endif endfunc " Test fix for issue #4507 diff --git a/test/old/testdir/test_execute_func.vim b/test/old/testdir/test_execute_func.vim index cc294b7316..61971ec587 100644 --- a/test/old/testdir/test_execute_func.vim +++ b/test/old/testdir/test_execute_func.vim @@ -1,6 +1,8 @@ " test execute() source view_util.vim +source check.vim +source vim9.vim func NestedEval() let nested = execute('echo "nested\nlines"') @@ -31,7 +33,6 @@ func Test_execute_string() call assert_equal("\nthat", evaled) call assert_fails('call execute("doesnotexist")', 'E492:') - call assert_fails('call execute(3.4)', 'E806:') " Nvim supports execute('... :redir ...'), so this test is intentionally " disabled. " call assert_fails('call execute("call NestedRedir()")', 'E930:') @@ -40,7 +41,11 @@ func Test_execute_string() call assert_equal("\nsomething", execute('echo "something"', 'silent')) call assert_equal("\nsomething", execute('echo "something"', 'silent!')) call assert_equal("", execute('burp', 'silent!')) - call assert_fails('call execute("echo \"x\"", 3.4)', 'E806:') + if has('float') + call assert_fails('call execute(3.4)', 'E492:') + call assert_equal("\nx", execute("echo \"x\"", 3.4)) + call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], 'E806:') + endif endfunc func Test_execute_list() diff --git a/test/old/testdir/test_float_func.vim b/test/old/testdir/test_float_func.vim index 902a011a9d..f9a7251d59 100644 --- a/test/old/testdir/test_float_func.vim +++ b/test/old/testdir/test_float_func.vim @@ -2,6 +2,7 @@ source check.vim CheckFeature float +source vim9.vim func Test_abs() call assert_equal('1.23', string(abs(1.23))) @@ -238,7 +239,9 @@ func Test_str2float() call assert_equal("str2float('nan')", string(str2float('NaN'))) call assert_equal("str2float('nan')", string(str2float(' nan '))) - call assert_fails("call str2float(1.2)", 'E806:') + call assert_equal(1.2, str2float(1.2)) + call CheckDefExecFailure(['str2float(1.2)'], 'E1013:') + call CheckScriptFailure(['vim9script', 'str2float(1.2)'], 'E806:') call assert_fails("call str2float([])", 'E730:') call assert_fails("call str2float({})", 'E731:') call assert_fails("call str2float(function('string'))", 'E729:') diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim index 39d994a2df..d80004d70f 100644 --- a/test/old/testdir/test_functions.vim +++ b/test/old/testdir/test_functions.vim @@ -153,11 +153,13 @@ func Test_strwidth() call assert_fails('call strwidth({->0})', 'E729:') call assert_fails('call strwidth([])', 'E730:') call assert_fails('call strwidth({})', 'E731:') - if has('float') - call assert_fails('call strwidth(1.2)', 'E806:') - endif endfor + if has('float') + call assert_equal(3, strwidth(1.2)) + call CheckDefExecAndScriptFailure(['echo strwidth(1.2)'], 'E806:') + endif + set ambiwidth& endfunc @@ -221,7 +223,9 @@ func Test_str2nr() call assert_fails('call str2nr([])', 'E730:') call assert_fails('call str2nr({->2})', 'E729:') if has('float') - call assert_fails('call str2nr(1.2)', 'E806:') + call assert_equal(1, str2nr(1.2)) + call CheckDefExecFailure(['echo str2nr(1.2)'], 'E1013:') + call CheckScriptFailure(['vim9script', 'echo str2nr(1.2)'], 'E806:') endif call assert_fails('call str2nr(10, [])', 'E745:') endfunc @@ -372,7 +376,8 @@ func Test_simplify() call assert_fails('call simplify([])', 'E730:') call assert_fails('call simplify({})', 'E731:') if has('float') - call assert_fails('call simplify(1.2)', 'E806:') + call assert_equal('1.2', simplify(1.2)) + call CheckDefExecAndScriptFailure(['echo simplify(1.2)'], 'E806:') endif endfunc diff --git a/test/old/testdir/test_glob2regpat.vim b/test/old/testdir/test_glob2regpat.vim index a423a4a9f0..b9c4c12059 100644 --- a/test/old/testdir/test_glob2regpat.vim +++ b/test/old/testdir/test_glob2regpat.vim @@ -1,8 +1,11 @@ " Test glob2regpat() +source vim9.vim + func Test_glob2regpat_invalid() if has('float') - call assert_fails('call glob2regpat(1.33)', 'E806:') + call assert_equal('^1\.33$', glob2regpat(1.33)) + call CheckDefExecAndScriptFailure(['echo glob2regpat(1.33)'], 'E806:') endif call assert_fails('call glob2regpat("}")', 'E219:') call assert_fails('call glob2regpat("{")', 'E220:') diff --git a/test/old/testdir/test_listdict.vim b/test/old/testdir/test_listdict.vim index 11dade18f3..3a87616a9e 100644 --- a/test/old/testdir/test_listdict.vim +++ b/test/old/testdir/test_listdict.vim @@ -882,7 +882,7 @@ func Test_listdict_extend() call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:') if has('float') - call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E806:') + call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:') endif call assert_equal({'a': 'A', 'b': 'B'}, d) @@ -1051,9 +1051,9 @@ func Test_listdict_index() call assert_fails("let l = insert([1,2,3], 4, [])", 'E745:') let l = [1, 2, 3] call assert_fails("let l[i] = 3", 'E121:') - call assert_fails("let l[1.1] = 4", 'E806:') + call assert_fails("let l[1.1] = 4", 'E805:') call assert_fails("let l[:i] = [4, 5]", 'E121:') - call assert_fails("let l[:3.2] = [4, 5]", 'E806:') + call assert_fails("let l[:3.2] = [4, 5]", 'E805:') " Nvim doesn't have test_unknown() " let t = test_unknown() " call assert_fails("echo t[0]", 'E685:') diff --git a/test/old/testdir/test_substitute.vim b/test/old/testdir/test_substitute.vim index daa9426911..a6640aac30 100644 --- a/test/old/testdir/test_substitute.vim +++ b/test/old/testdir/test_substitute.vim @@ -457,7 +457,14 @@ func Test_substitute_partial() " 20 arguments plus one is too many let Replacer = function('SubReplacer20', repeat(['foo'], 20)) - call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118') + call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:') +endfunc + +func Test_substitute_float() + CheckFeature float + + call assert_equal('number 1.23', substitute('number ', '$', { -> 1.23 }, '')) + " vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, '')) endfunc " Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. diff --git a/test/old/testdir/vim9.vim b/test/old/testdir/vim9.vim index 7b9ec4bfc4..ba0ef53e67 100644 --- a/test/old/testdir/vim9.vim +++ b/test/old/testdir/vim9.vim @@ -2,6 +2,10 @@ " Use a different file name for each run. let s:sequence = 1 +func CheckDefExecFailure(lines, error, lnum = -3) + return +endfunc + func CheckScriptFailure(lines, error, lnum = -3) if get(a:lines, 0, '') ==# 'vim9script' return diff --git a/test/unit/eval/typval_spec.lua b/test/unit/eval/typval_spec.lua index cde5a731cf..a60700287f 100644 --- a/test/unit/eval/typval_spec.lua +++ b/test/unit/eval/typval_spec.lua @@ -101,12 +101,13 @@ local function check_emsg(f, msg) saved_last_msg_hist = nil end local ret = {f()} + local last_msg = lib.last_msg_hist ~= nil and ffi.string(lib.last_msg_hist.msg) or nil if msg ~= nil then - eq(msg, ffi.string(lib.last_msg_hist.msg)) + eq(msg, last_msg) neq(saved_last_msg_hist, lib.last_msg_hist) else if saved_last_msg_hist ~= lib.last_msg_hist then - eq(nil, ffi.string(lib.last_msg_hist.msg)) + eq(nil, last_msg) else eq(saved_last_msg_hist, lib.last_msg_hist) end @@ -1429,15 +1430,16 @@ describe('typval.c', function() end describe('str()', function() itp('returns correct string', function() - local l = list(int(1), int(2), int(3), int(4), int(5)) + local l = list(int(1), 2.5, int(3), int(4), int(5)) alloc_log:clear() eq('1', tv_list_find_str(l, -5)) + eq('2.5', tv_list_find_str(l, 1)) eq('5', tv_list_find_str(l, 4)) eq('3', tv_list_find_str(l, 2)) eq('3', tv_list_find_str(l, -3)) - alloc_log:check({}) + alloc_log:check({a.freed(alloc_log.null)}) end) itp('returns string when used with VAR_STRING items', function() local l = list('1', '2', '3', '4', '5') @@ -1465,14 +1467,12 @@ describe('typval.c', function() eq(nil, tv_list_find_str(l, 5, 'E684: List index out of range: 5')) end) itp('fails with error message on invalid types', function() - local l = list(1, empty_list, {}) + local l = list(empty_list, {}) - eq('', tv_list_find_str(l, 0, 'E806: Using a Float as a String')) - eq('', tv_list_find_str(l, 1, 'E730: Using a List as a String')) - eq('', tv_list_find_str(l, 2, 'E731: Using a Dictionary as a String')) + eq('', tv_list_find_str(l, 0, 'E730: Using a List as a String')) + eq('', tv_list_find_str(l, 1, 'E731: Using a Dictionary as a String')) eq('', tv_list_find_str(l, -1, 'E731: Using a Dictionary as a String')) eq('', tv_list_find_str(l, -2, 'E730: Using a List as a String')) - eq('', tv_list_find_str(l, -3, 'E806: Using a Float as a String')) end) end) end) @@ -1765,18 +1765,24 @@ describe('typval.c', function() neq(s42, s43) eq(s43, dis.te.di_tv.vval.v_string) alloc_log:check({}) - eq('', ffi.string(check_emsg(function() return lib.tv_dict_get_string(d, 't', false) end, - 'E806: Using a Float as a String'))) + local s44 = check_emsg(function() return lib.tv_dict_get_string(d, 't', false) end, + nil) + eq('44.0', ffi.string(s44)) + alloc_log:check({a.freed(alloc_log.null)}) end) itp('allocates a string copy when requested', function() - local function tv_dict_get_string_alloc(d, key, emsg) + local function tv_dict_get_string_alloc(d, key, emsg, is_float) alloc_log:clear() local ret = check_emsg(function() return lib.tv_dict_get_string(d, key, true) end, emsg) local s_ret = (ret ~= nil) and ffi.string(ret) or nil if not emsg then if s_ret then - alloc_log:check({a.str(ret, s_ret)}) + if is_float then + alloc_log:check({a.freed(alloc_log.null), a.str(ret, s_ret)}) + else + alloc_log:check({a.str(ret, s_ret)}) + end else alloc_log:check({}) end @@ -1792,18 +1798,22 @@ describe('typval.c', function() eq('42', tv_dict_get_string_alloc(d, 'tes')) eq('45', tv_dict_get_string_alloc(d, 'xx')) eq('43', tv_dict_get_string_alloc(d, 'te')) - eq('', tv_dict_get_string_alloc(d, 't', 'E806: Using a Float as a String')) + eq('44.0', tv_dict_get_string_alloc(d, 't', nil, true)) end) end) describe('get_string_buf()', function() - local function tv_dict_get_string_buf(d, key, buf, emsg) + local function tv_dict_get_string_buf(d, key, is_float, buf, emsg) buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree) alloc_log:clear() local ret = check_emsg(function() return lib.tv_dict_get_string_buf(d, key, buf) end, emsg) local s_ret = (ret ~= nil) and ffi.string(ret) or nil if not emsg then - alloc_log:check({}) + if is_float then + alloc_log:check({a.freed(alloc_log.null)}) + else + alloc_log:check({}) + end end return s_ret, ret, buf end @@ -1827,16 +1837,16 @@ describe('typval.c', function() s, r, b = tv_dict_get_string_buf(d, 'test') neq(r, b) eq('tset', s) - s, r, b = tv_dict_get_string_buf(d, 't', nil, 'E806: Using a Float as a String') - neq(r, b) - eq('', s) + s, r, b = tv_dict_get_string_buf(d, 't', true) + eq(r, b) + eq('1.0', s) s, r, b = tv_dict_get_string_buf(d, 'te') eq(r, b) eq('2', s) end) end) describe('get_string_buf_chk()', function() - local function tv_dict_get_string_buf_chk(d, key, len, buf, def, emsg) + local function tv_dict_get_string_buf_chk(d, key, is_float, len, buf, def, emsg) buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree) def = def or ffi.gc(lib.xstrdup('DEFAULT'), lib.xfree) len = len or #key @@ -1845,7 +1855,11 @@ describe('typval.c', function() emsg) local s_ret = (ret ~= nil) and ffi.string(ret) or nil if not emsg then - alloc_log:check({}) + if is_float then + alloc_log:check({a.freed(alloc_log.null)}) + else + alloc_log:check({}) + end end return s_ret, ret, buf, def end @@ -1870,10 +1884,10 @@ describe('typval.c', function() neq(r, b) neq(r, def) eq('tset', s) - s, r, b, def = tv_dict_get_string_buf_chk(d, 'test', 1, nil, nil, 'E806: Using a Float as a String') - neq(r, b) + s, r, b, def = tv_dict_get_string_buf_chk(d, 'test', true, 1) + eq(r, b) neq(r, def) - eq(nil, s) + eq('1.0', s) s, r, b, def = tv_dict_get_string_buf_chk(d, 'te') eq(r, b) neq(r, def) @@ -2831,7 +2845,7 @@ describe('typval.c', function() alloc_log:clear() for _, v in ipairs({ {lib.VAR_NUMBER, nil}, - {lib.VAR_FLOAT, 'E806: Using a Float as a String'}, + {lib.VAR_FLOAT, nil}, {lib.VAR_PARTIAL, 'E729: Using a Funcref as a String'}, {lib.VAR_FUNC, 'E729: Using a Funcref as a String'}, {lib.VAR_LIST, 'E730: Using a List as a String'}, @@ -2978,15 +2992,47 @@ describe('typval.c', function() end end) end) + + local function test_string_fn(input, fn) + for _, v in ipairs(input) do + -- Using to_cstr in place of Neovim allocated string, cannot + -- tv_clear() that. + local tv = ffi.gc(typvalt(v[1], v[2]), nil) + alloc_log:check({}) + local emsg = v[3] + local ret = v[4] + eq(ret, check_emsg(function() + local res, buf = fn(tv) + if tv.v_type == lib.VAR_NUMBER or tv.v_type == lib.VAR_FLOAT + or tv.v_type == lib.VAR_SPECIAL or tv.v_type == lib.VAR_BOOL then + eq(buf, res) + else + neq(buf, res) + end + if res ~= nil then + return ffi.string(res) + else + return nil + end + end, emsg)) + if emsg then + alloc_log:clear() + elseif tv.v_type == lib.VAR_FLOAT then + alloc_log:check({a.freed(alloc_log.null)}) + else + alloc_log:check({}) + end + end + end describe('string()', function() itp('works', function() local buf = lib.tv_get_string(lua2typvalt(int(1))) local buf_chk = lib.tv_get_string_chk(lua2typvalt(int(1))) neq(buf, buf_chk) - for _, v in ipairs({ + test_string_fn({ {lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, - {lib.VAR_FLOAT, {v_float=42.53}, 'E806: Using a Float as a String', ''}, + {lib.VAR_FLOAT, {v_float=42.53}, nil, '42.53'}, {lib.VAR_PARTIAL, {v_partial=NULL}, 'E729: Using a Funcref as a String', ''}, {lib.VAR_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', ''}, {lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List as a String', ''}, @@ -2995,42 +3041,18 @@ describe('typval.c', function() {lib.VAR_BOOL, {v_bool=lib.kBoolVarTrue}, nil, 'v:true'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''}, - }) do - -- Using to_cstr in place of Neovim allocated string, cannot - -- tv_clear() that. - local tv = ffi.gc(typvalt(v[1], v[2]), nil) - alloc_log:check({}) - local emsg = v[3] - local ret = v[4] - eq(ret, check_emsg(function() - local res = lib.tv_get_string(tv) - if tv.v_type == lib.VAR_NUMBER or tv.v_type == lib.VAR_SPECIAL - or tv.v_type == lib.VAR_BOOL then - eq(buf, res) - else - neq(buf, res) - end - if res ~= nil then - return ffi.string(res) - else - return nil - end - end, emsg)) - if emsg then - alloc_log:clear() - else - alloc_log:check({}) - end - end + }, function(tv) + return lib.tv_get_string(tv), buf + end) end) end) describe('string_chk()', function() itp('works', function() local buf = lib.tv_get_string_chk(lua2typvalt(int(1))) - for _, v in ipairs({ + test_string_fn({ {lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, - {lib.VAR_FLOAT, {v_float=42.53}, 'E806: Using a Float as a String', nil}, + {lib.VAR_FLOAT, {v_float=42.53}, nil, '42.53'}, {lib.VAR_PARTIAL, {v_partial=NULL}, 'E729: Using a Funcref as a String', nil}, {lib.VAR_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', nil}, {lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List as a String', nil}, @@ -3039,40 +3061,17 @@ describe('typval.c', function() {lib.VAR_BOOL, {v_bool=lib.kBoolVarTrue}, nil, 'v:true'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil}, - }) do - -- Using to_cstr, cannot free with tv_clear - local tv = ffi.gc(typvalt(v[1], v[2]), nil) - alloc_log:check({}) - local emsg = v[3] - local ret = v[4] - eq(ret, check_emsg(function() - local res = lib.tv_get_string_chk(tv) - if tv.v_type == lib.VAR_NUMBER or tv.v_type == lib.VAR_SPECIAL - or tv.v_type == lib.VAR_BOOL then - eq(buf, res) - else - neq(buf, res) - end - if res ~= nil then - return ffi.string(res) - else - return nil - end - end, emsg)) - if emsg then - alloc_log:clear() - else - alloc_log:check({}) - end - end + }, function(tv) + return lib.tv_get_string_chk(tv), buf + end) end) end) describe('string_buf()', function() itp('works', function() - for _, v in ipairs({ + test_string_fn({ {lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, - {lib.VAR_FLOAT, {v_float=42.53}, 'E806: Using a Float as a String', ''}, + {lib.VAR_FLOAT, {v_float=42.53}, nil, '42.53'}, {lib.VAR_PARTIAL, {v_partial=NULL}, 'E729: Using a Funcref as a String', ''}, {lib.VAR_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', ''}, {lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List as a String', ''}, @@ -3081,41 +3080,18 @@ describe('typval.c', function() {lib.VAR_BOOL, {v_bool=lib.kBoolVarTrue}, nil, 'v:true'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''}, - }) do - -- Using to_cstr, cannot free with tv_clear - local tv = ffi.gc(typvalt(v[1], v[2]), nil) - alloc_log:check({}) - local emsg = v[3] - local ret = v[4] - eq(ret, check_emsg(function() - local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0}) - local res = lib.tv_get_string_buf(tv, buf) - if tv.v_type == lib.VAR_NUMBER or tv.v_type == lib.VAR_SPECIAL - or tv.v_type == lib.VAR_BOOL then - eq(buf, res) - else - neq(buf, res) - end - if res ~= nil then - return ffi.string(res) - else - return nil - end - end, emsg)) - if emsg then - alloc_log:clear() - else - alloc_log:check({}) - end - end + }, function(tv) + local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0}) + return lib.tv_get_string_buf(tv, buf), buf + end) end) end) describe('string_buf_chk()', function() itp('works', function() - for _, v in ipairs({ + test_string_fn({ {lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, - {lib.VAR_FLOAT, {v_float=42.53}, 'E806: Using a Float as a String', nil}, + {lib.VAR_FLOAT, {v_float=42.53}, nil, '42.53'}, {lib.VAR_PARTIAL, {v_partial=NULL}, 'E729: Using a Funcref as a String', nil}, {lib.VAR_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', nil}, {lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List as a String', nil}, @@ -3124,33 +3100,10 @@ describe('typval.c', function() {lib.VAR_BOOL, {v_bool=lib.kBoolVarTrue}, nil, 'v:true'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil}, - }) do - -- Using to_cstr, cannot free with tv_clear - local tv = ffi.gc(typvalt(v[1], v[2]), nil) - alloc_log:check({}) - local emsg = v[3] - local ret = v[4] - eq(ret, check_emsg(function() - local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0}) - local res = lib.tv_get_string_buf_chk(tv, buf) - if tv.v_type == lib.VAR_NUMBER or tv.v_type == lib.VAR_SPECIAL - or tv.v_type == lib.VAR_BOOL then - eq(buf, res) - else - neq(buf, res) - end - if res ~= nil then - return ffi.string(res) - else - return nil - end - end, emsg)) - if emsg then - alloc_log:clear() - else - alloc_log:check({}) - end - end + }, function(tv) + local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0}) + return lib.tv_get_string_buf_chk(tv, buf), buf + end) end) end) end) |