1#!/usr/bin/perl
2#
3# Test the parse_from_filehandle method.
4#
5# This backward compatibility interface is not provided by Pod::Simple, so
6# Pod::Man and Pod::Text had to implement it directly.  Test to be sure it's
7# working properly.
8#
9# Copyright 2006, 2009, 2012, 2014-2016, 2018-2019 Russ Allbery <rra@cpan.org>
10#
11# This program is free software; you may redistribute it and/or modify it
12# under the same terms as Perl itself.
13#
14# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
15
16use 5.008;
17use strict;
18use warnings;
19
20use lib 't/lib';
21
22use File::Spec;
23use Test::More tests => 4;
24use Test::Podlators qw(read_snippet slurp);
25
26# Ensure the modules load properly.
27BEGIN {
28    use_ok('Pod::Man');
29    use_ok('Pod::Text');
30}
31
32# Create a temporary directory to use for output, but don't fail if it already
33# exists.  If we failed to create it, we'll fail later on.  We unfortunately
34# have to create files on disk to easily create file handles for testing.
35my $tmpdir = File::Spec->catdir('t', 'tmp');
36if (!-d $tmpdir) {
37    mkdir($tmpdir, 0777);
38}
39
40# Load the tests.
41my $man_data_ref = read_snippet('man/cpp');
42my $text_data_ref = read_snippet('text/cpp');
43
44# Write the POD source to a temporary file for the input file handle.
45my $infile = File::Spec->catfile('t', 'tmp', "tmp$$.pod");
46open(my $input, '>', $infile) or BAIL_OUT("cannot create $infile: $!");
47print {$input} $man_data_ref->{input}
48  or BAIL_OUT("cannot write to $infile: $!");
49close($input) or BAIL_OUT("cannot write to $infile: $!");
50
51# Write the Pod::Man output to a file.
52my $outfile = File::Spec->catfile('t', 'tmp', "tmp$$.man");
53open($input, '<', $infile) or BAIL_OUT("cannot open $infile: $!");
54open(my $output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
55my $parser = Pod::Man->new;
56$parser->parse_from_filehandle($input, $output);
57close($input) or BAIL_OUT("cannot read from $infile: $!");
58close($output) or BAIL_OUT("cannot write to $outfile: $!");
59
60# Read the output back in and compare it.
61my $got = slurp($outfile, 'man');
62is($got, $man_data_ref->{output}, 'Pod::Man output');
63
64# Clean up the temporary output file.
65unlink($outfile);
66
67# Now, do the same drill with Pod::Text.  Parse the input to a temporary file.
68$outfile = File::Spec->catfile('t', 'tmp', "tmp$$.txt");
69open($input, '<', $infile) or BAIL_OUT("cannot open $infile: $!");
70open($output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
71$parser = Pod::Text->new;
72$parser->parse_from_filehandle($input, $output);
73close($input) or BAIL_OUT("cannot read from $infile: $!");
74close($output) or BAIL_OUT("cannot write to $outfile: $!");
75
76# Read the output back in and compare it.  Pod::Text adds a trailing blank
77# line that we need to strip out.
78$got = slurp($outfile);
79$got =~ s{ \n \s+ \z }{\n}xms;
80is($got, $text_data_ref->{output}, 'Pod::Text output');
81
82# Clean up temporary files.
83unlink($infile, $outfile);
84