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, 2015, 2016 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
14use 5.006;
15use strict;
16use warnings;
17
18use lib 't/lib';
19
20use File::Spec;
21use Test::More tests => 4;
22use Test::Podlators qw(read_snippet slurp);
23
24# Ensure the modules load properly.
25BEGIN {
26    use_ok('Pod::Man');
27    use_ok('Pod::Text');
28}
29
30# Create a temporary directory to use for output, but don't fail if it already
31# exists.  If we failed to create it, we'll fail later on.  We unfortunately
32# have to create files on disk to easily create file handles for testing.
33my $tmpdir = File::Spec->catdir('t', 'tmp');
34if (!-d $tmpdir) {
35    mkdir($tmpdir, 0777);
36}
37
38# Load the tests.
39my $man_data_ref  = read_snippet('man/cpp');
40my $text_data_ref = read_snippet('text/cpp');
41
42# Write the POD source to a temporary file for the input file handle.
43my $infile = File::Spec->catfile('t', 'tmp', "tmp$$.pod");
44open(my $input, '>', $infile) or BAIL_OUT("cannot create $infile: $!");
45print {$input} $man_data_ref->{input}
46  or BAIL_OUT("cannot write to $infile: $!");
47close($input) or BAIL_OUT("cannot write to $infile: $!");
48
49# Write the Pod::Man output to a file.
50my $outfile = File::Spec->catfile('t', 'tmp', "tmp$$.man");
51open($input,     '<', $infile)  or BAIL_OUT("cannot open $infile: $!");
52open(my $output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
53my $parser = Pod::Man->new;
54$parser->parse_from_filehandle($input, $output);
55close($input)  or BAIL_OUT("cannot read from $infile: $!");
56close($output) or BAIL_OUT("cannot write to $outfile: $!");
57
58# Read the output back in and compare it.
59my $got = slurp($outfile, 'man');
60is($got, $man_data_ref->{output}, 'Pod::Man output');
61
62# Clean up the temporary output file.
63unlink($outfile);
64
65# Now, do the same drill with Pod::Text.  Parse the input to a temporary file.
66$outfile = File::Spec->catfile('t', 'tmp', "tmp$$.txt");
67open($input,  '<', $infile)  or BAIL_OUT("cannot open $infile: $!");
68open($output, '>', $outfile) or BAIL_OUT("cannot open $outfile: $!");
69$parser = Pod::Text->new;
70$parser->parse_from_filehandle($input, $output);
71close($input)  or BAIL_OUT("cannot read from $infile: $!");
72close($output) or BAIL_OUT("cannot write to $outfile: $!");
73
74# Read the output back in and compare it.  Pod::Text adds a trailing blank
75# line that we need to strip out.
76$got = slurp($outfile);
77$got =~ s{ \n \s+ \z }{\n}xms;
78is($got, $text_data_ref->{output}, 'Pod::Text output');
79
80# Clean up temporary files.
81unlink($infile, $outfile);
82