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