This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add tests for object ~~ regexp
[perl5.git] / t / TEST
1 #!./perl
2
3 # This is written in a peculiar style, since we're trying to avoid
4 # most of the constructs we'll be testing for.  (This comment is
5 # probably obsolete on the avoidance side, though still currrent
6 # on the peculiarity side.)
7
8 $| = 1;
9
10 # for testing TEST only
11 #BEGIN { require '../lib/strict.pm'; strict->import() };
12 #BEGIN { require '../lib/warnings.pm'; warnings->import() };
13
14 # Let tests know they're running in the perl core.  Useful for modules
15 # which live dual lives on CPAN.
16 $ENV{PERL_CORE} = 1;
17 delete $ENV{PERL5LIB};
18
19 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
20 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
21 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
22
23 # Cheesy version of Getopt::Std.  Maybe we should replace it with that.
24 {
25     my @argv = ();
26     foreach my $idx (0..$#ARGV) {
27         push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
28         $::core    = 1 if $1 eq 'core';
29         $::verbose = 1 if $1 eq 'v';
30         $::torture = 1 if $1 eq 'torture';
31         $::with_utf8 = 1 if $1 eq 'utf8';
32         $::with_utf16 = 1 if $1 eq 'utf16';
33         $::taintwarn = 1 if $1 eq 'taintwarn';
34         $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
35         if ($1 =~ /^deparse(,.+)?$/) {
36             $::deparse = 1;
37             $::deparse_opts = $1;
38         }
39     }
40     @ARGV = @argv;
41 }
42
43 chdir 't' if -f 't/TEST';
44
45 die "You need to run \"make test\" first to set things up.\n"
46   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
47
48 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
49     unless (-x 'perl.third') {
50         unless (-x '../perl.third') {
51             die "You need to run \"make perl.third first.\n";
52         }
53         else {
54             print "Symlinking ../perl.third as perl.third...\n";
55             die "Failed to symlink: $!\n"
56                 unless symlink("../perl.third", "perl.third");
57             die "Symlinked but no executable perl.third: $!\n"
58                 unless -x 'perl.third';
59         }
60     }
61 }
62
63 # check leakage for embedders
64 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
65
66 $ENV{EMXSHELL} = 'sh';        # For OS/2
67
68 # Roll your own File::Find!
69 use TestInit;
70 use File::Spec;
71 if ($show_elapsed_time) { require Time::HiRes }
72 my $curdir = File::Spec->curdir;
73 my $updir  = File::Spec->updir;
74
75 sub _find_tests {
76     my($dir) = @_;
77     opendir DIR, $dir or die "Trouble opening $dir: $!";
78     foreach my $f (sort { $a cmp $b } readdir DIR) {
79         next if $f eq $curdir or $f eq $updir or
80             $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
81
82         my $fullpath = File::Spec->catfile($dir, $f);
83
84         _find_tests($fullpath) if -d $fullpath;
85         $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
86         push @ARGV, $fullpath if $f =~ /\.t$/;
87     }
88 }
89
90 sub _quote_args {
91     my ($args) = @_;
92     my $argstring = '';
93
94     foreach (split(/\s+/,$args)) {
95        # In VMS protect with doublequotes because otherwise
96        # DCL will lowercase -- unless already doublequoted.
97        $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
98        $argstring .= ' ' . $_;
99     }
100     return $argstring;
101 }
102
103 sub _populate_hash {
104     return map {$_, 1} split /\s+/, $_[0];
105 }
106
107 unless (@ARGV) {
108     foreach my $dir (qw(base comp cmd run io op uni mro)) {
109         _find_tests($dir);
110     }
111     _find_tests("lib") unless $::core;
112     # Config.pm may be broken for make minitest. And this is only a refinement
113     # for skipping tests on non-default builds, so it is allowed to fail.
114     # What we want to to is make a list of extensions which we did not build.
115     my $configsh = File::Spec->catfile($updir, "config.sh");
116     my %skip;
117     if (-f $configsh) {
118         my (%extensions, %known_extensions);
119         open FH, $configsh or die "Can't open $configsh: $!";
120         while (<FH>) {
121             if (/^extensions=['"](.*)['"]$/) {
122                 # Deliberate string interpolation to avoid triggering possible
123                 # $1 resetting bugs.
124                 %extensions = _populate_hash ("$1");
125             }
126             elsif (/^known_extensions=['"](.*)['"]$/) {
127                 %known_extensions = _populate_hash ($1);
128             }
129         }
130         if (%extensions) {
131             if (%known_extensions) {
132                 foreach (keys %known_extensions) {
133                     $skip{$_}++ unless $extensions{$_};
134                 }
135             } else {
136                 warn "No known_extensions line found in $configsh";
137             }
138         } else {
139             warn "No extensions line found in $configsh";
140         }
141     }
142     my $mani = File::Spec->catfile($updir, "MANIFEST");
143     if (open(MANI, $mani)) {
144         while (<MANI>) { # similar code in t/harness
145             if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
146                 my $t = $1;
147                 my $extension = $2;
148                 if (!$::core || $t =~ m!^lib/[a-z]!)
149                 {
150                     if (defined $extension) {
151                         $extension =~ s!/t$!!;
152                         # XXX Do I want to warn that I'm skipping these?
153                         next if $skip{$extension};
154                         my $flat_extension = $extension;
155                         $flat_extension =~ s!-!/!g;
156                         next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
157                     }
158                     my $path = File::Spec->catfile($updir, $t);
159                     push @ARGV, $path;
160                     $::path_to_name{$path} = $t;
161                 }
162             }
163         }
164         close MANI;
165     } else {
166         warn "$0: cannot open $mani: $!\n";
167     }
168     unless ($::core) {
169         _find_tests('pod');
170         _find_tests('x2p');
171         _find_tests('japh') if $::torture;
172     }
173 }
174
175 if ($::deparse) {
176     _testprogs('deparse', '',   @ARGV);
177 }
178 elsif ($::with_utf16) {
179     for my $e (0, 1) {
180         for my $b (0, 1) {
181             print STDERR "# ENDIAN $e BOM $b\n";
182             my @UARGV;
183             for my $a (@ARGV) {
184                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
185                 my $f = $e ? "v" : "n";
186                 push @UARGV, $u;
187                 unlink($u);
188                 if (open(A, $a)) {
189                     if (open(U, ">$u")) {
190                         print U pack("$f", 0xFEFF) if $b;
191                         while (<A>) {
192                             print U pack("$f*", unpack("C*", $_));
193                         }
194                         close(U);
195                     }
196                     close(A);
197                 }
198             }
199             _testprogs('perl', '', @UARGV);
200             unlink(@UARGV);
201         }
202     }
203 }
204 else {
205     _testprogs('perl',    '',   @ARGV);
206 }
207
208 sub _testprogs {
209     my ($type, $args, @tests) = @_;
210
211     print <<'EOT' if ($type eq 'deparse');
212 ------------------------------------------------------------------------------
213 TESTING DEPARSER
214 ------------------------------------------------------------------------------
215 EOT
216
217     $::bad_files = 0;
218
219     foreach my $t (@tests) {
220       unless (exists $::path_to_name{$t}) {
221         my $tname = File::Spec->catfile('t',$t);
222         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
223         $::path_to_name{$t} = $tname;
224       }
225     }
226     my $maxlen = 0;
227     foreach (@::path_to_name{@tests}) {
228         s/\.\w+\z/./;
229         my $len = length ;
230         $maxlen = $len if $len > $maxlen;
231     }
232     # + 3 : we want three dots between the test name and the "ok"
233     my $dotdotdot = $maxlen + 3 ;
234     my $valgrind = 0;
235     my $valgrind_log = 'current.valgrind';
236     my $total_files = @tests;
237     my $good_files = 0;
238     my $tested_files  = 0;
239     my $totmax = 0;
240     my %failed_tests;
241
242     while (my $test = shift @tests) {
243         my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
244
245         if ($test =~ /^$/) {
246             next;
247         }
248         if ($type eq 'deparse') {
249             if ($test eq "comp/redef.t") {
250                 # Redefinition happens at compile time
251                 next;
252             }
253             elsif ($test =~ m{lib/Switch/t/}) {
254                 # B::Deparse doesn't support source filtering
255                 next;
256             }
257         }
258         my $te = $::path_to_name{$test} . '.'
259                     x ($dotdotdot - length($::path_to_name{$test}));
260
261         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
262             print $te;
263             $te = '';
264         }
265
266         # XXX DAPM %OVER not defined anywhere
267         # $test = $OVER{$test} if exists $OVER{$test};
268
269         open(SCRIPT,"<",$test) or die "Can't run $test.\n";
270         $_ = <SCRIPT>;
271         close(SCRIPT) unless ($type eq 'deparse');
272         if ($::with_utf16) {
273             $_ =~ tr/\0//d;
274         }
275         my $switch;
276         if (/#!.*\bperl.*\s-\w*([tT])/) {
277             $switch = qq{"-$1"};
278         }
279         else {
280             if ($::taintwarn) {
281                 # not all tests are expected to pass with this option
282                 $switch = '"-t"';
283             }
284             else {
285                 $switch = '';
286             }
287         }
288
289         my $file_opts = "";
290         if ($type eq 'deparse') {
291             # Look for #line directives which change the filename
292             while (<SCRIPT>) {
293                 $file_opts .= ",-f$3$4"
294                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
295             }
296             close(SCRIPT);
297         }
298
299         my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
300         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
301         if ($type eq 'deparse') {
302             my $deparse_cmd =
303                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
304                 "-l$::deparse_opts$file_opts ".
305                 "$test > $test.dp ".
306                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
307             open(RESULTS, $deparse_cmd)
308                 or print "can't deparse '$deparse_cmd': $!.\n";
309         }
310         elsif ($type eq 'perl') {
311             my $perl = $ENV{PERL} || './perl';
312             my $redir = $^O eq 'VMS' ? '2>&1' : '';
313             if ($ENV{PERL_VALGRIND}) {
314                 my $valgrind = $ENV{VALGRIND} // 'valgrind';
315                 my $vg_opts = $ENV{VG_OPTS}
316                     //  "--suppressions=perl.supp --leak-check=yes "
317                         . "--leak-resolution=high --show-reachable=yes "
318                         . "--num-callers=50"; 
319                 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
320                 $redir = "3>$valgrind_log";
321             }
322             my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
323                               . " $test $redir|";
324             open(RESULTS,$run) or print "can't run '$run': $!.\n";
325         }
326         # Our environment may force us to use UTF-8, but we can't be sure that
327         # anything we're reading from will be generating (well formed) UTF-8
328         # This may not be the best way - possibly we should unset ${^OPEN} up
329         # top?
330         binmode RESULTS;
331
332         my $failure;
333         my $next = 0;
334         my $seen_leader = 0;
335         my $seen_ok = 0;
336         my $trailing_leader = 0;
337         my $max;
338         my %todo;
339         while (<RESULTS>) {
340             next if /^\s*$/; # skip blank lines
341             if (/^1..$/ && ($^O eq 'VMS')) {
342                 # VMS pipe bug inserts blank lines.
343                 my $l2 = <RESULTS>;
344                 if ($l2 =~ /^\s*$/) {
345                     $l2 = <RESULTS>;
346                 }
347                 $_ = '1..' . $l2;
348             }
349             if ($::verbose) {
350                 print $_;
351             }
352             unless (/^\#/) {
353                 if ($trailing_leader) {
354                     # shouldn't be anything following a postfix 1..n
355                     $failure = 'FAILED--extra output after trailing 1..n';
356                     last;
357                 }
358                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
359                     if ($seen_leader) {
360                         $failure = 'FAILED--seen duplicate leader';
361                         last;
362                     }
363                     $max = $1;
364                     %todo = map { $_ => 1 } split / /, $3 if $3;
365                     $totmax += $max;
366                     $tested_files++;
367                     if ($seen_ok) {
368                         # 1..n appears at end of file
369                         $trailing_leader = 1;
370                         if ($next != $max) {
371                             $failure = "FAILED--expected $max tests, saw $next";
372                             last;
373                         }
374                     }
375                     else {
376                         $next = 0;
377                     }
378                     $seen_leader = 1;
379                 }
380                 else {
381                     if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
382                         unless ($seen_leader) {
383                             unless ($seen_ok) {
384                                 $next = 0;
385                             }
386                         }
387                         $seen_ok = 1;
388                         $next++;
389                         my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
390                         $num = $next unless $num;
391
392                         if ($num == $next) {
393
394                             # SKIP is essentially the same as TODO for t/TEST
395                             # this still conforms to TAP:
396                             # http://search.cpan.org/dist/TAP/TAP.pod
397                             $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
398                             $istodo = 1 if $todo{$num};
399
400                             if( $not && !$istodo ) {
401                                 $failure = "FAILED at test $num";
402                                 last;
403                             }
404                         }
405                         else {
406                             $failure ="FAILED--expected test $next, saw test $num";
407                             last;
408                         }
409                     }
410                     elsif (/^Bail out!\s*(.*)/i) { # magic words
411                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
412                     }
413                     else {
414                         # module tests are allowed extra output,
415                         # because Test::Harness allows it
416                         next if $test =~ /^\W*(ext|lib)\b/;
417                         $failure = "FAILED--unexpected output at test $next";
418                         last;
419                     }
420                 }
421             }
422         }
423         close RESULTS;
424
425         if (not defined $failure) {
426             $failure = 'FAILED--no leader found' unless $seen_leader;
427         }
428
429         if ($ENV{PERL_VALGRIND}) {
430             my @valgrind;
431             if (-e $valgrind_log) {
432                 if (open(V, $valgrind_log)) {
433                     @valgrind = <V>;
434                     close V;
435                 } else {
436                     warn "$0: Failed to open '$valgrind_log': $!\n";
437                 }
438             }
439             if ($ENV{VG_OPTS} =~ /cachegrind/) {
440                 if (rename $valgrind_log, "$test.valgrind") {
441                     $valgrind++;
442                 } else {
443                     warn "$0: Failed to create '$test.valgrind': $!\n";
444                 }
445             }
446             elsif (@valgrind) {
447                 my $leaks = 0;
448                 my $errors = 0;
449                 for my $i (0..$#valgrind) {
450                     local $_ = $valgrind[$i];
451                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
452                         $errors += $1;   # there may be multiple error summaries
453                     } elsif (/^==\d+== LEAK SUMMARY:/) {
454                         for my $off (1 .. 4) {
455                             if ($valgrind[$i+$off] =~
456                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
457                                 $leaks += $1;
458                             }
459                         }
460                     }
461                 }
462                 if ($errors or $leaks) {
463                     if (rename $valgrind_log, "$test.valgrind") {
464                         $valgrind++;
465                     } else {
466                         warn "$0: Failed to create '$test.valgrind': $!\n";
467                     }
468                 }
469             } else {
470                 warn "No valgrind output?\n";
471             }
472             if (-e $valgrind_log) {
473                 unlink $valgrind_log
474                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
475             }
476         }
477         if ($type eq 'deparse') {
478             unlink "./$test.dp";
479         }
480         if ($ENV{PERL_3LOG}) {
481             my $tpp = $test;
482             $tpp =~ s:^\.\./::;
483             $tpp =~ s:/:_:g;
484             $tpp =~ s:\.t$:.3log:;
485             rename("perl.3log", $tpp) ||
486                 die "rename: perl3.log to $tpp: $!\n";
487         }
488         if (not defined $failure and $next != $max) {
489             $failure="FAILED--expected $max tests, saw $next";
490         }
491
492         if( !defined $failure  # don't mask a test failure
493             and $? )
494         {
495             $failure = "FAILED--non-zero wait status: $?";
496         }
497
498         if (defined $failure) {
499             print "${te}$failure\n";
500             $::bad_files++;
501             if ($test =~ /^base/) {
502                 die "Failed a basic test ($test) -- cannot continue.\n";
503             }
504             ++$failed_tests{$test};
505         }
506         else {
507             if ($max) {
508                 my $elapsed;
509                 if ( $show_elapsed_time ) {
510                     $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
511                 }
512                 else {
513                     $elapsed = "";
514                 }
515                 print "${te}ok$elapsed\n";
516                 $good_files++;
517             }
518             else {
519                 print "${te}skipped\n";
520                 $tested_files -= 1;
521             }
522         }
523     } # while tests
524
525     if ($::bad_files == 0) {
526         if ($good_files) {
527             print "All tests successful.\n";
528             # XXX add mention of 'perlbug -ok' ?
529         }
530         else {
531             die "FAILED--no tests were run for some reason.\n";
532         }
533     }
534     else {
535         my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
536         my $s = $::bad_files == 1 ? "" : "s";
537         warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
538         for my $test ( sort keys %failed_tests ) {
539             print "\t$test\n";
540         }
541         warn <<'SHRDLU_1';
542 ### Since not all tests were successful, you may want to run some of
543 ### them individually and examine any diagnostic messages they produce.
544 ### See the INSTALL document's section on "make test".
545 SHRDLU_1
546         warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
547 ### You have a good chance to get more information by running
548 ###   ./perl harness
549 ### in the 't' directory since most (>=80%) of the tests succeeded.
550 SHRDLU_2
551         if (eval {require Config; import Config; 1}) {
552             if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
553                 warn <<SHRDLU_3;
554 ### You may have to set your dynamic library search path,
555 ### $p, to point to the build directory:
556 SHRDLU_3
557                 if (exists $ENV{$p} && $ENV{$p} ne '') {
558                     warn <<SHRDLU_4a;
559 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
560 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
561 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
562 SHRDLU_4a
563                 } else {
564                     warn <<SHRDLU_4b;
565 ###   setenv $p `pwd`; cd t; ./perl harness
566 ###   $p=`pwd`; export $p; cd t; ./perl harness
567 ###   export $p=`pwd`; cd t; ./perl harness
568 SHRDLU_4b
569                 }
570                 warn <<SHRDLU_5;
571 ### for csh-style shells, like tcsh; or for traditional/modern
572 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
573 SHRDLU_5
574             }
575         }
576     }
577     my ($user,$sys,$cuser,$csys) = times;
578     print sprintf("u=%.2f  s=%.2f  cu=%.2f  cs=%.2f  scripts=%d  tests=%d\n",
579         $user,$sys,$cuser,$csys,$tested_files,$totmax);
580     if ($ENV{PERL_VALGRIND}) {
581         my $s = $valgrind == 1 ? '' : 's';
582         print "$valgrind valgrind report$s created.\n", ;
583     }
584 }
585 exit ($::bad_files != 0);
586
587 # ex: set ts=8 sts=4 sw=4 noet: