0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.0502: Vim9: some code is not tested

Problem:    Vim9: some code is not tested.
Solution:   Add more tests.  Fix uncovered problems.
This commit is contained in:
Bram Moolenaar
2020-04-02 21:13:25 +02:00
parent 2c869deeb7
commit e8c4abbbd7
16 changed files with 106 additions and 34 deletions

View File

@@ -1389,7 +1389,7 @@ set_one_cmd_context(
if (*arg != NUL) if (*arg != NUL)
{ {
xp->xp_context = EXPAND_NOTHING; xp->xp_context = EXPAND_NOTHING;
arg = skip_regexp(arg + 1, *arg, p_magic, NULL); arg = skip_regexp(arg + 1, *arg, p_magic);
} }
} }
return find_nextcmd(arg); return find_nextcmd(arg);
@@ -1427,7 +1427,7 @@ set_one_cmd_context(
{ {
// skip "from" part // skip "from" part
++arg; ++arg;
arg = skip_regexp(arg, delim, p_magic, NULL); arg = skip_regexp(arg, delim, p_magic);
} }
// skip "to" part // skip "to" part
while (arg[0] != NUL && arg[0] != delim) while (arg[0] != NUL && arg[0] != delim)

View File

@@ -451,7 +451,7 @@ ex_sort(exarg_T *eap)
} }
else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
{ {
s = skip_regexp(p + 1, *p, TRUE, NULL); s = skip_regexp(p + 1, *p, TRUE);
if (*s != *p) if (*s != *p)
{ {
emsg(_(e_invalpat)); emsg(_(e_invalpat));
@@ -3626,7 +3626,7 @@ do_sub(exarg_T *eap)
which_pat = RE_LAST; // use last used regexp which_pat = RE_LAST; // use last used regexp
delimiter = *cmd++; // remember delimiter character delimiter = *cmd++; // remember delimiter character
pat = cmd; // remember start of search pat pat = cmd; // remember start of search pat
cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); cmd = skip_regexp_ex(cmd, delimiter, p_magic, &eap->arg, NULL);
if (cmd[0] == delimiter) // end delimiter found if (cmd[0] == delimiter) // end delimiter found
*cmd++ = NUL; // replace it with a NUL *cmd++ = NUL; // replace it with a NUL
} }
@@ -4801,7 +4801,7 @@ ex_global(exarg_T *eap)
if (delim) if (delim)
++cmd; // skip delimiter if there is one ++cmd; // skip delimiter if there is one
pat = cmd; // remember start of pattern pat = cmd; // remember start of pattern
cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); cmd = skip_regexp_ex(cmd, delim, p_magic, &eap->arg, NULL);
if (cmd[0] == delim) // end delimiter found if (cmd[0] == delim) // end delimiter found
*cmd++ = NUL; // replace it with a NUL *cmd++ = NUL; // replace it with a NUL
} }
@@ -6441,7 +6441,7 @@ skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
if (s != NULL) if (s != NULL)
*s = p + 1; *s = p + 1;
c = *p; c = *p;
p = skip_regexp(p + 1, c, TRUE, NULL); p = skip_regexp(p + 1, c, TRUE);
if (*p != c) if (*p != c)
return NULL; return NULL;

View File

@@ -3663,7 +3663,7 @@ get_address(
} }
if (skip) // skip "/pat/" if (skip) // skip "/pat/"
{ {
cmd = skip_regexp(cmd, c, (int)p_magic, NULL); cmd = skip_regexp(cmd, c, (int)p_magic);
if (*cmd == c) if (*cmd == c)
++cmd; ++cmd;
} }
@@ -6123,7 +6123,7 @@ ex_open(exarg_T *eap)
{ {
// ":open /pattern/": put cursor in column found with pattern // ":open /pattern/": put cursor in column found with pattern
++eap->arg; ++eap->arg;
p = skip_regexp(eap->arg, '/', p_magic, NULL); p = skip_regexp(eap->arg, '/', p_magic);
*p = NUL; *p = NUL;
regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0); regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
if (regmatch.regprog != NULL) if (regmatch.regprog != NULL)
@@ -7857,7 +7857,7 @@ ex_findpat(exarg_T *eap)
{ {
whole = FALSE; whole = FALSE;
++eap->arg; ++eap->arg;
p = skip_regexp(eap->arg, '/', p_magic, NULL); p = skip_regexp(eap->arg, '/', p_magic);
if (*p) if (*p)
{ {
*p++ = NUL; *p++ = NUL;

View File

@@ -1527,7 +1527,7 @@ ex_catch(exarg_T *eap)
else else
{ {
pat = eap->arg + 1; pat = eap->arg + 1;
end = skip_regexp(pat, *eap->arg, TRUE, NULL); end = skip_regexp(pat, *eap->arg, TRUE);
} }
if (!give_up) if (!give_up)

View File

@@ -277,7 +277,7 @@ do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *is_s
p = skipwhite(p); p = skipwhite(p);
delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++; delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
*search_delim = delim; *search_delim = delim;
end = skip_regexp(p, delim, p_magic, NULL); end = skip_regexp(p, delim, p_magic);
use_last_pat = end == p && *end == delim; use_last_pat = end == p && *end == delim;

View File

@@ -4964,7 +4964,7 @@ ex_match(exarg_T *eap)
semsg(_(e_invarg2), eap->arg); semsg(_(e_invarg2), eap->arg);
return; return;
} }
end = skip_regexp(p + 1, *p, TRUE, NULL); end = skip_regexp(p + 1, *p, TRUE);
if (!eap->skip) if (!eap->skip)
{ {
if (*end != NUL && !ends_excmd(*skipwhite(end + 1))) if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))

View File

@@ -1,6 +1,7 @@
/* regexp.c */ /* regexp.c */
int re_multiline(regprog_T *prog); int re_multiline(regprog_T *prog);
char_u *skip_regexp(char_u *startp, int dirc, int magic, char_u **newp); char_u *skip_regexp(char_u *startp, int dirc, int magic);
char_u *skip_regexp_ex(char_u *startp, int dirc, int magic, char_u **newp, int *dropped);
reg_extmatch_T *ref_extmatch(reg_extmatch_T *em); reg_extmatch_T *ref_extmatch(reg_extmatch_T *em);
void unref_extmatch(reg_extmatch_T *em); void unref_extmatch(reg_extmatch_T *em);
char_u *regtilde(char_u *source, int magic); char_u *regtilde(char_u *source, int magic);

View File

@@ -537,16 +537,30 @@ skip_anyof(char_u *p)
* Stop at end of "startp" or where "dirc" is found ('/', '?', etc). * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
* Take care of characters with a backslash in front of it. * Take care of characters with a backslash in front of it.
* Skip strings inside [ and ]. * Skip strings inside [ and ].
* When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
* expression and change "\?" to "?". If "*newp" is not NULL the expression
* is changed in-place.
*/ */
char_u * char_u *
skip_regexp( skip_regexp(
char_u *startp,
int dirc,
int magic)
{
return skip_regexp_ex(startp, dirc, magic, NULL, NULL);
}
/*
* skip_regexp() with extra arguments:
* When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
* expression and change "\?" to "?". If "*newp" is not NULL the expression
* is changed in-place.
* If a "\?" is changed to "?" then "dropped" is incremented, unless NULL.
*/
char_u *
skip_regexp_ex(
char_u *startp, char_u *startp,
int dirc, int dirc,
int magic, int magic,
char_u **newp) char_u **newp,
int *dropped)
{ {
int mymagic; int mymagic;
char_u *p = startp; char_u *p = startp;
@@ -579,6 +593,8 @@ skip_regexp(
if (*newp != NULL) if (*newp != NULL)
p = *newp + (p - startp); p = *newp + (p - startp);
} }
if (dropped != NULL)
++*dropped;
if (*newp != NULL) if (*newp != NULL)
STRMOVE(p, p + 1); STRMOVE(p, p + 1);
else else

View File

@@ -1312,7 +1312,7 @@ do_search(
* If there is a matching '/' or '?', toss it. * If there is a matching '/' or '?', toss it.
*/ */
ps = strcopy; ps = strcopy;
p = skip_regexp(pat, search_delim, (int)p_magic, &strcopy); p = skip_regexp_ex(pat, search_delim, (int)p_magic, &strcopy, NULL);
if (strcopy != ps) if (strcopy != ps)
{ {
// made a copy of "pat" to change "\?" to "?" // made a copy of "pat" to change "\?" to "?"

View File

@@ -5598,7 +5598,7 @@ get_syn_pattern(char_u *arg, synpat_T *ci)
if (arg == NULL || arg[0] == NUL || arg[1] == NUL || arg[2] == NUL) if (arg == NULL || arg[0] == NUL || arg[1] == NUL || arg[2] == NUL)
return NULL; return NULL;
end = skip_regexp(arg + 1, *arg, TRUE, NULL); end = skip_regexp(arg + 1, *arg, TRUE);
if (*end != *arg) // end delimiter not found if (*end != *arg) // end delimiter not found
{ {
semsg(_("E401: Pattern delimiter not found: %s"), arg); semsg(_("E401: Pattern delimiter not found: %s"), arg);
@@ -5775,7 +5775,7 @@ syn_cmd_sync(exarg_T *eap, int syncing UNUSED)
finished = TRUE; finished = TRUE;
break; break;
} }
arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE, NULL); arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE);
if (*arg_end != *next_arg) // end delimiter not found if (*arg_end != *next_arg) // end delimiter not found
{ {
illegal = TRUE; illegal = TRUE;

View File

@@ -3530,7 +3530,7 @@ jumpto_tag(
*/ */
str = pbuf; str = pbuf;
if (pbuf[0] == '/' || pbuf[0] == '?') if (pbuf[0] == '/' || pbuf[0] == '?')
str = skip_regexp(pbuf + 1, pbuf[0], FALSE, NULL) + 1; str = skip_regexp(pbuf + 1, pbuf[0], FALSE) + 1;
if (str > pbuf_end - 1) // search command with nothing following if (str > pbuf_end - 1) // search command with nothing following
{ {
save_p_ws = p_ws; save_p_ws = p_ws;
@@ -3820,7 +3820,7 @@ find_extra(char_u **pp)
str = skipdigits(str); str = skipdigits(str);
else if (*str == '/' || *str == '?') else if (*str == '/' || *str == '?')
{ {
str = skip_regexp(str + 1, *str, FALSE, NULL); str = skip_regexp(str + 1, *str, FALSE);
if (*str != first_char) if (*str != first_char)
str = NULL; str = NULL;
else else

View File

@@ -809,6 +809,8 @@ def Test_disassemble_compare_const()
let cases = [ let cases = [
\ ['"xx" == "yy"', false], \ ['"xx" == "yy"', false],
\ ['"aa" == "aa"', true], \ ['"aa" == "aa"', true],
\ ['has("eval") ? true : false', true],
\ ['has("asdf") ? true : false', false],
\ ] \ ]
let nr = 1 let nr = 1

View File

@@ -468,12 +468,20 @@ def Test_try_catch_match()
seq ..= 'b' seq ..= 'b'
catch /asdf/ catch /asdf/
seq ..= 'x' seq ..= 'x'
catch ?a\?sdf?
seq ..= 'y'
finally finally
seq ..= 'c' seq ..= 'c'
endtry endtry
assert_equal('abc', seq) assert_equal('abc', seq)
enddef enddef
def Test_try_catch_fails()
call CheckDefFailure(['catch'], 'E603:')
call CheckDefFailure(['try', 'echo 0', 'catch','catch'], 'E1033:')
call CheckDefFailure(['try', 'echo 0', 'catch /pat'], 'E1067:')
enddef
let s:export_script_lines =<< trim END let s:export_script_lines =<< trim END
vim9script vim9script
let name: string = 'bob' let name: string = 'bob'
@@ -926,6 +934,13 @@ def Test_if_elseif_else()
assert_equal('three', IfElse(3)) assert_equal('three', IfElse(3))
enddef enddef
def Test_if_elseif_else_fails()
call CheckDefFailure(['elseif true'], 'E582:')
call CheckDefFailure(['else'], 'E581:')
call CheckDefFailure(['endif'], 'E580:')
call CheckDefFailure(['if true', 'elseif xxx'], 'E1001:')
enddef
let g:bool_true = v:true let g:bool_true = v:true
let g:bool_false = v:false let g:bool_false = v:false
@@ -972,6 +987,12 @@ def Test_if_const_expr()
endif endif
assert_equal(false, res) assert_equal(false, res)
res = false
if has('xyz') ? true : false
res = true
endif
assert_equal(false, res)
res = false res = false
if true && true if true && true
res = true res = true
@@ -1030,6 +1051,8 @@ enddef
def Test_if_const_expr_fails() def Test_if_const_expr_fails()
call CheckDefFailure(['if "aaa" == "bbb'], 'E114:') call CheckDefFailure(['if "aaa" == "bbb'], 'E114:')
call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:')
call CheckDefFailure(["if has('aaa'"], 'E110:')
call CheckDefFailure(["if has('aaa') ? true false"], 'E109:')
enddef enddef
def Test_delfunc() def Test_delfunc()
@@ -1096,6 +1119,30 @@ def Test_for_outside_of_function()
delete('Xvim9for.vim') delete('Xvim9for.vim')
enddef enddef
def Test_for_loop()
let result = ''
for cnt in range(7)
if cnt == 4
break
endif
if cnt == 2
continue
endif
result ..= cnt .. '_'
endfor
assert_equal('0_1_3_', result)
enddef
def Test_for_loop_fails()
call CheckDefFailure(['for # in range(5)'], 'E690:')
call CheckDefFailure(['for i In range(5)'], 'E690:')
call CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:')
call CheckScriptFailure(['def Func(arg)', 'for arg in range(5)', 'enddef'], 'E1006:')
call CheckDefFailure(['for i in "text"'], 'E1024:')
call CheckDefFailure(['for i in xxx'], 'E1001:')
call CheckDefFailure(['endfor'], 'E588:')
enddef
def Test_while_loop() def Test_while_loop()
let result = '' let result = ''
let cnt = 0 let cnt = 0
@@ -1112,12 +1159,13 @@ def Test_while_loop()
assert_equal('1_3_', result) assert_equal('1_3_', result)
enddef enddef
def Test_for_loop_fails() def Test_while_loop_fails()
call CheckDefFailure(['for # in range(5)'], 'E690:') call CheckDefFailure(['while xxx'], 'E1001:')
call CheckDefFailure(['for i In range(5)'], 'E690:') call CheckDefFailure(['endwhile'], 'E588:')
call CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:') call CheckDefFailure(['continue'], 'E586:')
call CheckScriptFailure(['def Func(arg)', 'for arg in range(5)', 'enddef'], 'E1006:') call CheckDefFailure(['if true', 'continue'], 'E586:')
call CheckDefFailure(['for i in "text"'], 'E1024:') call CheckDefFailure(['break'], 'E587:')
call CheckDefFailure(['if true', 'break'], 'E587:')
enddef enddef
def Test_interrupt_loop() def Test_interrupt_loop()

View File

@@ -2330,7 +2330,7 @@ ex_function(exarg_T *eap)
*/ */
if (*eap->arg == '/') if (*eap->arg == '/')
{ {
p = skip_regexp(eap->arg + 1, '/', TRUE, NULL); p = skip_regexp(eap->arg + 1, '/', TRUE);
if (!eap->skip) if (!eap->skip)
{ {
regmatch_T regmatch; regmatch_T regmatch;

View File

@@ -738,6 +738,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
502,
/**/ /**/
501, 501,
/**/ /**/

View File

@@ -4095,7 +4095,7 @@ evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
*arg = skipwhite(*arg); *arg = skipwhite(*arg);
if (**arg != ')') if (**arg != ')')
return FAIL; return FAIL;
*arg = skipwhite(*arg + 1); *arg = *arg + 1;
argvars[0] = *tv; argvars[0] = *tv;
argvars[1].v_type = VAR_UNKNOWN; argvars[1].v_type = VAR_UNKNOWN;
@@ -4269,6 +4269,7 @@ evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
int val = tv2bool(tv); int val = tv2bool(tv);
typval_T tv2; typval_T tv2;
// require space before and after the ?
if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
return FAIL; return FAIL;
@@ -4553,6 +4554,7 @@ compile_for(char_u *arg, cctx_T *cctx)
loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
if (loop_idx < 0) if (loop_idx < 0)
{ {
// only happens when out of memory
drop_scope(cctx); drop_scope(cctx);
return NULL; return NULL;
} }
@@ -4899,12 +4901,13 @@ compile_catch(char_u *arg, cctx_T *cctx UNUSED)
char_u *end; char_u *end;
char_u *pat; char_u *pat;
char_u *tofree = NULL; char_u *tofree = NULL;
int dropped = 0;
int len; int len;
// Push v:exception, push {expr} and MATCH // Push v:exception, push {expr} and MATCH
generate_instr_type(cctx, ISN_PUSHEXC, &t_string); generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
end = skip_regexp(p + 1, *p, TRUE, &tofree); end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
if (*end != *p) if (*end != *p)
{ {
semsg(_("E1067: Separator mismatch: %s"), p); semsg(_("E1067: Separator mismatch: %s"), p);
@@ -4914,10 +4917,10 @@ compile_catch(char_u *arg, cctx_T *cctx UNUSED)
if (tofree == NULL) if (tofree == NULL)
len = (int)(end - (p + 1)); len = (int)(end - (p + 1));
else else
len = (int)(end - (tofree + 1)); len = (int)(end - tofree);
pat = vim_strnsave(p + 1, len); pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
vim_free(tofree); vim_free(tofree);
p += len + 2; p += len + 2 + dropped;
if (pat == NULL) if (pat == NULL)
return FAIL; return FAIL;
if (generate_PUSHS(cctx, pat) == FAIL) if (generate_PUSHS(cctx, pat) == FAIL)