| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::CheckLib qw( find_lib ); |
| 3 |
use FFI::Platypus; |
| 4 |
use Carp (); |
| 5 |
|
| 6 |
my $ffi = FFI::Platypus->new; |
| 7 |
$ffi->lib(find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 8 |
|
| 9 |
subtest 'attached function' => sub { |
| 10 |
|
| 11 |
$ffi->attach( f0 => ['uint8'] => 'uint8' => sub { |
| 12 |
package Foo::Bar; |
| 13 |
my($xsub, $arg) = @_; |
| 14 |
Carp::croak "here"; |
| 15 |
$xsub->($arg); |
| 16 |
}); |
| 17 |
|
| 18 |
local $@ = ''; |
| 19 |
eval { f0(1) }; my $line = __LINE__; |
| 20 |
like "$@", qr/^here .*gh129\.t line \Q$line\E/; |
| 21 |
|
| 22 |
}; |
| 23 |
|
| 24 |
subtest 'dynamic function' => sub { |
| 25 |
|
| 26 |
my $f0 = $ffi->function( f0 => ['uint8'] => 'uint8' => sub { |
| 27 |
package Foo::Bar; |
| 28 |
my($xsub, $arg) = @_; |
| 29 |
Carp::croak "here"; |
| 30 |
$xsub->($arg); |
| 31 |
}); |
| 32 |
|
| 33 |
local $@ = ''; |
| 34 |
eval { $f0->call(1) }; my $line = __LINE__; |
| 35 |
like "$@", qr/^here .*gh129\.t line \Q$line\E/; |
| 36 |
|
| 37 |
}; |
| 38 |
|
| 39 |
subtest 'type wrapper argument' => sub { |
| 40 |
|
| 41 |
$ffi->custom_type( foo_t => { |
| 42 |
native_type => 'uint8', |
| 43 |
perl_to_native => sub { |
| 44 |
package Foo::Bar; |
| 45 |
Carp::croak "here"; |
| 46 |
}, |
| 47 |
}); |
| 48 |
|
| 49 |
my $f0 = $ffi->function( f0 => ['foo_t'] => 'uint8'); |
| 50 |
|
| 51 |
local $@ = ''; |
| 52 |
eval { $f0->call(22) }; my $line = __LINE__; |
| 53 |
like "$@", qr/^here .*gh129\.t line \Q$line\E/; |
| 54 |
|
| 55 |
}; |
| 56 |
|
| 57 |
subtest 'type wrapper argument post' => sub { |
| 58 |
|
| 59 |
$ffi->custom_type( baz_t => { |
| 60 |
native_type => 'uint8', |
| 61 |
perl_to_native_post => sub { |
| 62 |
package Foo::Bar; |
| 63 |
Carp::croak "here"; |
| 64 |
}, |
| 65 |
}); |
| 66 |
|
| 67 |
my $f0 = $ffi->function( f0 => ['baz_t'] => 'uint8'); |
| 68 |
|
| 69 |
local $@ = ''; |
| 70 |
eval { $f0->call(22) }; my $line = __LINE__; |
| 71 |
like "$@", qr/^here .*gh129\.t line \Q$line\E/; |
| 72 |
|
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'type wrapper return type' => sub { |
| 76 |
|
| 77 |
$ffi->custom_type( bar_t => { |
| 78 |
native_type => 'uint8', |
| 79 |
native_to_perl => sub { |
| 80 |
package Foo::Bar; |
| 81 |
Carp::croak "here"; |
| 82 |
}, |
| 83 |
}); |
| 84 |
|
| 85 |
my $f0 = $ffi->function( f0 => ['uint8'] => 'bar_t'); |
| 86 |
|
| 87 |
local $@ = ''; |
| 88 |
eval { $f0->call(22) }; my $line = __LINE__; |
| 89 |
like "$@", qr/^here .*gh129\.t line \Q$line\E/; |
| 90 |
|
| 91 |
}; |
| 92 |
|
| 93 |
done_testing; |