]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/7_template_00_base.t
CGI::Ex 2.14
[chaz/p5-CGI-Ex] / t / 7_template_00_base.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 7_template_00_base.t - Test the basic language functionality of CGI::Ex::Template - including many edge cases
6
7 =head1 DESCRIPTION
8
9 Test the basics of CGI::Ex::Template inheritance - but leave the full test suite to Template::Alloy.
10
11 =cut
12
13 use vars qw($module $is_tt);
14 BEGIN {
15 $module = 'CGI::Ex::Template';
16 if (grep {/tt/i} @ARGV) {
17 $module = 'Template';
18 }
19 $is_tt = $module eq 'Template';
20 };
21
22 use strict;
23 use Test::More tests => ! $is_tt ? 46 : 45;
24 use Data::Dumper qw(Dumper);
25
26 use_ok($module);
27
28 ###----------------------------------------------------------------###
29
30 sub process_ok { # process the value and say if it was ok
31 my $str = shift;
32 my $test = shift;
33 my $vars = shift || {};
34 my $conf = local $vars->{'tt_config'} = $vars->{'tt_config'} || [];
35 my $obj = shift || $module->new(@$conf); # new object each time
36 my $out = '';
37 my $line = (caller)[2];
38 delete $vars->{'tt_config'};
39
40 $obj->process(\$str, $vars, \$out);
41 my $ok = ref($test) ? $out =~ $test : $out eq $test;
42 if ($ok) {
43 ok(1, "Line $line \"$str\" => \"$out\"");
44 return $obj;
45 } else {
46 ok(0, "Line $line \"$str\"");
47 warn "# Was:\n$out\n# Should've been:\n$test\n";
48 print $obj->error if $obj->can('error');
49 print Dumper $obj->parse_tree(\$str) if $obj->can('parse_tree');
50 exit;
51 }
52 }
53
54 ###----------------------------------------------------------------###
55 print "### GET ##############################################################\n";
56
57 process_ok("[% foo %]" => "");
58 process_ok("[% foo %]" => "7", {foo => 7});
59 process_ok("[% foo %]" => "7", {tt_config => [VARIABLES => {foo => 7}]});
60 process_ok("[% foo %]" => "7", {tt_config => [PRE_DEFINE => {foo => 7}]});
61 process_ok("[% foo %][% foo %][% foo %]" => "777", {foo => 7});
62 process_ok("[% foo() %]" => "7", {foo => 7});
63 process_ok("[% foo.bar %]" => "");
64 process_ok("[% foo.bar %]" => "", {foo => {}});
65 process_ok("[% foo.bar %]" => "7", {foo => {bar => 7}});
66 process_ok("[% foo().bar %]" => "7", {foo => {bar => 7}});
67 process_ok("[% foo.0 %]" => "7", {foo => [7, 2, 3]});
68 process_ok("[% foo.10 %]" => "", {foo => [7, 2, 3]});
69 process_ok("[% foo %]" => 7, {foo => sub { 7 }});
70 process_ok("[% foo(7) %]" => 7, {foo => sub { $_[0] }});
71 process_ok("[% foo.length %]" => 1, {foo => sub { 7 }});
72 process_ok("[% foo.0 %]" => 7, {foo => sub { return 7, 2, 3 }});
73 process_ok("[% foo(bar) %]" => 7, {foo => sub { $_[0] }, bar => 7});
74 process_ok("[% foo(bar.baz) %]" => 7,{foo => sub { $_[0] }, bar => {baz => 7}});
75
76 # we don't do as many tests here - leave that to Template::Alloy
77 # See Template::Alloy t/05_tt_base.t
78
79 ###----------------------------------------------------------------###
80 print "### SET ##############################################################\n";
81
82 process_ok("[% SET foo bar %][% foo %]" => '');
83 process_ok("[% SET foo = 1 %][% foo %]" => '1');
84 process_ok("[% SET foo = 1 bar = 2 %][% foo %][% bar %]" => '12');
85 process_ok("[% SET foo bar = 1 %][% foo %]" => '');
86 process_ok("[% SET foo = 1 ; bar = 1 %][% foo %]" => '1');
87 process_ok("[% SET foo = 1 %][% SET foo %][% foo %]" => '');
88
89 process_ok("[% SET foo = [] %][% foo.0 %]" => "");
90 process_ok("[% SET foo = [1, 2, 3] %][% foo.1 %]" => 2);
91 process_ok("[% SET foo = {} %][% foo.0 %]" => "");
92 process_ok("[% SET foo = {'1' => 2} %][% foo.1 %]" => "2");
93
94 process_ok("[% SET name = 1 %][% SET foo = name %][% foo %]" => "1");
95 process_ok("[% SET name = 1 %][% SET foo = \$name %][% foo %]" => "");
96 process_ok("[% SET name = 1 %][% SET foo = \${name} %][% foo %]" => "");
97 process_ok("[% SET name = 1 %][% SET foo = \"\$name\" %][% foo %]" => "1");
98 process_ok("[% SET name = 1 foo = name %][% foo %]" => '1');
99 process_ok("[% SET name = 1 %][% SET foo = {\$name => 2} %][% foo.1 %]" => "2");
100 process_ok("[% SET name = 1 %][% SET foo = {\${name} => 2} %][% foo.1 %]" => "2");
101
102 process_ok("[% SET name = 7 %][% SET foo = {'2' => name} %][% foo.2 %]" => "7");
103 process_ok("[% SET name = 7 %][% SET foo = {'2' => \"\$name\"} %][% foo.2 %]" => "7");
104
105 process_ok("[% SET name = 7 %][% SET foo = [1, name, 3] %][% foo.1 %]" => "7");
106 process_ok("[% SET name = 7 %][% SET foo = [1, \"\$name\", 3] %][% foo.1 %]" => "7");
107
108 process_ok("[% SET foo = { bar => { baz => [0, 7, 2] } } %][% foo.bar.baz.1 %]" => "7");
109
110 process_ok("[% SET foo.bar = 1 %][% foo.bar %]" => '1');
111 process_ok("[% SET foo.bar.baz.bing = 1 %][% foo.bar.baz.bing %]" => '1');
112 process_ok("[% SET foo.bar.2 = 1 %][% foo.bar.2 %] [% foo.bar.size %]" => '1 1');
113 process_ok("[% SET foo.bar = [] %][% SET foo.bar.2 = 1 %][% foo.bar.2 %] [% foo.bar.size %]" => '1 3');
114
115 # We don't do as many tests here - leave that to Template::Alloy
116 # See Template::Alloy t/05_tt_base.t
117
118 ###----------------------------------------------------------------###
119 print "### LOOP #############################################################\n";
120
121 if (! $is_tt) {
122 local $CGI::Ex::Template::QR_PRIVATE = 0;
123 local $CGI::Ex::Template::QR_PRIVATE = 0; # warn clean
124 CGI::Ex::Template->define_vmethod('scalar', textjoin => sub {join(shift, @_)});
125
126 process_ok("[% var = [{key => 'a'}, {key => 'b'}, {key => 'c'}] -%]
127 [% LOOP var -%]
128 ([% textjoin('|', key, __first__, __last__, __inner__, __odd__) %])
129 [% END -%]" => "(a|1|0|0|1)
130 (b|0|0|1|0)
131 (c|0|1|0|1)
132 ", {tt_config => [LOOP_CONTEXT_VARS => 1]});
133 }
134
135 # See Template::Alloy t/05_tt_base.t
136
137 ###----------------------------------------------------------------###
138 print "### DONE #############################################################\n";
139 print "### See Template::Alloy t/05_tt_base.t\n";
This page took 0.036689 seconds and 4 git commands to generate.