comparison src/testdir/test_trycatch.vim @ 19487:0aba9ef12488 v8.2.0301

patch 8.2.0301: insufficient testing for exception handling Commit: https://github.com/vim/vim/commit/b654103ad1e379348616f354272db86804ab4bdb Author: Bram Moolenaar <Bram@vim.org> Date: Sat Feb 22 21:21:27 2020 +0100 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)
author Bram Moolenaar <Bram@vim.org>
date Sat, 22 Feb 2020 21:30:04 +0100
parents cb73f4ae6b7c
children 031184ace7c5
comparison
equal deleted inserted replaced
19486:8b6a8273ac5a 19487:0aba9ef12488
2015 if v:lang != "C" && v:lang !~ '^[Ee]n' 2015 if v:lang != "C" && v:lang !~ '^[Ee]n'
2016 return 2016 return
2017 endif 2017 endif
2018 2018
2019 set verbose=14 2019 set verbose=14
2020
2021 " Test for verbose messages displayed when an exception is caught
2020 redir => msg 2022 redir => msg
2021 try 2023 try
2022 echo i 2024 echo i
2023 catch /E121:/ 2025 catch /E121:/
2024 finally 2026 finally
2025 endtry 2027 endtry
2026 redir END 2028 redir END
2027 let expected = [ 2029 let expected = [
2028 \ 'Exception thrown: Vim(echo):E121: Undefined variable: i', 2030 \ 'Exception thrown: Vim(echo):E121: Undefined variable: i', '',
2029 \ '', 2031 \ 'Exception caught: Vim(echo):E121: Undefined variable: i', '',
2030 \ 'Exception caught: Vim(echo):E121: Undefined variable: i', 2032 \ 'Exception finished: Vim(echo):E121: Undefined variable: i']
2031 \ '',
2032 \ 'Exception finished: Vim(echo):E121: Undefined variable: i'
2033 \ ]
2034 call assert_equal(expected, split(msg, "\n")) 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']
2149 call assert_equal(expected, split(msg, "\n"))
2150
2035 set verbose& 2151 set verbose&
2152 endfunc
2153
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!
2036 endfunc 2201 endfunc
2037 2202
2038 " Modeline {{{1 2203 " Modeline {{{1
2039 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 2204 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker