Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 1 | " Tests for the Tcl interface. |
| 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | CheckFeature tcl |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 4 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 5 | " Helper function as there is no builtin tcleval() function similar |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 6 | " to perleval, luaeval(), pyeval(), etc. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 7 | func TclEval(tcl_expr) |
| 8 | let s = split(execute('tcl ' . a:tcl_expr), "\n") |
| 9 | return (len(s) == 0) ? '' : s[-1] |
| 10 | endfunc |
| 11 | |
| 12 | func Test_tcldo() |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 13 | new |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 14 | |
| 15 | " Check deleting lines does not trigger ml_get error. |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 16 | call setline(1, ['one', 'two', 'three']) |
| 17 | tcldo ::vim::command %d_ |
zeertzjq | e99f068 | 2024-01-29 19:32:39 +0100 | [diff] [blame] | 18 | 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 Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 32 | bwipe! |
| 33 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 34 | " Check that switching to another buffer does not trigger ml_get error. |
Bram Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 35 | 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 40 | |
| 41 | " Try to run a command in a 'nomodifiable' buffer |
| 42 | call setline(1, ['one', 'two', 'three']) |
| 43 | set nomodifiable |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 44 | call assert_fails('tcldo set line "abc"', |
| 45 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 46 | set modifiable |
| 47 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 48 | %bwipe! |
| 49 | endfunc |
| 50 | |
| 51 | " Test :tcldo with a range |
| 52 | func 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 Moolenaar | a4c906a | 2017-01-29 23:26:37 +0100 | [diff] [blame] | 57 | bwipe! |
| 58 | endfunc |
| 59 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 60 | " Test ::vim::beep |
| 61 | func Test_vim_beep() |
| 62 | call assert_beeps('tcl ::vim::beep') |
| 63 | call assert_fails('tcl ::vim::beep x', 'wrong # args: should be "::vim::beep"') |
| 64 | endfunc |
| 65 | |
| 66 | " Test ::vim::buffer |
| 67 | func 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 Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 92 | tcl << trim EOF |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 93 | proc eachbuf { cmd } { |
| 94 | foreach b [::vim::buffer list] { $b command $cmd } |
| 95 | } |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 96 | EOF |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 97 | 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 116 | " Invalid buffer command |
| 117 | call assert_fails('tcl $::vim::current(buffer) abcd', |
| 118 | \ 'bad option "abcd":') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 119 | |
| 120 | tcl rename eachbuf "" |
| 121 | %bwipe! |
| 122 | endfunc |
| 123 | |
| 124 | " Test ::vim::option |
| 125 | func 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 Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 137 | " 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 148 | 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& |
| 153 | endfunc |
| 154 | |
| 155 | " Test ::vim::expr |
| 156 | func 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 Moolenaar | dcfc311 | 2021-05-16 20:06:59 +0200 | [diff] [blame] | 162 | call assert_fails('tcl ::vim::expr 1-', 'E15: Invalid expression: "1-"') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 163 | endfunc |
| 164 | |
| 165 | " Test ::vim::command |
| 166 | func Test_vim_command() |
| 167 | call assert_equal('hello world', |
| 168 | \ TclEval('::vim::command {echo "hello world"}')) |
| 169 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 170 | " 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 175 | " 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 Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 186 | |
| 187 | tcl unset foo |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 188 | endfunc |
| 189 | |
| 190 | " Test ::vim::window list |
| 191 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 204 | call assert_fails('tcl $::vim::current(window) abcd', |
| 205 | \ 'bad option "abcd":') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 206 | |
| 207 | %bwipe |
| 208 | endfunc |
| 209 | |
| 210 | " Test output messages |
| 211 | func Test_output() |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 212 | 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 217 | let messages = split(execute('message'), "\n") |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 218 | call assert_equal('message #3', messages[-1]) |
| 219 | call assert_equal('message #2', messages[-2]) |
| 220 | call assert_equal('message #1', messages[-3]) |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 221 | |
| 222 | call assert_fails('tcl puts', |
| 223 | \ 'wrong # args: should be "puts ?-nonewline? ?channelId? string"') |
| 224 | endfunc |
| 225 | |
| 226 | " Test $win height (get and set window height) |
| 227 | func 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 |
| 241 | endfunc |
| 242 | |
| 243 | " Test $win cursor (get and set cursor) |
| 244 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 278 | " 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 291 | call assert_fails('tcl $win cursor 1 1 1', 'wrong # args:') |
| 292 | |
| 293 | tcl unset win here |
| 294 | bwipe! |
| 295 | endfunc |
| 296 | |
| 297 | " Test $win buffer |
| 298 | func 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 |
| 316 | endfunc |
| 317 | |
| 318 | " Test $win command |
| 319 | func 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! |
| 341 | endfunc |
| 342 | |
| 343 | " Test $win expr |
| 344 | func 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 |
| 359 | endfunc |
| 360 | |
| 361 | " Test $win option |
| 362 | func 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 |
| 383 | endfunc |
| 384 | |
| 385 | " Test $win delcmd {cmd} |
| 386 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 394 | call assert_fails('tcl $::vim::current(window) delcmd x x', 'wrong # args') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 395 | |
| 396 | tcl unset msg |
| 397 | bwipe |
| 398 | endfunc |
| 399 | |
| 400 | " Test $buf name |
| 401 | func 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 |
| 414 | endfunc |
| 415 | |
| 416 | " Test $buf number |
| 417 | func 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 |
| 426 | endfunc |
| 427 | |
| 428 | " Test $buf count and $buf last |
| 429 | func 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! |
| 445 | endfunc |
| 446 | |
| 447 | " Test $buf delete (delete line(s) in buffer) |
| 448 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 461 | 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 Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 467 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 468 | set modifiable |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 469 | |
| 470 | bwipe! |
| 471 | endfunc |
| 472 | |
| 473 | " Test $buf insert (insert line(s) in buffer) |
| 474 | func 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! |
| 490 | endfunc |
| 491 | |
| 492 | " Test $buf append (append line in buffer) |
| 493 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 510 | " Try to append lines to a 'nomodifiable' buffer |
| 511 | set nomodifiable |
| 512 | call assert_fails('tcl $buf append 1 "first"', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 513 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 514 | set modifiable |
| 515 | |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 516 | tcl unset buf |
| 517 | bwipe! |
| 518 | endfunc |
| 519 | |
| 520 | " Test $buf set (replacing line(s) in a buffer) |
| 521 | func 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 Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 526 | |
| 527 | " Test with fewer replacing lines than replaced lines: lines get deleted. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 528 | 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 Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 533 | " 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 539 | call assert_fails('tcl $::vim::current(buffer) set 0 "x"', 'line number out of range') |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 540 | call assert_fails('tcl $::vim::current(buffer) set 6 "x"', 'line number out of range') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 541 | |
| 542 | call assert_fails('tcl $::vim::current(buffer) set', 'wrong # args:') |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 543 | 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 Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 549 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 550 | call assert_fails('tcl $::vim::current(buffer) set 1 {a b}', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 551 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 552 | call assert_fails('tcl $::vim::current(buffer) set 1 2 {a b}', |
Bram Moolenaar | ecdd14a | 2020-07-11 22:49:59 +0200 | [diff] [blame] | 553 | \ ['E21:', 'cannot save undo information']) |
Bram Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 554 | set modifiable |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 555 | bwipe! |
| 556 | endfunc |
| 557 | |
| 558 | " Test $buf get (get line(s) from buffer) |
| 559 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 584 | call assert_fails('tcl $::vim::current(buffer) get 1 x', 'expected integer but got "x"') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 585 | call assert_fails('tcl $::vim::current(buffer) get 1 1 1', 'wrong # args:') |
| 586 | |
| 587 | tcl unset buf |
| 588 | bwipe! |
| 589 | endfunc |
| 590 | |
| 591 | " Test $buf mark (get position of a mark) |
| 592 | func 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! |
| 608 | endfunc |
| 609 | |
| 610 | " Test $buf option (test and set option in context of a buffer) |
| 611 | func 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 |
| 632 | endfunc |
| 633 | |
| 634 | " Test $buf expr (evaluate vim expression) |
| 635 | func 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! |
| 651 | endfunc |
| 652 | |
| 653 | " Test $buf delcmd {cmd} (command executed when buffer is deleted) |
| 654 | func 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 Moolenaar | e435890 | 2020-07-09 18:49:23 +0200 | [diff] [blame] | 663 | call assert_fails('tcl $::vim::current(buffer) delcmd', 'wrong # args') |
| 664 | call assert_fails('tcl $::vim::current(buffer) delcmd x x', 'wrong # args') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 665 | |
| 666 | tcl unset msg |
| 667 | %bwipe |
| 668 | endfunc |
| 669 | |
| 670 | func 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:') |
| 675 | endfunc |
| 676 | |
| 677 | " Test $buf windows (windows list of a buffer) |
| 678 | func 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 |
| 696 | endfunc |
| 697 | |
| 698 | " Test :tclfile |
| 699 | func Test_tclfile() |
| 700 | call delete('Xtcl_file') |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 701 | call writefile(['set pi [format "%.2f" [expr acos(-1.0)]]'], 'Xtcl_file', 'D') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 702 | 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 708 | endfunc |
| 709 | |
| 710 | " Test :tclfile with syntax error in tcl script |
| 711 | func Test_tclfile_error() |
| 712 | call delete('Xtcl_file') |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 713 | call writefile(['xyz'], 'Xtcl_file', 'D') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 714 | call setfperm('Xtcl_file', 'r-xr-xr-x') |
| 715 | |
| 716 | call assert_fails('tclfile Xtcl_file', 'invalid command name "xyz"') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 717 | endfunc |
| 718 | |
Bram Moolenaar | 2549acf | 2018-07-07 22:42:01 +0200 | [diff] [blame] | 719 | " Test exiting current Tcl interpreter and re-creating one. |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 720 | func Test_tcl_exit() |
Bram Moolenaar | 9b9be00 | 2020-03-22 14:41:22 +0100 | [diff] [blame] | 721 | 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 Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 724 | tcl set foo "foo" |
Bram Moolenaar | ec89223 | 2022-05-06 17:53:06 +0100 | [diff] [blame] | 725 | call assert_fails('tcl exit 3', 'E572: Exit code 3') |
Bram Moolenaar | fd34ceb | 2018-07-04 22:36:46 +0200 | [diff] [blame] | 726 | |
| 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 |
| 734 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 735 | |
| 736 | func 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('.')]) |
| 747 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 748 | |
| 749 | " Test for different syntax for ruby heredoc |
| 750 | func Test_tcl_heredoc() |
| 751 | tcl << END |
| 752 | ::vim::command {let s = "A"} |
| 753 | END |
| 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) |
| 764 | endfunc |
| 765 | |
| 766 | " vim: shiftwidth=2 sts=2 expandtab |