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