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