Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 1 | " Tests for Perl interface |
| 2 | |
| 3 | if !has('perl') |
Bram Moolenaar | b0f94c1 | 2019-06-13 22:19:53 +0200 | [diff] [blame] | 4 | throw 'Skipped, perl feature missing' |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 5 | end |
| 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("")')) |
| 34 | call assert_equal('0z31326162', perleval('VIM::Blob("12ab")')) |
| 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, '$')) |
| 56 | bwipe! |
| 57 | endfunc |
| 58 | |
| 59 | func Test_buffer_Set() |
| 60 | new |
| 61 | call setline(1, ['1', '2', '3', '4', '5']) |
| 62 | perl $curbuf->Set(2, 'a', 'b', 'c') |
| 63 | perl $curbuf->Set(4, 'A', 'B', 'C') |
| 64 | call assert_equal(['1', 'a', 'b', 'A', 'B'], getline(1, '$')) |
| 65 | bwipe! |
| 66 | endfunc |
| 67 | |
| 68 | func Test_buffer_Get() |
| 69 | new |
| 70 | call setline(1, ['1', '2', '3', '4']) |
| 71 | call assert_equal('2:3', perleval('join(":", $curbuf->Get(2, 3))')) |
| 72 | bwipe! |
| 73 | endfunc |
| 74 | |
| 75 | func Test_buffer_Count() |
| 76 | new |
| 77 | call setline(1, ['a', 'b', 'c']) |
| 78 | call assert_equal(3, perleval('$curbuf->Count()')) |
| 79 | bwipe! |
| 80 | endfunc |
| 81 | |
| 82 | func Test_buffer_Name() |
| 83 | new |
| 84 | call assert_equal('', perleval('$curbuf->Name()')) |
| 85 | bwipe! |
| 86 | new Xfoo |
| 87 | call assert_equal('Xfoo', perleval('$curbuf->Name()')) |
| 88 | bwipe! |
| 89 | endfunc |
| 90 | |
| 91 | func Test_buffer_Number() |
| 92 | call assert_equal(bufnr('%'), perleval('$curbuf->Number()')) |
| 93 | endfunc |
| 94 | |
| 95 | func Test_window_Cursor() |
| 96 | new |
| 97 | call setline(1, ['line1', 'line2']) |
| 98 | perl $curwin->Cursor(2, 3) |
| 99 | call assert_equal('2:3', perleval('join(":", $curwin->Cursor())')) |
| 100 | " Col is numbered from 0 in Perl, and from 1 in Vim script. |
| 101 | call assert_equal([0, 2, 4, 0], getpos('.')) |
| 102 | bwipe! |
| 103 | endfunc |
| 104 | |
| 105 | func Test_window_SetHeight() |
| 106 | new |
| 107 | perl $curwin->SetHeight(2) |
| 108 | call assert_equal(2, winheight(0)) |
| 109 | bwipe! |
| 110 | endfunc |
| 111 | |
| 112 | func Test_VIM_Windows() |
| 113 | new |
| 114 | " VIM::Windows() without argument in scalar and list context. |
| 115 | perl $winnr = VIM::Windows() |
| 116 | perl @winlist = VIM::Windows() |
| 117 | perl $curbuf->Append(0, $winnr, scalar(@winlist)) |
| 118 | call assert_equal(['2', '2', ''], getline(1, '$')) |
| 119 | |
| 120 | " VIM::Windows() with window number argument. |
| 121 | perl VIM::Windows(VIM::Eval('winnr()'))->Buffer()->Set(1, 'bar') |
| 122 | call assert_equal('bar', getline(1)) |
| 123 | bwipe! |
| 124 | endfunc |
| 125 | |
| 126 | func Test_VIM_Buffers() |
| 127 | new Xbar |
| 128 | " VIM::Buffers() without argument in scalar and list context. |
| 129 | perl $nbuf = VIM::Buffers() |
| 130 | perl @buflist = VIM::Buffers() |
| 131 | |
| 132 | " VIM::Buffers() with argument. |
| 133 | perl $mybuf = (VIM::Buffers('Xbar'))[0] |
| 134 | perl $mybuf->Append(0, $nbuf, scalar(@buflist)) |
| 135 | call assert_equal(['2', '2', ''], getline(1, '$')) |
| 136 | bwipe! |
| 137 | endfunc |
| 138 | |
| 139 | func <SID>catch_peval(expr) |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 140 | try |
| 141 | call perleval(a:expr) |
| 142 | catch |
| 143 | return v:exception |
| 144 | endtry |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 145 | call assert_report('no exception for `perleval("'.a:expr.'")`') |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 146 | return '' |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 147 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 148 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 149 | func Test_perleval() |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 150 | call assert_false(perleval('undef')) |
| 151 | |
| 152 | " scalar |
| 153 | call assert_equal(0, perleval('0')) |
| 154 | call assert_equal(2, perleval('2')) |
| 155 | call assert_equal(-2, perleval('-2')) |
| 156 | if has('float') |
| 157 | call assert_equal(2.5, perleval('2.5')) |
| 158 | else |
| 159 | call assert_equal(2, perleval('2.5')) |
| 160 | end |
| 161 | |
| 162 | sandbox call assert_equal(2, perleval('2')) |
| 163 | |
| 164 | call assert_equal('abc', perleval('"abc"')) |
| 165 | call assert_equal("abc\ndef", perleval('"abc\0def"')) |
| 166 | |
| 167 | " ref |
| 168 | call assert_equal([], perleval('[]')) |
| 169 | call assert_equal(['word', 42, [42],{}], perleval('["word", 42, [42], {}]')) |
| 170 | |
| 171 | call assert_equal({}, perleval('{}')) |
| 172 | call assert_equal({'foo': 'bar'}, perleval('{foo => "bar"}')) |
| 173 | |
| 174 | perl our %h; our @a; |
| 175 | let a = perleval('[\(%h, %h, @a, @a)]') |
| 176 | call assert_true((a[0] is a[1])) |
| 177 | call assert_true((a[2] is a[3])) |
| 178 | perl undef %h; undef @a; |
| 179 | |
| 180 | call assert_true(<SID>catch_peval('{"" , 0}') =~ 'Malformed key Dictionary') |
| 181 | call assert_true(<SID>catch_peval('{"\0" , 0}') =~ 'Malformed key Dictionary') |
| 182 | call assert_true(<SID>catch_peval('{"foo\0bar" , 0}') =~ 'Malformed key Dictionary') |
| 183 | |
| 184 | call assert_equal('*VIM', perleval('"*VIM"')) |
| 185 | call assert_true(perleval('\\0') =~ 'SCALAR(0x\x\+)') |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 186 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 187 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 188 | func Test_perldo() |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 189 | sp __TEST__ |
| 190 | exe 'read ' g:testname |
| 191 | perldo s/perl/vieux_chameau/g |
| 192 | 1 |
| 193 | call assert_false(search('\Cperl')) |
| 194 | bw! |
Bram Moolenaar | 85b5743 | 2017-01-29 22:59:12 +0100 | [diff] [blame] | 195 | |
| 196 | " Check deleting lines does not trigger ml_get error. |
| 197 | new |
| 198 | call setline(1, ['one', 'two', 'three']) |
| 199 | perldo VIM::DoCommand("%d_") |
| 200 | bwipe! |
| 201 | |
| 202 | " Check switching to another buffer does not trigger ml_get error. |
| 203 | new |
| 204 | let wincount = winnr('$') |
| 205 | call setline(1, ['one', 'two', 'three']) |
| 206 | perldo VIM::DoCommand("new") |
| 207 | call assert_equal(wincount + 1, winnr('$')) |
| 208 | bwipe! |
| 209 | bwipe! |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 210 | endfunc |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 211 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 212 | func Test_VIM_package() |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 213 | perl VIM::DoCommand('let l:var = "foo"') |
| 214 | call assert_equal(l:var, 'foo') |
| 215 | |
| 216 | set noet |
| 217 | perl VIM::SetOption('et') |
| 218 | call assert_true(&et) |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 219 | endfunc |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 220 | |
Bram Moolenaar | ae177b7 | 2017-02-23 13:45:57 +0100 | [diff] [blame] | 221 | func Test_stdio() |
Bram Moolenaar | 6244a0f | 2016-04-14 14:09:25 +0200 | [diff] [blame] | 222 | redir =>l:out |
| 223 | perl <<EOF |
| 224 | VIM::Msg("&VIM::Msg"); |
| 225 | print "STDOUT"; |
| 226 | print STDERR "STDERR"; |
| 227 | EOF |
| 228 | redir END |
| 229 | call assert_equal(['&VIM::Msg', 'STDOUT', 'STDERR'], split(l:out, "\n")) |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 230 | endfunc |
| 231 | |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 232 | " Run first to get a clean namespace |
| 233 | func Test_000_SvREFCNT() |
Bram Moolenaar | 3166afd | 2018-07-16 18:09:14 +0200 | [diff] [blame] | 234 | for i in range(8) |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 235 | exec 'new X'.i |
| 236 | endfor |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 237 | new t |
| 238 | perl <<--perl |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 239 | #line 5 "Test_000_SvREFCNT()" |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 240 | my ($b, $w); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 241 | |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 242 | my $num = 0; |
| 243 | for ( 0 .. 100 ) { |
| 244 | if ( ++$num >= 8 ) { $num = 0 } |
| 245 | VIM::DoCommand("buffer X$num"); |
| 246 | $b = $curbuf; |
| 247 | } |
| 248 | |
| 249 | VIM::DoCommand("buffer t"); |
| 250 | |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 251 | $b = $curbuf for 0 .. 100; |
| 252 | $w = $curwin for 0 .. 100; |
| 253 | () = VIM::Buffers for 0 .. 100; |
| 254 | () = VIM::Windows for 0 .. 100; |
| 255 | |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 256 | VIM::DoCommand('bw! t'); |
| 257 | if (exists &Internals::SvREFCNT) { |
| 258 | my $cb = Internals::SvREFCNT($$b); |
| 259 | my $cw = Internals::SvREFCNT($$w); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 260 | VIM::Eval("assert_equal(2, $cb, 'T1')"); |
| 261 | VIM::Eval("assert_equal(2, $cw, 'T2')"); |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 262 | my $strongref; |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 263 | foreach ( VIM::Buffers, VIM::Windows ) { |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 264 | VIM::DoCommand("%bw!"); |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 265 | my $c = Internals::SvREFCNT($_); |
| 266 | VIM::Eval("assert_equal(2, $c, 'T3')"); |
| 267 | $c = Internals::SvREFCNT($$_); |
Bram Moolenaar | 41c363a | 2018-08-02 21:46:51 +0200 | [diff] [blame] | 268 | next if $c == 2 && !$strongref++; |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 269 | VIM::Eval("assert_equal(1, $c, 'T4')"); |
| 270 | } |
| 271 | $cb = Internals::SvREFCNT($$curbuf); |
| 272 | $cw = Internals::SvREFCNT($$curwin); |
| 273 | VIM::Eval("assert_equal(3, $cb, 'T5')"); |
| 274 | VIM::Eval("assert_equal(3, $cw, 'T6')"); |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 275 | } |
| 276 | VIM::Eval("assert_false($$b)"); |
| 277 | VIM::Eval("assert_false($$w)"); |
| 278 | --perl |
Bram Moolenaar | 18c4f1b | 2018-07-16 17:45:38 +0200 | [diff] [blame] | 279 | %bw! |
Bram Moolenaar | 95509e1 | 2016-04-15 21:16:11 +0200 | [diff] [blame] | 280 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 281 | |
| 282 | func Test_set_cursor() |
| 283 | " Check that setting the cursor position works. |
| 284 | new |
| 285 | call setline(1, ['first line', 'second line']) |
| 286 | normal gg |
| 287 | perldo $curwin->Cursor(1, 5) |
| 288 | call assert_equal([1, 6], [line('.'), col('.')]) |
| 289 | |
| 290 | " Check that movement after setting cursor position keeps current column. |
| 291 | normal j |
| 292 | call assert_equal([2, 6], [line('.'), col('.')]) |
| 293 | endfunc |