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