xref: /openbsd-src/gnu/usr.bin/perl/dist/ExtUtils-ParseXS/t/001-basic.t (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1#!/usr/bin/perl
2
3use strict;
4use Test::More tests => 11;
5use Config;
6use DynaLoader;
7use ExtUtils::CBuilder;
8
9my ($source_file, $obj_file, $lib_file);
10
11require_ok( 'ExtUtils::ParseXS' );
12ExtUtils::ParseXS->import('process_file');
13
14chdir 't' or die "Can't chdir to t/, $!";
15
16use Carp; $SIG{__WARN__} = \&Carp::cluck;
17
18#########################
19
20# Try sending to filehandle
21tie *FH, 'Foo';
22process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 );
23like tied(*FH)->content, '/is_even/', "Test that output contains some text";
24
25$source_file = 'XSTest.c';
26
27# Try sending to file
28process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
29ok -e $source_file, "Create an output file";
30
31my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
32my $b = ExtUtils::CBuilder->new(quiet => $quiet);
33
34SKIP: {
35  skip "no compiler available", 2
36    if ! $b->have_compiler;
37  $obj_file = $b->compile( source => $source_file );
38  ok $obj_file, "ExtUtils::CBuilder::compile() returned true value";
39  ok -e $obj_file, "Make sure $obj_file exists";
40}
41
42SKIP: {
43  skip "no dynamic loading", 5
44    if !$b->have_compiler || !$Config{usedl};
45  my $module = 'XSTest';
46  $lib_file = $b->link( objects => $obj_file, module_name => $module );
47  ok $lib_file, "ExtUtils::CBuilder::link() returned true value";
48  ok -e $lib_file,  "Make sure $lib_file exists";
49
50  eval {require XSTest};
51  is $@, '', "No error message recorded, as expected";
52  ok  XSTest::is_even(8),
53    "Function created thru XS returned expected true value";
54  ok !XSTest::is_even(9),
55    "Function created thru XS returned expected false value";
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}
68
69my $seen = 0;
70open my $IN, '<', $source_file
71  or die "Unable to open $source_file: $!";
72while (my $l = <$IN>) {
73  $seen++ if $l =~ m/#line\s1\s/;
74}
75close $IN or die "Unable to close $source_file: $!";
76is( $seen, 1, "Linenumbers created in output file, as intended" );
77
78unless ($ENV{PERL_NO_CLEANUP}) {
79  for ( $obj_file, $lib_file, $source_file) {
80    next unless defined $_;
81    1 while unlink $_;
82  }
83}
84
85#####################################################################
86
87sub Foo::TIEHANDLE { bless {}, 'Foo' }
88sub Foo::PRINT { shift->{buf} .= join '', @_ }
89sub Foo::content { shift->{buf} }
90