Template-Parser-CET

 view release on metacpan or  search on metacpan

t/7_template_05_text_tmpl.t  view on Meta::CPAN

# -*- Mode: Perl; -*-

=head1 NAME

04_text_tmpl.t - Test the ability to parse and play Text::Tmpl

=cut

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

use Test::More tests => 44;
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', 'tmpl', START_TAG => qr{\#\[}, END_TAG => qr{\]\#}) 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;

    $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.tmpl";
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.tmpl";
END { unlink $bar_template };
open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
print $fh "(#[echo \$bar]#)";
close $fh;


###----------------------------------------------------------------###
print "### ECHO #############################################################\n";

process_ok("Foo" => "Foo");

process_ok('#[echo $foo]#bar' => "bar");
process_ok('#[echo $foo]#' => "FOO", {foo => "FOO"});
process_ok('#[echo $foo $foo]#' => "FOOFOO", {foo => "FOO"});
process_ok('#[echo $foo "bar" $foo]#' => "FOObarFOO", {foo => "FOO"});
process_ok('#[echo "hi"]#' => "hi", {foo => "FOO"});
process_ok('#[echo \'hi\']#' => "hi", {foo => "FOO"}) ;
process_ok('#[echo foo]#' => "FOO", {foo => "FOO"}) ;

###----------------------------------------------------------------###
print "### COMMENT ##########################################################\n";

process_ok('#[comment]# Hi there #[endcomment]#bar' => "bar", {foo => "FOO"});
process_ok('#[comment]# Hi there #[end]#bar' => "bar", {foo => "FOO"}) ;

###----------------------------------------------------------------###
print "### IF / ELSIF / ELSE / IFN ##########################################\n";

process_ok('#[if $foo]#bar#[endif]#bar' => "bar");
process_ok('#[if "1"]#bar#[endif]#' => "bar");
process_ok('#[if $foo]#bar#[endif]#' => "", {foo => ""});
process_ok('#[if $foo]#bar#[endif]#' => "bar", {foo => "1"});
process_ok('#[ifn $foo]#bar#[endifn]#' => "bar", {foo => ""});



( run in 1.228 second using v1.01-cache-2.11-cpan-800906f7e73 )