1use warnings;
2use strict;
3use lib qw(t);
4use Test::More tests => 7;
5use_ok('HTML::Template::Compiled');
6use HTC_Utils qw($cache $tdir &cdir);
7
8
9{
10    my $class = 'HTML::Template::Compiled::__Test';
11    my $iterator = bless [undef,[23..25]], $class;
12    my $next = sub {
13        my ($self) = @_;
14        my $index = $self->[0];
15        my $array = $self->[1];
16        return unless @$array;
17        unless (defined $index) {
18            $self->[0] = $index = 0;
19        }
20        elsif ($index < $#$array) {
21            $self->[0] = ++$index;
22        }
23        else {
24            $self->[0] = undef;
25            return;
26        }
27        return $array->[$index];
28    };
29    {
30        no strict 'refs';
31        *{$class."::next"} = $next;
32    }
33
34    my $htc = HTML::Template::Compiled->new(
35        filehandle => \*DATA,
36        debug => 0,
37        loop_context_vars => 1,
38    );
39    #while (my $row = $iterator->next) {
40        #print "row $row\n";
41        #}
42    $htc->param(iterator => $iterator);
43    my $out = $htc->output;
44    cmp_ok($out,"=~", qr{
45        23.*1odd.*
46        24.*2.*
47        25.*3odd.*
48        23.*1odd.*
49        24.*2.*
50        25.*3odd}xs, "while");
51    #print "out: $out\n";
52
53}
54
55{
56    my $htc = HTML::Template::Compiled->new(
57        scalarref => \'
58<%each letters%><%= __key__ %>=<%= __value__ %><%/each%>
59<%each numbers sort=num %><%= __key__ %>=<%= __value__ %><%/each%>
60<%each numbers sort=num reverse=1 %><%= __key__ %>=<%= __value__ %><%/each%>
61<%each letters_nested sort=alpha sortby="[0]" %>sortby <%set_var val value=__value__ %>key<%= __key__ %>=<%= $val[0] %> <%/each%>
62<%each letters_nested2 sort=alpha sortby="letter" %>sortby2 <%set_var val value=__value__ %>key<%= __key__ %>=<%= $val.letter %> <%/each%>',
63        debug => 0,
64        loop_context_vars => 1,
65    );
66    $htc->param(letters => { a => 1, b => 2, c => 3 });
67    $htc->param(numbers => { 21 => 'b', 2 => 'a', 100 => 'c' });
68    $htc->param(letters_nested => { 1 => ['b'], 2 => ['a'], 3 => ['c'] });
69    $htc->param(letters_nested2 => { 1 => {letter=>'b'}, 2 => {letter=>'a'}, 3 => {letter=>'c'} });
70    my $out = $htc->output;
71    #print "out: $out\n";
72    cmp_ok($out, '=~', 'a=1b=2c=3', 'each alpha sort');
73    cmp_ok($out, '=~', '2=a21=b100=c', 'each numeric sort');
74    cmp_ok($out, '=~', '100=c21=b2=a', 'each numeric sort reverse');
75    cmp_ok($out, '=~', 'sortby key2=a sortby key1=b sortby key3=c', 'each numeric sort reverse sortby');
76    cmp_ok($out, '=~', 'sortby2 key2=a sortby2 key1=b sortby2 key3=c', 'each numeric sort reverse sortby2');
77
78}
79
80
81__DATA__
82<%with iterator%>
83<%while next %>
84    <%VAR NAME="_" %>
85    <%= __counter__ %><%if __odd__ %>odd<%/if%>
86<%/while%>
87<%while next alias=hiThere%>
88    <%VAR NAME="$hiThere" %>
89    <%= __counter__ %><%if __odd__ %>odd<%/if%>
90<%/while%>
91<%/with iterator%>
92
93