5 7_template_01_includes.t - Test the file include functionality of CGI::Ex::Template - including some edge cases
9 use vars qw($module $is_tt);
11 $module = 'CGI::Ex::Template';
12 #$module = 'Template';
13 $is_tt = $module eq 'Template';
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 };
23 Taint::Runtime::taint_start() if test_taint;
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");
32 sub process_ok { # process the value and say if it was ok
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
39 my $line = (caller)[2];
40 delete $vars->{'tt_config'};
42 Taint::Runtime::taint(\$str) if test_taint;
44 $obj->process(\$str, $vars, \$out);
45 my $ok = ref($test) ? $out =~ $test : $out eq $test;
47 ok(1, "Line $line \"$str\" => \"$out\"");
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');
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 %])";
66 my $bar_template = "$test_dir/bar.tt";
67 END { unlink $bar_template };
68 open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
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 %]";
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";
85 ###----------------------------------------------------------------###
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;
94 ###----------------------------------------------------------------###
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;
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');
107 ###----------------------------------------------------------------###
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;
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');
120 ###----------------------------------------------------------------###
123 process_ok("([% WRAPPER wrap.tt %])" => '');
124 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(Hi one there)');