Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 1 | " Tests for the Tcl interface. |
| 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature tcl |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 5 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 6 | " Helper function as there is no builtin tcleval() function similar |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 7 | " to perleval, luaeval(), pyeval(), etc. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 8 | func TclEval(tcl_expr) |
| 9 | let s = split(execute('tcl ' . a:tcl_expr), "\n") |
| 10 | return (len(s) == 0) ? '' : s[-1] |
| 11 | endfunc |
| 12 | |
| 13 | func Test_tcldo() |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 14 | " Check deleting lines does not trigger ml_get error. |
| 15 | new |
| 16 | call setline(1, ['one', 'two', 'three']) |
| 17 | tcldo ::vim::command %d_ |
| 18 | bwipe! |
| 19 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 20 | " Check that switching to another buffer does not trigger ml_get error. |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 21 | new |
| 22 | let wincount = winnr('$') |
| 23 | call setline(1, ['one', 'two', 'three']) |
| 24 | tcldo ::vim::command new |
| 25 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 26 | %bwipe! |
| 27 | endfunc |
| 28 | |
| 29 | " Test :tcldo with a range |
| 30 | func Test_tcldo_range() |
| 31 | new |
| 32 | call setline(1, ['line1', 'line2', 'line3', 'line4']) |
| 33 | 2,3tcldo set line [string toupper $line] |
| 34 | call assert_equal(['line1', 'LINE2', 'LINE3', 'line4'], getline(1, '$')) |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 35 | bwipe! |
| 36 | endfunc |
| 37 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 38 | " Test ::vim::beep |
| 39 | func Test_vim_beep() |
| 40 | call assert_beeps('tcl ::vim::beep') |
| 41 | call assert_fails('tcl ::vim::beep x', 'wrong # args: should be "::vim::beep"') |
| 42 | endfunc |
| 43 | |
| 44 | " Test ::vim::buffer |
| 45 | func Test_vim_buffer() |
| 46 | " Test ::vim::buffer {nr} |
| 47 | e Xfoo1 |
| 48 | call setline(1, ['foobar']) |
| 49 | let bn1 = bufnr('%') |
| 50 | let b1 = TclEval('::vim::buffer ' . bn1) |
| 51 | call assert_equal(b1, TclEval('set ::vim::current(buffer)')) |
| 52 | |
| 53 | new Xfoo2 |
| 54 | call setline(1, ['barfoo']) |
| 55 | let bn2 = bufnr('%') |
| 56 | let b2 = TclEval('::vim::buffer ' . bn2) |
| 57 | call assert_equal(b2, TclEval('set ::vim::current(buffer)')) |
| 58 | |
| 59 | call assert_match('Xfoo1$', TclEval(b1 . ' name')) |
| 60 | call assert_match('Xfoo2$', TclEval(b2 . ' name')) |
| 61 | |
| 62 | " Test ::vim::buffer exists {nr} |
| 63 | call assert_match('^[1-9]\d*$', TclEval('::vim::buffer exists ' . bn1)) |
| 64 | call assert_match('^[1-9]\d*$', TclEval('::vim::buffer exists ' . bn2)) |
| 65 | call assert_equal('0', TclEval('::vim::buffer exists 54321')) |
| 66 | |
| 67 | " Test ::vim::buffer list |
| 68 | call assert_equal('2', TclEval('llength [::vim::buffer list]')) |
| 69 | call assert_equal(b1.' '.b2, TclEval('::vim::buffer list')) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 70 | tcl << trim EOF |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 71 | proc eachbuf { cmd } { |
| 72 | foreach b [::vim::buffer list] { $b command $cmd } |
| 73 | } |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 74 | EOF |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 75 | tcl eachbuf %s/foo/FOO/g |
| 76 | b! Xfoo1 |
| 77 | call assert_equal(['FOObar'], getline(1, '$')) |
| 78 | b! Xfoo2 |
| 79 | call assert_equal(['barFOO'], getline(1, '$')) |
| 80 | |
| 81 | call assert_fails('tcl ::vim::buffer', |
| 82 | \ 'wrong # args: should be "::vim::buffer option"') |
| 83 | call assert_fails('tcl ::vim::buffer ' . bn1 . ' x', |
| 84 | \ 'wrong # args: should be "::vim::buffer bufNumber"') |
| 85 | call assert_fails('tcl ::vim::buffer 4321', 'invalid buffer number') |
| 86 | call assert_fails('tcl ::vim::buffer x', |
| 87 | \ 'bad option "x": must be exists or list') |
| 88 | call assert_fails('tcl ::vim::buffer exists', |
| 89 | \ 'wrong # args: should be "::vim::buffer exists bufNumber"') |
| 90 | call assert_fails('tcl ::vim::buffer exists x', |
| 91 | \ 'expected integer but got "x"') |
| 92 | call assert_fails('tcl ::vim::buffer list x', |
| 93 | \ 'wrong # args: should be "::vim::buffer list "') |
| 94 | |
| 95 | tcl rename eachbuf "" |
| 96 | %bwipe! |
| 97 | endfunc |
| 98 | |
| 99 | " Test ::vim::option |
| 100 | func Test_vim_option() |
| 101 | set cc=3,5 |
| 102 | |
| 103 | " Test getting option 'cc' |
| 104 | call assert_equal('3,5', TclEval('::vim::option cc')) |
| 105 | call assert_equal('3,5', &cc) |
| 106 | |
| 107 | " Test setting option 'cc' (it returns the old option value) |
| 108 | call assert_equal('3,5', TclEval('::vim::option cc +4')) |
| 109 | call assert_equal('+4', &cc) |
| 110 | call assert_equal('+4', TclEval('::vim::option cc')) |
| 111 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 112 | " Test boolean option with 'toggle', 'on' and 'off' keywords. |
| 113 | call assert_equal('0', TclEval('::vim::option nu toggle')) |
| 114 | call assert_equal(1, &nu) |
| 115 | call assert_equal('1', TclEval('::vim::option nu toggle')) |
| 116 | call assert_equal(0, &nu) |
| 117 | call assert_equal('0', TclEval('::vim::option nu on')) |
| 118 | call assert_equal(1, &nu) |
| 119 | call assert_equal('1', TclEval('::vim::option nu off')) |
| 120 | call assert_equal(0, &nu) |
| 121 | |
| 122 | call assert_fails('tcl ::vim::option nu x', 'expected integer but got "x"') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 123 | call assert_fails('tcl ::vim::option xxx', 'unknown vimOption') |
| 124 | call assert_fails('tcl ::vim::option', |
| 125 | \ 'wrong # args: should be "::vim::option vimOption ?value?"') |
| 126 | |
| 127 | set cc& |
| 128 | endfunc |
| 129 | |
| 130 | " Test ::vim::expr |
| 131 | func Test_vim_expr() |
| 132 | call assert_equal(string(char2nr('X')), |
| 133 | \ TclEval('::vim::expr char2nr("X")')) |
| 134 | |
| 135 | call assert_fails('tcl ::vim::expr x y', |
| 136 | \ 'wrong # args: should be "::vim::expr vimExpr"') |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 137 | call assert_fails('tcl ::vim::expr 1-', 'E15: Invalid expression: 1-') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 138 | endfunc |
| 139 | |
| 140 | " Test ::vim::command |
| 141 | func Test_vim_command() |
| 142 | call assert_equal('hello world', |
| 143 | \ TclEval('::vim::command {echo "hello world"}')) |
| 144 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 145 | " Check that if ::vim::command created a new Tcl interpreter, it is removed. |
| 146 | tcl set foo 123 |
| 147 | call assert_equal('321', TclEval('::vim::command "tcl set foo 321"')) |
| 148 | call assert_equal('123', TclEval('set foo')) |
| 149 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 150 | " With the -quiet option, the error should silently be ignored. |
| 151 | call assert_equal('', TclEval('::vim::command -quiet xyz')) |
| 152 | |
| 153 | call assert_fails('tcl ::vim::command', |
| 154 | \ 'wrong # args: should be "::vim::command ?-quiet? exCommand"') |
| 155 | call assert_fails('tcl ::vim::command -foo xyz', 'unknown flag: -foo') |
| 156 | call assert_fails('tcl ::vim::command xyz', |
| 157 | \ 'E492: Not an editor command: xyz') |
| 158 | |
| 159 | " With the -quiet option, the error should silently be ignored. |
| 160 | call assert_equal('', TclEval('::vim::command -quiet xyz')) |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 161 | |
| 162 | tcl unset foo |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 163 | endfunc |
| 164 | |
| 165 | " Test ::vim::window list |
| 166 | func Test_vim_window_list() |
| 167 | e Xfoo1 |
| 168 | new Xfoo2 |
| 169 | let w2 = TclEval('set ::vim::current(window)') |
| 170 | wincmd j |
| 171 | let w1 = TclEval('set ::vim::current(window)') |
| 172 | |
| 173 | call assert_equal('2', TclEval('llength [::vim::window list]')) |
| 174 | call assert_equal(w2.' '.w1, TclEval('::vim::window list')) |
| 175 | |
| 176 | call assert_fails('tcl ::vim::window x', 'unknown option') |
| 177 | call assert_fails('tcl ::vim::window list x', |
| 178 | \ 'wrong # args: should be "::vim::window option"') |
| 179 | |
| 180 | %bwipe |
| 181 | endfunc |
| 182 | |
| 183 | " Test output messages |
| 184 | func Test_output() |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 185 | call assert_fails('tcl puts vimerr "error #1"', 'error #1') |
| 186 | call assert_fails('tcl puts stderr "error #2"', 'error #2') |
| 187 | tcl puts vimout "message #1" |
| 188 | tcl puts stdout "message #2" |
| 189 | tcl puts "message #3" |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 190 | let messages = split(execute('message'), "\n") |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 191 | call assert_equal('message #3', messages[-1]) |
| 192 | call assert_equal('message #2', messages[-2]) |
| 193 | call assert_equal('message #1', messages[-3]) |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 194 | |
| 195 | call assert_fails('tcl puts', |
| 196 | \ 'wrong # args: should be "puts ?-nonewline? ?channelId? string"') |
| 197 | endfunc |
| 198 | |
| 199 | " Test $win height (get and set window height) |
| 200 | func Test_window_height() |
| 201 | new |
| 202 | |
| 203 | " Test setting window height |
| 204 | tcl $::vim::current(window) height 2 |
| 205 | call assert_equal(2, winheight(0)) |
| 206 | |
| 207 | " Test getting window height |
| 208 | call assert_equal('2', TclEval('$::vim::current(window) height')) |
| 209 | |
| 210 | call assert_fails('tcl $::vim::current(window) height 2 2', 'wrong # args:') |
| 211 | call assert_fails('tcl $::vim::current(window) height x', |
| 212 | \ 'expected integer but got "x"') |
| 213 | bwipe |
| 214 | endfunc |
| 215 | |
| 216 | " Test $win cursor (get and set cursor) |
| 217 | func Test_window_cursor() |
| 218 | new |
| 219 | call setline(1, ['line1', 'line2', 'line3', 'line5']) |
| 220 | tcl set win $::vim::current(window) |
| 221 | |
| 222 | tcl $win cursor 2 4 |
| 223 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 224 | call assert_equal('row 2 column 4', TclEval('$win cursor')) |
| 225 | |
| 226 | " When setting ::vim::lbase to 0, line/col are counted from 0 |
| 227 | " instead of 1. |
| 228 | tcl set ::vim::lbase 0 |
| 229 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 230 | call assert_equal('row 1 column 3', TclEval('$win cursor')) |
| 231 | tcl $win cursor 2 4 |
| 232 | call assert_equal([0, 3, 5, 0], getpos('.')) |
| 233 | call assert_equal('row 2 column 4', TclEval('$win cursor')) |
| 234 | tcl set ::vim::lbase 1 |
| 235 | call assert_equal('row 3 column 5', TclEval('$win cursor')) |
| 236 | call assert_equal([0, 3, 5, 0], getpos('.')) |
| 237 | |
| 238 | " test $win cursor {$var} |
| 239 | call cursor(2, 3) |
| 240 | tcl array set here [$win cursor] |
| 241 | call assert_equal([0, 2, 3, 0], getpos('.')) |
| 242 | call cursor(3, 1) |
| 243 | call assert_equal([0, 3, 1, 0], getpos('.')) |
| 244 | tcl $win cursor here |
| 245 | call assert_equal([0, 2, 3, 0], getpos('.')) |
| 246 | call cursor(3, 1) |
| 247 | call assert_equal([0, 3, 1, 0], getpos('.')) |
| 248 | tcl $win cursor $here(row) $here(column) |
| 249 | call assert_equal([0, 2, 3, 0], getpos('.')) |
| 250 | |
| 251 | call assert_fails('tcl $win cursor 1 1 1', 'wrong # args:') |
| 252 | |
| 253 | tcl unset win here |
| 254 | bwipe! |
| 255 | endfunc |
| 256 | |
| 257 | " Test $win buffer |
| 258 | func Test_window_buffer() |
| 259 | new Xfoo1 |
| 260 | new Xfoo2 |
| 261 | tcl set b2 $::vim::current(buffer) |
| 262 | tcl set w2 $::vim::current(window) |
| 263 | wincmd j |
| 264 | tcl set b1 $::vim::current(buffer) |
| 265 | tcl set w1 $::vim::current(window) |
| 266 | |
| 267 | call assert_equal(TclEval('set b1'), TclEval('$w1 buffer')) |
| 268 | call assert_equal(TclEval('set b2'), TclEval('$w2 buffer')) |
| 269 | call assert_equal(string(bufnr('Xfoo1')), TclEval('[$w1 buffer] number')) |
| 270 | call assert_equal(string(bufnr('Xfoo2')), TclEval('[$w2 buffer] number')) |
| 271 | |
| 272 | call assert_fails('tcl $w1 buffer x', 'wrong # args:') |
| 273 | |
| 274 | tcl unset b1 b2 w1 w2 |
| 275 | %bwipe |
| 276 | endfunc |
| 277 | |
| 278 | " Test $win command |
| 279 | func Test_window_command() |
| 280 | new Xfoo1 |
| 281 | call setline(1, ['FOObar']) |
| 282 | new Xfoo2 |
| 283 | call setline(1, ['fooBAR']) |
| 284 | tcl set w2 $::vim::current(window) |
| 285 | wincmd j |
| 286 | tcl set w1 $::vim::current(window) |
| 287 | |
| 288 | tcl $w1 command "norm VU" |
| 289 | tcl $w2 command "norm Vu" |
| 290 | b! Xfoo1 |
| 291 | call assert_equal('FOOBAR', getline(1)) |
| 292 | b! Xfoo2 |
| 293 | call assert_equal('foobar', getline(1)) |
| 294 | |
| 295 | call assert_fails('tcl $w1 command xyz', |
| 296 | \ 'E492: Not an editor command: xyz') |
| 297 | tcl $w1 command -quiet xyz |
| 298 | |
| 299 | tcl unset w1 w2 |
| 300 | %bwipe! |
| 301 | endfunc |
| 302 | |
| 303 | " Test $win expr |
| 304 | func Test_window_expr() |
| 305 | new Xfoo1 |
| 306 | new Xfoo2 |
| 307 | tcl set w2 $::vim::current(window) |
| 308 | wincmd j |
| 309 | tcl set w1 $::vim::current(window) |
| 310 | |
| 311 | call assert_equal('Xfoo1', TclEval('$w1 expr bufname("%")')) |
| 312 | call assert_equal('Xfoo2', TclEval('$w2 expr bufname("%")')) |
| 313 | |
| 314 | call assert_fails('tcl $w1 expr', 'wrong # args:') |
| 315 | call assert_fails('tcl $w1 expr x x', 'wrong # args:') |
| 316 | |
| 317 | tcl unset w1 w2 |
| 318 | %bwipe |
| 319 | endfunc |
| 320 | |
| 321 | " Test $win option |
| 322 | func Test_window_option() |
| 323 | new Xfoo1 |
| 324 | new Xfoo2 |
| 325 | tcl set w2 $::vim::current(window) |
| 326 | wincmd j |
| 327 | tcl set w1 $::vim::current(window) |
| 328 | |
| 329 | " Test setting window option |
| 330 | tcl $w1 option syntax java |
| 331 | tcl $w2 option syntax rust |
| 332 | |
| 333 | call assert_equal('java', &syntax) |
| 334 | wincmd k |
| 335 | call assert_equal('rust', &syntax) |
| 336 | |
| 337 | " Test getting window option |
| 338 | call assert_equal('java', TclEval('$w1 option syntax')) |
| 339 | call assert_equal('rust', TclEval('$w2 option syntax')) |
| 340 | |
| 341 | tcl unset w1 w2 |
| 342 | %bwipe |
| 343 | endfunc |
| 344 | |
| 345 | " Test $win delcmd {cmd} |
| 346 | func Test_window_delcmd() |
| 347 | new |
| 348 | tcl $::vim::current(window) delcmd [list set msg "window deleted"] |
| 349 | call assert_fails('tcl set msg', "can't read \"msg\": no such variable") |
| 350 | q |
| 351 | call assert_equal('window deleted', TclEval('set msg')) |
| 352 | |
| 353 | call assert_fails('tcl $::vim::current(window) delcmd', 'wrong # args') |
| 354 | |
| 355 | tcl unset msg |
| 356 | bwipe |
| 357 | endfunc |
| 358 | |
| 359 | " Test $buf name |
| 360 | func Test_buffer_name() |
| 361 | " Test buffer name with a named buffer |
| 362 | new Xfoo |
| 363 | call assert_equal(expand('%:p'), TclEval('$::vim::current(buffer) name')) |
| 364 | bwipe |
| 365 | |
| 366 | " Test buffer name with an unnamed buffer |
| 367 | new |
| 368 | call assert_equal('', TclEval('$::vim::current(buffer) name')) |
| 369 | |
| 370 | call assert_fails('tcl $::vim::current(buffer) name x', 'wrong # args:') |
| 371 | |
| 372 | bwipe |
| 373 | endfunc |
| 374 | |
| 375 | " Test $buf number |
| 376 | func Test_buffer_number() |
| 377 | new |
| 378 | call assert_equal(string(bufnr('%')), TclEval('$::vim::current(buffer) number')) |
| 379 | new |
| 380 | call assert_equal(string(bufnr('%')), TclEval('$::vim::current(buffer) number')) |
| 381 | |
| 382 | call assert_fails('tcl $::vim::current(buffer) number x', 'wrong # args:') |
| 383 | |
| 384 | %bwipe |
| 385 | endfunc |
| 386 | |
| 387 | " Test $buf count and $buf last |
| 388 | func Test_buffer_count() |
| 389 | new |
| 390 | call setline(1, ['one', 'two', 'three']) |
| 391 | call assert_equal('3', TclEval('$::vim::current(buffer) count')) |
| 392 | call assert_equal('3', TclEval('$::vim::current(buffer) last')) |
| 393 | |
| 394 | " Check that $buf count and $buf last differ when ::vim::lbase is 0. |
| 395 | tcl set ::vim::lbase 0 |
| 396 | call assert_equal('3', TclEval('$::vim::current(buffer) count')) |
| 397 | call assert_equal('2', TclEval('$::vim::current(buffer) last')) |
| 398 | |
| 399 | call assert_fails('tcl $::vim::current(buffer) count x', 'wrong # args:') |
| 400 | call assert_fails('tcl $::vim::current(buffer) last x', 'wrong # args:') |
| 401 | |
| 402 | tcl set ::vim::lbase 1 |
| 403 | bwipe! |
| 404 | endfunc |
| 405 | |
| 406 | " Test $buf delete (delete line(s) in buffer) |
| 407 | func Test_buffer_delete() |
| 408 | new |
| 409 | call setline(1, ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']) |
| 410 | tcl $::vim::current(buffer) delete 4 6 |
| 411 | tcl $::vim::current(buffer) delete 2 |
| 412 | call assert_equal(['one', 'three', 'seven', 'eight'], getline(1, '$')) |
| 413 | |
| 414 | call assert_fails('tcl $::vim::current(buffer) delete -1', 'line number out of range') |
| 415 | call assert_fails('tcl $::vim::current(buffer) delete 0', 'line number out of range') |
| 416 | call assert_fails('tcl $::vim::current(buffer) delete 5', 'line number out of range') |
| 417 | |
| 418 | call assert_fails('tcl $::vim::current(buffer) delete', 'wrong # args:') |
| 419 | call assert_fails('tcl $::vim::current(buffer) delete 1 2 3', 'wrong # args:') |
| 420 | |
| 421 | bwipe! |
| 422 | endfunc |
| 423 | |
| 424 | " Test $buf insert (insert line(s) in buffer) |
| 425 | func Test_buffer_insert() |
| 426 | new |
| 427 | tcl set buf $::vim::current(buffer) |
| 428 | tcl $buf insert 1 "first" |
| 429 | tcl $buf insert 2 "second" |
| 430 | tcl $buf insert 2 "third" |
| 431 | tcl $buf insert 4 "fourth" |
| 432 | tcl $buf insert 1 "fifth" |
| 433 | call assert_equal(['fifth', 'first', 'third', 'second', 'fourth', ''], getline(1, '$')) |
| 434 | |
| 435 | call assert_fails('tcl $buf insert -1 "x"', 'line number out of range') |
| 436 | call assert_fails('tcl $buf insert 0 "x"', 'line number out of range') |
| 437 | call assert_fails('tcl $buf insert 7 "x"', 'line number out of range') |
| 438 | |
| 439 | tcl unset buf |
| 440 | bwipe! |
| 441 | endfunc |
| 442 | |
| 443 | " Test $buf append (append line in buffer) |
| 444 | func Test_buffer_append() |
| 445 | new |
| 446 | tcl set buf $::vim::current(buffer) |
| 447 | tcl $buf append 1 "first" |
| 448 | tcl $buf append 2 "second" |
| 449 | tcl $buf append 2 "third" |
| 450 | tcl $buf append 4 "fourth" |
| 451 | tcl $buf append 1 "fifth" |
| 452 | call assert_equal(['', 'fifth', 'first', 'third', 'second', 'fourth'], getline(1, '$')) |
| 453 | |
| 454 | call assert_fails('tcl $buf append -1 "x"', 'line number out of range') |
| 455 | call assert_fails('tcl $buf append 0 "x"', 'line number out of range') |
| 456 | call assert_fails('tcl $buf append 7 "x"', 'line number out of range') |
| 457 | |
| 458 | call assert_fails('tcl $buf append', 'wrong # args:') |
| 459 | call assert_fails('tcl $buf append 1 x x', 'wrong # args:') |
| 460 | |
| 461 | tcl unset buf |
| 462 | bwipe! |
| 463 | endfunc |
| 464 | |
| 465 | " Test $buf set (replacing line(s) in a buffer) |
| 466 | func Test_buffer_set() |
| 467 | new |
| 468 | call setline(1, ['line1', 'line2', 'line3', 'line4', 'line5']) |
| 469 | tcl $::vim::current(buffer) set 2 a |
| 470 | call assert_equal(['line1', 'a', 'line3', 'line4', 'line5'], getline(1, '$')) |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 471 | |
| 472 | " Test with fewer replacing lines than replaced lines: lines get deleted. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 473 | tcl $::vim::current(buffer) set 3 4 b |
| 474 | call assert_equal(['line1', 'a', 'b', 'line5'], getline(1, '$')) |
| 475 | tcl $::vim::current(buffer) set 4 3 c |
| 476 | call assert_equal(['line1', 'a', 'c'], getline(1, '$')) |
| 477 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 478 | " Test with more replacing lines than replaced lines: lines get added. |
| 479 | tcl $::vim::current(buffer) set 2 3 {x y z} |
| 480 | call assert_equal(['line1', 'x', 'y', 'z'], getline(1, '$')) |
| 481 | tcl $::vim::current(buffer) set 3 2 {X Y Z} |
| 482 | call assert_equal(['line1', 'X', 'Y', 'Z', 'z'], getline(1, '$')) |
| 483 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 484 | call assert_fails('tcl $::vim::current(buffer) set 0 "x"', 'line number out of range') |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 485 | call assert_fails('tcl $::vim::current(buffer) set 6 "x"', 'line number out of range') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 486 | |
| 487 | call assert_fails('tcl $::vim::current(buffer) set', 'wrong # args:') |
| 488 | bwipe! |
| 489 | endfunc |
| 490 | |
| 491 | " Test $buf get (get line(s) from buffer) |
| 492 | func Test_buffer_get() |
| 493 | new |
| 494 | call setline(1, ['first line', 'two', 'three', 'last line']) |
| 495 | tcl set buf $::vim::current(buffer) |
| 496 | |
| 497 | call assert_equal('first line', TclEval('$buf get top')) |
| 498 | call assert_equal('first line', TclEval('$buf get begin')) |
| 499 | call assert_equal('last line', TclEval('$buf get bottom')) |
| 500 | call assert_equal('last line', TclEval('$buf get last')) |
| 501 | |
| 502 | call assert_equal('first line', TclEval('$buf get 1')) |
| 503 | call assert_equal('two', TclEval('$buf get 2')) |
| 504 | call assert_equal('three', TclEval('$buf get 3')) |
| 505 | call assert_equal('last line', TclEval('$buf get 4')) |
| 506 | |
| 507 | call assert_equal('two three', TclEval('$buf get 2 3')) |
| 508 | call assert_equal('two three', TclEval('$buf get 3 2')) |
| 509 | call assert_equal('three {last line}', TclEval('$buf get 3 last')) |
| 510 | |
| 511 | call assert_fails('tcl $buf get -1', 'line number out of range') |
| 512 | call assert_fails('tcl $buf get 0', 'line number out of range') |
| 513 | call assert_fails('tcl $buf get 5', 'line number out of range') |
| 514 | call assert_fails('tcl $buf get 0 1', 'line number out of range') |
| 515 | |
| 516 | call assert_fails('tcl $::vim::current(buffer) get x', 'expected integer but got "x"') |
| 517 | call assert_fails('tcl $::vim::current(buffer) get 1 1 1', 'wrong # args:') |
| 518 | |
| 519 | tcl unset buf |
| 520 | bwipe! |
| 521 | endfunc |
| 522 | |
| 523 | " Test $buf mark (get position of a mark) |
| 524 | func Test_buffer_mark() |
| 525 | new |
| 526 | call setline(1, ['one', 'two', 'three', 'four']) |
| 527 | /three |
| 528 | norm! ma |
| 529 | norm! jllmB |
| 530 | |
| 531 | call assert_equal('row 3 column 1', TclEval('$::vim::current(buffer) mark a')) |
| 532 | call assert_equal('row 4 column 3', TclEval('$::vim::current(buffer) mark B')) |
| 533 | |
| 534 | call assert_fails('tcl $::vim::current(buffer) mark /', 'invalid mark name') |
| 535 | call assert_fails('tcl $::vim::current(buffer) mark z', 'mark not set') |
| 536 | call assert_fails('tcl $::vim::current(buffer) mark', 'wrong # args:') |
| 537 | |
| 538 | delmarks aB |
| 539 | bwipe! |
| 540 | endfunc |
| 541 | |
| 542 | " Test $buf option (test and set option in context of a buffer) |
| 543 | func Test_buffer_option() |
| 544 | new Xfoo1 |
| 545 | tcl set b1 $::vim::current(buffer) |
| 546 | new Xfoo2 |
| 547 | tcl set b2 $::vim::current(buffer) |
| 548 | |
| 549 | tcl $b1 option foldcolumn 2 |
| 550 | tcl $b2 option foldcolumn 3 |
| 551 | |
| 552 | call assert_equal(3, &foldcolumn) |
| 553 | wincmd j |
| 554 | call assert_equal(2, &foldcolumn) |
| 555 | |
| 556 | call assert_equal('2', TclEval('$b1 option foldcolumn')) |
| 557 | call assert_equal('3', TclEval('$b2 option foldcolumn')) |
| 558 | |
| 559 | call assert_fails('tcl $::vim::current(buffer) option', 'wrong # args:') |
| 560 | |
| 561 | set foldcolumn& |
| 562 | tcl unset b1 b2 |
| 563 | %bwipe |
| 564 | endfunc |
| 565 | |
| 566 | " Test $buf expr (evaluate vim expression) |
| 567 | func Test_buffer_expr() |
| 568 | new Xfoo1 |
| 569 | norm ifoo1 |
| 570 | tcl set b1 $::vim::current(buffer) |
| 571 | |
| 572 | new Xfoo2 |
| 573 | norm ifoo2 |
| 574 | tcl set b2 $::vim::current(buffer) |
| 575 | |
| 576 | call assert_equal('foo1', TclEval('$b1 expr getline(1)')) |
| 577 | call assert_equal('foo2', TclEval('$b2 expr getline(1)')) |
| 578 | |
| 579 | call assert_fails('tcl expr', 'wrong # args:') |
| 580 | |
| 581 | tcl unset b1 b2 |
| 582 | %bwipe! |
| 583 | endfunc |
| 584 | |
| 585 | " Test $buf delcmd {cmd} (command executed when buffer is deleted) |
| 586 | func Test_buffer_delcmd() |
| 587 | new Xfoo |
| 588 | split |
| 589 | tcl $::vim::current(buffer) delcmd [list set msg "buffer deleted"] |
| 590 | q |
| 591 | call assert_fails('tcl set msg', "can't read \"msg\": no such variable") |
| 592 | q |
| 593 | call assert_equal('buffer deleted', TclEval('set msg')) |
| 594 | |
| 595 | call assert_fails('tcl $::vim::current(window) delcmd', 'wrong # args') |
| 596 | call assert_fails('tcl $::vim::current(window) delcmd x x', 'wrong # args') |
| 597 | |
| 598 | tcl unset msg |
| 599 | %bwipe |
| 600 | endfunc |
| 601 | |
| 602 | func Test_vim_current() |
| 603 | " Only test errors as ::vim::current(...) is already indirectly |
| 604 | " tested by many other tests. |
| 605 | call assert_fails('tcl $::vim::current(buffer)', 'wrong # args:') |
| 606 | call assert_fails('tcl $::vim::current(window)', 'wrong # args:') |
| 607 | endfunc |
| 608 | |
| 609 | " Test $buf windows (windows list of a buffer) |
| 610 | func Test_buffer_windows() |
| 611 | new Xfoo |
| 612 | split |
| 613 | new Xbar |
| 614 | split |
| 615 | vsplit |
| 616 | |
| 617 | tcl set bar_wl [$::vim::current(buffer) windows] |
| 618 | 2wincmd j |
| 619 | tcl set foo_wl [$::vim::current(buffer) windows] |
| 620 | |
| 621 | call assert_equal('2', TclEval('llength $foo_wl')) |
| 622 | call assert_equal('3', TclEval('llength $bar_wl')) |
| 623 | |
| 624 | call assert_fails('tcl $::vim::current(buffer) windows x', 'wrong # args:') |
| 625 | |
| 626 | tcl unset bar_wl foo_wl |
| 627 | %bwipe |
| 628 | endfunc |
| 629 | |
| 630 | " Test :tclfile |
| 631 | func Test_tclfile() |
| 632 | call delete('Xtcl_file') |
| 633 | call writefile(['set pi [format "%.2f" [expr acos(-1.0)]]'], 'Xtcl_file') |
| 634 | call setfperm('Xtcl_file', 'r-xr-xr-x') |
| 635 | |
| 636 | tclfile Xtcl_file |
| 637 | call assert_equal('3.14', TclEval('set pi')) |
| 638 | |
| 639 | tcl unset pi |
| 640 | call delete('Xtcl_file') |
| 641 | endfunc |
| 642 | |
| 643 | " Test :tclfile with syntax error in tcl script |
| 644 | func Test_tclfile_error() |
| 645 | call delete('Xtcl_file') |
| 646 | call writefile(['xyz'], 'Xtcl_file') |
| 647 | call setfperm('Xtcl_file', 'r-xr-xr-x') |
| 648 | |
| 649 | call assert_fails('tclfile Xtcl_file', 'invalid command name "xyz"') |
| 650 | |
| 651 | call delete('Xtcl_file') |
| 652 | endfunc |
| 653 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 654 | " Test exiting current Tcl interpreter and re-creating one. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 655 | func Test_tcl_exit() |
Bram Moolenaar | 9b9be00 | 2020-03-22 14:41:22 +0100 | [diff] [blame] | 656 | call assert_fails('tcl exit 1 1', 'wrong # args: should be "exit ?returnCode?"') |
| 657 | call assert_fails('tcl exit x', 'expected integer but got "x"') |
| 658 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 659 | tcl set foo "foo" |
| 660 | call assert_fails('tcl exit 3', 'E572: exit code 3') |
| 661 | |
| 662 | " The Tcl interpreter should have been deleted and a new one |
| 663 | " is re-created with the next :tcl command. |
| 664 | call assert_fails('tcl set foo', "can't read \"foo\": no such variable") |
| 665 | tcl set bar "bar" |
| 666 | call assert_equal('bar', TclEval('set bar')) |
| 667 | |
| 668 | tcl unset bar |
| 669 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 670 | |
| 671 | func Test_set_cursor() |
| 672 | " Check that setting the cursor position works. |
| 673 | new |
| 674 | call setline(1, ['first line', 'second line']) |
| 675 | normal gg |
| 676 | tcldo $::vim::current(window) cursor 1 5 |
| 677 | call assert_equal([1, 5], [line('.'), col('.')]) |
| 678 | |
| 679 | " Check that movement after setting cursor position keeps current column. |
| 680 | normal j |
| 681 | call assert_equal([2, 5], [line('.'), col('.')]) |
| 682 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 683 | |
| 684 | " Test for different syntax for ruby heredoc |
| 685 | func Test_tcl_heredoc() |
| 686 | tcl << END |
| 687 | ::vim::command {let s = "A"} |
| 688 | END |
| 689 | tcl << |
| 690 | ::vim::command {let s ..= "B"} |
| 691 | . |
| 692 | tcl << trim END |
| 693 | ::vim::command {let s ..= "C"} |
| 694 | END |
| 695 | tcl << trim |
| 696 | ::vim::command {let s ..= "D"} |
| 697 | . |
| 698 | call assert_equal('ABCD', s) |
| 699 | endfunc |
| 700 | |
| 701 | " vim: shiftwidth=2 sts=2 expandtab |