|
2 | 2 |
|
3 | 3 | source view_util.vim |
4 | 4 | source check.vim |
| 5 | +source vim9.vim |
5 | 6 |
|
6 | 7 | let s:imactivatefunc_called = 0 |
7 | 8 | let s:imstatusfunc_called = 0 |
@@ -107,4 +108,143 @@ func Test_iminsert_toggle() |
107 | 108 | close! |
108 | 109 | endfunc |
109 | 110 |
|
| 111 | +" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc' |
| 112 | +" options |
| 113 | +func Test_imactivatefunc_imstatusfunc_callback() |
| 114 | + CheckNotMSWindows |
| 115 | + func IMactivatefunc1(active) |
| 116 | + let g:IMactivatefunc_called += 1 |
| 117 | + endfunc |
| 118 | + func IMstatusfunc1() |
| 119 | + let g:IMstatusfunc_called += 1 |
| 120 | + return 1 |
| 121 | + endfunc |
| 122 | + let g:IMactivatefunc_called = 0 |
| 123 | + let g:IMstatusfunc_called = 0 |
| 124 | + set iminsert=2 |
| 125 | + |
| 126 | + " Test for using a function() |
| 127 | + set imactivatefunc=function('IMactivatefunc1') |
| 128 | + set imstatusfunc=function('IMstatusfunc1') |
| 129 | + normal! i |
| 130 | + |
| 131 | + " Using a funcref variable to set 'completefunc' |
| 132 | + let Fn1 = function('IMactivatefunc1') |
| 133 | + let &imactivatefunc = string(Fn1) |
| 134 | + let Fn2 = function('IMstatusfunc1') |
| 135 | + let &imstatusfunc = string(Fn2) |
| 136 | + normal! i |
| 137 | + call assert_fails('let &imactivatefunc = Fn1', 'E729:') |
| 138 | + call assert_fails('let &imstatusfunc = Fn2', 'E729:') |
| 139 | + |
| 140 | + " Test for using a funcref() |
| 141 | + set imactivatefunc=funcref('IMactivatefunc1') |
| 142 | + set imstatusfunc=funcref('IMstatusfunc1') |
| 143 | + normal! i |
| 144 | + |
| 145 | + " Using a funcref variable to set 'imactivatefunc' |
| 146 | + let Fn1 = funcref('IMactivatefunc1') |
| 147 | + let &imactivatefunc = string(Fn1) |
| 148 | + let Fn2 = funcref('IMstatusfunc1') |
| 149 | + let &imstatusfunc = string(Fn2) |
| 150 | + normal! i |
| 151 | + call assert_fails('let &imactivatefunc = Fn1', 'E729:') |
| 152 | + call assert_fails('let &imstatusfunc = Fn2', 'E729:') |
| 153 | + |
| 154 | + " Test for using a lambda function |
| 155 | + set imactivatefunc={a\ ->\ IMactivatefunc1(a)} |
| 156 | + set imstatusfunc={\ ->\ IMstatusfunc1()} |
| 157 | + normal! i |
| 158 | + |
| 159 | + " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression |
| 160 | + let &imactivatefunc = '{a -> IMactivatefunc1(a)}' |
| 161 | + let &imstatusfunc = '{ -> IMstatusfunc1()}' |
| 162 | + normal! i |
| 163 | + |
| 164 | + " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression |
| 165 | + let Lambda1 = {a -> IMactivatefunc1(a)} |
| 166 | + let Lambda2 = { -> IMstatusfunc1()} |
| 167 | + let &imactivatefunc = string(Lambda1) |
| 168 | + let &imstatusfunc = string(Lambda2) |
| 169 | + normal! i |
| 170 | + call assert_fails('let &imactivatefunc = Lambda1', 'E729:') |
| 171 | + call assert_fails('let &imstatusfunc = Lambda2', 'E729:') |
| 172 | + |
| 173 | + " Test for clearing the 'completefunc' option |
| 174 | + set imactivatefunc='' imstatusfunc='' |
| 175 | + set imactivatefunc& imstatusfunc& |
| 176 | + |
| 177 | + call assert_fails("set imactivatefunc=function('abc')", "E700:") |
| 178 | + call assert_fails("set imstatusfunc=function('abc')", "E700:") |
| 179 | + call assert_fails("set imactivatefunc=funcref('abc')", "E700:") |
| 180 | + call assert_fails("set imstatusfunc=funcref('abc')", "E700:") |
| 181 | + |
| 182 | + call assert_equal(7, g:IMactivatefunc_called) |
| 183 | + call assert_equal(14, g:IMstatusfunc_called) |
| 184 | + |
| 185 | + " Vim9 tests |
| 186 | + let lines =<< trim END |
| 187 | + vim9script |
| 188 | + |
| 189 | + # Test for using function() |
| 190 | + def IMactivatefunc1(active: number): any |
| 191 | + g:IMactivatefunc_called += 1 |
| 192 | + return 1 |
| 193 | + enddef |
| 194 | + def IMstatusfunc1(): number |
| 195 | + g:IMstatusfunc_called += 1 |
| 196 | + return 1 |
| 197 | + enddef |
| 198 | + g:IMactivatefunc_called = 0 |
| 199 | + g:IMstatusfunc_called = 0 |
| 200 | + set iminsert=2 |
| 201 | + set imactivatefunc=function('IMactivatefunc1') |
| 202 | + set imstatusfunc=function('IMstatusfunc1') |
| 203 | + normal! i |
| 204 | + |
| 205 | + # Test for using a lambda |
| 206 | + &imactivatefunc = '(a) => IMactivatefunc1(a)' |
| 207 | + &imstatusfunc = '() => IMstatusfunc1()' |
| 208 | + normal! i |
| 209 | + |
| 210 | + # Test for using a variable with a lambda expression |
| 211 | + var Fn1: func = (active) => { |
| 212 | + g:IMactivatefunc_called += 1 |
| 213 | + return 1 |
| 214 | + } |
| 215 | + var Fn2: func = () => { |
| 216 | + g:IMstatusfunc_called += 1 |
| 217 | + return 1 |
| 218 | + } |
| 219 | + &imactivatefunc = string(Fn1) |
| 220 | + &imstatusfunc = string(Fn2) |
| 221 | + normal! i |
| 222 | + |
| 223 | + assert_equal(3, g:IMactivatefunc_called) |
| 224 | + assert_equal(6, g:IMstatusfunc_called) |
| 225 | + |
| 226 | + set iminsert=0 |
| 227 | + set imactivatefunc= |
| 228 | + set imstatusfunc= |
| 229 | + END |
| 230 | + call CheckScriptSuccess(lines) |
| 231 | + |
| 232 | + " Using Vim9 lambda expression in legacy context should fail |
| 233 | + set imactivatefunc=(a)\ =>\ IMactivatefunc1(a) |
| 234 | + set imstatusfunc=IMstatusfunc1 |
| 235 | + call assert_fails('normal! i', 'E117:') |
| 236 | + set imactivatefunc=IMactivatefunc1 |
| 237 | + set imstatusfunc=()\ =>\ IMstatusfunc1(a) |
| 238 | + call assert_fails('normal! i', 'E117:') |
| 239 | + |
| 240 | + " cleanup |
| 241 | + delfunc IMactivatefunc1 |
| 242 | + delfunc IMstatusfunc1 |
| 243 | + set iminsert=0 |
| 244 | + set imactivatefunc= |
| 245 | + set imstatusfunc= |
| 246 | + |
| 247 | + %bw! |
| 248 | +endfunc |
| 249 | + |
110 | 250 | " vim: shiftwidth=2 sts=2 expandtab |
0 commit comments