Undo #16091, a time-warped escapee.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Constant.t
CommitLineData
9f0ea43f 1#!/usr/bin/perl -w
2
6557ab03 3print "1..48\n";
af6c647e 4
5BEGIN {
39234879 6 if( $ENV{PERL_CORE} ) {
7 chdir 't' if -d 't';
8 @INC = '../lib';
9 }
af6c647e 10}
11
d7f97632 12# use warnings;
af6c647e 13use strict;
14use ExtUtils::MakeMaker;
15use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
16use Config;
357229c3 17use File::Spec::Functions qw(catfile rel2abs);
835f860c 18# Because were are going to be changing directory before running Makefile.PL
8338e367 19my $perl;
d7f97632 20$perl = rel2abs( $^X ) unless $] < 5.006; # Hack. Until 5.00503 has rel2abs
6d79cad2 21# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
22# compare output to ensure that it is the same. We were probably run as ./perl
23# whereas we will run the child with the full path in $perl. So make $^X for
24# us the same as our child will see.
25$^X = $perl;
af6c647e 26
835f860c 27print "# perl=$perl\n";
39234879 28my $runperl = "$perl \"-I../../lib\"";
94b1a389 29
af6c647e 30$| = 1;
31
32my $dir = "ext-$$";
0ddb8edc 33my @files;
94b1a389 34
35print "# $dir being created...\n";
36mkdir $dir, 0777 or die "mkdir: $!\n";
37
535acd0f 38my $output = "output";
af6c647e 39
6557ab03 40# For debugging set this to 1.
41my $keep_files = 0;
42
af6c647e 43END {
94b1a389 44 use File::Path;
45 print "# $dir being removed...\n";
6557ab03 46 rmtree($dir) unless $keep_files;
af6c647e 47}
48
6d79cad2 49my $package = "ExtTest";
50
8ac27563 51# Test the code that generates 1 and 2 letter name comparisons.
52my %compass = (
d7f97632 53N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
8ac27563 54);
55
cea00dc5 56my $parent_rfc1149 =
57 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
6557ab03 58# Check that 8 bit and unicode names don't cause problems.
59my $pound = chr 163; # A pound sign. (Currency)
60my $inf = chr 0x221E;
61# Check that we can distiguish the pathological case of a string, and the
62# utf8 representation of that string.
63my $pound_bytes = my $pound_utf8 = $pound . '1';
64utf8::encode ($pound_bytes);
cea00dc5 65
835f860c 66my @names = ("FIVE", {name=>"OK6", type=>"PV",},
67 {name=>"OK7", type=>"PVN",
68 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
af6c647e 69 {name => "FARTHING", type=>"NV"},
6d79cad2 70 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
72f7b9a1 71 {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
6d79cad2 72 {name => "CLOSE", type=>"PV", value=>'"*/"',
73 macro=>["#if 1\n", "#endif\n"]},
3414cef0 74 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
75 {name => "Yes", type=>"YES"},
76 {name => "No", type=>"NO"},
19d75eda 77 {name => "Undef", type=>"UNDEF"},
cea00dc5 78# OK. It wasn't really designed to allow the creation of dual valued constants.
79# It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
80 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
81 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
82 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
83 . "SvIVX(temp_sv) = 1149;"},
6557ab03 84 {name=>"perl", type=>"PV",},
3414cef0 85);
af6c647e 86
8ac27563 87push @names, $_ foreach keys %compass;
88
6557ab03 89# Automatically compile the list of all the macro names, and make them
90# exported constants.
af6c647e 91my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
92
6557ab03 93# Exporter::Heavy (currently) isn't able to export these names:
94push @names, ({name=>"*/", type=>"PV", value=>'"CLOSE"', macro=>1},
95 {name=>"/*", type=>"PV", value=>'"OPEN"', macro=>1},
96 {name=>$pound, type=>"PV", value=>'"Sterling"', macro=>1},
97 {name=>$inf, type=>"PV", value=>'"Infinity"', macro=>1},
98 {name=>$pound_utf8, type=>"PV", value=>'"1 Pound"', macro=>1},
99 {name=>$pound_bytes, type=>"PV", value=>'"1 Pound (as bytes)"',
100 macro=>1},
101 );
102
103=pod
104
105The above set of names seems to produce a suitably bad set of compile
106problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
107
108nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
1091..33
110# perl=/stuff/perl5/15439-32-utf/perl
111# ext-30370 being created...
112Wide character in print at lib/ExtUtils/t/Constant.t line 140.
113ok 1
114ok 2
115# make = 'make'
116ExtTest.xs: In function `constant_1':
117ExtTest.xs:80: warning: multi-character character constant
118ExtTest.xs:80: warning: case value out of range
119ok 3
120
121=cut
122
6d79cad2 123my $types = {};
124my $constant_types = constant_types(); # macro defs
125my $C_constant = join "\n",
126 C_constant ($package, undef, "IV", $types, undef, undef, @names);
127my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
128
af6c647e 129################ Header
94b1a389 130my $header = catfile($dir, "test.h");
0ddb8edc 131push @files, "test.h";
94b1a389 132open FH, ">$header" or die "open >$header: $!\n";
cea00dc5 133print FH <<"EOT";
835f860c 134#define FIVE 5
7b6ffde5 135#define OK6 "ok 6\\n"
835f860c 136#define OK7 1
af6c647e 137#define FARTHING 0.25
138#define NOT_ZERO 1
3414cef0 139#define Yes 0
140#define No 1
141#define Undef 1
cea00dc5 142#define RFC1149 "$parent_rfc1149"
6d79cad2 143#undef NOTDEF
6557ab03 144#define perl "rules"
af6c647e 145EOT
8ac27563 146
147while (my ($point, $bearing) = each %compass) {
148 print FH "#define $point $bearing\n"
149}
94b1a389 150close FH or die "close $header: $!\n";
af6c647e 151
152################ XS
94b1a389 153my $xs = catfile($dir, "$package.xs");
0ddb8edc 154push @files, "$package.xs";
94b1a389 155open FH, ">$xs" or die "open >$xs: $!\n";
af6c647e 156
157print FH <<'EOT';
158#include "EXTERN.h"
159#include "perl.h"
160#include "XSUB.h"
161EOT
162
163print FH "#include \"test.h\"\n\n";
6d79cad2 164print FH $constant_types;
165print FH $C_constant, "\n";
af6c647e 166print FH "MODULE = $package PACKAGE = $package\n";
167print FH "PROTOTYPES: ENABLE\n";
6d79cad2 168print FH $XS_constant;
94b1a389 169close FH or die "close $xs: $!\n";
af6c647e 170
171################ PM
94b1a389 172my $pm = catfile($dir, "$package.pm");
0ddb8edc 173push @files, "$package.pm";
94b1a389 174open FH, ">$pm" or die "open >$pm: $!\n";
af6c647e 175print FH "package $package;\n";
176print FH "use $];\n";
177
178print FH <<'EOT';
179
180use strict;
d7f97632 181EOT
182printf FH "use warnings;\n" unless $] < 5.006;
183print FH <<'EOT';
af6c647e 184use Carp;
185
186require Exporter;
187require DynaLoader;
d7f97632 188use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
af6c647e 189
190$VERSION = '0.01';
191@ISA = qw(Exporter DynaLoader);
192@EXPORT_OK = qw(
193EOT
194
6557ab03 195# Print the names of all our autoloaded constants
af6c647e 196print FH "\t$_\n" foreach (@names_only);
197print FH ");\n";
6557ab03 198# Print the AUTOLOAD subroutine ExtUtils::Constant generated for us
af6c647e 199print FH autoload ($package, $]);
200print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
94b1a389 201close FH or die "close $pm: $!\n";
af6c647e 202
203################ test.pl
94b1a389 204my $testpl = catfile($dir, "test.pl");
0ddb8edc 205push @files, "test.pl";
94b1a389 206open FH, ">$testpl" or die "open >$testpl: $!\n";
af6c647e 207
6d79cad2 208print FH "use strict;\n";
af6c647e 209print FH "use $package qw(@names_only);\n";
535acd0f 210print FH <<"EOT";
211
6557ab03 212use utf8;
213
535acd0f 214print "1..1\n";
215if (open OUTPUT, ">$output") {
216 print "ok 1\n";
217 select OUTPUT;
218} else {
219 print "not ok 1 # Failed to open '$output': $!\n";
220 exit 1;
221}
222EOT
af6c647e 223
535acd0f 224print FH << 'EOT';
225
226# What follows goes to the temporary file.
6d79cad2 227# IV
835f860c 228my $five = FIVE;
229if ($five == 5) {
230 print "ok 5\n";
af6c647e 231} else {
835f860c 232 print "not ok 5 # $five\n";
af6c647e 233}
234
6d79cad2 235# PV
835f860c 236print OK6;
af6c647e 237
6d79cad2 238# PVN containing embedded \0s
835f860c 239$_ = OK7;
af6c647e 240s/.*\0//s;
241print;
242
6d79cad2 243# NV
af6c647e 244my $farthing = FARTHING;
245if ($farthing == 0.25) {
835f860c 246 print "ok 8\n";
af6c647e 247} else {
835f860c 248 print "not ok 8 # $farthing\n";
af6c647e 249}
250
6d79cad2 251# UV
af6c647e 252my $not_zero = NOT_ZERO;
253if ($not_zero > 0 && $not_zero == ~0) {
835f860c 254 print "ok 9\n";
af6c647e 255} else {
835f860c 256 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 257}
258
6d79cad2 259# Value includes a "*/" in an attempt to bust out of a C comment.
260# Also tests custom cpp #if clauses
261my $close = CLOSE;
262if ($close eq '*/') {
263 print "ok 10\n";
264} else {
265 print "not ok 10 # \$close='$close'\n";
266}
267
268# Default values if macro not defined.
269my $answer = ANSWER;
270if ($answer == 42) {
271 print "ok 11\n";
272} else {
273 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
274}
275
276# not defined macro
277my $notdef = eval { NOTDEF; };
278if (defined $notdef) {
279 print "not ok 12 # \$notdef='$notdef'\n";
280} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
281 print "not ok 12 # \$@='$@'\n";
282} else {
283 print "ok 12\n";
284}
285
286# not a macro
287my $notthere = eval { &ExtTest::NOTTHERE; };
288if (defined $notthere) {
289 print "not ok 13 # \$notthere='$notthere'\n";
290} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
291 chomp $@;
292 print "not ok 13 # \$@='$@'\n";
293} else {
294 print "ok 13\n";
295}
af6c647e 296
3414cef0 297# Truth
298my $yes = Yes;
299if ($yes) {
300 print "ok 14\n";
301} else {
302 print "not ok 14 # $yes='\$yes'\n";
303}
304
305# Falsehood
306my $no = No;
307if (defined $no and !$no) {
308 print "ok 15\n";
309} else {
310 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
311}
312
313# Undef
314my $undef = Undef;
315unless (defined $undef) {
316 print "ok 16\n";
317} else {
318 print "not ok 16 # \$undef='$undef'\n";
319}
320
8ac27563 321
322# invalid macro (chosen to look like a mix up between No and SW)
323$notdef = eval { &ExtTest::So };
324if (defined $notdef) {
325 print "not ok 17 # \$notdef='$notdef'\n";
326} elsif ($@ !~ /^So is not a valid ExtTest macro/) {
327 print "not ok 17 # \$@='$@'\n";
328} else {
329 print "ok 17\n";
330}
331
332# invalid defined macro
333$notdef = eval { &ExtTest::EW };
334if (defined $notdef) {
335 print "not ok 18 # \$notdef='$notdef'\n";
336} elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
337 print "not ok 18 # \$@='$@'\n";
338} else {
339 print "ok 18\n";
340}
341
342my %compass = (
343EOT
344
345while (my ($point, $bearing) = each %compass) {
d7f97632 346 print FH "'$point' => $bearing, "
8ac27563 347}
348
349print FH <<'EOT';
350
351);
352
353my $fail;
354while (my ($point, $bearing) = each %compass) {
355 my $val = eval $point;
356 if ($@) {
357 print "# $point: \$@='$@'\n";
358 $fail = 1;
359 } elsif (!defined $bearing) {
360 print "# $point: \$val=undef\n";
361 $fail = 1;
362 } elsif ($val != $bearing) {
363 print "# $point: \$val=$val, not $bearing\n";
364 $fail = 1;
365 }
366}
367if ($fail) {
368 print "not ok 19\n";
369} else {
370 print "ok 19\n";
371}
372
af6c647e 373EOT
374
cea00dc5 375print FH <<"EOT";
376my \$rfc1149 = RFC1149;
377if (\$rfc1149 ne "$parent_rfc1149") {
378 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
379} else {
380 print "ok 20\n";
381}
382
383if (\$rfc1149 != 1149) {
384 printf "not ok 21 # %d != 1149\n", \$rfc1149;
385} else {
386 print "ok 21\n";
387}
72f7b9a1 388
389EOT
390
391print FH <<'EOT';
392# test macro=>1
393my $open = OPEN;
394if ($open eq '/*') {
395 print "ok 22\n";
396} else {
397 print "not ok 22 # \$open='$open'\n";
398}
cea00dc5 399EOT
6557ab03 400
401# Do this in 7 bit in case someone is testing with some settings that cause
402# 8 bit files incapable of storing this character.
403my @values
404 = map {"'" . join (",", unpack "U*", $_) . "'"}
405 ($pound, $inf, $pound_bytes, $pound_utf8);
406# Values is a list of strings, such as ('194,163,49', '163,49')
407
408print FH <<'EOT';
409
410# I can see that this child test program might be about to use parts of
411# Test::Builder
412
413my $test = 23;
414my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
415EOT
416
417print FH join ",", @values;
418
419print FH << 'EOT';
420;
421
422foreach (["perl", "rules", "rules"],
423 ["/*", "OPEN", "OPEN"],
424 ["*/", "CLOSE", "CLOSE"],
425 [$pound, 'Sterling', []],
426 [$inf, 'Infinity', []],
427 [$pound_utf8, '1 Pound', '1 Pound (as bytes)'],
428 [$pound_bytes, '1 Pound (as bytes)', []],
429 ) {
430 # Flag an expected error with a reference for the expect string.
431 my ($string, $expect, $expect_bytes) = @$_;
432 (my $name = $string) =~ s/([^ -~])/sprintf '\x{%X}', ord $1/ges;
433 print "# \"$name\" => \'$expect\'\n";
434 # Try to force this to be bytes if possible.
435 utf8::downgrade ($string, 1);
436EOT
437
438print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
439
440print FH <<'EOT';
441 if ($error or $got ne $expect) {
442 print "not ok $test # error '$error', got '$got'\n";
443 } else {
444 print "ok $test\n";
445 }
446 $test++;
447 print "# Now upgrade '$name' to utf8\n";
448 utf8::upgrade ($string);
449EOT
450
451print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
452
453print FH <<'EOT';
454 if ($error or $got ne $expect) {
455 print "not ok $test # error '$error', got '$got'\n";
456 } else {
457 print "ok $test\n";
458 }
459 $test++;
460 if (defined $expect_bytes) {
461 print "# And now with the utf8 byte sequence for name\n";
462 # Try the encoded bytes.
463 utf8::encode ($string);
464EOT
465
466print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
467
468print FH <<'EOT';
469 if (ref $expect_bytes) {
470 # Error expected.
471 if ($error) {
472 print "ok $test # error='$error' (as expected)\n";
473 } else {
474 print "not ok $test # expected error, got no error and '$got'\n";
475 }
476 } elsif ($got ne $expect_bytes) {
477 print "not ok $test # error '$error', expect '$expect_bytes', got '$got'\n";
478 } else {
479 print "ok $test\n";
480 }
481 $test++;
482 }
483}
484EOT
485
94b1a389 486close FH or die "close $testpl: $!\n";
af6c647e 487
6557ab03 488# This is where the test numbers carry on after the test number above are
489# relayed
490my $test = 44;
491
835f860c 492################ Makefile.PL
6d79cad2 493# We really need a Makefile.PL because make test for a no dynamic linking perl
494# will run Makefile.PL again as part of the "make perl" target.
94b1a389 495my $makefilePL = catfile($dir, "Makefile.PL");
0ddb8edc 496push @files, "Makefile.PL";
94b1a389 497open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
835f860c 498print FH <<"EOT";
6d79cad2 499#!$perl -w
835f860c 500use ExtUtils::MakeMaker;
501WriteMakefile(
502 'NAME' => "$package",
503 'VERSION_FROM' => "$package.pm", # finds \$VERSION
504 (\$] >= 5.005 ?
505 (#ABSTRACT_FROM => "$package.pm", # XXX add this
506 AUTHOR => "$0") : ())
507 );
508EOT
509
94b1a389 510close FH or die "close $makefilePL: $!\n";
af6c647e 511
512chdir $dir or die $!; push @INC, '../../lib';
513END {chdir ".." or warn $!};
514
4bfb3f62 515my @perlout = `$runperl Makefile.PL PERL_CORE=1`;
835f860c 516if ($?) {
517 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
518 print "# $_" foreach @perlout;
519 exit($?);
520} else {
521 print "ok 1\n";
522}
523
524
24874030 525my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
526my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
527if (-f "$makefile$makefile_ext") {
835f860c 528 print "ok 2\n";
af6c647e 529} else {
835f860c 530 print "not ok 2\n";
af6c647e 531}
24874030 532my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
533push @files, "$makefile$makefile_rename"; # Renamed by make clean
af6c647e 534
535my $make = $Config{make};
94b1a389 536
af6c647e 537$make = $ENV{MAKE} if exists $ENV{MAKE};
94b1a389 538
91e5ac1f 539if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
76f26e35 540
535acd0f 541my @makeout;
94b1a389 542
8b2eb6ba 543if ($^O eq 'VMS') { $make .= ' all'; }
af6c647e 544print "# make = '$make'\n";
535acd0f 545@makeout = `$make`;
94b1a389 546if ($?) {
835f860c 547 print "not ok 3 # $make failed: $?\n";
535acd0f 548 print "# $_" foreach @makeout;
94b1a389 549 exit($?);
af6c647e 550} else {
835f860c 551 print "ok 3\n";
552}
553
8b2eb6ba 554if ($^O eq 'VMS') { $make =~ s{ all}{}; }
555
835f860c 556if ($Config{usedl}) {
557 print "ok 4\n";
558} else {
835f860c 559 my $makeperl = "$make perl";
560 print "# make = '$makeperl'\n";
535acd0f 561 @makeout = `$makeperl`;
835f860c 562 if ($?) {
563 print "not ok 4 # $makeperl failed: $?\n";
535acd0f 564 print "# $_" foreach @makeout;
835f860c 565 exit($?);
566 } else {
567 print "ok 4\n";
568 }
af6c647e 569}
570
535acd0f 571push @files, $output;
572
0ddb8edc 573my $maketest = "$make test";
574print "# make = '$maketest'\n";
3414cef0 575
535acd0f 576@makeout = `$maketest`;
94b1a389 577
535acd0f 578if (open OUTPUT, "<$output") {
579 print while <OUTPUT>;
580 close OUTPUT or print "# Close $output failed: $!\n";
581} else {
582 # Harness will report missing test results at this point.
583 print "# Open <$output failed: $!\n";
584}
835f860c 585
3414cef0 586if ($?) {
587 print "not ok $test # $maketest failed: $?\n";
535acd0f 588 print "# $_" foreach @makeout;
3414cef0 589} else {
39234879 590 print "ok $test - maketest\n";
6d79cad2 591}
592$test++;
593
39234879 594
595# -x is busted on Win32 < 5.6.1, so we emulate it.
596my $regen;
597if( $^O eq 'MSWin32' && $] <= 5.006001 ) {
598 open(REGENTMP, ">regentmp") or die $!;
599 open(XS, "$package.xs") or die $!;
600 my $saw_shebang;
601 while(<XS>) {
602 $saw_shebang++ if /^#!.*/i ;
603 print REGENTMP $_ if $saw_shebang;
604 }
605 close XS; close REGENTMP;
606 $regen = `$runperl regentmp`;
607 unlink 'regentmp';
608}
609else {
610 $regen = `$runperl -x $package.xs`;
611}
6d79cad2 612if ($?) {
39234879 613 print "not ok $test # $runperl -x $package.xs failed: $?\n";
6d79cad2 614} else {
39234879 615 print "ok $test - regen\n";
af6c647e 616}
6d79cad2 617$test++;
618
619my $expect = $constant_types . $C_constant .
620 "\n#### XS Section:\n" . $XS_constant;
621
622if ($expect eq $regen) {
39234879 623 print "ok $test - regen worked\n";
6d79cad2 624} else {
39234879 625 print "not ok $test - regen worked\n";
6d79cad2 626 # open FOO, ">expect"; print FOO $expect;
627 # open FOO, ">regen"; print FOO $regen; close FOO;
628}
629$test++;
0ddb8edc 630
631my $makeclean = "$make clean";
632print "# make = '$makeclean'\n";
535acd0f 633@makeout = `$makeclean`;
0ddb8edc 634if ($?) {
6d79cad2 635 print "not ok $test # $make failed: $?\n";
535acd0f 636 print "# $_" foreach @makeout;
0ddb8edc 637} else {
6d79cad2 638 print "ok $test\n";
0ddb8edc 639}
6d79cad2 640$test++;
0ddb8edc 641
6557ab03 642unless ($keep_files) {
643 foreach (@files) {
644 unlink $_ or warn "unlink $_: $!";
645 }
0ddb8edc 646}
647
648my $fail;
649opendir DIR, "." or die "opendir '.': $!";
650while (defined (my $entry = readdir DIR)) {
651 next if $entry =~ /^\.\.?$/;
652 print "# Extra file '$entry'\n";
653 $fail = 1;
654}
655closedir DIR or warn "closedir '.': $!";
656if ($fail) {
6d79cad2 657 print "not ok $test\n";
0ddb8edc 658} else {
6d79cad2 659 print "ok $test\n";
0ddb8edc 660}