This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
A typemap is a file, not a directory.
[perl5.git] / dist / ExtUtils-ParseXS / t / 001-basic.t
CommitLineData
6b09c160
YST
1#!/usr/bin/perl
2
6b09c160 3use strict;
84ce4b0a 4use Test::More tests => 11;
6986f47f 5use Config;
da2b6f33 6use DynaLoader;
6b09c160 7use ExtUtils::CBuilder;
6986f47f 8
6986f47f
DG
9my ($source_file, $obj_file, $lib_file);
10
11require_ok( 'ExtUtils::ParseXS' );
12ExtUtils::ParseXS->import('process_file');
6b09c160
YST
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 );
6986f47f 23like tied(*FH)->content, '/is_even/', "Test that output contains some text";
6b09c160 24
6986f47f 25$source_file = 'XSTest.c';
da2b6f33 26
6b09c160 27# Try sending to file
da2b6f33 28process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
6986f47f 29ok -e $source_file, "Create an output file";
6b09c160 30
6b09c160 31my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
6b09c160 32my $b = ExtUtils::CBuilder->new(quiet => $quiet);
6b09c160 33
6986f47f
DG
34SKIP: {
35 skip "no compiler available", 2
36 if ! $b->have_compiler;
37 $obj_file = $b->compile( source => $source_file );
84ce4b0a 38 ok $obj_file, "ExtUtils::CBuilder::compile() returned true value";
6986f47f
DG
39 ok -e $obj_file, "Make sure $obj_file exists";
40}
6b09c160 41
6986f47f
DG
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 );
84ce4b0a 47 ok $lib_file, "ExtUtils::CBuilder::link() returned true value";
6986f47f 48 ok -e $lib_file, "Make sure $lib_file exists";
6b09c160
YST
49
50 eval {require XSTest};
84ce4b0a
JK
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";
6b09c160 56
da2b6f33
SH
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 }
6b09c160
YST
67}
68
84ce4b0a
JK
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
708f9ca6 78unless ($ENV{PERL_NO_CLEANUP}) {
6986f47f
DG
79 for ( $obj_file, $lib_file, $source_file) {
80 next unless defined $_;
81 1 while unlink $_;
82 }
708f9ca6 83}
da2b6f33 84
6b09c160
YST
85#####################################################################
86
87sub Foo::TIEHANDLE { bless {}, 'Foo' }
88sub Foo::PRINT { shift->{buf} .= join '', @_ }
89sub Foo::content { shift->{buf} }