Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 1 | " Tests for Perl interface |
| 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 4 | source shared.vim |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 5 | CheckFeature perl |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 6 | |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 7 | " FIXME: RunTest don't see any error when Perl abort... |
| 8 | perl $SIG{__WARN__} = sub { die "Unexpected warnings from perl: @_" }; |
| 9 | |
Bram Moolenaar | 021b593 | 2016-01-17 22:05:48 +0100 | [diff] [blame] | 10 | func Test_change_buffer() |
| 11 | call setline(line('$'), ['1 line 1']) |
| 12 | perl VIM::DoCommand("normal /^1\n") |
| 13 | perl $curline = VIM::Eval("line('.')") |
| 14 | perl $curbuf->Set($curline, "1 changed line 1") |
| 15 | call assert_equal('1 changed line 1', getline('$')) |
| 16 | endfunc |
| 17 | |
| 18 | func Test_evaluate_list() |
| 19 | call setline(line('$'), ['2 line 2']) |
| 20 | perl VIM::DoCommand("normal /^2\n") |
| 21 | perl $curline = VIM::Eval("line('.')") |
| 22 | let l = ["abc", "def"] |
| 23 | perl << EOF |
| 24 | $l = VIM::Eval("l"); |
| 25 | $curbuf->Append($curline, $l); |
| 26 | EOF |
| 27 | normal j |
| 28 | .perldo s|\n|/|g |
| 29 | call assert_equal('abc/def/', getline('$')) |
| 30 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 31 | |
Bram Moolenaar | 2472ae8 | 2019-02-23 15:04:17 +0100 | [diff] [blame] | 32 | funct Test_VIM_Blob() |
| 33 | call assert_equal('0z', perleval('VIM::Blob("")')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 34 | call assert_equal('0z31326162', 'VIM::Blob("12ab")'->perleval()) |
Bram Moolenaar | 2472ae8 | 2019-02-23 15:04:17 +0100 | [diff] [blame] | 35 | call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")')) |
| 36 | call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")')) |
| 37 | endfunc |
| 38 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 39 | func Test_buffer_Delete() |
| 40 | new |
| 41 | call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) |
| 42 | perl $curbuf->Delete(7) |
| 43 | perl $curbuf->Delete(2, 5) |
| 44 | perl $curbuf->Delete(10) |
| 45 | call assert_equal(['a', 'f', 'h'], getline(1, '$')) |
| 46 | bwipe! |
| 47 | endfunc |
| 48 | |
| 49 | func Test_buffer_Append() |
| 50 | new |
| 51 | perl $curbuf->Append(1, '1') |
| 52 | perl $curbuf->Append(2, '2', '3', '4') |
| 53 | perl @l = ('5' ..'7') |
| 54 | perl $curbuf->Append(0, @l) |
| 55 | call assert_equal(['5', '6', '7', '', '1', '2', '3', '4'], getline(1, '$')) |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 56 | |
| 57 | perl $curbuf->Append(0) |
| 58 | call assert_match('^Usage: VIBUF::Append(vimbuf, lnum, @lines) at .* line 1\.$', |
| 59 | \ GetMessages()[-1]) |
| 60 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 61 | bwipe! |
| 62 | endfunc |
| 63 | |
| 64 | func Test_buffer_Set() |
| 65 | new |
| 66 | call setline(1, ['1', '2', '3', '4', '5']) |
| 67 | perl $curbuf->Set(2, 'a', 'b', 'c') |
| 68 | perl $curbuf->Set(4, 'A', 'B', 'C') |
| 69 | call assert_equal(['1', 'a', 'b', 'A', 'B'], getline(1, '$')) |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 70 | |
| 71 | perl $curbuf->Set(0) |
| 72 | call assert_match('^Usage: VIBUF::Set(vimbuf, lnum, @lines) at .* line 1\.$', |
| 73 | \ GetMessages()[-1]) |
| 74 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 75 | bwipe! |
| 76 | endfunc |
| 77 | |
| 78 | func Test_buffer_Get() |
| 79 | new |
| 80 | call setline(1, ['1', '2', '3', '4']) |
| 81 | call assert_equal('2:3', perleval('join(":", $curbuf->Get(2, 3))')) |
| 82 | bwipe! |
| 83 | endfunc |
| 84 | |
| 85 | func Test_buffer_Count() |
| 86 | new |
| 87 | call setline(1, ['a', 'b', 'c']) |
| 88 | call assert_equal(3, perleval('$curbuf->Count()')) |
| 89 | bwipe! |
| 90 | endfunc |
| 91 | |
| 92 | func Test_buffer_Name() |
| 93 | new |
| 94 | call assert_equal('', perleval('$curbuf->Name()')) |
| 95 | bwipe! |
| 96 | new Xfoo |
| 97 | call assert_equal('Xfoo', perleval('$curbuf->Name()')) |
| 98 | bwipe! |
| 99 | endfunc |
| 100 | |
| 101 | func Test_buffer_Number() |
| 102 | call assert_equal(bufnr('%'), perleval('$curbuf->Number()')) |
| 103 | endfunc |
| 104 | |
| 105 | func Test_window_Cursor() |
| 106 | new |
| 107 | call setline(1, ['line1', 'line2']) |
| 108 | perl $curwin->Cursor(2, 3) |
| 109 | call assert_equal('2:3', perleval('join(":", $curwin->Cursor())')) |
| 110 | " Col is numbered from 0 in Perl, and from 1 in Vim script. |
| 111 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 112 | bwipe! |
| 113 | endfunc |
| 114 | |
| 115 | func Test_window_SetHeight() |
| 116 | new |
| 117 | perl $curwin->SetHeight(2) |
| 118 | call assert_equal(2, winheight(0)) |
| 119 | bwipe! |
| 120 | endfunc |
| 121 | |
| 122 | func Test_VIM_Windows() |
| 123 | new |
| 124 | " VIM::Windows() without argument in scalar and list context. |
| 125 | perl $winnr = VIM::Windows() |
| 126 | perl @winlist = VIM::Windows() |
| 127 | perl $curbuf->Append(0, $winnr, scalar(@winlist)) |
| 128 | call assert_equal(['2', '2', ''], getline(1, '$')) |
| 129 | |
| 130 | " VIM::Windows() with window number argument. |
| 131 | perl VIM::Windows(VIM::Eval('winnr()'))->Buffer()->Set(1, 'bar') |
| 132 | call assert_equal('bar', getline(1)) |
| 133 | bwipe! |
| 134 | endfunc |
| 135 | |
| 136 | func Test_VIM_Buffers() |
| 137 | new Xbar |
| 138 | " VIM::Buffers() without argument in scalar and list context. |
| 139 | perl $nbuf = VIM::Buffers() |
| 140 | perl @buflist = VIM::Buffers() |
| 141 | |
| 142 | " VIM::Buffers() with argument. |
| 143 | perl $mybuf = (VIM::Buffers('Xbar'))[0] |
| 144 | perl $mybuf->Append(0, $nbuf, scalar(@buflist)) |
| 145 | call assert_equal(['2', '2', ''], getline(1, '$')) |
| 146 | bwipe! |
| 147 | endfunc |
| 148 | |
| 149 | func <SID>catch_peval(expr) |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 150 | try |
| 151 | call perleval(a:expr) |
| 152 | catch |
| 153 | return v:exception |
| 154 | endtry |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 155 | call assert_report('no exception for `perleval("'.a:expr.'")`') |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 156 | return '' |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 157 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 158 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 159 | func Test_perleval() |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 160 | call assert_false(perleval('undef')) |
| 161 | |
| 162 | " scalar |
| 163 | call assert_equal(0, perleval('0')) |
| 164 | call assert_equal(2, perleval('2')) |
| 165 | call assert_equal(-2, perleval('-2')) |
| 166 | if has('float') |
| 167 | call assert_equal(2.5, perleval('2.5')) |
| 168 | else |
| 169 | call assert_equal(2, perleval('2.5')) |
| 170 | end |
| 171 | |
| 172 | sandbox call assert_equal(2, perleval('2')) |
| 173 | |
| 174 | call assert_equal('abc', perleval('"abc"')) |
| 175 | call assert_equal("abc\ndef", perleval('"abc\0def"')) |
| 176 | |
| 177 | " ref |
| 178 | call assert_equal([], perleval('[]')) |
| 179 | call assert_equal(['word', 42, [42],{}], perleval('["word", 42, [42], {}]')) |
| 180 | |
| 181 | call assert_equal({}, perleval('{}')) |
| 182 | call assert_equal({'foo': 'bar'}, perleval('{foo => "bar"}')) |
| 183 | |
| 184 | perl our %h; our @a; |
| 185 | let a = perleval('[\(%h, %h, @a, @a)]') |
| 186 | call assert_true((a[0] is a[1])) |
| 187 | call assert_true((a[2] is a[3])) |
| 188 | perl undef %h; undef @a; |
| 189 | |
| 190 | call assert_true(<SID>catch_peval('{"" , 0}') =~ 'Malformed key Dictionary') |
| 191 | call assert_true(<SID>catch_peval('{"\0" , 0}') =~ 'Malformed key Dictionary') |
| 192 | call assert_true(<SID>catch_peval('{"foo\0bar" , 0}') =~ 'Malformed key Dictionary') |
| 193 | |
| 194 | call assert_equal('*VIM', perleval('"*VIM"')) |
| 195 | call assert_true(perleval('\\0') =~ 'SCALAR(0x\x\+)') |
Bram Moolenaar | 730677a | 2020-12-09 16:05:45 +0100 | [diff] [blame] | 196 | |
| 197 | " typeglob |
| 198 | call assert_equal('*main::STDOUT', perleval('*STDOUT')) |
| 199 | ' |
| 200 | call perleval("++-$foo") |
| 201 | let messages = split(execute('message'), "\n") |
| 202 | call assert_match("Can't modify negation", messages[-1]) |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 203 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 204 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 205 | func Test_perldo() |
Bram Moolenaar | 730677a | 2020-12-09 16:05:45 +0100 | [diff] [blame] | 206 | new |
| 207 | " :perldo in empty buffer does nothing. |
| 208 | perldo ++$counter |
| 209 | call assert_equal(0, perleval("$counter")) |
| 210 | |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 211 | sp __TEST__ |
| 212 | exe 'read ' g:testname |
| 213 | perldo s/perl/vieux_chameau/g |
| 214 | 1 |
| 215 | call assert_false(search('\Cperl')) |
| 216 | bw! |
Bram Moolenaar | 85b5743 | 2017-01-29 22:59:12 +0100 | [diff] [blame] | 217 | |
| 218 | " Check deleting lines does not trigger ml_get error. |
| 219 | new |
| 220 | call setline(1, ['one', 'two', 'three']) |
| 221 | perldo VIM::DoCommand("%d_") |
| 222 | bwipe! |
| 223 | |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 224 | " Check a Perl expression which gives an error. |
| 225 | new |
| 226 | call setline(1, 'one') |
| 227 | perldo 1/0 |
| 228 | call assert_match('^Illegal division by zero at .* line 1\.$', GetMessages()[-1]) |
| 229 | bwipe! |
| 230 | |
Bram Moolenaar | 85b5743 | 2017-01-29 22:59:12 +0100 | [diff] [blame] | 231 | " Check switching to another buffer does not trigger ml_get error. |
| 232 | new |
| 233 | let wincount = winnr('$') |
| 234 | call setline(1, ['one', 'two', 'three']) |
| 235 | perldo VIM::DoCommand("new") |
| 236 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | 730677a | 2020-12-09 16:05:45 +0100 | [diff] [blame] | 237 | %bwipe! |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 238 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 239 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 240 | func Test_VIM_package() |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 241 | perl VIM::DoCommand('let l:var = "foo"') |
| 242 | call assert_equal(l:var, 'foo') |
| 243 | |
| 244 | set noet |
| 245 | perl VIM::SetOption('et') |
| 246 | call assert_true(&et) |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 247 | endfunc |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 248 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 249 | func Test_stdio() |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 250 | redir =>l:out |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 251 | perl << trim EOF |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 252 | VIM::Msg("VIM::Msg"); |
| 253 | VIM::Msg("VIM::Msg Error", "Error"); |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 254 | print "STDOUT"; |
| 255 | print STDERR "STDERR"; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 256 | EOF |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 257 | redir END |
Dominique Pelle | 588cf75 | 2021-05-10 23:49:39 +0200 | [diff] [blame] | 258 | call assert_equal(['VIM::Msg', 'VIM::Msg Error', 'STDOUT', 'STDERR'], split(l:out, "\n")) |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 259 | endfunc |
| 260 | |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 261 | " Run first to get a clean namespace |
| 262 | func Test_000_SvREFCNT() |
Bram Moolenaar | 3166afd | 2018-07-16 18:09:14 +0200 | [diff] [blame] | 263 | for i in range(8) |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 264 | exec 'new X'.i |
| 265 | endfor |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 266 | new t |
| 267 | perl <<--perl |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 268 | #line 5 "Test_000_SvREFCNT()" |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 269 | my ($b, $w); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 270 | |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 271 | my $num = 0; |
| 272 | for ( 0 .. 100 ) { |
| 273 | if ( ++$num >= 8 ) { $num = 0 } |
| 274 | VIM::DoCommand("buffer X$num"); |
| 275 | $b = $curbuf; |
| 276 | } |
| 277 | |
| 278 | VIM::DoCommand("buffer t"); |
| 279 | |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 280 | $b = $curbuf for 0 .. 100; |
| 281 | $w = $curwin for 0 .. 100; |
| 282 | () = VIM::Buffers for 0 .. 100; |
| 283 | () = VIM::Windows for 0 .. 100; |
| 284 | |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 285 | VIM::DoCommand('bw! t'); |
| 286 | if (exists &Internals::SvREFCNT) { |
| 287 | my $cb = Internals::SvREFCNT($$b); |
| 288 | my $cw = Internals::SvREFCNT($$w); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 289 | VIM::Eval("assert_equal(2, $cb, 'T1')"); |
| 290 | VIM::Eval("assert_equal(2, $cw, 'T2')"); |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 291 | my $strongref; |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 292 | foreach ( VIM::Buffers, VIM::Windows ) { |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 293 | VIM::DoCommand("%bw!"); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 294 | my $c = Internals::SvREFCNT($_); |
| 295 | VIM::Eval("assert_equal(2, $c, 'T3')"); |
| 296 | $c = Internals::SvREFCNT($$_); |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 297 | next if $c == 2 && !$strongref++; |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 298 | VIM::Eval("assert_equal(1, $c, 'T4')"); |
| 299 | } |
| 300 | $cb = Internals::SvREFCNT($$curbuf); |
| 301 | $cw = Internals::SvREFCNT($$curwin); |
| 302 | VIM::Eval("assert_equal(3, $cb, 'T5')"); |
| 303 | VIM::Eval("assert_equal(3, $cw, 'T6')"); |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 304 | } |
| 305 | VIM::Eval("assert_false($$b)"); |
| 306 | VIM::Eval("assert_false($$w)"); |
| 307 | --perl |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 308 | %bw! |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 309 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 310 | |
| 311 | func Test_set_cursor() |
| 312 | " Check that setting the cursor position works. |
| 313 | new |
| 314 | call setline(1, ['first line', 'second line']) |
| 315 | normal gg |
| 316 | perldo $curwin->Cursor(1, 5) |
| 317 | call assert_equal([1, 6], [line('.'), col('.')]) |
| 318 | |
| 319 | " Check that movement after setting cursor position keeps current column. |
| 320 | normal j |
| 321 | call assert_equal([2, 6], [line('.'), col('.')]) |
| 322 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 323 | |
| 324 | " Test for various heredoc syntax |
| 325 | func Test_perl_heredoc() |
| 326 | perl << END |
| 327 | VIM::DoCommand('let s = "A"') |
| 328 | END |
| 329 | perl << |
| 330 | VIM::DoCommand('let s ..= "B"') |
| 331 | . |
| 332 | perl << trim END |
| 333 | VIM::DoCommand('let s ..= "C"') |
| 334 | END |
| 335 | perl << trim |
| 336 | VIM::DoCommand('let s ..= "D"') |
| 337 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 338 | perl << trim eof |
| 339 | VIM::DoCommand('let s ..= "E"') |
| 340 | eof |
| 341 | call assert_equal('ABCDE', s) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 342 | endfunc |
| 343 | |
Bram Moolenaar | 730677a | 2020-12-09 16:05:45 +0100 | [diff] [blame] | 344 | func Test_perl_in_sandbox() |
| 345 | sandbox perl print 'test' |
| 346 | let messages = split(execute('message'), "\n") |
| 347 | call assert_match("'print' trapped by operation mask", messages[-1]) |
| 348 | endfunc |
| 349 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 350 | " vim: shiftwidth=2 sts=2 expandtab |