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