no-encode.t revision 1.3
1#!/usr/bin/perl
2#
3# Test for graceful degradation to non-utf8 output without Encode module.
4#
5# Copyright 2016 Niko Tyni <ntyni@iki.fi>
6# Copyright 2016, 2018-2019 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.008;
14use strict;
15use warnings;
16
17use Test::More tests => 5;
18
19# Remove the record of the Encode module being loaded if it already was (it
20# may have been loaded before the test suite runs), and then make it
21# impossible to load it.  This should be enough to trigger the fallback code
22# in Pod::Man.
23BEGIN {
24    delete $INC{'Encode.pm'};
25    my $reject_encode = sub {
26        if ($_[1] eq 'Encode.pm') {
27            die "refusing to load Encode\n";
28        }
29    };
30    unshift(@INC, $reject_encode);
31    ok(!eval { require Encode }, 'Cannot load Encode any more');
32}
33
34# Load the module.
35BEGIN {
36    use_ok('Pod::Man');
37}
38
39# Ensure we don't get warnings by throwing an exception if we see any.  This
40# is overridden below when we enable utf8 and do expect a warning.
41local $SIG{__WARN__} = sub { die "No warnings expected\n" };
42
43# First, check that everything works properly when utf8 isn't set.  We expect
44# to get accent-mangled ASCII output.  Don't use Test::Podlators, since it
45# wants to import Encode.
46#
47## no critic (ValuesAndExpressions::ProhibitEscapedCharacters)
48my $pod = "=encoding latin1\n\n=head1 NAME\n\nBeyonc\xE9!";
49my $parser = Pod::Man->new(utf8 => 0, name => 'test');
50my $output;
51$parser->output_string(\$output);
52$parser->parse_string_document($pod);
53like(
54    $output,
55    qr{ Beyonce\\[*]\' }xms,
56    'Works without Encode for non-utf8 output'
57);
58
59# Now, try with the utf8 option set.  We should then get a warning that we're
60# falling back to non-utf8 output.
61{
62    local $SIG{__WARN__} = sub {
63        like(
64            $_[0],
65            qr{ falling [ ] back [ ] to [ ] non-utf8 }xms,
66            'Pod::Man warns on utf8 option with no Encode module'
67        );
68    };
69    $parser = Pod::Man->new(utf8 => 1, name => 'test');
70}
71my $output_fallback;
72$parser->output_string(\$output_fallback);
73$parser->parse_string_document($pod);
74is($output_fallback, $output, 'Degraded gracefully to non-utf8 output');
75