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