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
|
# $Id: 26_expr.t 1103 2009-08-23 13:10:31Z tinita $
use warnings;
use strict;
use lib 't';
# implement this later
use Test::More tests => 12;
eval { require Parse::RecDescent; };
my $prd = $@ ? 0 : 1;
use_ok('HTML::Template::Compiled');
use HTC_Utils qw($cache $tdir &cdir);
sub HT_Utils::list { my @a = qw/ a b c /; return @a }
sub HT_Utils::each { my %a = ( a => 1, b => 2 ); return %a }
SKIP: {
skip "No Parse::RecDescent installed", 11 unless $prd;
use_ok('HTML::Template::Compiled::Expr');
my $htc;
eval {
$htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
[%= expr="(foo.count < 4) && ( foo.count > 2)" %]
EOM
use_expressions => 0,
tagstyle => [qw/ -classic -comment -asp +tt /],
loop_context_vars => 1,
);
};
my $error = $@;
#warn __PACKAGE__.':'.__LINE__.": $@\n";
cmp_ok($error, '=~', qr/\QSyntax error in <TMPL_*> tag/, "No expressions allowed");
my @tests = (
[ q#[%= expr="(foo.count < 4) && ( foo.count > 2)" %]#, 1],
[ q#[%= expr="(foo.count > 4) && ( foo.count % 2)" %]#, ''],
[ q#[%= expr="lcfirst( .string )" %]#, 'aBC'],
[ q#[%if expr="lcfirst( .string ) eq 'aBC'" %]23[%/if %]#, '23'],
[ q#[%if expr="'string\'' eq 'string\''" %]23[%/if %]#, '23'],
[ q#[%= expr="object.param('foo', .foo.count )" %]#, '424242'],
[ q#[%if expr="0" %]zero[%elsif expr="foo.count < 4" %]< 4[%/if %]#, '< 4'],
[ q#[%loop expr="list_object.list" context="list" %][%= _ %][%/loop %]#, 'abc'],
[ q#[%each expr="list_object.each" context="list" %]k:[%= __key__ %] [%/each %]#, 'k:a k:b '],
);
for my $i (0 .. $#tests) {
my $test = $tests[$i];
my ($tmpl, $exp) = @$test;
my $htc = HTML::Template::Compiled->new(
scalarref => \$tmpl,
use_expressions => 1,
debug => 0,
tagstyle => [qw/ -classic -comment -asp +tt /],
loop_context_vars => 1,
);
my $list_object = bless {}, 'HT_Utils';
$htc->param(foo => {
count => '3',
},
object => bless ({ foo => 42 }, 'HTC::DUMMY'),
string => 'ABC',
list_object => $list_object,
);
my $out = $htc->output;
#print "out: $out\n";
cmp_ok($out, 'eq', $exp, "Expressions $i");
}
}
sub HTC::DUMMY::param {
return $_[0]->{ $_[1] } x $_[2]
}
|