1#!/usr/bin/perl 2# 3# Basic tests for podlators. 4# 5# This test case uses a single sample file and runs it through all available 6# formatting modules, comparing the results to known-good output that's 7# included with the package. This provides a general sanity check that the 8# modules are working properly. 9# 10# New regression tests and special cases should probably not be added to the 11# sample input file, since updating all the output files is painful. Instead, 12# the machinery to run small POD snippets through the specific formatter being 13# tested should probably be used instead. 14# 15# Copyright 2001-2002, 2004, 2006, 2009, 2012, 2014-2015, 2018 16# Russ Allbery <rra@cpan.org> 17# 18# This program is free software; you may redistribute it and/or modify it 19# under the same terms as Perl itself. 20# 21# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl 22 23use 5.006; 24use strict; 25use warnings; 26 27use lib 't/lib'; 28 29use File::Spec; 30use Test::More tests => 15; 31use Test::Podlators qw(slurp); 32 33# Check that all the modules can be loaded. 34BEGIN { 35 use_ok('Pod::Man'); 36 use_ok('Pod::Text'); 37 use_ok('Pod::Text::Color'); 38 use_ok('Pod::Text::Overstrike'); 39 use_ok('Pod::Text::Termcap'); 40} 41 42# Flush output, since otherwise our diag messages come after other tests. 43local $| = 1; 44 45# Hard-code configuration for Term::Cap to get predictable results. 46local $ENV{COLUMNS} = 80; 47local $ENV{TERM} = 'xterm'; 48local $ENV{TERMPATH} = File::Spec->catfile('t', 'data', 'termcap'); 49local $ENV{TERMCAP} = 'xterm:co=#80:do=^J:md=\\E[1m:us=\\E[4m:me=\\E[m'; 50 51# Find the source of the test file. 52my $INPUT = File::Spec->catfile('t', 'data', 'basic.pod'); 53 54# Map of translators to the file containing the formatted output to compare 55# against. 56my %OUTPUT = ( 57 'Pod::Man' => File::Spec->catfile('t', 'data', 'basic.man'), 58 'Pod::Text' => File::Spec->catfile('t', 'data', 'basic.txt'), 59 'Pod::Text::Color' => File::Spec->catfile('t', 'data', 'basic.clr'), 60 'Pod::Text::Overstrike' => File::Spec->catfile('t', 'data', 'basic.ovr'), 61 'Pod::Text::Termcap' => File::Spec->catfile('t', 'data', 'basic.cap'), 62); 63 64# Walk through teach of the modules and format the sample file, checking to 65# ensure the results match the pre-generated file. 66for my $module (sort keys %OUTPUT) { 67 my $parser = $module->new(); 68 isa_ok($parser, $module, 'parser object'); 69 70 # Run the formatting module. Store the output into a Perl variable 71 # instead of a file. 72 my $got; 73 $parser->output_string(\$got); 74 $parser->parse_file($INPUT); 75 76 # If the test module is Pod::Man, strip off the header. This test does 77 # not attempt to compare it, since it contains version numbers that 78 # change. 79 if ($module eq 'Pod::Man') { 80 $got =~ s{ \A .* \n [.]nh \n }{}xms; 81 } 82 83 # OS/390 is EBCDIC, which apparently uses a different character for ESC. 84 # Try to convert so that the test still works. 85 if ($^O eq 'os390' && $module eq 'Pod::Text::Termcap') { 86 $got =~ tr{\033}{\047}; 87 } 88 89 # Check the output. If it doesn't match, save the erroneous output in a 90 # file for later inspection. 91 my $expected = slurp($OUTPUT{$module}); 92 if (!ok($got eq $expected, "$module output is correct")) { 93 my ($suffix) = ($OUTPUT{$module} =~ m{ [.] ([^.]+) \z }xms); 94 my $tmpdir = File::Spec->catdir('t', 'tmp'); 95 if (!-d $tmpdir) { 96 mkdir($tmpdir, 0777); 97 } 98 my $outfile = File::Spec->catfile('t', 'tmp', "out$$.$suffix"); 99 open(my $output, '>', $outfile) 100 or BAIL_OUT("cannot create $outfile for failed output: $!"); 101 print {$output} $got 102 or BAIL_OUT("cannot write failed output to $outfile: $!"); 103 close($output) 104 or BAIL_OUT("cannot write failed output to $outfile: $!"); 105 diag("Non-matching output left in $outfile"); 106 } 107} 108