1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6use Config;
7use DynaLoader;
8use ExtUtils::CBuilder;
9use attributes;
10use overload;
11
12plan tests => 33;
13
14my ($source_file, $obj_file, $lib_file);
15
16require_ok( 'ExtUtils::ParseXS' );
17ExtUtils::ParseXS->import('process_file');
18
19chdir 't' if -d 't';
20push @INC, '.';
21
22use Carp; #$SIG{__WARN__} = \&Carp::cluck;
23
24# See the comments about this in 001-basics.t
25@INC = map { File::Spec->rel2abs($_) } @INC;
26
27#########################
28
29$source_file = 'XSMore.c';
30
31# Try sending to file
32ExtUtils::ParseXS->process_file(
33	filename => 'XSMore.xs',
34	output   => $source_file,
35);
36ok -e $source_file, "Create an output file";
37
38my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
39my $b = ExtUtils::CBuilder->new(quiet => $quiet);
40
41SKIP: {
42  skip "no compiler available", 2
43    if ! $b->have_compiler;
44  $obj_file = $b->compile( source => $source_file );
45  ok $obj_file, "ExtUtils::CBuilder::compile() returned true value";
46  ok -e $obj_file, "Make sure $obj_file exists";
47}
48
49SKIP: {
50  skip "no dynamic loading", 29
51    if !$b->have_compiler || !$Config{usedl};
52  my $module = 'XSMore';
53  $lib_file = $b->link( objects => $obj_file, module_name => $module );
54  ok $lib_file, "ExtUtils::CBuilder::link() returned true value";
55  ok -e $lib_file,  "Make sure $lib_file exists";
56
57  eval{
58    package XSMore;
59    our $VERSION = 42;
60    our $boot_ok;
61    DynaLoader::bootstrap_inherit(__PACKAGE__, $VERSION); # VERSIONCHECK disabled
62
63    sub new{ bless {}, shift }
64  };
65  is $@, '', "No error message recorded, as expected";
66  is ExtUtils::ParseXS::report_error_count(), 0, 'ExtUtils::ParseXS::errors()';
67
68  is $XSMore::boot_ok, 100, 'the BOOT keyword';
69
70  ok XSMore::include_ok(), 'the INCLUDE keyword';
71  is prototype(\&XSMore::include_ok), "", 'the PROTOTYPES keyword';
72
73  is prototype(\&XSMore::prototype_ssa), '$$@', 'the PROTOTYPE keyword';
74
75  is_deeply [attributes::get(\&XSMore::attr_method)], [qw(method)], 'the ATTRS keyword';
76  is prototype(\&XSMore::attr_method), '$;@', 'ATTRS with prototype';
77
78  is XSMore::return_1(), 1, 'the CASE keyword (1)';
79  is XSMore::return_2(), 2, 'the CASE keyword (2)';
80  is prototype(\&XSMore::return_1), "", 'ALIAS with prototype (1)';
81  is prototype(\&XSMore::return_2), "", 'ALIAS with prototype (2)';
82
83  is XSMore::arg_init(200), 200, 'argument init';
84
85  ok overload::Overloaded(XSMore->new), 'the FALLBACK keyword';
86  is abs(XSMore->new), 42, 'the OVERLOAD keyword';
87
88  my $overload_sub_name = "XSMore::More::(+";
89  is prototype(\&$overload_sub_name), "", 'OVERLOAD following prototyped xsub';
90
91  my @a;
92  XSMore::hook(\@a);
93  is_deeply \@a, [qw(INIT CODE POSTCALL CLEANUP)], 'the INIT & POSTCALL & CLEANUP keywords';
94
95  is_deeply [XSMore::outlist()], [ord('a'), ord('b')], 'the OUTLIST keyword';
96
97  is_deeply [XSMore::outlist_bool("a", "b")], [ !0, "ab" ],
98             "OUTLIST with a bool RETVAL";
99
100  is_deeply [XSMore::outlist_int("c", "d")], [ 11, "cd" ],
101             "OUTLIST with an int RETVAL";
102
103  # eval so compile-time sees any prototype
104  is_deeply [ eval 'XSMore::outlist()' ], [ord('a'), ord('b')], 'OUTLIST prototypes';
105
106  is XSMore::len("foo"), 3, 'the length keyword';
107
108  is XSMore::sum(5, 9), 14, 'the INCLUDE_COMMAND directive';
109
110  # Tests for embedded typemaps
111  is XSMore::typemaptest1(), 42, 'Simple embedded typemap works';
112  is XSMore::typemaptest2(), 42, 'Simple embedded typemap works with funny end marker';
113  is XSMore::typemaptest3(12, 13, 14), 12, 'Simple embedded typemap works for input, too';
114  is XSMore::typemaptest6(5), 5, '<<END; (with semicolon) matches delimiter "END"';
115
116  # Win32 needs to close the DLL before it can unlink it, but unfortunately
117  # dl_unload_file was missing on Win32 prior to perl change #24679!
118  if ($^O eq 'MSWin32' and defined &DynaLoader::dl_unload_file) {
119    for (my $i = 0; $i < @DynaLoader::dl_modules; $i++) {
120      if ($DynaLoader::dl_modules[$i] eq $module) {
121        DynaLoader::dl_unload_file($DynaLoader::dl_librefs[$i]);
122        last;
123      }
124    }
125  }
126}
127
128unless ($ENV{PERL_NO_CLEANUP}) {
129  for ( $obj_file, $lib_file, $source_file) {
130    next unless defined $_;
131    1 while unlink $_;
132  }
133}
134