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