This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
efaf9684340cfa51ab52c86c579b92948f7b7f2f
[perl5.git] / lib / ExtUtils / ParseXS / t / basic.t
1 #!/usr/bin/perl
2
3 BEGIN {
4   if ($ENV{PERL_CORE}) {
5     chdir 't' if -d 't';
6     chdir '../lib/ExtUtils/ParseXS'
7       or die "Can't chdir to lib/ExtUtils/ParseXS: $!";
8     @INC = qw(../.. ../../.. .);
9   }
10 }
11 use strict;
12 use Test;
13 BEGIN { plan tests => 10 };
14 use DynaLoader;
15 use ExtUtils::ParseXS qw(process_file);
16 use ExtUtils::CBuilder;
17 ok(1); # If we made it this far, we're loaded.
18
19 chdir 't' or die "Can't chdir to t/, $!";
20
21 use Carp; $SIG{__WARN__} = \&Carp::cluck;
22
23 #########################
24
25 # Try sending to filehandle
26 tie *FH, 'Foo';
27 process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 );
28 ok tied(*FH)->content, '/is_even/', "Test that output contains some text";
29
30 my $source_file = 'XSTest.c';
31
32 # Try sending to file
33 process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
34 ok -e $source_file, 1, "Create an output file";
35
36 # TEST doesn't like extraneous output
37 my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
38
39 # Try to compile the file!  Don't get too fancy, though.
40 my $b = ExtUtils::CBuilder->new(quiet => $quiet);
41 if ($b->have_compiler) {
42   my $module = 'XSTest';
43
44   my $obj_file = $b->compile( source => $source_file );
45   ok $obj_file;
46   ok -e $obj_file, 1, "Make sure $obj_file exists";
47
48   my $lib_file = $b->link( objects => $obj_file, module_name => $module );
49   ok $lib_file;
50   ok -e $lib_file, 1, "Make sure $lib_file exists";
51
52   eval {require XSTest};
53   ok $@, '';
54   ok  XSTest::is_even(8);
55   ok !XSTest::is_even(9);
56
57   # Win32 needs to close the DLL before it can unlink it, but unfortunately
58   # dl_unload_file was missing on Win32 prior to perl change #24679!
59   if ($^O eq 'MSWin32' and defined &DynaLoader::dl_unload_file) {
60     for (my $i = 0; $i < @DynaLoader::dl_modules; $i++) {
61       if ($DynaLoader::dl_modules[$i] eq $module) {
62         DynaLoader::dl_unload_file($DynaLoader::dl_librefs[$i]);
63         last;
64       }
65     }
66   }
67   unlink $lib_file;
68 } else {
69   skip "Skipped can't find a C compiler & linker", 1 for 1..7;
70 }
71
72 unlink $source_file;
73
74 #####################################################################
75
76 sub Foo::TIEHANDLE { bless {}, 'Foo' }
77 sub Foo::PRINT { shift->{buf} .= join '', @_ }
78 sub Foo::content { shift->{buf} }