]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/7_template_01_includes.t
CGI::Ex 2.11
[chaz/p5-CGI-Ex] / t / 7_template_01_includes.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 7_template_01_includes.t - Test the file include functionality of CGI::Ex::Template - including some edge cases
6
7 =cut
8
9 use vars qw($module $is_tt);
10 BEGIN {
11 $module = 'CGI::Ex::Template';
12 #$module = 'Template';
13 $is_tt = $module eq 'Template';
14 };
15
16 use strict;
17 use Test::More tests => 25 - ($is_tt ? 6 : 0);
18 use Data::Dumper qw(Dumper);
19 use constant test_taint => 0 && eval { require Taint::Runtime };
20
21 use_ok($module);
22
23 Taint::Runtime::taint_start() if test_taint;
24
25 ### find a place to allow for testing
26 my $test_dir = $0 .'.test_dir';
27 END { rmdir $test_dir }
28 mkdir $test_dir, 0755;
29 ok(-d $test_dir, "Got a test dir up and running");
30
31
32 sub process_ok { # process the value and say if it was ok
33 my $str = shift;
34 my $test = shift;
35 my $vars = shift || {};
36 my $conf = local $vars->{'tt_config'} = $vars->{'tt_config'} || [];
37 my $obj = shift || $module->new(@$conf, ABSOLUTE => 1, INCLUDE_PATH => $test_dir); # new object each time
38 my $out = '';
39 my $line = (caller)[2];
40 delete $vars->{'tt_config'};
41
42 Taint::Runtime::taint(\$str) if test_taint;
43
44 $obj->process(\$str, $vars, \$out);
45 my $ok = ref($test) ? $out =~ $test : $out eq $test;
46 if ($ok) {
47 ok(1, "Line $line \"$str\" => \"$out\"");
48 return $obj;
49 } else {
50 ok(0, "Line $line \"$str\"");
51 warn "# Was:\n$out\n# Should've been:\n$test\n";
52 print $obj->error if $obj->can('error');
53 print Dumper $obj->parse_tree(\$str) if $obj->can('parse_tree');
54 exit;
55 }
56 }
57
58 ### create some files to include
59 my $foo_template = "$test_dir/foo.tt";
60 END { unlink $foo_template };
61 open(my $fh, ">$foo_template") || die "Couldn't open $foo_template: $!";
62 print $fh "([% INCLUDE bar.tt %])";
63 close $fh;
64
65 ###
66 my $bar_template = "$test_dir/bar.tt";
67 END { unlink $bar_template };
68 open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
69 print $fh "BAR";
70 close $fh;
71
72 my $baz_template = "$test_dir/baz.tt";
73 END { unlink $baz_template };
74 open($fh, ">$baz_template") || die "Couldn't open $baz_template: $!";
75 print $fh "[% SET baz = 42 %][% baz %][% bing %]";
76 close $fh;
77
78 ###
79 my $wrap_template = "$test_dir/wrap.tt";
80 END { unlink $wrap_template };
81 open($fh, ">$wrap_template") || die "Couldn't open $wrap_template: $!";
82 print $fh "Hi[% content %]there";
83 close $fh;
84
85 ###----------------------------------------------------------------###
86 ### INSERT
87
88 process_ok("([% INSERT bar.tt %])" => '(BAR)');
89 process_ok("([% SET file = 'bar.tt' %][% INSERT \$file %])" => '(BAR)');
90 process_ok("([% SET file = 'bar.tt' %][% INSERT \${file} %])" => '(BAR)') if ! $is_tt;
91 process_ok("([% SET file = 'bar.tt' %][% INSERT \"\$file\" %])" => '(BAR)');
92 process_ok("([% SET file = 'bar' %][% INSERT \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
93
94 ###----------------------------------------------------------------###
95 ### INCLUDE
96
97 process_ok("([% INCLUDE bar.tt %])" => '(BAR)');
98 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \$file %])" => '(BAR)');
99 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \${file} %])" => '(BAR)') if ! $is_tt;
100 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \"\$file\" %])" => '(BAR)');
101 process_ok("([% SET file = 'bar' %][% INCLUDE \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
102
103 process_ok("([% INCLUDE baz.tt %])" => '(42)');
104 process_ok("([% INCLUDE baz.tt %])[% baz %]" => '(42)');
105 process_ok("[% SET baz = 21 %]([% INCLUDE baz.tt %])[% baz %]" => '(42)21');
106
107 ###----------------------------------------------------------------###
108 ### PROCESS
109
110 process_ok("([% PROCESS bar.tt %])" => '(BAR)');
111 process_ok("([% SET file = 'bar.tt' %][% PROCESS \$file %])" => '(BAR)');
112 process_ok("([% SET file = 'bar.tt' %][% PROCESS \${file} %])" => '(BAR)') if ! $is_tt;
113 process_ok("([% SET file = 'bar.tt' %][% PROCESS \"\$file\" %])" => '(BAR)');
114 process_ok("([% SET file = 'bar' %][% PROCESS \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
115
116 process_ok("([% PROCESS baz.tt %])" => '(42)');
117 process_ok("([% PROCESS baz.tt %])[% baz %]" => '(42)42');
118 process_ok("[% SET baz = 21 %]([% PROCESS baz.tt %])[% baz %]" => '(42)42');
119
120 ###----------------------------------------------------------------###
121 ### WRAPPER
122
123 process_ok("([% WRAPPER wrap.tt %])" => '');
124 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(Hi one there)');
This page took 0.03577 seconds and 4 git commands to generate.