xref: /openbsd-src/gnu/usr.bin/perl/cpan/podlators/t/man/encoding.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1#!/usr/bin/perl
2#
3# Encoding tests for Pod::Man.
4#
5# This test uses a single test file with UTF-8 characters and escapes and
6# processes it with different encoding configurations for Pod::Man, comparing
7# it with pre-generated and hand-checked output files.
8#
9# The primary purpose of these test files is for portability testing on
10# different operating systems, but this test ensures that they remain accurate
11# for any changes to Pod::Man.  It doubles as a test that the preamble is
12# emitted correctly.
13#
14# Copyright 2022 Russ Allbery <rra@cpan.org>
15#
16# This program is free software; you may redistribute it and/or modify it
17# under the same terms as Perl itself.
18#
19# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
20
21use 5.008;
22use strict;
23use warnings;
24
25use lib 't/lib';
26
27use File::Spec;
28use Test::More tests => 4;
29use Test::Podlators qw(slurp);
30
31BEGIN {
32    use_ok('Pod::Man');
33}
34
35# Force the timestamp on the input file since it will otherwise depend on the
36# checkout.
37local $ENV{SOURCE_DATE_EPOCH} = 1664146047;
38
39# Get the path to the input and output files.
40my $input = File::Spec->catfile('t', 'data', 'man', 'encoding.pod');
41#<<<
42my %output = (
43    groff => File::Spec->catfile('t', 'data', 'man', 'encoding.groff'),
44    roff  => File::Spec->catfile('t', 'data', 'man', 'encoding.roff'),
45    utf8  => File::Spec->catfile('t', 'data', 'man', 'encoding.utf8'),
46);
47#>>>
48
49# For each encoding, load the input, generate the output, and check that the
50# output matches.
51for my $encoding (sort(keys(%output))) {
52    my $parser = Pod::Man->new(
53        encoding => $encoding,
54        center => 'podlators',
55        release => 'testing',
56    );
57    my $got;
58    $parser->output_string(\$got);
59    $parser->parse_file($input);
60
61    # Strip off the version line.
62    $got =~ s{ ^ [^\n]+ Automatically [ ] generated [ ] by [^\n]+ \n }{}xms;
63
64    # Check the output.  If it doesn't match, save the erroneous output in a
65    # file for later inspection.
66    my $expected = slurp($output{$encoding});
67    if (!ok($got eq $expected, "encoding.pod output with $encoding")) {
68        my $tmpdir = File::Spec->catdir('t', 'tmp');
69        if (!-d $tmpdir) {
70            mkdir($tmpdir, 0777);
71        }
72        my $outfile = File::Spec->catfile('t', 'tmp', "encoding$$.$encoding");
73        open(my $output, '>', $outfile)
74          or BAIL_OUT("cannot create $outfile for failed output: $!");
75        print {$output} $got
76          or BAIL_OUT("cannot write failed output to $outfile: $!");
77        close($output)
78          or BAIL_OUT("cannot write failed output to $outfile: $!");
79        diag("Non-matching output left in $outfile");
80    }
81}
82