Debian Codegraph @

t/ffi_probe.t

raw
1 use Test2::V0 -no_srand => 1;
2 use 5.008004;
3 use FFI::Probe;
4 use FFI::Probe::Runner;
5 use Capture::Tiny qw( capture_merged );
6 use FFI::Temp;
7 use File::Basename qw( basename );
8 use Config;
9
10 sub n (&)
11 {
12 my($code) = @_;
13 my($out, @ret) = capture_merged {
14 $code->();
15 };
16 note $out;
17 @ret;
18 }
19
20 sub f (@)
21 {
22 foreach my $filename (@_)
23 {
24 note "==@{[ basename $filename ]}==";
25 my $fh;
26 open $fh, '<', $filename;
27 note do { local $/; <$fh> };
28 close $fh;
29 }
30 }
31
32 my $runner = do {
33 my $exe = "blib/lib/auto/share/dist/FFI-Platypus/probe/bin/dlrun$Config{exe_ext}";
34
35 unless(-f $exe)
36 {
37 require FFI::Probe::Runner::Builder;
38 my $out;
39 my $exception;
40 ($out, $exe, $exception) = capture_merged {
41 my $exe = eval {
42 FFI::Probe::Runner::Builder->new( dir => FFI::Temp->newdir( CLEANUP => 1, TEMPLATE => 'test-probe-XXXXXX' ) )->build;
43 };
44 ($exe, $exception);
45 };
46 note $out;
47 die $exception if $exception;
48 }
49
50 FFI::Probe::Runner->new(
51 exe => $exe,
52 );
53
54 };
55
56 subtest 'check_header' => sub {
57
58 my $dir = FFI::Temp->newdir;
59
60 my $probe = FFI::Probe->new(
61 log => "$dir/probe.log",
62 data_filename => "$dir/probe.pl",
63 runner => $runner,
64 );
65
66 isa_ok $probe, 'FFI::Probe';
67
68 n {
69 $probe->check_header('stdio.h');
70 $probe->check_header('bogus/does/not/exist.h');
71 };
72
73 is($probe->data->{header}->{"stdio.h"}, 1);
74 is($probe->data->{header}->{"bogus/does/not/exist.h"}, 0);
75
76 undef $probe;
77
78 f "$dir/probe.log",
79 "$dir/probe.pl";
80
81 # make sure that we cache that data correctly.
82 my $probe2 = FFI::Probe->new(
83 log => "$dir/probe2.log",
84 data_filename => "$dir/probe.pl",
85 runner => $runner,
86 );
87
88 is($probe2->data->{header}->{"stdio.h"}, 1);
89 is($probe2->data->{header}->{"bogus/does/not/exist.h"}, 0);
90
91 n {
92 $probe2->check_header('stdio.h');
93 $probe2->check_header('bogus/does/not/exist.h');
94 };
95
96 is($probe2->data->{header}->{"stdio.h"}, 1);
97 is($probe2->data->{header}->{"bogus/does/not/exist.h"}, 0);
98
99 f "$dir/probe2.log",
100 "$dir/probe.pl";
101 };
102
103 subtest check_eval => sub {
104
105 my $dir = FFI::Temp->newdir;
106
107 # make sure that we cache that data correctly.
108 my $probe = FFI::Probe->new(
109 log => "$dir/probe.log",
110 data_filename => "$dir/probe.pl",
111 runner => $runner,
112 );
113
114 my $ret;
115
116 n {
117 $ret = $probe->check_eval(
118 eval => {
119 'foo.bar.baz' => [ '%d' => '1+2' ],
120 },
121 );
122 };
123
124 ok $ret, 'foo.bar.baz';
125 is $probe->data, { foo => { bar => { baz => 3 } } };
126
127 n {
128 $ret = $probe->check_eval(
129 decl => ['char buffer[256];'],
130 stmt => ['sprintf(buffer, "hello world %d", 3+4);'],
131 eval => {
132 'foo.bar.string' => [ '%s' => 'buffer' ],
133 },
134 );
135 };
136
137 ok $ret, 'foo.bar.string';
138 is $probe->data, { foo => { bar => { baz => 3, string => 'hello world 7' } } };
139
140 n {
141 $ret = $probe->check_type_int('unsigned char');
142 };
143
144 is $ret, 'uint8';
145 is $probe->data->{type}->{'unsigned char'}->{size}, 1;
146 is $probe->data->{type}->{'unsigned char'}->{sign}, 'unsigned';
147 like $probe->data->{type}->{'unsigned char'}->{align}, qr/^[0-9]+$/;
148
149 n {
150 $ret = $probe->check_type_float('float');
151 };
152
153 is $ret, 'float';
154 is $probe->data->{type}->{'float'}->{size}, 4;
155 like $probe->data->{type}->{'float'}->{align}, qr/^[0-9]+$/;
156
157 n {
158 $ret = $probe->check_type_pointer;
159 };
160
161 is $ret, 'pointer';
162 like $probe->data->{type}->{pointer}->{size}, qr/^[0-9]+$/;
163 like $probe->data->{type}->{pointer}->{align}, qr/^[0-9]+$/;
164
165 $probe->save;
166 undef $probe;
167
168 f "$dir/probe.log",
169 "$dir/probe.pl";
170
171 };
172
173 done_testing;