This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The "package;" syntax was removed in 5.10
[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::More;
13 use Config;
14 use DynaLoader;
15 use ExtUtils::CBuilder;
16
17 plan tests => 10;
18
19 my ($source_file, $obj_file, $lib_file);
20
21 require_ok( 'ExtUtils::ParseXS' );
22 ExtUtils::ParseXS->import('process_file');
23
24 chdir 't' or die "Can't chdir to t/, $!";
25
26 use Carp; $SIG{__WARN__} = \&Carp::cluck;
27
28 #########################
29
30 # Try sending to filehandle
31 tie *FH, 'Foo';
32 process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 );
33 like tied(*FH)->content, '/is_even/', "Test that output contains some text";
34
35 $source_file = 'XSTest.c';
36
37 # Try sending to file
38 process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
39 ok -e $source_file, "Create an output file";
40
41 my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
42 my $b = ExtUtils::CBuilder->new(quiet => $quiet);
43
44 SKIP: {
45   skip "no compiler available", 2
46     if ! $b->have_compiler;
47   $obj_file = $b->compile( source => $source_file );
48   ok $obj_file;
49   ok -e $obj_file, "Make sure $obj_file exists";
50 }
51
52 SKIP: {
53   skip "no dynamic loading", 5
54     if !$b->have_compiler || !$Config{usedl};
55   my $module = 'XSTest';
56   $lib_file = $b->link( objects => $obj_file, module_name => $module );
57   ok $lib_file;
58   ok -e $lib_file,  "Make sure $lib_file exists";
59
60   eval {require XSTest};
61   is $@, '';
62   ok  XSTest::is_even(8);
63   ok !XSTest::is_even(9);
64
65   # Win32 needs to close the DLL before it can unlink it, but unfortunately
66   # dl_unload_file was missing on Win32 prior to perl change #24679!
67   if ($^O eq 'MSWin32' and defined &DynaLoader::dl_unload_file) {
68     for (my $i = 0; $i < @DynaLoader::dl_modules; $i++) {
69       if ($DynaLoader::dl_modules[$i] eq $module) {
70         DynaLoader::dl_unload_file($DynaLoader::dl_librefs[$i]);
71         last;
72       }
73     }
74   }
75 }
76
77 unless ($ENV{PERL_NO_CLEANUP}) {
78   for ( $obj_file, $lib_file, $source_file) {
79     next unless defined $_;
80     1 while unlink $_;
81   }
82 }
83
84 #####################################################################
85
86 sub Foo::TIEHANDLE { bless {}, 'Foo' }
87 sub Foo::PRINT { shift->{buf} .= join '', @_ }
88 sub Foo::content { shift->{buf} }