Merge pull request #20941 from zeertzjq/vim-8.2.1183

vim-patch:8.2.{partial:1183,1184,1199,1479,1484,1631,1632}: assert_fails() patches
This commit is contained in:
zeertzjq
2022-11-05 14:17:06 +08:00
committed by GitHub
41 changed files with 275 additions and 126 deletions

View File

@@ -98,18 +98,46 @@ assert_exception({error} [, {msg}]) *assert_exception()*
catch
call assert_exception('E492:')
endtry
assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
<
*assert_fails()*
assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Run {cmd} and add an error message to |v:errors| if it does
NOT produce an error. Also see |assert-return|.
When {error} is given it must match in |v:errmsg|.
NOT produce an error or when {error} is not found in the
error message. Also see |assert-return|.
When {error} is a string it must be found literally in the
first reported error. Most often this will be the error code,
including the colon, e.g. "E123:". >
assert_fails('bad cmd', 'E987:')
<
When {error} is a |List| with one or two strings, these are
used as patterns. The first pattern is matched against the
first reported error: >
assert_fails('cmd', ['E987:.*expected bool'])
< The second pattern, if present, is matched against the last
reported error. To only match the last error use an empty
string for the first error: >
assert_fails('cmd', ['', 'E987:'])
<
If {msg} is empty then it is not used. Do this to get the
default message when passing the {lnum} argument.
When {lnum} is present and not negative, and the {error}
argument is present and matches, then this is compared with
the line number at which the error was reported. That can be
the line number in a function or in a script.
When {context} is present it is used as a pattern and matched
against the context (script name or function name) where
{lnum} is located in.
Note that beeping is not considered an error, and some failing
commands only beep. Use |assert_beeps()| for those.
Can also be used as a |method|: >
GetCmd()->assert_fails('E99:')
assert_false({actual} [, {msg}]) *assert_false()*
assert_false({actual} [, {msg}]) *assert_false()*
When {actual} is not false an error message is added to
|v:errors|, like with |assert_equal()|.
Also see |assert-return|.

View File

@@ -545,7 +545,7 @@ int var_redir_start(char *name, int append)
// check if we can write to the variable: set it to or append an empty
// string
int save_emsg = did_emsg;
const int called_emsg_before = called_emsg;
did_emsg = false;
typval_T tv;
tv.v_type = VAR_STRING;
@@ -556,9 +556,7 @@ int var_redir_start(char *name, int append)
set_var_lval(redir_lval, redir_endp, &tv, true, false, "=");
}
clear_lval(redir_lval);
int err = did_emsg;
did_emsg |= save_emsg;
if (err) {
if (called_emsg > called_emsg_before) {
redir_endp = NULL; // don't store a value, only cleanup
var_redir_stop();
return FAIL;
@@ -2185,7 +2183,7 @@ char *get_user_var_name(expand_T *xp, int idx)
/// Does not use 'cpo' and always uses 'magic'.
///
/// @return true if "pat" matches "text".
int pattern_match(char *pat, char *text, bool ic)
int pattern_match(const char *pat, const char *text, bool ic)
{
int matches = 0;
regmatch_T regmatch;
@@ -2193,7 +2191,7 @@ int pattern_match(char *pat, char *text, bool ic)
// avoid 'l' flag in 'cpoptions'
char *save_cpo = p_cpo;
p_cpo = empty_option;
regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
regmatch.regprog = vim_regcomp((char *)pat, RE_MAGIC + RE_STRING);
if (regmatch.regprog != NULL) {
regmatch.rm_ic = ic;
matches = vim_regexec_nl(&regmatch, (char_u *)text, (colnr_T)0);
@@ -8881,7 +8879,7 @@ int typval_compare(typval_T *typ1, typval_T *typ2, exprtype_T type, bool ic)
case EXPR_MATCH:
case EXPR_NOMATCH:
n1 = pattern_match((char *)s2, (char *)s1, ic);
n1 = pattern_match(s2, s1, ic);
if (type == EXPR_NOMATCH) {
n1 = !n1;
}

View File

@@ -38,7 +38,7 @@ return {
assert_equal={args={2, 3}, base=2},
assert_equalfile={args={2, 3}, base=1},
assert_exception={args={1, 2}},
assert_fails={args={1, 3}, base=1},
assert_fails={args={1, 5}, base=1},
assert_false={args={1, 2}, base=1},
assert_inrange={args={3, 4}, base=3},
assert_match={args={2, 3}, base=2},

View File

@@ -194,6 +194,12 @@ EXTERN int emsg_skip INIT(= 0); // don't display errors for
// expression that is skipped
EXTERN bool emsg_severe INIT(= false); // use message of next of several
// emsg() calls for throw
// used by assert_fails()
EXTERN bool emsg_assert_fails_used INIT(= false);
EXTERN char *emsg_assert_fails_msg INIT(= NULL);
EXTERN long emsg_assert_fails_lnum INIT(= 0);
EXTERN char *emsg_assert_fails_context INIT(= NULL);
EXTERN bool did_endif INIT(= false); // just had ":endif"
EXTERN dict_T vimvardict; // Dictionary with v: variables
EXTERN dict_T globvardict; // Dictionary with g: variables

View File

@@ -663,6 +663,13 @@ static bool emsg_multiline(const char *s, bool multiline)
return true;
}
if (emsg_assert_fails_used && emsg_assert_fails_msg == NULL) {
emsg_assert_fails_msg = xstrdup(s);
emsg_assert_fails_lnum = SOURCING_LNUM;
xfree(emsg_assert_fails_context);
emsg_assert_fails_context = xstrdup(SOURCING_NAME == NULL ? "" : SOURCING_NAME);
}
// set "v:errmsg", also when using ":silent! cmd"
set_vim_var_string(VV_ERRMSG, s, -1);

View File

@@ -48,6 +48,11 @@ func Test_assert_equal()
call assert_equal('XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
call assert_match("Expected 'X\\\\\\[x occurs 21 times]X' but got 'X\\\\\\[y occurs 25 times]X'", v:errors[0])
call remove(v:errors, 0)
" special characters are escaped
call assert_equal("\b\e\f\n\t\r\\\x01\x7f", 'x')
call assert_match('Expected ''\\b\\e\\f\\n\\t\\r\\\\\\x01\\x7f'' but got ''x''', v:errors[0])
call remove(v:errors, 0)
endfunc
func Test_assert_equal_dict()
@@ -143,6 +148,14 @@ func Test_assert_exception()
call assert_equal(0, assert_exception('E492:'))
endtry
try
nocommand
catch
call assert_equal(1, assert_exception('E12345:'))
endtry
call assert_match("Expected 'E12345:' but got 'Vim:E492: ", v:errors[0])
call remove(v:errors, 0)
try
nocommand
catch
@@ -153,6 +166,10 @@ func Test_assert_exception()
call assert_equal(0, assert_exception('E730:'))
endtry
endtry
call assert_equal(1, assert_exception('E492:'))
call assert_match('v:exception is not set', v:errors[0])
call remove(v:errors, 0)
endfunc
func Test_wrong_error_type()
@@ -202,6 +219,14 @@ func Test_assert_fail_fails()
call assert_match("stupid: Expected 'E9876' but got 'E492:", v:errors[0])
call remove(v:errors, 0)
call assert_equal(1, assert_fails('xxx', ['E9876']))
call assert_match("Expected \\['E9876'\\] but got 'E492:", v:errors[0])
call remove(v:errors, 0)
call assert_equal(1, assert_fails('xxx', ['E492:', 'E9876']))
call assert_match("Expected \\['E492:', 'E9876'\\] but got 'E492:", v:errors[0])
call remove(v:errors, 0)
call assert_equal(1, assert_fails('echo', '', 'echo command'))
call assert_match("command did not fail: echo command", v:errors[0])
call remove(v:errors, 0)
@@ -209,6 +234,41 @@ func Test_assert_fail_fails()
call assert_equal(1, 'echo'->assert_fails('', 'echo command'))
call assert_match("command did not fail: echo command", v:errors[0])
call remove(v:errors, 0)
try
call assert_equal(1, assert_fails('xxx', []))
catch
let exp = v:exception
endtry
call assert_match("E856: assert_fails() second argument", exp)
try
call assert_equal(1, assert_fails('xxx', ['1', '2', '3']))
catch
let exp = v:exception
endtry
call assert_match("E856: assert_fails() second argument", exp)
try
call assert_equal(1, assert_fails('xxx', #{one: 1}))
catch
let exp = v:exception
endtry
call assert_match("E856: assert_fails() second argument", exp)
try
call assert_equal(1, assert_fails('xxx', 'E492', '', 'burp'))
catch
let exp = v:exception
endtry
call assert_match("E1115: assert_fails() fourth argument must be a number", exp)
try
call assert_equal(1, assert_fails('xxx', 'E492', '', 54, 123))
catch
let exp = v:exception
endtry
call assert_match("E1116: assert_fails() fifth argument must be a string", exp)
endfunc
func Test_assert_fails_in_try_block()

View File

@@ -122,9 +122,7 @@ endfunc
func Test_multibyte()
" using an invalid character should not cause a crash
set wic
" Except on Windows, E472 is also thrown last, but v8.1.1183 isn't ported yet
" call assert_fails('tc <20><><EFBFBD><EFBFBD><EFBFBD>*', has('win32') ? 'E480:' : 'E344:')
call assert_fails('tc <20><><EFBFBD><EFBFBD><EFBFBD>*', has('win32') ? 'E480:' : 'E472:')
call assert_fails('tc <20><><EFBFBD><EFBFBD><EFBFBD>*', has('win32') ? 'E480:' : 'E344:')
set nowic
endfunc

View File

@@ -175,9 +175,7 @@ func Test_autocmd_bufunload_avoiding_SEGV_01()
exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
augroup END
" Todo: check for E937 generated first
" call assert_fails('edit bb.txt', 'E937:')
call assert_fails('edit bb.txt', 'E517:')
call assert_fails('edit bb.txt', ['E937:', 'E517:'])
autocmd! test_autocmd_bufunload
augroup! test_autocmd_bufunload
@@ -2933,7 +2931,7 @@ func Test_BufDelete_changebuf()
augroup END
let save_cpo = &cpo
set cpo+=f
call assert_fails('r Xfile', 'E484:')
call assert_fails('r Xfile', ['E812:', 'E484:'])
call assert_equal('somefile', @%)
let &cpo = save_cpo
augroup TestAuCmd

View File

@@ -76,7 +76,7 @@ func Test_bunload_with_offset()
let caught_E90 = 1
endtry
call assert_equal(1, caught_E90)
call assert_fails('$bunload', 'E515:')
call assert_fails('$bunload', 'E90:')
endfunc
" Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
@@ -282,7 +282,7 @@ func Test_goto_buf_with_confirm()
call assert_equal(1, &modified)
call assert_equal('', @%)
call feedkeys('y', 'L')
call assert_fails('confirm b Xfile', 'E37:')
call assert_fails('confirm b Xfile', ['', 'E37:'])
call assert_equal(1, &modified)
call assert_equal('', @%)
call feedkeys('n', 'L')

View File

@@ -5,7 +5,7 @@ source check.vim
func Test_cd_large_path()
" This used to crash with a heap write overflow.
call assert_fails('cd ' . repeat('x', 5000), 'E472:')
call assert_fails('cd ' . repeat('x', 5000), 'E344:')
endfunc
func Test_cd_up_and_down()
@@ -45,9 +45,7 @@ func Test_cd_minus()
call assert_equal(path, getcwd())
" Test for :cd - after a failed :cd
" v8.2.1183 is not ported yet
" call assert_fails('cd /nonexistent', 'E344:')
call assert_fails('cd /nonexistent', 'E472:')
call assert_fails('cd /nonexistent', 'E344:')
call assert_equal(path, getcwd())
cd -
call assert_equal(path_dotdot, getcwd())
@@ -103,7 +101,7 @@ func Test_chdir_func()
call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
" Error case
call assert_fails("call chdir('dir-abcd')", 'E472:')
call assert_fails("call chdir('dir-abcd')", 'E344:')
silent! let d = chdir("dir_abcd")
call assert_equal("", d)
" Should not crash

View File

@@ -102,7 +102,7 @@ func Test_client_server()
call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
call feedkeys('', 'x')
call assert_equal(75, g:testvar2)
call assert_fails('let v = remote_expr(v:servername, "/2")', 'E449:')
call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2'])
call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
call assert_equal('got it', g:myserverid->remote_read(2))
@@ -184,8 +184,8 @@ func Test_client_server()
call assert_fails('call remote_startserver([])', 'E730:')
call assert_fails("let x = remote_peek([])", 'E730:')
call assert_fails("let x = remote_read('vim10')", 'E277:')
call assert_fails("call server2client('abc', 'xyz')", 'E258:')
call assert_fails("let x = remote_read('vim10')", ['E573:.*vim10'])
call assert_fails("call server2client('abc', 'xyz')", ['E573:.*abc'])
endfunc
" Uncomment this line to get a debugging log

View File

@@ -1154,7 +1154,7 @@ func Test_cmdline_search_range()
call assert_equal('B', getline(2))
let @/ = 'apple'
call assert_fails('\/print', 'E486:')
call assert_fails('\/print', ['E486:.*apple'])
bwipe!
endfunc
@@ -1272,7 +1272,7 @@ func Test_verbosefile()
call delete('Xlog')
call mkdir('Xdir')
if !has('win32') " FIXME: no error on Windows, libuv bug?
call assert_fails('set verbosefile=Xdir', 'E474:')
call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
endif
call delete('Xdir', 'd')
endfunc
@@ -1525,7 +1525,7 @@ func Test_cmdwin_jump_to_win()
call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
new
set modified
call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', 'E162:')
call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
close!
call feedkeys("q/:close\<CR>", "xt")
call assert_equal(1, winnr('$'))
@@ -1539,13 +1539,7 @@ endfunc
func Test_cmdwin_tabpage()
tabedit
" v8.2.1919 isn't ported yet, so E492 is thrown after E11 here.
" v8.2.1183 also isn't ported yet, so we also can't assert E11 directly.
" For now, assert E11 and E492 separately. When v8.2.1183 is ported, the
" assert for E492 will fail and this workaround should be removed.
" call assert_fails("silent norm q/g :I\<Esc>", 'E11:')
call assert_fails("silent norm q/g ", 'E11:')
call assert_fails("silent norm q/g :I\<Esc>", 'E492:')
call assert_fails("silent norm q/g :I\<Esc>", 'E11:')
tabclose!
endfunc

View File

@@ -104,7 +104,7 @@ func Test_cpo_C()
source Xfile
call assert_equal([1, 2], g:l)
set cpo+=C
call assert_fails('source Xfile', 'E10:')
call assert_fails('source Xfile', ['E697:', 'E10:'])
call delete('Xfile')
let &cpo = save_cpo
endfunc

View File

@@ -20,13 +20,8 @@ func Test_nocatch_restore_silent_emsg()
throw 1
catch
endtry
echoerr 'wrong'
let c1 = nr2char(screenchar(&lines, 1))
let c2 = nr2char(screenchar(&lines, 2))
let c3 = nr2char(screenchar(&lines, 3))
let c4 = nr2char(screenchar(&lines, 4))
let c5 = nr2char(screenchar(&lines, 5))
call assert_equal('wrong', c1 . c2 . c3 . c4 . c5)
echoerr 'wrong again'
call assert_equal('wrong again', ScreenLine(&lines))
endfunc
func Test_mkdir_p()

View File

@@ -481,8 +481,8 @@ func Test_redir_cmd()
call assert_fails('redir abc', 'E475:')
call assert_fails('redir => 1abc', 'E474:')
call assert_fails('redir => a b', 'E488:')
call assert_fails('redir => abc[1]', 'E475:')
let b=0zFF
call assert_fails('redir => abc[1]', 'E121:')
let b = 0zFF
call assert_fails('redir =>> b', 'E734:')
unlet b

View File

@@ -528,7 +528,7 @@ func Test_setmatches()
endif
eval set->setmatches()
call assert_equal(exp, getmatches())
call assert_fails('let m = setmatches([], [])', 'E957:')
call assert_fails('let m = setmatches([], [])', 'E745:')
endfunc
func Test_empty_concatenate()

View File

@@ -211,7 +211,7 @@ func Test_str2nr()
if has('float')
call assert_fails('call str2nr(1.2)', 'E806:')
endif
call assert_fails('call str2nr(10, [])', 'E474:')
call assert_fails('call str2nr(10, [])', 'E745:')
endfunc
func Test_strftime()
@@ -1728,11 +1728,11 @@ func Test_libcall_libcallnr()
call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:'])
call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:'])
endfunc
sandbox function Fsandbox()

View File

@@ -39,7 +39,7 @@ endfunc
func Test_global_error()
call assert_fails('g\\a', 'E10:')
call assert_fails('g', 'E148:')
call assert_fails('g/\(/y', 'E476:')
call assert_fails('g/\(/y', 'E54:')
endfunc
" Test for printing lines using :g with different search patterns

View File

@@ -262,7 +262,7 @@ func Test_let_errors()
let l = [1, 2, 3]
call assert_fails('let l[:] = 5', 'E709:')
call assert_fails('let x:lnum=5', 'E488:')
call assert_fails('let x:lnum=5', ['E121:', 'E488:'])
call assert_fails('let v:=5', 'E461:')
call assert_fails('let [a]', 'E474:')
call assert_fails('let [a, b] = [', 'E697:')

View File

@@ -680,10 +680,10 @@ func Test_reverse_sort_uniq()
endif
call assert_fails('call reverse("")', 'E899:')
call assert_fails('call uniq([1, 2], {x, y -> []})', 'E882:')
call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
call assert_fails("call sort([1, 2], function('min'), 1)", "E715:")
call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
call assert_fails("call sort([1, 2], function('min'))", "E702:")
call assert_fails("call sort([1, 2], function('min'))", "E118:")
endfunc
" reduce a list or a blob
@@ -983,7 +983,7 @@ func Test_listdict_index()
call assert_fails('echo d[1:2]', 'E719:')
call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
call assert_fails("let v = range(5)[2:[]]", 'E730:')
call assert_fails("let v = range(5)[2:{-> 2}(]", 'E116:')
call assert_fails("let v = range(5)[2:{-> 2}(]", ['E15:', 'E116:'])
call assert_fails("let v = range(5)[2:3", 'E111:')
call assert_fails("let l = insert([1,2,3], 4, 10)", 'E684:')
call assert_fails("let l = insert([1,2,3], 4, -10)", 'E684:')

View File

@@ -163,7 +163,7 @@ func Test_matchadd_error()
" call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
call matchadd('GroupDoesNotExist', 'X')
call assert_equal([{'group': 'GroupDoesNotExist', 'pattern': 'X', 'priority': 10, 'id': 1206}], getmatches())
call assert_fails("call matchadd('Search', '\\(')", 'E475:')
call assert_fails("call matchadd('Search', '\\(')", 'E54:')
call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
call assert_fails("call matchadd('Error', 'XXX', 1, 0)", 'E799:')

View File

@@ -6,9 +6,7 @@ source check.vim
" Test for matchfuzzy()
func Test_matchfuzzy()
call assert_fails('call matchfuzzy(10, "abc")', 'E686:')
" Needs v8.2.1183; match the final error that's thrown for now
" call assert_fails('call matchfuzzy(["abc"], [])', 'E730:')
call assert_fails('call matchfuzzy(["abc"], [])', 'E475:')
call assert_fails('call matchfuzzy(["abc"], [])', 'E730:')
call assert_fails("let x = matchfuzzy(v:_null_list, 'foo')", 'E686:')
call assert_fails('call matchfuzzy(["abc"], v:_null_string)', 'E475:')
call assert_equal([], matchfuzzy([], 'abc'))
@@ -75,12 +73,9 @@ func Test_matchfuzzy()
call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
call assert_equal([], matchfuzzy(l, 'cam'))
" Nvim's callback implementation is different, so E6000 is expected instead,
" but we need v8.2.1183 to assert it
" call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
" call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E6000:')
call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E475:')
" call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E475:')
call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E6000:')
call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
call assert_fails("let x = matchfuzzy(l, 'cam', v:_null_dict)", 'E715:')
call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : v:_null_string})", 'E475:')
" Nvim doesn't have null functions
@@ -155,12 +150,9 @@ func Test_matchfuzzypos()
call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
call assert_equal([[], [], []], matchfuzzypos(l, 'cam'))
" Nvim's callback implementation is different, so E6000 is expected instead,
" but we need v8.2.1183 to assert it
" call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
" call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E6000:')
call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E475:')
" call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E475:')
call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E6000:')
call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
call assert_fails("let x = matchfuzzypos(l, 'cam', v:_null_dict)", 'E715:')
call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : v:_null_string})", 'E475:')
" Nvim doesn't have null functions

View File

@@ -153,7 +153,7 @@ func Test_menu_errors()
call assert_fails('menu Test.Foo.Bar', 'E327:')
call assert_fails('cmenu Test.Foo', 'E328:')
call assert_fails('emenu x Test.Foo', 'E475:')
call assert_fails('emenu Test.Foo.Bar', 'E334:')
call assert_fails('emenu Test.Foo.Bar', 'E327:')
call assert_fails('menutranslate Test', 'E474:')
silent! unmenu Foo

View File

@@ -131,9 +131,9 @@ func Test_method_syntax()
eval [1, 2, 3]
\ ->sort(
\ )
call assert_fails('eval [1, 2, 3]-> sort()', 'E260:')
call assert_fails('eval [1, 2, 3]-> sort()', 'E15:')
call assert_fails('eval [1, 2, 3]->sort ()', 'E274:')
call assert_fails('eval [1, 2, 3]-> sort ()', 'E260:')
call assert_fails('eval [1, 2, 3]-> sort ()', 'E15:')
endfunc
func Test_method_lambda()

View File

@@ -1650,7 +1650,7 @@ func Test_normal23_K()
call setline(1, '---')
call assert_fails('normal! ggv2lK', 'E349:')
call setline(1, ['abc', 'xyz'])
call assert_fails("normal! gg2lv2h\<C-]>", 'E426:')
call assert_fails("normal! gg2lv2h\<C-]>", 'E433:')
call assert_beeps("normal! ggVjK")
" clean up

View File

@@ -740,7 +740,7 @@ func s:test_xhelpgrep(cchar)
" Search for non existing help string
call assert_fails('Xhelpgrep a1b2c3', 'E480:')
" Invalid regular expression
call assert_fails('Xhelpgrep \@<!', 'E480:')
call assert_fails('Xhelpgrep \@<!', 'E866:')
endfunc
func Test_helpgrep()
@@ -4101,8 +4101,8 @@ endfunc
func Test_lvimgrep_crash2()
au BufNewFile x sfind
call assert_fails('lvimgrep x x', 'E480:')
call assert_fails('lvimgrep x x x', 'E480:')
call assert_fails('lvimgrep x x', 'E471:')
call assert_fails('lvimgrep x x x', 'E471:')
au! BufNewFile
endfunc

View File

@@ -33,11 +33,11 @@ func Test_Rand()
endif
call assert_fails('echo srand([1])', 'E745:')
call assert_fails('echo rand("burp")', 'E475:')
call assert_fails('echo rand([1, 2, 3])', 'E475:')
call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:')
call assert_fails('echo rand([1, [2], 3, 4])', 'E475:')
call assert_fails('echo rand([1, 2, [3], 4])', 'E475:')
call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:')
call assert_fails('echo rand([1, 2, 3])', 'E730:')
call assert_fails('echo rand([[1], 2, 3, 4])', 'E730:')
call assert_fails('echo rand([1, [2], 3, 4])', 'E730:')
call assert_fails('echo rand([1, 2, [3], 4])', 'E730:')
call assert_fails('echo rand([1, 2, 3, [4]])', 'E730:')
" call test_settime(0)
endfunc

View File

@@ -101,7 +101,7 @@ func Test_multi_failure()
set re=2
call assert_fails('/a**', 'E871:')
call assert_fails('/a*\+', 'E871:')
call assert_fails('/a\{a}', 'E870:')
call assert_fails('/a\{a}', 'E554:')
set re=0
endfunc

View File

@@ -1647,8 +1647,8 @@ func Test_search_with_no_last_pat()
call assert_fails(";//p", 'E35:')
call assert_fails("??p", 'E35:')
call assert_fails(";??p", 'E35:')
call assert_fails('g//p', 'E476:')
call assert_fails('v//p', 'E476:')
call assert_fails('g//p', ['E35:', 'E476:'])
call assert_fails('v//p', ['E35:', 'E476:'])
call writefile(v:errors, 'Xresult')
qall!
[SCRIPT]
@@ -1669,8 +1669,8 @@ func Test_search_tilde_pat()
call assert_fails('exe "normal /~\<CR>"', 'E33:')
call assert_fails('exe "normal ?~\<CR>"', 'E33:')
set regexpengine=2
call assert_fails('exe "normal /~\<CR>"', 'E383:')
call assert_fails('exe "normal ?~\<CR>"', 'E383:')
call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
set regexpengine&
call writefile(v:errors, 'Xresult')
qall!

View File

@@ -483,13 +483,13 @@ func Test_sign_funcs()
call assert_fails('call sign_place(5, "", "sign1", "@", {"lnum" : 10})',
\ 'E158:')
call assert_fails('call sign_place(5, "", "sign1", [], {"lnum" : 10})',
\ 'E158:')
\ 'E730:')
call assert_fails('call sign_place(21, "", "sign1", "Xsign",
\ {"lnum" : -1})', 'E474:')
call assert_fails('call sign_place(22, "", "sign1", "Xsign",
\ {"lnum" : 0})', 'E474:')
call assert_fails('call sign_place(22, "", "sign1", "Xsign",
\ {"lnum" : []})', 'E474:')
\ {"lnum" : []})', 'E745:')
call assert_equal(-1, sign_place(1, "*", "sign1", "Xsign", {"lnum" : 10}))
" Tests for sign_getplaced()
@@ -1731,7 +1731,7 @@ func Test_sign_jump_func()
call assert_fails("call sign_jump(5, 'g5', 'foo')", 'E157:')
call assert_fails('call sign_jump([], "", "foo")', 'E745:')
call assert_fails('call sign_jump(2, [], "foo")', 'E730:')
call assert_fails('call sign_jump(2, "", {})', 'E158:')
call assert_fails('call sign_jump(2, "", {})', 'E731:')
call assert_fails('call sign_jump(2, "", "baz")', 'E158:')
sign unplace * group=*

View File

@@ -495,7 +495,7 @@ func Test_spellsuggest_expr_errors()
return [[{}, {}]]
endfunc
set spellsuggest=expr:MySuggest3()
call assert_fails("call spellsuggest('baord')", 'E728:')
call assert_fails("call spellsuggest('baord')", 'E731:')
set nospell spellsuggest&
delfunc MySuggest

View File

@@ -446,7 +446,7 @@ func Test_substitute_errors()
call assert_fails('s/FOO/bar/', 'E486:')
call assert_fails('s/foo/bar/@', 'E488:')
call assert_fails('s/\(/bar/', 'E476:')
call assert_fails('s/\(/bar/', 'E54:')
call assert_fails('s afooabara', 'E146:')
call assert_fails('s\\a', 'E10:')
@@ -841,9 +841,9 @@ endfunc
func Test_sub_with_no_last_pat()
let lines =<< trim [SCRIPT]
call assert_fails('~', 'E33:')
call assert_fails('s//abc/g', 'E476:')
call assert_fails('s\/bar', 'E476:')
call assert_fails('s\&bar&', 'E476:')
call assert_fails('s//abc/g', 'E35:')
call assert_fails('s\/bar', 'E35:')
call assert_fails('s\&bar&', 'E33:')
call writefile(v:errors, 'Xresult')
qall!
[SCRIPT]

View File

@@ -378,7 +378,7 @@ func Test_syntax_invalid_arg()
call assert_fails('syntax sync x', 'E404:')
call assert_fails('syntax keyword Abc a[', 'E789:')
call assert_fails('syntax keyword Abc a[bc]d', 'E890:')
call assert_fails('syntax cluster Abc add=A add=', 'E475:')
call assert_fails('syntax cluster Abc add=A add=', 'E406:')
" Test for too many \z\( and unmatched \z\(
" Not able to use assert_fails() here because both E50:/E879: and E475:

View File

@@ -85,7 +85,7 @@ func Test_tagfunc()
return v:null
endfunc
set tags= tfu=NullTagFunc
call assert_fails('tag nothing', 'E426')
call assert_fails('tag nothing', 'E433')
delf NullTagFunc
bwipe!

View File

@@ -8,7 +8,7 @@ func Test_ptag_with_notagstack()
CheckFeature quickfix
set notagstack
call assert_fails('ptag does_not_exist_tag_name', 'E426')
call assert_fails('ptag does_not_exist_tag_name', 'E433')
set tagstack&vim
endfunc
@@ -346,7 +346,7 @@ func Test_tagjump_etags()
\ "Xmain.c,64",
\ ";;;;\x7f1,0",
\ ], 'Xtags')
call assert_fails('tag foo', 'E426:')
call assert_fails('tag foo', 'E431:')
call delete('Xtags')
call delete('Xtags2')

View File

@@ -84,13 +84,11 @@ func Test_taglist_ctags_etags()
endfunc
func Test_tags_too_long()
call assert_fails('tag ' . repeat('x', 1020), 'E426')
call assert_fails('tag ' . repeat('x', 1020), ['E433', 'E426'])
tags
endfunc
func Test_tagfiles()
" Nvim: different default for 'tags'.
set tags=./tags,tags
call assert_equal([], tagfiles())
call writefile(["FFoo\tXfoo\t1"], 'Xtags1')

View File

@@ -2000,13 +2000,11 @@ endfunc
func Test_try_catch_errors()
call assert_fails('throw |', 'E471:')
call assert_fails("throw \n ", 'E471:')
call assert_fails('catch abc', 'E603:')
call assert_fails('catch abc', 'E654:')
call assert_fails('try | let i = 1| finally | catch | endtry', 'E604:')
call assert_fails('finally', 'E606:')
call assert_fails('try | finally | finally | endtry', 'E607:')
" v8.2.3486 has been ported, but v8.2.1183 hasn't, so E170 appears here.
" call assert_fails('try | for i in range(5) | endif | endtry', 'E580:')
call assert_fails('try | for i in range(5) | endif | endtry', 'E170:')
call assert_fails('try | for i in range(5) | endif | endtry', 'E580:')
call assert_fails('try | while v:true | endtry', 'E170:')
call assert_fails('try | if v:true | endtry', 'E171:')
endfunc

View File

@@ -21,7 +21,7 @@ func Test_strchars()
call assert_equal(exp[i][1], inp[i]->strchars(0))
call assert_equal(exp[i][2], strchars(inp[i], 1))
endfor
call assert_fails("let v=strchars('abc', [])", 'E474:')
call assert_fails("let v=strchars('abc', [])", 'E745:')
call assert_fails("let v=strchars('abc', 2)", 'E474:')
endfunc

View File

@@ -115,7 +115,7 @@ func Test_winbuf_close()
call assert_equal('Xtest2', bufname('%'))
quit!
call assert_equal('Xtest3', bufname('%'))
call assert_fails('silent! quit!', 'E162')
call assert_fails('silent! quit!', 'E37')
call assert_equal('Xtest1', bufname('%'))
call delete('Xtest1')

View File

@@ -325,7 +325,7 @@ func Test_write_autocmd_unloadbuf_lockmark()
autocmd BufWritePre Xfile enew | write
augroup END
e Xfile
call assert_fails('lockmarks write', 'E203:')
call assert_fails('lockmarks write', ['E32', 'E203:'])
augroup WriteTest
au!
augroup END

View File

@@ -14,6 +14,12 @@
# include "testing.c.generated.h"
#endif
static char e_assert_fails_second_arg[]
= N_("E856: assert_fails() second argument must be a string or a list with one or two strings");
static char e_assert_fails_fourth_argument[]
= N_("E1115: assert_fails() fourth argument must be a number");
static char e_assert_fails_fifth_argument[]
= N_("E1116: assert_fails() fifth argument must be a string");
static char e_calling_test_garbagecollect_now_while_v_testing_is_not_set[]
= N_("E1142: Calling test_garbagecollect_now() while v:testing is not set");
@@ -68,7 +74,7 @@ static void ga_concat_esc(garray_T *gap, const char_u *p, int clen)
case '\\':
ga_concat(gap, "\\\\"); break;
default:
if (*p < ' ') {
if (*p < ' ' || *p == 0x7f) {
vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
ga_concat(gap, (char *)buf);
} else {
@@ -124,7 +130,10 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_s
bool did_copy = false;
int omitted = 0;
if (opt_msg_tv->v_type != VAR_UNKNOWN) {
if (opt_msg_tv->v_type != VAR_UNKNOWN
&& !(opt_msg_tv->v_type == VAR_STRING
&& (opt_msg_tv->vval.v_string == NULL
|| *opt_msg_tv->vval.v_string == NUL))) {
tofree = (char_u *)encode_tv2echo(opt_msg_tv, NULL);
ga_concat(gap, (char *)tofree);
xfree(tofree);
@@ -244,13 +253,12 @@ static int assert_match_common(typval_T *argvars, assert_type_T atype)
{
char buf1[NUMBUFLEN];
char buf2[NUMBUFLEN];
const int called_emsg_before = called_emsg;
const char *const pat = tv_get_string_buf_chk(&argvars[0], buf1);
const char *const text = tv_get_string_buf_chk(&argvars[1], buf2);
if (pat == NULL || text == NULL) {
emsg(_(e_invarg));
} else if (pattern_match((char *)pat, (char *)text, false)
!= (atype == ASSERT_MATCH)) {
if (called_emsg == called_emsg_before
&& pattern_match(pat, text, false) != (atype == ASSERT_MATCH)) {
garray_T ga;
prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], atype);
@@ -351,11 +359,12 @@ static int assert_equalfile(typval_T *argvars)
{
char buf1[NUMBUFLEN];
char buf2[NUMBUFLEN];
const int called_emsg_before = called_emsg;
const char *const fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
const char *const fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
garray_T ga;
if (fname1 == NULL || fname2 == NULL) {
if (called_emsg > called_emsg_before) {
return 0;
}
@@ -477,11 +486,13 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
garray_T ga;
int save_trylevel = trylevel;
const int called_emsg_before = called_emsg;
char *wrong_arg_msg = NULL;
// trylevel must be zero for a ":throw" command to be considered failed
trylevel = 0;
suppress_errthrow = true;
emsg_silent = true;
emsg_assert_fails_used = true;
do_cmdline_cmd(cmd);
if (called_emsg == called_emsg_before) {
@@ -493,13 +504,75 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
rettv->vval.v_number = 1;
} else if (argvars[1].v_type != VAR_UNKNOWN) {
char buf[NUMBUFLEN];
const char *const error = tv_get_string_buf_chk(&argvars[1], buf);
const char *expected;
bool error_found = false;
int error_found_index = 1;
char *actual = emsg_assert_fails_msg == NULL ? "[unknown]" : emsg_assert_fails_msg;
if (error == NULL
|| strstr(get_vim_var_str(VV_ERRMSG), error) == NULL) {
if (argvars[1].v_type == VAR_STRING) {
expected = tv_get_string_buf_chk(&argvars[1], buf);
error_found = expected == NULL || strstr(actual, expected) == NULL;
} else if (argvars[1].v_type == VAR_LIST) {
const list_T *const list = argvars[1].vval.v_list;
if (list == NULL || tv_list_len(list) < 1 || tv_list_len(list) > 2) {
wrong_arg_msg = e_assert_fails_second_arg;
goto theend;
}
const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list));
expected = tv_get_string_buf_chk(tv, buf);
if (!pattern_match(expected, actual, false)) {
error_found = true;
} else if (tv_list_len(list) == 2) {
tv = TV_LIST_ITEM_TV(tv_list_last(list));
actual = get_vim_var_str(VV_ERRMSG);
expected = tv_get_string_buf_chk(tv, buf);
if (!pattern_match(expected, actual, false)) {
error_found = true;
}
}
} else {
wrong_arg_msg = e_assert_fails_second_arg;
goto theend;
}
if (!error_found && argvars[2].v_type != VAR_UNKNOWN
&& argvars[3].v_type != VAR_UNKNOWN) {
if (argvars[3].v_type != VAR_NUMBER) {
wrong_arg_msg = e_assert_fails_fourth_argument;
goto theend;
} else if (argvars[3].vval.v_number >= 0
&& argvars[3].vval.v_number != emsg_assert_fails_lnum) {
error_found = true;
error_found_index = 3;
}
if (!error_found && argvars[4].v_type != VAR_UNKNOWN) {
if (argvars[4].v_type != VAR_STRING) {
wrong_arg_msg = e_assert_fails_fifth_argument;
goto theend;
} else if (argvars[4].vval.v_string != NULL
&& !pattern_match(argvars[4].vval.v_string,
emsg_assert_fails_context, false)) {
error_found = true;
error_found_index = 4;
}
}
}
if (error_found) {
typval_T actual_tv;
prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
get_vim_var_tv(VV_ERRMSG), ASSERT_OTHER);
if (error_found_index == 3) {
actual_tv.v_type = VAR_NUMBER;
actual_tv.vval.v_number = emsg_assert_fails_lnum;
} else if (error_found_index == 4) {
actual_tv.v_type = VAR_STRING;
actual_tv.vval.v_string = emsg_assert_fails_context;
} else {
actual_tv.v_type = VAR_STRING;
actual_tv.vval.v_string = actual;
}
fill_assert_error(&ga, &argvars[2], NULL,
&argvars[error_found_index], &actual_tv, ASSERT_OTHER);
ga_concat(&ga, ": ");
assert_append_cmd_or_arg(&ga, argvars, cmd);
assert_error(&ga);
@@ -508,11 +581,17 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
}
theend:
trylevel = save_trylevel;
suppress_errthrow = false;
emsg_silent = false;
emsg_on_display = false;
emsg_assert_fails_used = false;
XFREE_CLEAR(emsg_assert_fails_msg);
set_vim_var_string(VV_ERRMSG, NULL, 0);
if (wrong_arg_msg != NULL) {
emsg(_(wrong_arg_msg));
}
}
// "assert_false(actual[, msg])" function