This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update ExtUtils::ParseXS to 2.19_04
[perl5.git] / lib / ExtUtils / ParseXS / t / usage.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 if ( $] < 5.008 ) {
18   plan skip_all => "INTERFACE keyword support broken before 5.8";
19 }
20 else {
21   plan tests => 24;
22 }
23
24 my ($source_file, $obj_file, $lib_file, $module);
25
26 require_ok( 'ExtUtils::ParseXS' );
27 ExtUtils::ParseXS->import('process_file');
28
29 chdir 't' or die "Can't chdir to t/, $!";
30
31 use Carp; $SIG{__WARN__} = \&Carp::cluck;
32
33 #########################
34
35 $source_file = 'XSUsage.c';
36
37 # Try sending to file
38 process_file(filename => 'XSUsage.xs', output => $source_file);
39 ok -e $source_file, "Create an output file";
40
41 # TEST doesn't like extraneous output
42 my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
43
44 # Try to compile the file!  Don't get too fancy, though.
45 my $b = ExtUtils::CBuilder->new(quiet => $quiet);
46
47 SKIP: {
48   skip "no compiler available", 2
49     if ! $b->have_compiler;
50   $module = 'XSUsage';
51
52   $obj_file = $b->compile( source => $source_file );
53   ok $obj_file;
54   ok -e $obj_file, "Make sure $obj_file exists";
55 }
56 SKIP: {
57   skip "no dynamic loading", 20 
58     if !$b->have_compiler || !$Config{usedl};
59
60   $lib_file = $b->link( objects => $obj_file, module_name => $module );
61   ok $lib_file;
62   ok -e $lib_file, "Make sure $lib_file exists";
63
64   eval {require XSUsage};
65   is $@, '';
66
67   # The real tests here - for each way of calling the functions, call with the
68   # wrong number of arguments and check the Usage line is what we expect
69
70   eval { XSUsage::one(1) };
71   ok $@;
72   ok $@ =~ /^Usage: XSUsage::one/;
73
74   eval { XSUsage::two(1) };
75   ok $@;
76   ok $@ =~ /^Usage: XSUsage::two/;
77
78   eval { XSUsage::two_x(1) };
79   ok $@;
80   ok $@ =~ /^Usage: XSUsage::two_x/;
81
82   eval { FOO::two(1) };
83   ok $@;
84   ok $@ =~ /^Usage: FOO::two/;
85
86   eval { XSUsage::three(1) };
87   ok $@;
88   ok $@ =~ /^Usage: XSUsage::three/;
89
90   eval { XSUsage::four(1) };
91   ok !$@;
92
93   eval { XSUsage::five() };
94   ok $@;
95   ok $@ =~ /^Usage: XSUsage::five/;
96
97   eval { XSUsage::six() };
98   ok !$@;
99
100   eval { XSUsage::six(1) };
101   ok !$@;
102
103   eval { XSUsage::six(1,2) };
104   ok $@;
105   ok $@ =~ /^Usage: XSUsage::six/;
106
107   # Win32 needs to close the DLL before it can unlink it, but unfortunately
108   # dl_unload_file was missing on Win32 prior to perl change #24679!
109   if ($^O eq 'MSWin32' and defined &DynaLoader::dl_unload_file) {
110     for (my $i = 0; $i < @DynaLoader::dl_modules; $i++) {
111       if ($DynaLoader::dl_modules[$i] eq $module) {
112         DynaLoader::dl_unload_file($DynaLoader::dl_librefs[$i]);
113         last;
114       }
115     }
116   }
117 }
118
119 unless ($ENV{PERL_NO_CLEANUP}) {
120   for ( $obj_file, $lib_file, $source_file) {
121     next unless defined $_;
122     1 while unlink $_;
123   }
124 }
125