]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/7_template_01_includes.t
CGI::Ex 2.00
[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
33 my $str = shift;
34 my $test = shift;
35 my $args = shift;
36 my $out = '';
37
38 Taint::Runtime::taint(\$str) if test_taint;
39
40 my $obj = $module->new(ABSOLUTE => 1, INCLUDE_PATH => $test_dir);
41 $obj->process(\$str, $args, \$out);
42 my $ok = $out eq $test;
43 ok($ok, "\"$str\" => \"$out\"" . ($ok ? '' : " - should've been \"$test\""));
44 my $line = (caller)[2];
45 warn "# process_ok called at line $line.\n" if ! $ok;
46 }
47
48 ### create some files to include
49 my $foo_template = "$test_dir/foo.tt";
50 END { unlink $foo_template };
51 open(my $fh, ">$foo_template") || die "Couldn't open $foo_template: $!";
52 print $fh "([% INCLUDE bar.tt %])";
53 close $fh;
54
55 ###
56 my $bar_template = "$test_dir/bar.tt";
57 END { unlink $bar_template };
58 open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
59 print $fh "BAR";
60 close $fh;
61
62 my $baz_template = "$test_dir/baz.tt";
63 END { unlink $baz_template };
64 open($fh, ">$baz_template") || die "Couldn't open $baz_template: $!";
65 print $fh "[% SET baz = 42 %][% baz %][% bing %]";
66 close $fh;
67
68 ###
69 my $wrap_template = "$test_dir/wrap.tt";
70 END { unlink $wrap_template };
71 open($fh, ">$wrap_template") || die "Couldn't open $wrap_template: $!";
72 print $fh "Hi[% content %]there";
73 close $fh;
74
75 ###----------------------------------------------------------------###
76 ### INSERT
77
78 process_ok("([% INSERT bar.tt %])" => '(BAR)');
79 process_ok("([% SET file = 'bar.tt' %][% INSERT \$file %])" => '(BAR)');
80 process_ok("([% SET file = 'bar.tt' %][% INSERT \${file} %])" => '(BAR)') if ! $is_tt;
81 process_ok("([% SET file = 'bar.tt' %][% INSERT \"\$file\" %])" => '(BAR)');
82 process_ok("([% SET file = 'bar' %][% INSERT \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
83
84 ###----------------------------------------------------------------###
85 ### INCLUDE
86
87 process_ok("([% INCLUDE bar.tt %])" => '(BAR)');
88 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \$file %])" => '(BAR)');
89 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \${file} %])" => '(BAR)') if ! $is_tt;
90 process_ok("([% SET file = 'bar.tt' %][% INCLUDE \"\$file\" %])" => '(BAR)');
91 process_ok("([% SET file = 'bar' %][% INCLUDE \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
92
93 process_ok("([% INCLUDE baz.tt %])" => '(42)');
94 process_ok("([% INCLUDE baz.tt %])[% baz %]" => '(42)');
95 process_ok("[% SET baz = 21 %]([% INCLUDE baz.tt %])[% baz %]" => '(42)21');
96
97 ###----------------------------------------------------------------###
98 ### PROCESS
99
100 process_ok("([% PROCESS bar.tt %])" => '(BAR)');
101 process_ok("([% SET file = 'bar.tt' %][% PROCESS \$file %])" => '(BAR)');
102 process_ok("([% SET file = 'bar.tt' %][% PROCESS \${file} %])" => '(BAR)') if ! $is_tt;
103 process_ok("([% SET file = 'bar.tt' %][% PROCESS \"\$file\" %])" => '(BAR)');
104 process_ok("([% SET file = 'bar' %][% PROCESS \"\$file.tt\" %])" => '(BAR)') if ! $is_tt;
105
106 process_ok("([% PROCESS baz.tt %])" => '(42)');
107 process_ok("([% PROCESS baz.tt %])[% baz %]" => '(42)42');
108 process_ok("[% SET baz = 21 %]([% PROCESS baz.tt %])[% baz %]" => '(42)42');
109
110 ###----------------------------------------------------------------###
111 ### WRAPPER
112
113 process_ok("([% WRAPPER wrap.tt %])" => '');
114 process_ok("([% WRAPPER wrap.tt %] one [% END %])" => '(Hi one there)');
This page took 0.034785 seconds and 4 git commands to generate.