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
|
# $Id: 21_while.t 1078 2008-09-09 20:44:07Z tinita $
use warnings;
use strict;
use lib qw(blib/lib t);
use Test::More tests => 5;
use_ok('HTML::Template::Compiled');
use HTC_Utils qw($cache $tdir &cdir);
{
my $class = 'HTML::Template::Compiled::__Test';
my $iterator = bless [undef,[23..25]], $class;
my $next = sub {
my ($self) = @_;
my $index = $self->[0];
my $array = $self->[1];
return unless @$array;
unless (defined $index) {
$self->[0] = $index = 0;
}
elsif ($index < $#$array) {
$self->[0] = ++$index;
}
else {
$self->[0] = undef;
return;
}
return $array->[$index];
};
{
no strict 'refs';
*{$class."::next"} = $next;
}
my $htc = HTML::Template::Compiled->new(
filehandle => \*DATA,
debug => 0,
loop_context_vars => 1,
);
#while (my $row = $iterator->next) {
#print "row $row\n";
#}
$htc->param(iterator => $iterator);
my $out = $htc->output;
cmp_ok($out,"=~", qr{
23.*1odd.*
24.*2.*
25.*3odd.*
23.*1odd.*
24.*2.*
25.*3odd}xs, "while");
#print "out: $out\n";
}
{
my $htc = HTML::Template::Compiled->new(
scalarref => \'
<%each letters%><%= __key__ %>=<%= __value__ %><%/each%>
<%each numbers sort=num %><%= __key__ %>=<%= __value__ %><%/each%>
<%each numbers sort=num reverse=1 %><%= __key__ %>=<%= __value__ %><%/each%>',
debug => 0,
loop_context_vars => 1,
);
$htc->param(letters => { a => 1, b => 2, c => 3 });
$htc->param(numbers => { 21 => 'b', 2 => 'a', 100 => 'c' });
my $out = $htc->output;
#print "out: $out\n";
cmp_ok($out, '=~', 'a=1b=2c=3', 'each alpha sort');
cmp_ok($out, '=~', '2=a21=b100=c', 'each numeric sort');
cmp_ok($out, '=~', '100=c21=b2=a', 'each numeric sort');
}
__DATA__
<%with iterator%>
<%while next %>
<%VAR NAME="_" %>
<%= __counter__ %><%if __odd__ %>odd<%/if%>
<%/while%>
<%while next alias=hiThere%>
<%VAR NAME="hiThere" %>
<%= __counter__ %><%if __odd__ %>odd<%/if%>
<%/while%>
<%/with iterator%>
|