no-encode.t revision 1.4
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 join("\n",
42                                      "No warnings expected; instead got:",
43                                      @_);
44                           };
45# First, check that everything works properly when utf8 isn't set.  We expect
46# to get accent-mangled ASCII output.  Don't use Test::Podlators, since it
47# wants to import Encode.
48#
49## no critic (ValuesAndExpressions::ProhibitEscapedCharacters)
50my $pod = "=encoding latin1\n\n=head1 NAME\n\nBeyonc"
51        . chr(utf8::unicode_to_native(0xE9))
52        . "!";
53my $parser = Pod::Man->new(utf8 => 0, name => 'test');
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 non-utf8 output'
61);
62
63# Now, try with the utf8 option set.  We should then get a warning that we're
64# falling back to non-utf8 output.
65{
66    local $SIG{__WARN__} = sub {
67        like(
68            $_[0],
69            qr{ falling [ ] back [ ] to [ ] non-utf8 }xms,
70            'Pod::Man warns on utf8 option with no Encode module'
71        );
72    };
73    $parser = Pod::Man->new(utf8 => 1, name => 'test');
74}
75my $output_fallback;
76$parser->output_string(\$output_fallback);
77$parser->parse_string_document($pod);
78is($output_fallback, $output, 'Degraded gracefully to non-utf8 output');
79