Debian Codegraph @

t/ffi_platypus_function_wrapper.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus::Function;
3 use FFI::Platypus;
4 use FFI::CheckLib;
5
6 my $libtest = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
7
8 subtest 'built in type' => sub {
9 my $ffi = FFI::Platypus->new;
10 $ffi->lib($libtest);
11 my $wrapper = sub {
12 my($xsub, $arg1) = @_;
13 $xsub->( $arg1 * 2 );
14 };
15 my $function = eval { $ffi->function('f0', [ 'uint8' ] => 'uint8', $wrapper ) };
16 is $@, '', 'ffi.function(f0, [uint8] => uint8)';
17 isa_ok $function, 'FFI::Platypus::Function';
18 isa_ok $function, 'FFI::Platypus::Function::Wrapper';
19 is $function->call(22), 44, 'function.call(22) = 44';
20 is $function->(22), 44, 'function.(22) = 44';
21
22 $function->attach('baboon');
23 is( baboon(11), 22, "baboon(11) = 22" );
24 };
25
26 subtest 'sub_ref' => sub {
27
28 my $ffi = FFI::Platypus->new;
29 $ffi->lib($libtest);
30 my $sub_ref = $ffi->function('f0', [ 'uint8' ] => 'uint8', sub { my($xsub, $arg) = @_; $arg*2})->sub_ref;
31
32 is $sub_ref->(99), 99*2, 'calls okay';
33 is ref($sub_ref), 'CODE', 'it is a code reference';
34
35 if(eval { require Sub::Identify; 1 })
36 {
37 my $name = Sub::Identify::sub_name($sub_ref);
38 my $package = Sub::Identify::stash_name($sub_ref);
39 note "name = ${package}::$name";
40 }
41 };
42
43 subtest 'prototype' => sub {
44
45 subtest one => sub {
46
47 my $ffi = FFI::Platypus->new;
48 $ffi->lib($libtest);
49 my $sub_ref = $ffi->attach(['f0' => 'f0_prototyped1'], [ 'uint8' ] => 'uint8', '$', sub { my($xsub, $arg) = @_; $arg*2});
50
51 is(f0_prototyped1(2), 4); # just make sure it attached okay
52 is(prototype(\&f0_prototyped1), '$');
53
54 };
55
56 subtest two => sub {
57
58 my $ffi = FFI::Platypus->new;
59 $ffi->lib($libtest);
60 my $sub_ref = $ffi->function('f0', [ 'uint8' ] => 'uint8', sub { my($xsub, $arg) = @_; $arg*2})->attach('f0_prototyped2', '$');
61
62 is(f0_prototyped2(2), 4); # just make sure it attached okay
63 is(prototype(\&f0_prototyped2), '$');
64
65 };
66
67 };
68
69 done_testing;