HTML-Template-Dumper-0.1/ 0040755 0153041 0001040 00000000000 07757164024 014167 5 ustar tmurray all HTML-Template-Dumper-0.1/CHANGES 0100644 0153041 0001040 00000000034 07757164024 015154 0 ustar tmurray all
0.1 -
- Initial release
HTML-Template-Dumper-0.1/MANIFEST.SKIP 0100644 0153041 0001040 00000000006 07757164024 016056 0 ustar tmurray all ~$
^.
HTML-Template-Dumper-0.1/README 0100644 0153041 0001040 00000001257 07757164024 015051 0 ustar tmurray all This module helps in debugging HTML::Template-based programs. Testing
the output of your programs is as easy as walking a hash. Note that it
will only test the variables set in the template, not the design of the
page. For that, you really need to use human.pl, a throughly excelent
peice of software that has been stable for many, many years.
The prefered installation method is to use Module::Build:
perl Build.PL
./Build
./Build test
./Build install
A Makefile.PL is also included for backwards compatibility, and is
thus installable using the old-school method:
perl Makefile.PL
make
make test
make install
Note that Module::Build needs to be installed in either case.
HTML-Template-Dumper-0.1/Build.PL 0100755 0153041 0001040 00000000746 07757164024 015472 0 ustar tmurray all #!/usr/bin/perl
use strict;
use warnings;
use Module::Build;
my $build = Module::Build->new(
module_name => 'HTML::Template::Dumper',
license => 'perl',
create_makefile_pl => 'passthrough',
requires => {
'HTML::Template' => 2.6,
'Data::Dumper' => 0,
},
build_requires => {
'Struct::Compare' => 0,
'IO::Scalar' => 0,
},
recommends => {
'YAML' => 0,
},
);
$build->create_build_script;
HTML-Template-Dumper-0.1/examples/ 0040755 0153041 0001040 00000000000 07757164024 016005 5 ustar tmurray all HTML-Template-Dumper-0.1/examples/test_page.pl 0100755 0153041 0001040 00000002505 07757164024 020317 0 ustar tmurray all #!/usr/local/bin/perl
#
# A simple example of writing a test for a CGI.
#
# Requirements:
#
# - Sends a MIME-type of text/html
# - When sending a parameter $foo, then there will
# be a parameter $bar, where $bar == $foo + 1
#
=head1 COPYRIGHT
Copyright 2003, American Society of Agronomy. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of either:
a) the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version, or
b) the "Artistic License" which comes with Perl.
=cut
use strict;
use warnings;
use LWP::UserAgent;
use HTML::Template::Dumper;
use Test::More tests => 20;
use constant URI => 'http://www.example.com/cgi-bin/test.cgi';
sub run_test
{
my $foo_value = shift || 0;
my $ua = LWP::UserAgent->new;
my $response = $ua->post( URI, foo => $foo_value );
if($response->is_success) {
ok( $response->header('Content-Type') eq 'text/html',
"Checking MIME type" );
my $data = HTML::Template::Dumper->parse(
$response->content
);
ok( $data->{bar} == $foo_value + 1,
"Checking value of 'bar'" );
}
else {
warn $response->status_line, "\n";
# Failed both tests
ok(0, "Checking MIME type" );
ok(0, "Checking value of 'bar'" );
}
}
run_test($_) for (0 .. 9);
HTML-Template-Dumper-0.1/examples/test.cgi 0100755 0153041 0001040 00000002620 07757164024 017450 0 ustar tmurray all #!/usr/local/bin/perl -T
#
# Author: Timm Murray
# Name: test
# Description: Demonstates the use of a test template
#
=head1 COPYRIGHT
Copyright 2003, American Society of Agronomy. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of either:
a) the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version, or
b) the "Artistic License" which comes with Perl.
=cut
use strict;
use warnings;
use constant DEBUG => 1;
{
my @FIELDS = qw( foo );
sub set_params
{
my $q = shift;
my %params = map { $_ => $q->param($_) || 0 } @FIELDS;
return \%params;
}
}
sub get_template
{
my $template_data;
while(my $line = ) { $template_data .= $line }
my $tmpl;
if(DEBUG) {
use HTML::Template::Dumper;
$tmpl = HTML::Template::Dumper->new(
scalarref => \$template_data,
);
}
else {
use HTML::Template;
$tmpl = HTML::Template->new(
scalarref => \$template_data,
);
}
return $tmpl;
}
{
use CGI;
my $q = CGI->new();
my $params = set_params($q);
my $tmpl = get_template();
$tmpl->param( bar => $params->{foo} + 1 );
print $q->header('text/html');
$tmpl->output( print_to => *STDOUT );
}
__DATA__
Test Page
HTML-Template-Dumper-0.1/lib/ 0040755 0153041 0001040 00000000000 07757164024 014735 5 ustar tmurray all HTML-Template-Dumper-0.1/lib/HTML/ 0040755 0153041 0001040 00000000000 07757164024 015501 5 ustar tmurray all HTML-Template-Dumper-0.1/lib/HTML/Template/ 0040755 0153041 0001040 00000000000 07757164024 017254 5 ustar tmurray all HTML-Template-Dumper-0.1/lib/HTML/Template/Dumper.pm 0100644 0153041 0001040 00000015737 07757164024 021060 0 ustar tmurray all
package HTML::Template::Dumper;
use strict;
use warnings;
use base 'HTML::Template';
our $VERSION = 0.1;
my ($format_obj, $output_filter);
BEGIN {
use HTML::Template::Dumper::Data_Dumper;
$format_obj = HTML::Template::Dumper::Data_Dumper->new();
}
sub set_output_format
{
my $self = shift;
my $format = shift || die "Need an output format";
my @rest = @_;
my $full_format = $format; # Rule 1 (see POD doc)
if($full_format !~ /::/) { # Rule 2
$full_format = "HTML::Template::Dumper::$format";
}
$format_obj = eval {
eval "require $full_format";
$@ and die $@;
$full_format->new(@rest);
};
if($@) {
# Rule 3
$format_obj = eval {
eval "require $format";
$@ and die $@;
$format->new(@rest);
};
}
# Give up trying to load the module and just attempt
# to call it. This would work if the module was in a
# package declaration placed inline with the calling
# file instead of a seperate file in @INC.
#
$@ and ($format_obj = eval { $full_format ->new(@rest) });
$@ and ($format_obj = eval { $format ->new(@rest) });
# If we still don't have it, give up (Rule 4)
$@ and die "No such module -- $full_format";
$format_obj->isa( 'HTML::Template::Dumper::Format' ) or die
ref $format_obj .
" is not a HTML::Template::Dumper::Format implementation";
return 1;
}
sub get_output_format { ref $format_obj }
sub output
{
my $self = shift;
my %in = @_ ? @_ : ( );
# Call HTML::Template->output(), since it could return
# errors if there was a problem with the input parameters
eval { $self->SUPER::output(@_) };
$@ and die $@;
my $ref = {
map { $_ => $self->param($_) } $self->param(),
};
my $output = $format_obj->dump($ref);
$output_filter->(\$output) if $output_filter;
if($in{print_to}) {
print {$in{print_to}} ( $output );
return undef; # As per HTML::Template docs
}
return $output;
}
sub set_output_filter
{
my $self = shift;
my $filter = shift;
die "set_output_filter() needs to be called with a code reference"
unless ref $filter eq 'CODE';
$output_filter = $filter;
}
sub parse
{
my $self = shift;
my $data = shift || return;
if(! ref $self ) {
# Called as a class method
my $format = shift || 'Data_Dumper';
my $dummy_tmpl = '';
$self = $self->new( scalarref => \$dummy_tmpl );
$self->set_output_format( $format );
}
return $format_obj->parse( $data );
}
1;
__END__
=head1 NAME
HTML::Template::Dumper - Output template data in a test-friendly format
=head1 SYNOPSIS
# Switch the module used to the regular HTML::Template when you're
# finished testing
#
#use HTML::Template;
use HTML::Template::Dumper;
my $tmpl =
#HTML::Template
HTML::Template::Dumper
->new( . . . );
$tmpl->set_output_format( 'YAML' ) if $tmpl->isa( 'HTML::Template::Dumper' );
# Do processing for the template
$tmpl->output();
=head1 DESCRIPTION
This module helps you to test HTML::Template-based programs by printing only
the information used to fill-in the template data. This makes it much
easier to automatically parse the output of your program. Currently, data
can be outputed by C (default) or C.
Note that the underlieing HTML::Template methods are still called, so
options like C and C will still throw errors.
=head1 USAGE
=head2 new
Called just like the C<< HTML::Template->new() >> method.
=head2 set_output_format
$tmpl->set_output_format( 'YAML', @extra_params );
Set the output format. Currently known formats are:
Format Name Module
------------- --------
Data_Dumper HTML::Template::Dumper::Data_Dumper
YAML HTML::Template::Dumper::YAML
The module is found by applying the following rules:
=over 4
=item 1.
If the name has a C<::> anywhere, then it is taken as the full name
of the module.
=item 2.
Otherwise, the module is loaded from C,
where C<$NAME> is what you passed to C.
=item 3.
If the name didn't have a C<::> in it, but it didn't pass rule #2, then
take it as the full name of the module.
=item 4.
If none of the above work, then call C.
=back
In any of the cases, the module returned must inheirt from
C. Otherwise, C is called.
Any parameters you pass after the format will be put directly into the
formatter's C method.
=head2 output
Called just like the regular C<< HTML::Template->output() >>, but will
return a simplified view of the template data instead of the full
object.
The C parameter is respected as specified in the
C documentation.
=head2 set_output_filter
Called with a reference to a subroutine. Before C