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