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