1#!/usr/bin/perl
2#
3# Tests for backward compatibility with Pod::Parser.
4#
5# Copyright 2006, 2008-2009, 2012, 2015, 2018 by Russ Allbery <rra@cpan.org>
6#
7# This program is free software; you may redistribute it and/or modify it
8# under the same terms as Perl itself.
9#
10# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
11
12use 5.006;
13use strict;
14use warnings;
15
16use lib 't/lib';
17
18use File::Spec;
19use Test::More tests => 7;
20use Test::Podlators qw(slurp);
21
22# Ensure the modules load properly.
23BEGIN {
24    use_ok('Pod::Man');
25    use_ok('Pod::Text');
26}
27
28# Create a temporary directory to use for output, but don't fail if it already
29# exists.  If we failed to create it, we'll fail later on.  We unfortunately
30# have to create files on disk to easily create file handles for testing.
31my $tmpdir = File::Spec->catdir('t', 'tmp');
32if (!-d $tmpdir) {
33    mkdir($tmpdir, 0777);
34}
35
36# Create some test POD to use to test the -cutting option.
37my $infile = File::Spec->catfile('t', 'tmp', "tmp$$.pod");
38open(my $input, '>', $infile) or BAIL_OUT("cannot create $infile: $!");
39print {$input} "Some random B<text>.\n"
40  or BAIL_OUT("cannot write to $infile: $!");
41close($input) or BAIL_OUT("cannot write to $infile: $!");
42
43# Test the -cutting option with Pod::Man.
44my $parser = Pod::Man->new;
45isa_ok($parser, 'Pod::Man', 'Pod::Man parser object');
46my $outfile = File::Spec->catfile('t', 'tmp', "tmp$$.man");
47open(my $output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
48$parser->parse_from_file({ -cutting => 0 }, $infile, $output);
49close($output) or BAIL_OUT("cannot write to $outfile: $!");
50my $got = slurp($outfile, 'man');
51is($got, "Some random \\fBtext\\fR.\n", 'Pod::Man -cutting output');
52unlink($outfile);
53
54# Likewise for Pod::Text.
55$parser = Pod::Text->new;
56isa_ok($parser, 'Pod::Text', 'Pod::Text parser object');
57$outfile = File::Spec->catfile('t', 'tmp', "tmp$$.txt");
58open($output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
59$parser->parse_from_file({ -cutting => 0 }, $infile, $output);
60close($output) or BAIL_OUT("cannot write to $outfile: $!");
61$got = slurp($outfile);
62is($got, "    Some random text.\n\n", 'Pod::Text -cutting output');
63unlink($outfile);
64
65# Rewrite the input file to be fully valid POD since we won't use -cutting.
66unlink($infile);
67open($input, '>', $infile) or BAIL_OUT("cannot create $infile: $!");
68print {$input} "=pod\n\nSome random B<text>.\n"
69  or BAIL_OUT("cannot write to $infile: $!");
70close($input) or BAIL_OUT("cannot write to $infile: $!");
71
72# Now test the pod2text function with a single output.  This will send the
73# results to standard output, so we need to redirect that to a file.
74open($output,         '>',  $outfile) or BAIL_OUT("cannot open $outfile: $!");
75open(my $save_stdout, '>&', STDOUT)   or BAIL_OUT("cannot dup stdout: $!");
76open(STDOUT, '>&', $output) or BAIL_OUT("cannot redirect stdout: $!");
77pod2text($infile);
78close($output) or BAIL_OUT("cannot write to $outfile: $!");
79open(STDOUT, '>&', $save_stdout) or BAIL_OUT("cannot fix stdout: $!");
80close($save_stdout) or BAIL_OUT("cannot close saved stdout: $!");
81$got = slurp($outfile);
82is($got, "    Some random text.\n\n", 'Pod::Text pod2text function');
83
84# Clean up.
85unlink($infile, $outfile);
86