Skip to content

Commit b654103

Browse files
committed
patch 8.2.0301: insufficient testing for exception handling
Problem: Insufficient testing for exception handling and the "attention" prompt. Solution: Add test cases. (Yegappan Lakshmanan, closes #5681)
1 parent b4d2cb1 commit b654103

3 files changed

Lines changed: 199 additions & 6 deletions

File tree

src/testdir/test_swap.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
" Tests for the swap feature
22

33
source shared.vim
4+
source term_util.vim
45

56
func s:swapname()
67
return trim(execute('swapname'))
@@ -349,4 +350,29 @@ func Test_swap_split_win()
349350
augroup! test_swap_splitwin
350351
endfunc
351352

353+
" Test for selecting 'q' in the attention prompt
354+
func Test_swap_prompt_splitwin()
355+
if !CanRunVimInTerminal()
356+
throw 'Skipped: cannot run vim in terminal'
357+
endif
358+
call writefile(['foo bar'], 'Xfile1')
359+
edit Xfile1
360+
let buf = RunVimInTerminal('', {'rows': 20})
361+
call term_sendkeys(buf, ":set nomore\n")
362+
call term_sendkeys(buf, ":set noruler\n")
363+
call term_sendkeys(buf, ":split Xfile1\n")
364+
call term_wait(buf)
365+
call WaitForAssert({-> assert_match('^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: $', term_getline(buf, 20))})
366+
call term_sendkeys(buf, "q")
367+
call term_wait(buf)
368+
call term_sendkeys(buf, ":")
369+
call WaitForAssert({-> assert_match('^:$', term_getline(buf, 20))})
370+
call term_sendkeys(buf, "echomsg winnr('$')\<CR>")
371+
call term_wait(buf)
372+
call WaitForAssert({-> assert_match('^1$', term_getline(buf, 20))})
373+
call StopVimInTerminal(buf)
374+
%bwipe!
375+
call delete('Xfile1')
376+
endfunc
377+
352378
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_trycatch.vim

Lines changed: 171 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,8 @@ func Test_try_catch_verbose()
20172017
endif
20182018

20192019
set verbose=14
2020+
2021+
" Test for verbose messages displayed when an exception is caught
20202022
redir => msg
20212023
try
20222024
echo i
@@ -2025,15 +2027,178 @@ func Test_try_catch_verbose()
20252027
endtry
20262028
redir END
20272029
let expected = [
2028-
\ 'Exception thrown: Vim(echo):E121: Undefined variable: i',
2029-
\ '',
2030-
\ 'Exception caught: Vim(echo):E121: Undefined variable: i',
2031-
\ '',
2032-
\ 'Exception finished: Vim(echo):E121: Undefined variable: i'
2033-
\ ]
2030+
\ 'Exception thrown: Vim(echo):E121: Undefined variable: i', '',
2031+
\ 'Exception caught: Vim(echo):E121: Undefined variable: i', '',
2032+
\ 'Exception finished: Vim(echo):E121: Undefined variable: i']
2033+
call assert_equal(expected, split(msg, "\n"))
2034+
2035+
" Test for verbose messages displayed when an exception is discarded
2036+
redir => msg
2037+
try
2038+
try
2039+
throw 'abc'
2040+
finally
2041+
throw 'xyz'
2042+
endtry
2043+
catch
2044+
endtry
2045+
redir END
2046+
let expected = [
2047+
\ 'Exception thrown: abc', '',
2048+
\ 'Exception made pending: abc', '',
2049+
\ 'Exception thrown: xyz', '',
2050+
\ 'Exception discarded: abc', '',
2051+
\ 'Exception caught: xyz', '',
2052+
\ 'Exception finished: xyz']
2053+
call assert_equal(expected, split(msg, "\n"))
2054+
2055+
" Test for messages displayed when :throw is resumed after :finally
2056+
redir => msg
2057+
try
2058+
try
2059+
throw 'abc'
2060+
finally
2061+
endtry
2062+
catch
2063+
endtry
2064+
redir END
2065+
let expected = [
2066+
\ 'Exception thrown: abc', '',
2067+
\ 'Exception made pending: abc', '',
2068+
\ 'Exception resumed: abc', '',
2069+
\ 'Exception caught: abc', '',
2070+
\ 'Exception finished: abc']
2071+
call assert_equal(expected, split(msg, "\n"))
2072+
2073+
" Test for messages displayed when :break is resumed after :finally
2074+
redir => msg
2075+
for i in range(1)
2076+
try
2077+
break
2078+
finally
2079+
endtry
2080+
endfor
2081+
redir END
2082+
let expected = [':break made pending', '', ':break resumed']
2083+
call assert_equal(expected, split(msg, "\n"))
2084+
2085+
" Test for messages displayed when :continue is resumed after :finally
2086+
redir => msg
2087+
for i in range(1)
2088+
try
2089+
continue
2090+
finally
2091+
endtry
2092+
endfor
2093+
redir END
2094+
let expected = [':continue made pending', '', ':continue resumed']
2095+
call assert_equal(expected, split(msg, "\n"))
2096+
2097+
" Test for messages displayed when :return is resumed after :finally
2098+
func Xtest()
2099+
try
2100+
return 'vim'
2101+
finally
2102+
endtry
2103+
endfunc
2104+
redir => msg
2105+
call Xtest()
2106+
redir END
2107+
let expected = [
2108+
\ 'calling Xtest()', '',
2109+
\ ':return vim made pending', '',
2110+
\ ':return vim resumed', '',
2111+
\ 'Xtest returning ''vim''', '',
2112+
\ 'continuing in Test_try_catch_verbose']
2113+
call assert_equal(expected, split(msg, "\n"))
2114+
delfunc Xtest
2115+
2116+
" Test for messages displayed when :finish is resumed after :finally
2117+
call writefile(['try', 'finish', 'finally', 'endtry'], 'Xscript')
2118+
redir => msg
2119+
source Xscript
2120+
redir END
2121+
let expected = [
2122+
\ ':finish made pending', '',
2123+
\ ':finish resumed', '',
2124+
\ 'finished sourcing Xscript',
2125+
\ 'continuing in Test_try_catch_verbose']
2126+
call assert_equal(expected, split(msg, "\n")[1:])
2127+
call delete('Xscript')
2128+
2129+
" Test for messages displayed when a pending :continue is discarded by an
2130+
" exception in a finally handler
2131+
redir => msg
2132+
try
2133+
for i in range(1)
2134+
try
2135+
continue
2136+
finally
2137+
throw 'abc'
2138+
endtry
2139+
endfor
2140+
catch
2141+
endtry
2142+
redir END
2143+
let expected = [
2144+
\ ':continue made pending', '',
2145+
\ 'Exception thrown: abc', '',
2146+
\ ':continue discarded', '',
2147+
\ 'Exception caught: abc', '',
2148+
\ 'Exception finished: abc']
20342149
call assert_equal(expected, split(msg, "\n"))
2150+
20352151
set verbose&
20362152
endfunc
20372153

2154+
" Test for throwing an exception from a BufEnter autocmd {{{1
2155+
func Test_BufEnter_exception()
2156+
augroup bufenter_exception
2157+
au!
2158+
autocmd BufEnter Xfile1 throw 'abc'
2159+
augroup END
2160+
2161+
let caught_abc = 0
2162+
try
2163+
sp Xfile1
2164+
catch /^abc/
2165+
let caught_abc = 1
2166+
endtry
2167+
call assert_equal(1, caught_abc)
2168+
call assert_equal(1, winnr('$'))
2169+
2170+
augroup bufenter_exception
2171+
au!
2172+
augroup END
2173+
augroup! bufenter_exception
2174+
%bwipe!
2175+
2176+
" Test for recursively throwing exceptions in autocmds
2177+
augroup bufenter_exception
2178+
au!
2179+
autocmd BufEnter Xfile1 throw 'bufenter'
2180+
autocmd BufLeave Xfile1 throw 'bufleave'
2181+
augroup END
2182+
2183+
let ex_count = 0
2184+
try
2185+
try
2186+
sp Xfile1
2187+
catch /^bufenter/
2188+
let ex_count += 1
2189+
endtry
2190+
catch /^bufleave/
2191+
let ex_count += 10
2192+
endtry
2193+
call assert_equal(10, ex_count)
2194+
call assert_equal(2, winnr('$'))
2195+
2196+
augroup bufenter_exception
2197+
au!
2198+
augroup END
2199+
augroup! bufenter_exception
2200+
%bwipe!
2201+
endfunc
2202+
20382203
" Modeline {{{1
20392204
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
301,
741743
/**/
742744
300,
743745
/**/

0 commit comments

Comments
 (0)