token_re, include cleanup, git cleanup
[urisagit/Template-Simple.git] / t / include.t
1 #!perl
2
3 use strict ;
4 use lib qw(t) ;
5 use common ;
6
7 use File::Slurp ;
8 use Data::Dumper ;
9
10 # these dirs must be in order to the deepest for rmdir to work properly
11
12 my @tmpl_dirs = qw( templates templates/deeper templates/deeper/deepest ) ;
13
14 my %tmpl_files = (
15
16         'templates/FOO.tmpl'    => <<FOO,
17 this loads bar <[%include BAR%]>
18 FOO
19         'templates/deeper/BAR.tmpl'     => <<BAR,
20 {this should hide}
21 BAR
22         'templates/deeper/deepest/BAR.tmpl'     => <<BAR,
23 [this should be hidden then revealed]
24 BAR
25
26 ) ;
27
28 my $tests = [
29         {
30                 name    => 'simple include',
31                 skip    => 0,
32                 opts    => {
33                         templates => {
34                                 'foo'   => 'bar',
35                         }
36                 },
37                 data    => {},
38                 template => '[%INCLUDE foo%]',
39                 expected => 'bar',
40         },
41         {
42                 name    => 'nested includes',
43                 skip    => 0,
44                 opts    => {
45                         templates => {
46                                 foo     => '[%include bar%]',
47                                 bar     => 'quux',
48                         },
49                 },
50                 data    => {},
51                 template => '[%INCLUDE foo%]',
52                 expected => 'quux',
53         },
54         {
55                 name    => 'serial includes',
56                 skip    => 0,
57                 opts    => {
58                         templates => {
59                                 foo     => 'foo is here',
60                                 bar     => 'bar is too',
61                                 quux    => 'quux is on the drums',
62                         },
63                 },
64                 data    => {},
65                 template => '[%INCLUDE foo%] [%INCLUDE bar%] [%INCLUDE quux%]',
66                 expected => 'foo is here bar is too quux is on the drums',
67         },
68         {
69                 name    => 'missing include',
70                 skip    => 0,
71                 data    => {},
72                 keep_obj => 1,
73                 pretest => sub { $_[0]{obj}->delete_templates() },
74                 error   => qr/can't find/,
75         },
76         {
77                 name    => 'load include files',
78                 skip    => 0,
79                 opts    => {
80                         search_dirs => [ qw(
81                                 templates
82                                 templates/deeper
83                                 templates/deeper/deepest
84                         ) ],
85                 },
86                 data    => {},
87                 template => '[%INCLUDE FOO%]',
88                 expected => <<EXPECTED,
89 this loads bar <{this should hide}
90 >
91 EXPECTED
92
93         },
94         {
95                 name    => 'use lower path',
96                 skip    => 0,
97                 opts    => {
98                         include_paths => [ qw(
99                                 templates
100                                 templates/deeper/deepest
101                         ) ],
102                 },
103                 data    => {},
104                 expected => <<EXPECTED,
105 this loads bar <[this should be hidden then revealed]
106 >
107 EXPECTED
108
109         },
110         {
111                 name    => 'delete covering file',
112                 skip    => 0,
113                 opts    => {
114                         search_dirs => [ qw(
115                                 templates
116                                 templates/deeper
117                                 templates/deeper/deepest
118                         ) ],
119                 },
120                 pretest => sub { unlink 'templates/deeper/BAR.tmpl' },
121                 posttest => sub { write_tmpl_files() },
122
123                 data    => {},
124                 expected => <<EXPECTED,
125 this loads bar <[this should be hidden then revealed]
126 >
127 EXPECTED
128
129         },
130 ] ;
131
132
133 write_tmpl_files() ;
134
135 template_tester( $tests ) ;
136
137 #remove_tmpl_files() ;
138
139 exit ;
140
141
142 sub write_tmpl_files {
143
144         mkdir $_, 0755 for @tmpl_dirs ;
145
146         while( my( $file, $tmpl ) = each %tmpl_files ) {
147
148                 write_file( $file, $tmpl ) ;
149         }
150 }
151
152 sub remove_tmpl_files {
153
154         unlink keys %tmpl_files ;
155         rmdir $_ for reverse @tmpl_dirs ;
156 }