Template-Parser-CET
view release on metacpan or search on metacpan
t/7_template_04_html_template.t view on Meta::CPAN
# -*- Mode: Perl; -*-
=head1 NAME
03_html_template.t - Test the ability to parse and play html template
=cut
use strict;
use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;
use Test::More tests => 87;
use constant test_taint => 0 && eval { require Taint::Runtime };
Taint::Runtime::taint_start() if test_taint;
### find a place to allow for testing
my $test_dir = $0 .'.test_dir';
END { rmdir $test_dir }
mkdir $test_dir, 0755;
ok(-d $test_dir, "Got a test dir up and running");
sub process_ok { # process the value and say if it was ok
my $str = shift;
my $test = shift;
my $vars = shift || {};
my $conf = local $vars->{'tt_config'} = $vars->{'tt_config'} || [];
push(@$conf, 'SYNTAX', 'hte') if ! grep {/SYNTAX/i} @$conf;
push @$conf, 'INCLUDE_PATH', $test_dir;
my $obj = shift || Template->new(@$conf); # new object each time
my $out = '';
my $line = (caller)[2];
delete $vars->{'tt_config'};
Taint::Runtime::taint(\$str) if test_taint;
Template::Parser::CET->add_top_level_functions($vars);
$obj->process(\$str, $vars, \$out);
my $ok = ref($test) ? $out =~ $test : $out eq $test;
if ($ok) {
ok(1, "Line $line \"$str\" => \"$out\"");
return $obj;
} else {
ok(0, "Line $line \"$str\"");
warn "# Was:\n$out\n# Should've been:\n$test\n";
print $obj->error if $obj->can('error');
print Template::Alloy->dump_parse_tree(\$str);
exit;
}
}
### create some files to include
my $foo_template = "$test_dir/foo.ht";
END { unlink $foo_template };
open(my $fh, ">$foo_template") || die "Couldn't open $foo_template: $!";
print $fh "Good Day!";
close $fh;
### create some files to include
my $bar_template = "$test_dir/bar.ht";
END { unlink $bar_template };
open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
print $fh "(<TMPL_VAR bar>)";
close $fh;
###----------------------------------------------------------------###
print "### VAR ##############################################################\n";
process_ok("Foo" => "Foo");
process_ok("<TMPL_VAR foo>" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR foo>" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR name=foo>" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR NAME=foo>" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR NAME=\"foo\">" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR NAME='foo'>" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR NAME='foo' >" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR foo >" => "FOO", {foo => "FOO"});
process_ok("<TMPL_VAR foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR foo>" => "<>", {foo => "<>", tt_config => [default_escape => 'html']});
process_ok("<TMPL_VAR ESCAPE=html foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=HTML foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=\"HTML\" foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE='HTML' foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=1 foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=0 foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=NONE foo>" => "<>", {foo => "<>"});
process_ok("<TMPL_VAR ESCAPE=URL foo>" => "%3C%3E", {foo => "<>"});
#process_ok("<TMPL_VAR ESCAPE=JS foo>" => "<>\\n\\r\t\\\"\\\'", {foo => "<>\n\r\t\"\'"});
process_ok("<TMPL_VAR foo ESCAPE=html>" => "<>", {foo => "<>"});
( run in 1.123 second using v1.01-cache-2.11-cpan-ff9377addf4 )