Debian Codegraph @

t/ffi_platypus_lang.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus::Lang;
3 use FFI::CheckLib;
4 use FFI::Platypus;
5
6 my $libtest = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
7
8 subtest 'Foo constructor' => sub {
9 my $ffi = FFI::Platypus->new(lang => 'Foo');
10 $ffi->lib($libtest);
11
12 eval { $ffi->type('int') };
13 isnt $@, '', 'int is not an okay type';
14 note $@;
15 eval { $ffi->type('foo_t') };
16 is $@, '', 'foo_t is an okay type';
17 eval { $ffi->type('sint16') };
18 is $@, '', 'sint16 is an okay type';
19
20 is $ffi->sizeof('foo_t'), 2, 'sizeof foo_t = 2';
21 is $ffi->sizeof('bar_t'), 4, 'sizeof foo_t = 4';
22
23 is $ffi->function('UnMangled::Name(int i)' => ['bmyint'] => 'bmyint')->call(22), 22;
24 };
25
26 subtest 'Foo attribute' => sub {
27 my $ffi = FFI::Platypus->new;
28 $ffi->lib($libtest);
29 $ffi->lang('Foo');
30
31 eval { $ffi->type('int') };
32 isnt $@, '', 'int is not an okay type';
33 note $@;
34 eval { $ffi->type('foo_t') };
35 is $@, '', 'foo_t is an okay type';
36 eval { $ffi->type('sint16') };
37 is $@, '', 'sint16 is an okay type';
38
39 is $ffi->sizeof('foo_t'), 2, 'sizeof foo_t = 2';
40 is $ffi->sizeof('bar_t'), 4, 'sizeof foo_t = 4';
41
42 is $ffi->function('UnMangled::Name(int i)' => ['bmyint'] => 'bmyint')->call(22), 22;
43 };
44
45 subtest 'MyLang::Roger' => sub {
46 my $ffi = FFI::Platypus->new;
47 $ffi->lang('=MyLang::Roger');
48
49 eval { $ffi->type('int') };
50 isnt $@, '', 'int is not an okay type';
51 note $@;
52
53 is $ffi->sizeof('foo_t'), 4, 'sizeof foo_t = 4';
54 };
55
56 done_testing;
57
58 package
59 MyLang::Roger;
60
61 sub native_type_map
62 {
63 {
64 foo_t => 'sint32',
65 }
66 }
67
68 package
69 FFI::Platypus::Lang::Foo;
70
71 sub native_type_map
72 {
73 {
74 foo_t => 'sint16',
75 bar_t => 'uint32',
76 myint => 'sint32',
77 bmyint => 'uint8',
78 }
79 }
80
81 sub mangler
82 {
83 die "not a class method of FFI::Platypus::Lang::Foo"
84 unless $_[0] eq 'FFI::Platypus::Lang::Foo';
85 die "libtest not passed in as second argument"
86 unless $_[1] eq $libtest;
87
88 my %mangle = (
89 'UnMangled::Name(int i)' => 'f0',
90 );
91
92 sub {
93 defined $mangle{$_[0]} ? $mangle{$_[0]} : $_[0];
94 };
95 }
96