1#!/usr/bin/perl
2#
3# Test for graceful degradation to non-UTF-8 output without Encode module.
4#
5# Copyright 2016 Niko Tyni <ntyni@iki.fi>
6# Copyright 2016, 2018-2019, 2022 Russ Allbery <rra@cpan.org>
7#
8# This program is free software; you may redistribute it and/or modify it
9# under the same terms as Perl itself.
10#
11# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
12
13use 5.010;
14use strict;
15use warnings;
16
17use Carp qw(croak);
18
19use Test::More tests => 8;
20
21# Remove the record of the Encode module being loaded if it already was (it
22# may have been loaded before the test suite runs), and then make it
23# impossible to load it.  This should be enough to trigger the fallback code
24# in Pod::Man.
25BEGIN {
26    delete $INC{'Encode.pm'};
27    my $reject_encode = sub {
28        if ($_[1] eq 'Encode.pm') {
29            croak('refusing to load Encode');
30        }
31    };
32    unshift(@INC, $reject_encode);
33    ok(!eval { require Encode }, 'Cannot load Encode any more');
34}
35
36# Load the module.
37BEGIN {
38    use_ok('Pod::Man');
39}
40
41# Ensure we don't get warnings by throwing an exception if we see any.  This
42# is overridden below when we enable utf8 and do expect a warning.
43local $SIG{__WARN__} = sub {
44    my @warnings = @_;
45    croak(join("\n", 'No warnings expected; instead got:', @warnings));
46};
47
48# First, check that everything works properly if an encoding of roff is set.
49# We expect to get accent-mangled ASCII output.  Don't use Test::Podlators,
50# since it wants to import Encode.
51my $invalid_char = chr(utf8::unicode_to_native(0xE9));
52my $pod = "=encoding latin1\n\n=head1 NAME\n\nBeyonc${invalid_char}!";
53my $parser = Pod::Man->new(name => 'test', encoding => 'roff');
54my $output;
55$parser->output_string(\$output);
56$parser->parse_string_document($pod);
57like(
58    $output,
59    qr{ Beyonce\\[*]\' }xms,
60    'Works without Encode for roff encoding',
61);
62
63# Likewise for an encoding of groff.
64$parser = Pod::Man->new(name => 'test', encoding => 'groff');
65my $output_groff = q{};
66$parser->output_string(\$output_groff);
67$parser->parse_string_document($pod);
68like(
69    $output_groff,
70    qr{ Beyonc\\\[u00E9\] }xms,
71    'Works without Encode for groff encoding',
72);
73
74# The default output format is UTF-8, so it should produce an error message
75# and then degrade to groff.
76{
77    local $SIG{__WARN__} = sub {
78        like(
79            $_[0],
80            qr{ falling [ ] back [ ] to [ ] groff [ ] escapes }xms,
81            'Pod::Man warns with no Encode module',
82        );
83    };
84    $parser = Pod::Man->new(name => 'test');
85}
86$output = q{};
87$parser->output_string(\$output);
88$parser->parse_string_document($pod);
89is($output, $output_groff, 'Degraded gracefull to groff output');
90
91# Now try with an explicit output encoding, which should produce an error
92# message and then degrade to groff.
93{
94    local $SIG{__WARN__} = sub {
95        like(
96            $_[0],
97            qr{ falling [ ] back [ ] to [ ] groff [ ] escapes }xms,
98            'Pod::Man warns with no Encode module',
99        );
100    };
101    $parser = Pod::Man->new(name => 'test', encoding => 'iso-8859-1');
102}
103$output = q{};
104$parser->output_string(\$output);
105$parser->parse_string_document($pod);
106is(
107    $output, $output_groff,
108    'Explicit degraded gracefully to groff output',
109);
110