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