1*0Sstevel@tonic-gatepackage TestPodIncPlainText; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate use File::Basename; 5*0Sstevel@tonic-gate use File::Spec; 6*0Sstevel@tonic-gate use Cwd qw(abs_path); 7*0Sstevel@tonic-gate push @INC, '..'; 8*0Sstevel@tonic-gate my $THISDIR = abs_path(dirname $0); 9*0Sstevel@tonic-gate unshift @INC, $THISDIR; 10*0Sstevel@tonic-gate require "testcmp.pl"; 11*0Sstevel@tonic-gate import TestCompare; 12*0Sstevel@tonic-gate my $PARENTDIR = dirname $THISDIR; 13*0Sstevel@tonic-gate push @INC, map { File::Spec->catfile($_, 'lib') } ($PARENTDIR, $THISDIR); 14*0Sstevel@tonic-gate} 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate#use strict; 17*0Sstevel@tonic-gate#use diagnostics; 18*0Sstevel@tonic-gateuse Carp; 19*0Sstevel@tonic-gateuse Exporter; 20*0Sstevel@tonic-gate#use File::Compare; 21*0Sstevel@tonic-gate#use Cwd qw(abs_path); 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gateuse vars qw($MYPKG @EXPORT @ISA); 24*0Sstevel@tonic-gate$MYPKG = eval { (caller)[0] }; 25*0Sstevel@tonic-gate@EXPORT = qw(&testpodplaintext); 26*0Sstevel@tonic-gateBEGIN { 27*0Sstevel@tonic-gate require Pod::PlainText; 28*0Sstevel@tonic-gate @ISA = qw( Pod::PlainText ); 29*0Sstevel@tonic-gate require VMS::Filespec if $^O eq 'VMS'; 30*0Sstevel@tonic-gate} 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate## Hardcode settings for TERMCAP and COLUMNS so we can try to get 33*0Sstevel@tonic-gate## reproducible results between environments 34*0Sstevel@tonic-gate@ENV{qw(TERMCAP COLUMNS)} = ('co=76:do=^J', 76); 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gatesub catfile(@) { File::Spec->catfile(@_); } 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gatemy $INSTDIR = abs_path(dirname $0); 39*0Sstevel@tonic-gate$INSTDIR = VMS::Filespec::unixpath($INSTDIR) if $^O eq 'VMS'; 40*0Sstevel@tonic-gate$INSTDIR =~ s#/$## if $^O eq 'VMS'; 41*0Sstevel@tonic-gate$INSTDIR =~ s#:$## if $^O eq 'MacOS'; 42*0Sstevel@tonic-gate$INSTDIR = (dirname $INSTDIR) if (basename($INSTDIR) eq 'pod'); 43*0Sstevel@tonic-gate$INSTDIR =~ s#:$## if $^O eq 'MacOS'; 44*0Sstevel@tonic-gate$INSTDIR = (dirname $INSTDIR) if (basename($INSTDIR) eq 't'); 45*0Sstevel@tonic-gatemy @PODINCDIRS = ( catfile($INSTDIR, 'lib', 'Pod'), 46*0Sstevel@tonic-gate catfile($INSTDIR, 'scripts'), 47*0Sstevel@tonic-gate catfile($INSTDIR, 'pod'), 48*0Sstevel@tonic-gate catfile($INSTDIR, 't', 'pod') 49*0Sstevel@tonic-gate ); 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate## Find the path to the file to =include 52*0Sstevel@tonic-gatesub findinclude { 53*0Sstevel@tonic-gate my $self = shift; 54*0Sstevel@tonic-gate my $incname = shift; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate ## See if its already found w/out any "searching; 57*0Sstevel@tonic-gate return $incname if (-r $incname); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate ## Need to search for it. Look in the following directories ... 60*0Sstevel@tonic-gate ## 1. the directory containing this pod file 61*0Sstevel@tonic-gate my $thispoddir = dirname $self->input_file; 62*0Sstevel@tonic-gate ## 2. the parent directory of the above 63*0Sstevel@tonic-gate my $parentdir = dirname $thispoddir; 64*0Sstevel@tonic-gate my @podincdirs = ($thispoddir, $parentdir, @PODINCDIRS); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate for (@podincdirs) { 67*0Sstevel@tonic-gate my $incfile = catfile($_, $incname); 68*0Sstevel@tonic-gate return $incfile if (-r $incfile); 69*0Sstevel@tonic-gate } 70*0Sstevel@tonic-gate warn("*** Can't find =include file $incname in @podincdirs\n"); 71*0Sstevel@tonic-gate return ""; 72*0Sstevel@tonic-gate} 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gatesub command { 75*0Sstevel@tonic-gate my $self = shift; 76*0Sstevel@tonic-gate my ($cmd, $text, $line_num, $pod_para) = @_; 77*0Sstevel@tonic-gate $cmd = '' unless (defined $cmd); 78*0Sstevel@tonic-gate local $_ = $text || ''; 79*0Sstevel@tonic-gate my $out_fh = $self->output_handle; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate ## Defer to the superclass for everything except '=include' 82*0Sstevel@tonic-gate return $self->SUPER::command(@_) unless ($cmd eq "include"); 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate ## We have an '=include' command 85*0Sstevel@tonic-gate my $incdebug = 1; ## debugging 86*0Sstevel@tonic-gate my @incargs = split; 87*0Sstevel@tonic-gate if (@incargs == 0) { 88*0Sstevel@tonic-gate warn("*** No filename given for '=include'\n"); 89*0Sstevel@tonic-gate return; 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate my $incfile = $self->findinclude(shift @incargs) or return; 92*0Sstevel@tonic-gate my $incbase = basename $incfile; 93*0Sstevel@tonic-gate print $out_fh "###### begin =include $incbase #####\n" if ($incdebug); 94*0Sstevel@tonic-gate $self->parse_from_file( {-cutting => 1}, $incfile ); 95*0Sstevel@tonic-gate print $out_fh "###### end =include $incbase #####\n" if ($incdebug); 96*0Sstevel@tonic-gate} 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gatesub begin_input { 99*0Sstevel@tonic-gate $_[0]->{_INFILE} = VMS::Filespec::unixify($_[0]->{_INFILE}) if $^O eq 'VMS'; 100*0Sstevel@tonic-gate} 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gatesub podinc2plaintext( $ $ ) { 103*0Sstevel@tonic-gate my ($infile, $outfile) = @_; 104*0Sstevel@tonic-gate local $_; 105*0Sstevel@tonic-gate my $text_parser = $MYPKG->new; 106*0Sstevel@tonic-gate $text_parser->parse_from_file($infile, $outfile); 107*0Sstevel@tonic-gate} 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gatesub testpodinc2plaintext( @ ) { 110*0Sstevel@tonic-gate my %args = @_; 111*0Sstevel@tonic-gate my $infile = $args{'-In'} || croak "No input file given!"; 112*0Sstevel@tonic-gate my $outfile = $args{'-Out'} || croak "No output file given!"; 113*0Sstevel@tonic-gate my $cmpfile = $args{'-Cmp'} || croak "No compare-result file given!"; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate my $different = ''; 116*0Sstevel@tonic-gate my $testname = basename $cmpfile, '.t', '.xr'; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate unless (-e $cmpfile) { 119*0Sstevel@tonic-gate my $msg = "*** Can't find comparison file $cmpfile for testing $infile"; 120*0Sstevel@tonic-gate warn "$msg\n"; 121*0Sstevel@tonic-gate return $msg; 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate print "# Running testpodinc2plaintext for '$testname'...\n"; 125*0Sstevel@tonic-gate ## Compare the output against the expected result 126*0Sstevel@tonic-gate podinc2plaintext($infile, $outfile); 127*0Sstevel@tonic-gate if ( testcmp($outfile, $cmpfile) ) { 128*0Sstevel@tonic-gate $different = "$outfile is different from $cmpfile"; 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate else { 131*0Sstevel@tonic-gate unlink($outfile); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate return $different; 134*0Sstevel@tonic-gate} 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gatesub testpodplaintext( @ ) { 137*0Sstevel@tonic-gate my %opts = (ref $_[0] eq 'HASH') ? %{shift()} : (); 138*0Sstevel@tonic-gate my @testpods = @_; 139*0Sstevel@tonic-gate my ($testname, $testdir) = ("", ""); 140*0Sstevel@tonic-gate my ($podfile, $cmpfile) = ("", ""); 141*0Sstevel@tonic-gate my ($outfile, $errfile) = ("", ""); 142*0Sstevel@tonic-gate my $passes = 0; 143*0Sstevel@tonic-gate my $failed = 0; 144*0Sstevel@tonic-gate local $_; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate print "1..", scalar @testpods, "\n" unless ($opts{'-xrgen'}); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate for $podfile (@testpods) { 149*0Sstevel@tonic-gate ($testname, $_) = fileparse($podfile); 150*0Sstevel@tonic-gate $testdir ||= $_; 151*0Sstevel@tonic-gate $testname =~ s/\.t$//; 152*0Sstevel@tonic-gate $cmpfile = $testdir . $testname . '.xr'; 153*0Sstevel@tonic-gate $outfile = $testdir . $testname . '.OUT'; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if ($opts{'-xrgen'}) { 156*0Sstevel@tonic-gate if ($opts{'-force'} or ! -e $cmpfile) { 157*0Sstevel@tonic-gate ## Create the comparison file 158*0Sstevel@tonic-gate print "# Creating expected result for \"$testname\"" . 159*0Sstevel@tonic-gate " pod2plaintext test ...\n"; 160*0Sstevel@tonic-gate podinc2plaintext($podfile, $cmpfile); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate else { 163*0Sstevel@tonic-gate print "# File $cmpfile already exists" . 164*0Sstevel@tonic-gate " (use '-force' to regenerate it).\n"; 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate next; 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate my $failmsg = testpodinc2plaintext 170*0Sstevel@tonic-gate -In => $podfile, 171*0Sstevel@tonic-gate -Out => $outfile, 172*0Sstevel@tonic-gate -Cmp => $cmpfile; 173*0Sstevel@tonic-gate if ($failmsg) { 174*0Sstevel@tonic-gate ++$failed; 175*0Sstevel@tonic-gate print "#\tFAILED. ($failmsg)\n"; 176*0Sstevel@tonic-gate print "not ok ", $failed+$passes, "\n"; 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate else { 179*0Sstevel@tonic-gate ++$passes; 180*0Sstevel@tonic-gate unlink($outfile); 181*0Sstevel@tonic-gate print "#\tPASSED.\n"; 182*0Sstevel@tonic-gate print "ok ", $failed+$passes, "\n"; 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate return $passes; 186*0Sstevel@tonic-gate} 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate1; 189