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