]> iEval git - pod-constants.git/blame - t/01-constants.t
Make code shorter and wider
[pod-constants.git] / t / 01-constants.t
CommitLineData
f46dd377
MG
1#!/usr/bin/perl -w
2
3use strict;
4use Test::More tests => 20;
5use Data::Dumper;
6
7use vars qw($section_1 $section_2 $section_3 $section_4 %options);
8
9use_ok(
10 "Pod::Constants",
11 section_1 => \$section_1,
12 -trim => 1,
13 section_2 => \$section_2,
14 section_3 => sub { tr/[a-z]/[A-Z]/; $section_3 = $_ },
15 section_4 => sub { eval },
16 'command line parameters' => sub {
17 &Pod::Constants::add_hook
18 (
19 #-trim => 0,
20 '*item' => sub {
21 my ($options, $description) =
22 m/^(.*?)\n\n(.*)/s;
23 my (@options, $longest);
24 $longest = "";
25 for my $option
26 ($options =~ m/\G((?:-\w|--\w+))(?:,\s*)?/g) {
27 push @options, $option;
28 if ( length $option > length $longest) {
29 $longest = $option;
30 }
31 }
32 $longest =~ s/^-*//;
33 $options{$longest} =
34 {
35 options => \@options,
36 description => $description,
37 };
38 }
39 )
40 },
41 );
42
43# try as hard as we can to get the path to perl
44ok($Pod::Constants::VERSION,
45 "Pod::Constants sets its own VERSION");
46
47BEGIN {
48 push @INC, "t";
49};
50# to avoid a warning
51if ( 0 ) { $Cheese::foo = $ReEntrancyTest::wohoo = $Cheese::quux; }
52eval "use Cheese";
53
54is($section_1, "Down with Pants!\n\n", "no trim from main");
55is($section_2, "42", "with trim from main");
56is($section_3, "CLANK_EST", "sub");
57is($section_4, "touche", "eval");
58is($Cheese::foo, "detcepxe", "From module");
59is($ReEntrancyTest::wohoo, "Re-entrancy works!", "From module");
60is($Cheese::quux, "Blah.", "From module(2)");
61like(`$^X -c t/Cheese.pm 2>&1`, qr/syntax OK/, "perl -c module");
62like(`$^X -c t/cheese.pl 2>&1`, qr/syntax OK/, "perl -c script");
63
64# test the examples on the man page :)
65package Pod::Constants;
66Pod::Constants->import (SYNOPSIS => sub {
67 $main::section_1 = join "\n", map { s/^ //; $_ } split /\n/, $_
68});
69
70package main;
71open NEWPKG, ">t/TestManPage.pm" or die $!;
72# why define your test results when you can read them in from POD?
612aada2 73$section_1 =~ s/myhash\)/myhash, %myhash2)/;
f46dd377
MG
74$section_1 =~ s/myhash;/myhash, "%myhash\'s value after the above:" => sub { %myhash2 = eval };/;
75print NEWPKG "package TestManPage;\n$section_1\n2.818;\n";
76close NEWPKG;
77
78use_ok("TestManPage");
79
80is($TestManPage::myvar, 'This string will be loaded into $myvar',
81 "man page example 1");
82is($TestManPage::VERSION, $Pod::Constants::VERSION,
83 "man page example 2");
84ok($TestManPage::VERSION,
85 "man page example 2 cross-check");
86is($TestManPage::myarray[2], 'For example, this is $myarray[2].',
87 "man page example 3");
88my $ok = 0;
89while (my ($k, $v) = each %TestManPage::myhash) {
90 if (exists $TestManPage::myhash2{$k}) { $ok ++ };
91 if ($v eq $TestManPage::myhash2{$k}) { $ok ++ };
92}
93is($ok, 4,
94 "man page example 4");
95is(scalar keys %TestManPage::myhash, 2,
96 "man page example 4 cross-check");
97is($TestManPage::html, '<p>This text will be in $html</p>',
98 "man page example 5");
99# supress warnings
100$TestManPage::myvar = $TestManPage::html = undef;
101@TestManPage::myarray = ();
102
103is($options{foo}->{options}->[0], "-f", "Pod::Constants::add_hook");
104
105=head2 section_1
106
107Down with Pants!
108
109=head2 section_2
110
11142
112
113=head2 section_3
114
115clank_est
116
117=head2 section_4
118
119$section_4 = "touche"
120
121=cut
122
123=head1 command line parameters
124
125the following command line parameters are supported
126
127=item -f, --foo
128
129This does something cool.
130
131=item -h, --help
132
133This also does something pretty cool.
134
135=cut
This page took 0.025455 seconds and 4 git commands to generate.