1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#!/usr/bin/env perl
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use vars qw{$VAR1 $VAR2};
use Test::More;
use File::Spec::Functions ':ALL';
eval "require Template";
if ($@) {
plan( skip_all => 'Template Toolkit is not installed' );
}
use Ref::Util qw<is_hashref>;
use FindBin qw($Bin);
my $SAMPLES = catdir( $Bin, 'samples' );
unless ( -d $SAMPLES ) {
die("Failed to find samples directory");
}
opendir( DIR, $SAMPLES ) or die("opendir($SAMPLES): $!");
my @TEMPLATES = sort grep {/\.tt$/} readdir(DIR);
closedir(DIR) or die("closedir($SAMPLES): $!");
plan( tests => scalar(@TEMPLATES) * 7 );
######################################################################
# Main Tests
foreach my $name (@TEMPLATES) {
$name =~ s/\.tt$//;
my $file = catfile( $SAMPLES, $name );
my $tt_file = "$file.tt";
my $var_file = "$file.var";
my $txt_file = "$file.txt";
ok( -f $tt_file, "$name: Found $tt_file" );
ok( -f $txt_file, "$name: Found $txt_file" );
ok( -f $var_file, "$name: Found $var_file" );
# Load the resources
my $tt = slurp($tt_file);
my $var = slurp($var_file);
my $txt = slurp($txt_file);
eval $var;
die $@ if $@;
ok( is_hashref($VAR1), "$name: Loaded stash from file" );
# Create the template processor
my %params = ( INCLUDE_PATH => $SAMPLES, );
%params = ( %params, %$VAR2 ) if $VAR2;
my $template = Template->new(%params);
isa_ok( $template, 'Template' );
# Execute the template
my $out = '';
ok( $template->process( \$tt, $VAR1, \$out ),
"$name: ->process returns true"
);
is( $out, $txt, "$name: Output matches expected" );
}
sub slurp {
my $f = shift;
local $/ = undef;
open( VAR, $f ) or die("open($f): $!");
my $buffer = <VAR>;
close VAR;
return $buffer;
}
######################################################################
# Support Classes for object tests
SCOPE: {
package UpperCase;
sub foo {
uc $_[0]->{foo};
}
1;
}
SCOPE: {
package False;
use overload 'bool' => sub {0};
use overload '""' => sub {'Hello'};
1;
}
SCOPE: {
package Private;
sub public {'foo'}
sub _private {'foo'}
1;
}
|