X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-CGI-Ex;a=blobdiff_plain;f=t%2F7_template_03_html_template.t;fp=t%2F7_template_03_html_template.t;h=97ae0b37b4c2477866642d673d86fade2598cc08;hp=0000000000000000000000000000000000000000;hb=80a766126b7d0281ee013d369d9e6af45cc2cf42;hpb=8cd30501f5be7e40e26b3dc885dfe25520d39df9 diff --git a/t/7_template_03_html_template.t b/t/7_template_03_html_template.t new file mode 100644 index 0000000..97ae0b3 --- /dev/null +++ b/t/7_template_03_html_template.t @@ -0,0 +1,233 @@ +# -*- Mode: Perl; -*- + +=head1 NAME + +7_template_03_html_template.t - Test the ability to parse and play html template + +=cut + +use vars qw($module $is_ht $is_hte $is_cet); +BEGIN { + $module = 'CGI::Ex::Template'; +# $module = 'HTML::Template'; +# $module = 'HTML::Template::Expr'; + $is_hte = $module eq 'HTML::Template::Expr'; + $is_ht = $module eq 'HTML::Template'; + $is_cet = $module eq 'CGI::Ex::Template'; +}; + +use strict; +use Test::More tests => ($is_cet) ? 92 : ($is_ht) ? 60 : 64; +use Data::Dumper qw(Dumper); +use constant test_taint => 0 && eval { require Taint::Runtime }; + +use_ok($module); + +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'} || []; + my $line = (caller)[2]; + delete $vars->{'tt_config'}; + + Taint::Runtime::taint(\$str) if test_taint; + + my $obj; + my $out; + eval { + $obj = shift || $module->new(scalarref => \$str, die_on_bad_params => 0, path => $test_dir, @$conf); # new object each time + $obj->param($vars); + $out = $obj->output; + }; + my $err = $@; + $out = '' if ! defined $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 "$err\n"; + if ($obj && $obj->can('parse_tree')) { + local $obj->{'SYNTAX'} = 'hte'; + print Dumper $obj->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; + +###----------------------------------------------------------------### +print "### VAR ##############################################################\n"; + +process_ok("Foo" => "Foo"); + +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); +process_ok("" => "FOO", {foo => "FOO"}); + +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>", tt_config => [default_escape => 'html']}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "%3C%3E", {foo => "<>"}); +process_ok("" => "<>\\n\\r\t\\\"\\\'", {foo => "<>\n\r\t\"\'"}); + +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); +process_ok("" => "<>", {foo => "<>"}); + +process_ok("" => "FOO", {foo => "FOO", bar => "BAR"}); +process_ok("d" => "bard", {foo => undef, bar => "BAR"}); +process_ok("d" => "bard", {foo => undef, bar => "BAR"}); +process_ok("d" => "bard"); + +process_ok("" => "FOO", {foo => "FOO"}) if $is_cet; +process_ok("" => "FOO", {foo => "FOO"}); + +###----------------------------------------------------------------### +print "### IF / ELSE / UNLESS ###############################################\n"; + +process_ok("bar" => "", {foo => ""}); +process_ok("bar" => "bar", {foo => "1"}); +process_ok("barbing" => "bing", {foo => ''}); +process_ok("barbing" => "bar", {foo => '1'}); +process_ok("bar" => "bar", {foo => ""}); +process_ok("bar" => "", {foo => "1"}); + +process_ok("barbaz" => "", {foo => "1"}); +process_ok("barbaz" => "", {foo => "1"}); + +###----------------------------------------------------------------### +print "### INCLUDE ##########################################################\n"; + +process_ok("" => ""); +process_ok("" => "Good Day!"); +process_ok("" => "Good Day!", {tt_config => [path => '']}); +process_ok("" => "Good Day!"); +process_ok("" => "Good Day!"); +process_ok("" => "", {tt_config => [no_includes => 1]}); + +process_ok("" => ""); +process_ok("" => ""); + +process_ok("" => "Good Day!") if $is_cet; +process_ok("" => "Good Day!", {foo => 'foo.ht'}) if $is_cet; +process_ok("" => "Good Day!") if $is_cet; + +###----------------------------------------------------------------### +print "### EXPR #############################################################\n"; + +process_ok("" => "777", {foo => "777"}) if ! $is_ht; +process_ok("" => "777", {foo => "777"}) if ! $is_ht; +process_ok("" => "777", {foo => "777"}) if ! $is_ht && ! $is_hte; # odd that HTE can't parse this +process_ok("" => "777", {foo => "777"}) if ! $is_ht; +process_ok("" => "777", {foo => "777"}) if ! $is_ht && ! $is_hte; +process_ok("" => "<>", {foo => "<>"}) if ! $is_ht; +process_ok("" => "", {foo => "<>"}); +process_ok("" => "", {foo => "FOO", bar => "BAR"}); + +process_ok("" => "FOO", {foo => "FOO"}) if ! $is_ht;; + +###----------------------------------------------------------------### +print "### LOOP #############################################################\n"; + +process_ok("foo" => "foo"); +process_ok("Hifoo" => "", {blah => 1}); +process_ok("Hifoo" => "Hifoo", {blah => {wow => 1}}) if $is_cet; +process_ok("Hifoo" => "HiHifoo", {blah => [{}, {}]}); +process_ok("()foo" => "(1)(2)(3)foo", {blah => [{i=>1}, {i=>2}, {i=>3}]}); +process_ok("()foo" => "(1)(2)(3)foo", {blah => [{i=>1}, {i=>2}, {i=>3}]}); +process_ok("()foo" => "(1)(2)(3)foo", {blah => [{i=>1}, {i=>2}, {i=>3}]}) if $is_cet; +process_ok("()()foo" => "(1)()(2)()(3)()foo", {blah => [{i=>1}, {i=>2}, {i=>3}], blue => 'B'}) if $is_ht; +process_ok("()()foo" => "(1)(B)(2)(B)(3)(B)foo", {blah => [{i=>1}, {i=>2}, {i=>3}], blue => 'B', tt_config => [GLOBAL_VARS => 1]}); + +process_ok("()()foo" => "(1)()(2)()(3)()foo", {blah => [{i=>1}, {i=>2}, {i=>3}], blue => 'B', tt_config => [SYNTAX => 'ht']}) if $is_cet; +process_ok("()()foo" => "(1)(B)(2)(B)(3)(B)foo", {blah => [{i=>1}, {i=>2}, {i=>3}], blue => 'B', tt_config => [GLOBAL_VARS => 1, SYNTAX => 'ht']}) if $is_cet; + +process_ok("()foo" => "(1)()(3)foo", {blah => [{i=>1}, undef, {i=>3}]}); + +process_ok("\n(||||)foo" => " +(||||) +(||||) +(||||)foo", {blah => [undef, undef, undef]}); + +process_ok("\n(||||)foo" => " +(1||1|0|1) +(0|0||1|2) +(0|1|1|0|3)foo", {blah => [undef, undef, undef], tt_config => [LOOP_CONTEXT_VARS => 1]}) if ! $is_cet; + +process_ok("\n(||||)foo" => " +(1|0|1|0|1) +(0|0|0|1|2) +(0|1|1|0|3)foo", {blah => [undef, undef, undef], tt_config => [LOOP_CONTEXT_VARS => 1]}) if $is_cet; + + +process_ok("()foo" => "(1)(3)foo", {blah => [{i=>1}, {i=>2}, {i=>3}]}) if $is_cet; + +###----------------------------------------------------------------### +print "### TT3 DIRECTIVES ###################################################\n"; + +process_ok("" => "FOO", {foo => "FOO"}) if $is_cet; +process_ok("" => "", {foo => "FOO", tt_config => [NO_TT => 1]}) if $is_cet; +process_ok("" => "", {foo => "FOO", tt_config => [SYNTAX => 'ht']}) if $is_cet; +process_ok("" => "10", {foo => "FOO"}) if $is_cet; + +process_ok("barweebing" => "bar", {foo => "1"}) if $is_cet; + +process_ok("()" => "(foo)") if $is_cet; +process_ok("()" => "(foo)") if $is_cet; +process_ok("()" => "(1)(2)(3)") if $is_cet; + +process_ok("()" => "(bar)") if $is_cet; +process_ok("()" => "(bar)") if $is_cet; + +process_ok("" => "bar") if $is_cet; + +process_ok('You said ' => 'You said hello') if $is_cet; + +###----------------------------------------------------------------### +print "### TT3 CHOMPING #####################################################\n"; + +process_ok("\n" => "\nFOO", {foo => "FOO"}) if $is_cet; +process_ok("\n" => "FOO", {foo => "FOO"}) if $is_cet; +process_ok("\n<-TMPL_GET foo>" => "FOO", {foo => "FOO"}) if $is_cet; + +###----------------------------------------------------------------### +print "### TT3 INTERPOLATE ##################################################\n"; + +process_ok('$foo ${ 1 + 2 }' => '$foo FOO ${ 1 + 2 }', {foo => "FOO"}); +process_ok('$foo ${ 1 + 2 }' => 'FOO FOO 3', {foo => "FOO", tt_config => [INTERPOLATE => 1]}) if $is_cet; +process_ok(' 1>$foo ${ 1 + 2 }' => 'FOO FOO 3', {foo => "FOO"}) if $is_cet; + +###----------------------------------------------------------------### +print "### DONE #############################################################\n";