Debian Codegraph @

t/type_custom.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::CheckLib;
4 use FFI::Platypus::ShareConfig;
5
6 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
7
8 my @legal = qw( float double opaque );
9 push @legal, map { ("sint$_","uint$_") } qw( 8 16 32 64 );
10
11 my $return_ok = FFI::Platypus::ShareConfig->get('probe')->{recordvalue};
12
13 subtest 'legal custom types' => sub {
14
15 my $ffi = FFI::Platypus->new( api => 1 );
16
17 foreach my $type (@legal)
18 {
19 local $@ = "";
20 eval {
21 $ffi->custom_type( "foo_$type" => {
22 native_type => $type,
23 native_to_perl => sub {},
24 });
25 };
26 is "$@", "";
27 }
28
29 };
30
31 subtest 'illegal types' => sub {
32
33 my $ffi = FFI::Platypus->new( api => 1 );
34
35 foreach my $type (qw( sint8[32] sint8* ))
36 {
37 local $@ = "";
38 my $alias = "foo_$type";
39 $alias =~ s/[\*\[\]]/_/g;
40 note "alias = $alias";
41 eval {
42 $ffi->custom_type( $alias => {
43 native_type => $type,
44 native_to_perl => sub {},
45 });
46 };
47 like "$@", qr/\Q$type\E is not a legal basis for a custom type/;
48 }
49
50 };
51
52 subtest 'records' => sub {
53
54 {
55 package Foo;
56 use FFI::Platypus::Record;
57 record_layout qw(
58 string(16) name
59 sint32 value
60 );
61 }
62
63
64 subtest 'pointer' => sub {
65
66 my $ffi = FFI::Platypus->new( api => 2, lib => [@lib] );
67 local $@ = '';
68 eval {
69 $ffi->custom_type(
70 'foo_t' => {
71 native_type => 'record(Foo)*',
72 perl_to_native => sub {
73 my $var = shift;
74 return Foo->new(name => $var->[0], value => $var->[1]);
75 },
76 native_to_perl => sub {
77 my $rec = shift;
78 return defined $rec ? [$rec->name, $rec->value] : [];
79 },
80 },
81 );
82 };
83
84 is "$@", '' or return;
85
86 {
87 is(
88 $ffi->function( foo_get_name => [ 'foo_t' ] => 'string' )
89 ->call( ["Graham", 47] ),
90 "Graham",
91 );
92 is(
93 $ffi->function( foo_get_value => [ 'foo_t' ] => 'sint32' )
94 ->call( ["Graham", 47] ),
95 47,
96 );
97 is(
98 $ffi->function( foo_create => ['string','sint32'] => 'foo_t' )
99 ->call("Adams", 42),
100 ["Adams\0\0\0\0\0\0\0\0\0\0\0", 42],
101 );
102 is(
103 $ffi->function( pointer_null => [] => 'foo_t' )
104 ->call,
105 [],
106 );
107 }
108
109 };
110
111 subtest 'by-value' => sub {
112
113 my $ffi = FFI::Platypus->new( api => 1, lib => [@lib] );
114 local $@ = '';
115 eval {
116 $ffi->custom_type(
117 'foo_t' => {
118 native_type => 'record(Foo)',
119 perl_to_native => sub {
120 my $var = shift;
121 return Foo->new(name => $var->[0], value => $var->[1]);
122 },
123 native_to_perl => sub {
124 my $rec = shift;
125 return [$rec->name, $rec->value];
126 },
127 },
128 );
129 };
130
131 is "$@", '' or return;
132
133 {
134 is(
135 $ffi->function( foo_value_get_name => [ 'foo_t' ] => 'string' )
136 ->call( ["Graham", 47] ),
137 "Graham",
138 );
139 is(
140 $ffi->function( foo_value_get_value => [ 'foo_t' ] => 'sint32' )
141 ->call( ["Graham", 47] ),
142 47,
143 );
144 subtest 'return-value' => sub {
145 skip_all 'test requires working return records-by-value'
146 unless $return_ok;
147
148 is(
149 $ffi->function( foo_value_create => ['string','sint32'] => 'foo_t' )
150 ->call("Adams", 42),
151 ["Adams\0\0\0\0\0\0\0\0\0\0\0", 42],
152 );
153 };
154 }
155
156 };
157
158 };
159
160 done_testing;