Merge pull request #23995 from zeertzjq/vim-8.2.1524

vim-patch:8.2.{1524,2948,2949}: Float to String conversion
This commit is contained in:
zeertzjq 2023-06-12 14:41:19 +08:00 committed by GitHub
commit 551cc3a2a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 144 additions and 172 deletions

View File

@ -3876,7 +3876,6 @@ static const char *const str_errors[] = {
[VAR_FUNC]= N_(FUNC_ERROR), [VAR_FUNC]= N_(FUNC_ERROR),
[VAR_LIST]= N_("E730: Using a List as a String"), [VAR_LIST]= N_("E730: Using a List as a String"),
[VAR_DICT]= N_("E731: Using a Dictionary 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_BLOB]= N_("E976: Using a Blob as a String"),
[VAR_UNKNOWN]= e_using_invalid_value_as_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_BOOL:
case VAR_SPECIAL: case VAR_SPECIAL:
case VAR_STRING: case VAR_STRING:
case VAR_FLOAT:
return true; return true;
case VAR_PARTIAL: case VAR_PARTIAL:
case VAR_FUNC: case VAR_FUNC:
case VAR_LIST: case VAR_LIST:
case VAR_DICT: case VAR_DICT:
case VAR_FLOAT:
case VAR_BLOB: case VAR_BLOB:
case VAR_UNKNOWN: case VAR_UNKNOWN:
emsg(_(str_errors[tv->v_type])); 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: case VAR_NUMBER:
snprintf(buf, NUMBUFLEN, "%" PRIdVARNUMBER, tv->vval.v_number); // -V576 snprintf(buf, NUMBUFLEN, "%" PRIdVARNUMBER, tv->vval.v_number); // -V576
return buf; return buf;
case VAR_FLOAT:
vim_snprintf(buf, NUMBUFLEN, "%g", tv->vval.v_float);
return buf;
case VAR_STRING: case VAR_STRING:
if (tv->vval.v_string != NULL) { if (tv->vval.v_string != NULL) {
return tv->vval.v_string; 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_FUNC:
case VAR_LIST: case VAR_LIST:
case VAR_DICT: case VAR_DICT:
case VAR_FLOAT:
case VAR_BLOB: case VAR_BLOB:
case VAR_UNKNOWN: case VAR_UNKNOWN:
emsg(_(str_errors[tv->v_type])); emsg(_(str_errors[tv->v_type]));

View File

@ -1,16 +1,10 @@
-- Tests for signs
local helpers = require('test.functional.helpers')(after_each) 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 local eq, eval = helpers.eq, helpers.eval
describe('glob2regpat()', function() describe('glob2regpat()', function()
before_each(clear) 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() it('returns ^$ for empty input', function()
eq('^$', eval("glob2regpat('')")) eq('^$', eval("glob2regpat('')"))
end) end)

View File

@ -93,14 +93,10 @@ describe('execute()', function()
it('captures errors', function() it('captures errors', function()
local ret 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)') ret = exc_exec('call execute(v:_null_dict)')
eq('Vim(call):E731: Using a Dictionary as a String', ret) eq('Vim(call):E731: Using a Dictionary as a String', ret)
ret = exc_exec('call execute(function("tr"))') ret = exc_exec('call execute(function("tr"))')
eq('Vim(call):E729: Using a Funcref as a String', ret) 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"])') ret = exc_exec('call execute(["echo 42", v:_null_dict, "echo 44"])')
eq('Vim:E731: Using a Dictionary as a String', ret) eq('Vim:E731: Using a Dictionary as a String', ret)
ret = exc_exec('call execute(["echo 42", function("tr"), "echo 44"])') 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() it('propagates errors for "" and "silent"', function()
local ret 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")') ret = exc_exec('call execute(v:_null_dict, "silent")')
eq('Vim(call):E731: Using a Dictionary as a String', ret) eq('Vim(call):E731: Using a Dictionary as a String', ret)

View File

@ -145,8 +145,6 @@ describe('writefile()', function()
pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname))) pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname)))
end end
for _, args in ipairs({'[], %s, "b"', '[], "' .. fname .. '", %s'}) do 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', eq('Vim(call):E730: Using a List as a String',
pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))) pcall_err(command, ('call writefile(%s)'):format(args:format('[]'))))
eq('Vim(call):E731: Using a Dictionary as a String', eq('Vim(call):E731: Using a Dictionary as a String',

View File

@ -230,6 +230,12 @@ func Test_string_concatenation()
let a = 'a' let a = 'a'
let a..=b let a..=b
call assert_equal('ab', a) call assert_equal('ab', a)
if has('float')
let a = 'A'
let b = 1.234
call assert_equal('A1.234', a .. b)
endif
endfunc endfunc
" Test fix for issue #4507 " Test fix for issue #4507

View File

@ -1,6 +1,8 @@
" test execute() " test execute()
source view_util.vim source view_util.vim
source check.vim
source vim9.vim
func NestedEval() func NestedEval()
let nested = execute('echo "nested\nlines"') let nested = execute('echo "nested\nlines"')
@ -31,7 +33,6 @@ func Test_execute_string()
call assert_equal("\nthat", evaled) call assert_equal("\nthat", evaled)
call assert_fails('call execute("doesnotexist")', 'E492:') call assert_fails('call execute("doesnotexist")', 'E492:')
call assert_fails('call execute(3.4)', 'E806:')
" Nvim supports execute('... :redir ...'), so this test is intentionally " Nvim supports execute('... :redir ...'), so this test is intentionally
" disabled. " disabled.
" call assert_fails('call execute("call NestedRedir()")', 'E930:') " 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("\nsomething", execute('echo "something"', 'silent!')) call assert_equal("\nsomething", execute('echo "something"', 'silent!'))
call assert_equal("", execute('burp', '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 endfunc
func Test_execute_list() func Test_execute_list()

View File

@ -2,6 +2,7 @@
source check.vim source check.vim
CheckFeature float CheckFeature float
source vim9.vim
func Test_abs() func Test_abs()
call assert_equal('1.23', string(abs(1.23))) 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_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([])", 'E730:')
call assert_fails("call str2float({})", 'E731:') call assert_fails("call str2float({})", 'E731:')
call assert_fails("call str2float(function('string'))", 'E729:') call assert_fails("call str2float(function('string'))", 'E729:')

View File

@ -153,11 +153,13 @@ func Test_strwidth()
call assert_fails('call strwidth({->0})', 'E729:') call assert_fails('call strwidth({->0})', 'E729:')
call assert_fails('call strwidth([])', 'E730:') call assert_fails('call strwidth([])', 'E730:')
call assert_fails('call strwidth({})', 'E731:') call assert_fails('call strwidth({})', 'E731:')
if has('float')
call assert_fails('call strwidth(1.2)', 'E806:')
endif
endfor endfor
if has('float')
call assert_equal(3, strwidth(1.2))
call CheckDefExecAndScriptFailure(['echo strwidth(1.2)'], 'E806:')
endif
set ambiwidth& set ambiwidth&
endfunc endfunc
@ -221,7 +223,9 @@ func Test_str2nr()
call assert_fails('call str2nr([])', 'E730:') call assert_fails('call str2nr([])', 'E730:')
call assert_fails('call str2nr({->2})', 'E729:') call assert_fails('call str2nr({->2})', 'E729:')
if has('float') 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 endif
call assert_fails('call str2nr(10, [])', 'E745:') call assert_fails('call str2nr(10, [])', 'E745:')
endfunc endfunc
@ -372,7 +376,8 @@ func Test_simplify()
call assert_fails('call simplify([])', 'E730:') call assert_fails('call simplify([])', 'E730:')
call assert_fails('call simplify({})', 'E731:') call assert_fails('call simplify({})', 'E731:')
if has('float') 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 endif
endfunc endfunc

View File

@ -1,8 +1,11 @@
" Test glob2regpat() " Test glob2regpat()
source vim9.vim
func Test_glob2regpat_invalid() func Test_glob2regpat_invalid()
if has('float') 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 endif
call assert_fails('call glob2regpat("}")', 'E219:') call assert_fails('call glob2regpat("}")', 'E219:')
call assert_fails('call glob2regpat("{")', 'E220:') call assert_fails('call glob2regpat("{")', 'E220:')

View File

@ -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'}, 'error')", 'E737:')
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
if has('float') 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 endif
call assert_equal({'a': 'A', 'b': 'B'}, d) 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:') call assert_fails("let l = insert([1,2,3], 4, [])", 'E745:')
let l = [1, 2, 3] let l = [1, 2, 3]
call assert_fails("let l[i] = 3", 'E121:') 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[: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() " Nvim doesn't have test_unknown()
" let t = test_unknown() " let t = test_unknown()
" call assert_fails("echo t[0]", 'E685:') " call assert_fails("echo t[0]", 'E685:')

View File

@ -457,7 +457,14 @@ func Test_substitute_partial()
" 20 arguments plus one is too many " 20 arguments plus one is too many
let Replacer = function('SubReplacer20', repeat(['foo'], 20)) 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 endfunc
" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. " Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.

View File

@ -2,6 +2,10 @@
" Use a different file name for each run. " Use a different file name for each run.
let s:sequence = 1 let s:sequence = 1
func CheckDefExecFailure(lines, error, lnum = -3)
return
endfunc
func CheckScriptFailure(lines, error, lnum = -3) func CheckScriptFailure(lines, error, lnum = -3)
if get(a:lines, 0, '') ==# 'vim9script' if get(a:lines, 0, '') ==# 'vim9script'
return return

View File

@ -101,12 +101,13 @@ local function check_emsg(f, msg)
saved_last_msg_hist = nil saved_last_msg_hist = nil
end end
local ret = {f()} 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 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) neq(saved_last_msg_hist, lib.last_msg_hist)
else else
if saved_last_msg_hist ~= lib.last_msg_hist then if saved_last_msg_hist ~= lib.last_msg_hist then
eq(nil, ffi.string(lib.last_msg_hist.msg)) eq(nil, last_msg)
else else
eq(saved_last_msg_hist, lib.last_msg_hist) eq(saved_last_msg_hist, lib.last_msg_hist)
end end
@ -1429,15 +1430,16 @@ describe('typval.c', function()
end end
describe('str()', function() describe('str()', function()
itp('returns correct string', 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() alloc_log:clear()
eq('1', tv_list_find_str(l, -5)) 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('5', tv_list_find_str(l, 4))
eq('3', tv_list_find_str(l, 2)) eq('3', tv_list_find_str(l, 2))
eq('3', tv_list_find_str(l, -3)) eq('3', tv_list_find_str(l, -3))
alloc_log:check({}) alloc_log:check({a.freed(alloc_log.null)})
end) end)
itp('returns string when used with VAR_STRING items', function() itp('returns string when used with VAR_STRING items', function()
local l = list('1', '2', '3', '4', '5') 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')) eq(nil, tv_list_find_str(l, 5, 'E684: List index out of range: 5'))
end) end)
itp('fails with error message on invalid types', function() 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, 0, 'E730: Using a List as a String'))
eq('', tv_list_find_str(l, 1, '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, 2, '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, -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, -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) end)
end) end)
@ -1765,18 +1765,24 @@ describe('typval.c', function()
neq(s42, s43) neq(s42, s43)
eq(s43, dis.te.di_tv.vval.v_string) eq(s43, dis.te.di_tv.vval.v_string)
alloc_log:check({}) alloc_log:check({})
eq('', ffi.string(check_emsg(function() return lib.tv_dict_get_string(d, 't', false) end, local s44 = check_emsg(function() return lib.tv_dict_get_string(d, 't', false) end,
'E806: Using a Float as a String'))) nil)
eq('44.0', ffi.string(s44))
alloc_log:check({a.freed(alloc_log.null)})
end) end)
itp('allocates a string copy when requested', function() 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() alloc_log:clear()
local ret = check_emsg(function() return lib.tv_dict_get_string(d, key, true) end, local ret = check_emsg(function() return lib.tv_dict_get_string(d, key, true) end,
emsg) emsg)
local s_ret = (ret ~= nil) and ffi.string(ret) or nil local s_ret = (ret ~= nil) and ffi.string(ret) or nil
if not emsg then if not emsg then
if s_ret 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 else
alloc_log:check({}) alloc_log:check({})
end end
@ -1792,18 +1798,22 @@ describe('typval.c', function()
eq('42', tv_dict_get_string_alloc(d, 'tes')) eq('42', tv_dict_get_string_alloc(d, 'tes'))
eq('45', tv_dict_get_string_alloc(d, 'xx')) eq('45', tv_dict_get_string_alloc(d, 'xx'))
eq('43', tv_dict_get_string_alloc(d, 'te')) 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)
end) end)
describe('get_string_buf()', function() 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) buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
alloc_log:clear() alloc_log:clear()
local ret = check_emsg(function() return lib.tv_dict_get_string_buf(d, key, buf) end, local ret = check_emsg(function() return lib.tv_dict_get_string_buf(d, key, buf) end,
emsg) emsg)
local s_ret = (ret ~= nil) and ffi.string(ret) or nil local s_ret = (ret ~= nil) and ffi.string(ret) or nil
if not emsg then 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 end
return s_ret, ret, buf return s_ret, ret, buf
end end
@ -1827,16 +1837,16 @@ describe('typval.c', function()
s, r, b = tv_dict_get_string_buf(d, 'test') s, r, b = tv_dict_get_string_buf(d, 'test')
neq(r, b) neq(r, b)
eq('tset', s) eq('tset', s)
s, r, b = tv_dict_get_string_buf(d, 't', nil, 'E806: Using a Float as a String') s, r, b = tv_dict_get_string_buf(d, 't', true)
neq(r, b) eq(r, b)
eq('', s) eq('1.0', s)
s, r, b = tv_dict_get_string_buf(d, 'te') s, r, b = tv_dict_get_string_buf(d, 'te')
eq(r, b) eq(r, b)
eq('2', s) eq('2', s)
end) end)
end) end)
describe('get_string_buf_chk()', function() 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) buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
def = def or ffi.gc(lib.xstrdup('DEFAULT'), lib.xfree) def = def or ffi.gc(lib.xstrdup('DEFAULT'), lib.xfree)
len = len or #key len = len or #key
@ -1845,7 +1855,11 @@ describe('typval.c', function()
emsg) emsg)
local s_ret = (ret ~= nil) and ffi.string(ret) or nil local s_ret = (ret ~= nil) and ffi.string(ret) or nil
if not emsg then 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 end
return s_ret, ret, buf, def return s_ret, ret, buf, def
end end
@ -1870,10 +1884,10 @@ describe('typval.c', function()
neq(r, b) neq(r, b)
neq(r, def) neq(r, def)
eq('tset', s) 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') s, r, b, def = tv_dict_get_string_buf_chk(d, 'test', true, 1)
neq(r, b) eq(r, b)
neq(r, def) neq(r, def)
eq(nil, s) eq('1.0', s)
s, r, b, def = tv_dict_get_string_buf_chk(d, 'te') s, r, b, def = tv_dict_get_string_buf_chk(d, 'te')
eq(r, b) eq(r, b)
neq(r, def) neq(r, def)
@ -2831,7 +2845,7 @@ describe('typval.c', function()
alloc_log:clear() alloc_log:clear()
for _, v in ipairs({ for _, v in ipairs({
{lib.VAR_NUMBER, nil}, {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_PARTIAL, 'E729: Using a Funcref as a String'},
{lib.VAR_FUNC, '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'}, {lib.VAR_LIST, 'E730: Using a List as a String'},
@ -2978,15 +2992,47 @@ describe('typval.c', function()
end end
end) 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() describe('string()', function()
itp('works', function() itp('works', function()
local buf = lib.tv_get_string(lua2typvalt(int(1))) local buf = lib.tv_get_string(lua2typvalt(int(1)))
local buf_chk = lib.tv_get_string_chk(lua2typvalt(int(1))) local buf_chk = lib.tv_get_string_chk(lua2typvalt(int(1)))
neq(buf, buf_chk) neq(buf, buf_chk)
for _, v in ipairs({ test_string_fn({
{lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_NUMBER, {v_number=42}, nil, '42'},
{lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, {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_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_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', ''},
{lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List 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.kBoolVarTrue}, nil, 'v:true'},
{lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'},
{lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''},
}) do }, function(tv)
-- Using to_cstr in place of Neovim allocated string, cannot return lib.tv_get_string(tv), buf
-- tv_clear() that. end)
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
end) end)
end) end)
describe('string_chk()', function() describe('string_chk()', function()
itp('works', function() itp('works', function()
local buf = lib.tv_get_string_chk(lua2typvalt(int(1))) 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_NUMBER, {v_number=42}, nil, '42'},
{lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, {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_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_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}, {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.kBoolVarTrue}, nil, 'v:true'},
{lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'},
{lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil},
}) do }, function(tv)
-- Using to_cstr, cannot free with tv_clear return lib.tv_get_string_chk(tv), buf
local tv = ffi.gc(typvalt(v[1], v[2]), nil) end)
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
end) end)
end) end)
describe('string_buf()', function() describe('string_buf()', function()
itp('works', function() itp('works', function()
for _, v in ipairs({ test_string_fn({
{lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_NUMBER, {v_number=42}, nil, '42'},
{lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, {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_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_FUNC, {v_string=NULL}, 'E729: Using a Funcref as a String', ''},
{lib.VAR_LIST, {v_list=NULL}, 'E730: Using a List 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.kBoolVarTrue}, nil, 'v:true'},
{lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'},
{lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', ''},
}) do }, function(tv)
-- Using to_cstr, cannot free with tv_clear local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0})
local tv = ffi.gc(typvalt(v[1], v[2]), nil) return lib.tv_get_string_buf(tv, buf), buf
alloc_log:check({}) end)
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
end) end)
end) end)
describe('string_buf_chk()', function() describe('string_buf_chk()', function()
itp('works', function() itp('works', function()
for _, v in ipairs({ test_string_fn({
{lib.VAR_NUMBER, {v_number=42}, nil, '42'}, {lib.VAR_NUMBER, {v_number=42}, nil, '42'},
{lib.VAR_STRING, {v_string=to_cstr('100500')}, nil, '100500'}, {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_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_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}, {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.kBoolVarTrue}, nil, 'v:true'},
{lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'}, {lib.VAR_BOOL, {v_bool=lib.kBoolVarFalse}, nil, 'v:false'},
{lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil}, {lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil},
}) do }, function(tv)
-- Using to_cstr, cannot free with tv_clear local buf = ffi.new('char[?]', lib.NUMBUFLEN, {0})
local tv = ffi.gc(typvalt(v[1], v[2]), nil) return lib.tv_get_string_buf_chk(tv, buf), buf
alloc_log:check({}) end)
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
end) end)
end) end)
end) end)