This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
do not count tests, instead use done_testing()
[perl5.git] / lib / Pod / t / Usage.t
CommitLineData
d44282b9
AT
1#!perl
2use strict;
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use File::Basename;
9use File::Spec;
10use Test::More;
d44282b9
AT
11
12use_ok( 'Pod::Usage' );
13
14# Test verbose level 0
15my $vbl_0 = << 'EOMSG';
16Usage:
17 The SYNOPSIS section is displayed with -verbose >= 0.
18
19EOMSG
20my $fake_out = tie *FAKEOUT, 'CatchOut';
21pod2usage({ -verbose => 0, -exit => 'noexit', -output => \*FAKEOUT });
22is( $$fake_out, $vbl_0, 'Verbose level 0' );
23
24my $msg = "Prefix message for pod2usage()";
25$$fake_out = '';
26pod2usage({ -verbose => 0, -exit => 'noexit', -output => \*FAKEOUT,
27 -message => $msg });
28is( $$fake_out, "$msg\n$vbl_0", '-message parameter' );
29
30SKIP: {
31 my( $file, $path ) = fileparse( $0 );
32 skip( 'File in current directory', 2 ) if -e $file;
33 $$fake_out = '';
34 eval {
35 pod2usage({ -verbose => 0, -exit => 'noexit',
36 -output => \*FAKEOUT, -input => $file });
37 };
7eb7d786 38 like( $@, qr/^Can't open $file/,
d44282b9
AT
39 'File not found without -pathlist' );
40
41 eval {
42 pod2usage({ -verbose => 0, -exit => 'noexit',
43 -output => \*FAKEOUT, -input => $file,
44 -pathlist => $path });
45 };
46 is( $$fake_out, $vbl_0, '-pathlist parameter' );
47}
48
70360898 49{ # Test exit status from pod2usage()
0cb07b6b 50 my $exit = ($^O eq 'VMS' ? 2 : 42);
d44282b9
AT
51 my $dev_null = File::Spec->devnull;
52 my $args = join ", ", (
53 "-verbose => 0",
54 "-exit => $exit",
0cb07b6b
CB
55 "-output => q{$dev_null}",
56 "-input => q{$0}",
d44282b9 57 );
0cb07b6b 58 my $cq = (($^O eq 'MSWin32'
0cb07b6b 59 || $^O eq 'VMS') ? '"'
2c05888b 60 : "");
0cb07b6b
CB
61 my @params = ( "${cq}-I../lib$cq", "${cq}-MPod::Usage$cq", '-e' );
62 my $prg = qq[${cq}pod2usage({ $args })$cq];
63 my @cmd = ( $^X, @params, $prg );
d44282b9 64
2c05888b
JH
65 print "# cmd = @cmd\n";
66
d44282b9
AT
67 is( system( @cmd ) >> 8, $exit, 'Exit status of pod2usage()' );
68}
69
70# Test verbose level 1
71my $vbl_1 = << 'EOMSG';
72Usage:
73 The SYNOPSIS section is displayed with -verbose >= 0.
74
75Options:
76 The OPTIONS section is displayed with -verbose >= 1.
77
78Arguments:
79 The ARGUMENTS section is displayed with -verbose >= 1.
80
81EOMSG
82$$fake_out = '';
83pod2usage( { -verbose => 1, -exit => 'noexit', -output => \*FAKEOUT } );
84is( $$fake_out, $vbl_1, 'Verbose level 1' );
85
86# Test verbose level 2
87$$fake_out = '';
88require Pod::Text; # Pod::Usage->isa( 'Pod::Text' )
89
90( my $p2tp = new Pod::Text )->parse_from_file( $0, \*FAKEOUT );
91my $pod2text = $$fake_out;
92
93$$fake_out = '';
94pod2usage( { -verbose => 2, -exit => 'noexit', -output => \*FAKEOUT } );
95my $pod2usage = $$fake_out;
96
97is( $pod2usage, $pod2text, 'Verbose level >= 2 eq pod2text' );
98
70cbce25 99done_testing();
d44282b9
AT
100
101package CatchOut;
102sub TIEHANDLE { bless \( my $self ), shift }
103sub PRINT { my $self = shift; $$self .= $_[0] }
104
105__END__
106
107=head1 NAME
108
109Usage.t - Tests for Pod::Usage
110
111=head1 SYNOPSIS
112
113The B<SYNOPSIS> section is displayed with -verbose >= 0.
114
115=head1 DESCRIPTION
116
117Testing Pod::Usage. This section is not displayed with -verbose < 2.
118
119=head1 OPTIONS
120
121The B<OPTIONS> section is displayed with -verbose >= 1.
122
123=head1 ARGUMENTS
124
125The B<ARGUMENTS> section is displayed with -verbose >= 1.
126
127=head1 AUTHOR
128
12920020105 Abe Timmerman <abe@ztreet.demon.nl>
130
131=cut