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