File: dot_for_loops.t

package info (click to toggle)
libhtml-template-pluggable-perl 0.17%2B~cs0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 276 kB
  • sloc: perl: 401; makefile: 17
file content (146 lines) | stat: -rw-r--r-- 5,142 bytes parent folder | download | duplicates (5)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use Test::More;
use Test::MockObject;
use Data::Dumper;

use lib 'lib';
use strict;

my $tests = 14;
plan tests => 3						# use_ok
			+ $tests 			    # my tests
	;

use_ok('HTML::Template::Pluggable');
use_ok('HTML::Template::Plugin::Dot');
use_ok('Test::MockObject');

my $mock = Test::MockObject->new();
$mock->mock( 'name', sub { 'mock' } );
$mock->mock( 'that_returns_loopdata', sub {
		my ($self, $count) = @_;
		$count ||= 5;
		return $self->ar($count);
	} );
$mock->mock( 'ar', sub {
		my ($self, $count) = @_;
		my @ret = ();
		
		for( 1.. $count) {
			push @ret, {
					a => $_,
					b => $_*10,
			};
		}
		return @ret;
	} );
$mock->mock( 'arrayref', sub {
		my ($self, $count) = @_;
		$count ||= 5;
		return [ $self->ar($count) ];
	} );
$mock->mock( empty_Loop => sub { return [] } );
$mock->mock( 'nested', sub {
		my ($self, $count) = @_;
		$count ||= 2;
		my @ret;
		for (1..$count) {
			push @ret, {
				a => $_,
				b => [ map { { c=>$_*3 } } (1..$_) ],
			};
		}
		return @ret;
	} );

my $hashref = { loop =>  $mock->arrayref };
my ($tag, $out);

$tag = q{ <tmpl_var object.name>: <tmpl_loop name="object.that_returns_loopdata(3)"><tmpl_var this.a>, <tmpl_var this.b>. </tmpl_loop> };
$out = get_output($tag, $mock);
SKIP: {
	skip "HTML::Template subclassing bug for tmpl_loop support. See: http://rt.cpan.org/NoAuth/Bug.html?id=14037", $tests if $@;
	like($out, qr/1, 10/, 'Wrapped loops work with implicit "this" mapping');

	$tag = q{ <tmpl_loop name="object.that_returns_loopdata(4) : that"><tmpl_var that.a>, <tmpl_var that.b>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/1, 10/, 'Wrapped loops work with explicit "that" mapping');

	$tag = q{ <tmpl_loop name="object.arrayref : d"><tmpl_var d.a>, <tmpl_var d.b>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/1, 10/, 'Wrapped loops work with arrayrefs');

	$tag = q{ no <tmpl_loop name="object.empty_Loop"><tmpl_var this.a>, <tmpl_var this.b>. </tmpl_loop> data };
	$out = get_output($tag, $mock);
	like($out, qr/no\s+data/, 'Wrapped loops work with no data');

	$tag = q{ <tmpl_var object.name> no <tmpl_loop name="non_object.empty_loop"><tmpl_var a>, <tmpl_var b>. </tmpl_loop> data };
	$out = get_output($tag, $mock, 'non_object.empty_loop' => []);
	like($out, qr/no\s+data/, 'Wrapped loops work with no object and no data');

	$tag = q{ <tmpl_loop name="object.nested(3) : outer"><tmpl_var outer.a>, <tmpl_loop outer.b>(<tmpl_var this.c>)</tmpl_loop>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/\Q3, (3)(6)(9)./, 'Wrapped nested loops work with arrayrefs');

	$tag = q{ <tmpl_loop name="object.nested(3) : outer"><tmpl_var outer.a>, <tmpl_loop outer.b:inner>(<tmpl_var inner.c>)</tmpl_loop>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/\Q3, (3)(6)(9)./, 'Wrapped nested loops work with arrayrefs and explicit "inner" mapping');

	$tag = q{ <tmpl_var object.name> <tmpl_loop name="object.nested(1)"><tmpl_var this.a>, <tmpl_loop this.b>(<tmpl_var this.c>)</tmpl_loop>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/\Q1, (3)./, 'Wrapped nested loops work with arrayrefs and implicit mapping everywhere (single return value)');

	$tag = q{ <tmpl_var object.name> <tmpl_loop name="object.nested(3)"><tmpl_var this.a>, <tmpl_loop this.b>(<tmpl_var this.c>)</tmpl_loop>. </tmpl_loop> };
	$out = get_output($tag, $mock);
	like($out, qr/\Q3, (3)(6)(9)./, 'Wrapped nested loops work with arrayrefs and implicit mapping everywhere');

	$tag = q{ <tmpl_loop name="object.loop:item"><tmpl_var item.a>, <tmpl_var item.b>. </tmpl_loop> };
	$out = get_output($tag, $hashref);
	like($out, qr/\Q3, 30./, 'simple hashref with an arrayref');

	$tag = q{ <tmpl_if object.loop><tmpl_loop name="object.loop:item"><tmpl_var item.a>, <tmpl_var item.b>. </tmpl_loop></tmpl_if> };
	$out = get_output($tag, $hashref);
	like($out, qr/\Q3, 30./, 'simple hashref with an arrayref, which is tested in a tmpl_if');

	$tag = q{ :<tmpl_var object.loop>: <tmpl_loop name="object.loop:item"><tmpl_var item.a>, <tmpl_var item.b>. </tmpl_loop> };
	$out = get_output($tag, $hashref);
	like($out, qr/\Q3, 30./, 'simple hashref with an arrayref, which is used as a var');

	$hashref={loop=>[]};
	$tag = q{ <tmpl_loop name="object.loop:item"><tmpl_var item.a>, <tmpl_var item.b>. </tmpl_loop> };
	$out = get_output($tag, $hashref);
	like($out, qr/^  $/, 'simple hashref with an emtpy arrayref');
	
	$hashref={loop=>[]};
	$tag = q{ <tmpl_if object.loop><tmpl_loop name="object.loop:item"><tmpl_var item.a>, <tmpl_var item.b>. </tmpl_loop></tmpl_if> };
	$out = get_output($tag, $hashref);
	like($out, qr/^  $/, 'simple hashref with an emtpy arrayref, used in a tmpl_if');


}

sub get_output {
	my ($tag, @data) = @_;
	my ( $output );

    # diag("");
	my $t = HTML::Template::Pluggable->new(
			scalarref	=> \$tag,
			global_vars	=> 0,
			debug		=> 0,
            die_on_bad_params => 1,
		);
	eval {
		$t->param( object => @data );
		$output = $t->output;
	};

     
	#diag("template tag is $tag");
	#diag("output is $output");
	#diag("exception is $@") if $@;
	return $output;
}

# vi: filetype=perl

__END__