Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 290 lines (234 sloc) 7.767 kb
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/provider.t
4 #
5 # Test the Template::Provider module.
6 #
7 # Written by Andy Wardley <abw@kfs.org>
8 #
9 # Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
10 # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
11 #
12 # This is free software; you can redistribute it and/or modify it
13 # under the same terms as Perl itself.
14 #
15 # $Id$
16 #
17 #========================================================================
18
19 use strict;
20 use lib qw( ./lib ../lib );
21 use Template::Test;
22 use Template::Config;
d52480d @abw *** empty log message ***
authored
23 use Template::Provider;
3ebfa07 @abw Initial revision
authored
24 use Cwd 'abs_path';
25
26 $^W = 1;
27 $Template::Test::DEBUG = 0;
1303d12 @abw *** empty log message ***
authored
28 #$Template::Provider::DEBUG = 0;
3e45060 @abw *** empty log message ***
authored
29 #$Template::Parser::DEBUG = 1;
30 #$Template::Directive::PRETTY = 1;
31
6ddeb78 @abw added leak.t
authored
32 # uncommenting the next line should cause test 43 to fail because
33 # the provider doesn't stat the file.
34 # $Template::Provider::STAT_TTL = 10;
35
3ebfa07 @abw Initial revision
authored
36 my $DEBUG = 0;
37
38 my $factory = 'Template::Config';
39
40 # script may be being run in distribution root or 't' directory
41 my $dir = -d 't' ? 't/test/src' : 'test/src';
42 my $file = 'foo';
43 my $relfile = "./$dir/$file";
44 my $absfile = abs_path($dir) . '/' . $file;
b4f721c @abw *** empty log message ***
authored
45 my $newfile = "$dir/foobar";
3ebfa07 @abw Initial revision
authored
46 my $vars = {
47 file => $file,
48 relfile => $relfile,
49 absfile => $absfile,
50 fixfile => \&update_file,
51 };
52
53
54 #------------------------------------------------------------------------
55 # This is used to test that source files are automatically reloaded
56 # when updated on disk. we call it first to write a template file,
57 # which is then included in one of the -- test -- sections below.
58 # Then we call update_file() (via the 'fixfile' variable) and
59 # include it again to see if the new file contents were loaded.
60 #------------------------------------------------------------------------
61
62 sub update_file {
63 local *FP;
64 sleep(2); # ensure file time stamps are different
65 open(FP, ">$newfile") || die "$newfile: $!\n";
d52480d @abw *** empty log message ***
authored
66 print(FP @_) || die "failed to write $newfile: $!\n";
3ebfa07 @abw Initial revision
authored
67 close(FP);
68 }
69
70 update_file('This is the old content');
71
72
73 #------------------------------------------------------------------------
74 # instantiate a bunch of providers, using various different techniques,
75 # with different load options but sharing the same parser; then set them
76 # to work fetching some files and check they respond as expected
77 #------------------------------------------------------------------------
78
79 my $parser = $factory->parser(POST_CHOMP => 1)
80 || die $factory->error();
81 ok( $parser );
82
49d1e32 @abw *** empty log message ***
authored
83 my $provinc = $factory->provider(INCLUDE_PATH => $dir,
84 PARSER => $parser,
85 TOLERANT => 1)
3ebfa07 @abw Initial revision
authored
86 || die $factory->error();
87 ok( $provinc );
88
49d1e32 @abw *** empty log message ***
authored
89 my $provabs = $factory->provider({ ABSOLUTE => 1,
90 PARSER => $parser, })
3ebfa07 @abw Initial revision
authored
91 || die $factory->error();
92 ok( $provabs );
93
49d1e32 @abw *** empty log message ***
authored
94 my $provrel = Template::Provider->new({ RELATIVE => 1,
95 PARSER => $parser, })
3ebfa07 @abw Initial revision
authored
96 || die $Template::Provider::ERROR;
97 ok( $provrel );
98
99 ok( $provinc->{ PARSER } == $provabs->{ PARSER } );
100 ok( $provabs->{ PARSER } == $provrel->{ PARSER } );
101
49d1e32 @abw *** empty log message ***
authored
102 banner('matrix');
103
3ebfa07 @abw Initial revision
authored
104 ok( delivered( $provinc, $file ) );
105 ok( declined( $provinc, $absfile ) );
106 ok( declined( $provinc, $relfile ) );
107
108 ok( declined( $provabs, $file ) );
109 ok( delivered( $provabs, $absfile ) );
49d1e32 @abw *** empty log message ***
authored
110 ok( denied( $provabs, $relfile ) );
3ebfa07 @abw Initial revision
authored
111
112 ok( declined( $provrel, $file ) );
49d1e32 @abw *** empty log message ***
authored
113 ok( denied( $provrel, $absfile ) );
3ebfa07 @abw Initial revision
authored
114 ok( delivered( $provrel, $relfile ) );
115
116
117 sub delivered {
118 my ($provider, $file) = @_;
119 my ($result, $error) = $provider->fetch($file);
120 print STDERR "$provider->fetch($file) -> [$result] [$error]\n"
121 if $DEBUG;
122 return ! $error;
123 }
124
125 sub declined {
126 my ($provider, $file) = @_;
127 my ($result, $error) = $provider->fetch($file);
128 print STDERR "$provider->fetch($file) -> [$result] [$error]\n"
129 if $DEBUG;
130 return ($error == Template::Constants::STATUS_DECLINED);
131 }
132
49d1e32 @abw *** empty log message ***
authored
133 sub denied {
134 my ($provider, $file) = @_;
135 my ($result, $error) = $provider->fetch($file);
136 print STDERR "$provider->fetch($file) -> [$result] [$error]\n"
137 if $DEBUG;
138 return ($error == Template::Constants::STATUS_ERROR);
139 }
140
3ebfa07 @abw Initial revision
authored
141 #------------------------------------------------------------------------
142 # now we'll fold those providers up into some Template objects that
143 # we can pass to text_expect() to do some template driven testing
144 #------------------------------------------------------------------------
145
e39913f @abw *** empty log message ***
authored
146 my $ttinc = Template->new( LOAD_TEMPLATES => [ $provinc ] )
3ebfa07 @abw Initial revision
authored
147 || die "$Template::ERROR\n";
148 ok( $ttinc );
149
e39913f @abw *** empty log message ***
authored
150 my $ttabs = Template->new( LOAD_TEMPLATES => [ $provabs ] )
3ebfa07 @abw Initial revision
authored
151 || die "$Template::ERROR\n";
152 ok( $ttabs );
153
e39913f @abw *** empty log message ***
authored
154 my $ttrel = Template->new( LOAD_TEMPLATES => [ $provrel ] )
3ebfa07 @abw Initial revision
authored
155 || die "$Template::ERROR\n";
156 ok( $ttrel );
157
158
159 my $uselist = [ ttinc => $ttinc, ttabs => $ttabs, ttrel => $ttrel ];
160
161 test_expect(\*DATA, $uselist, $vars);
162
163
164 __DATA__
165 -- test --
166 -- use ttinc --
167 [% TRY %]
168 [% INCLUDE foo %]
169 [% INCLUDE $relfile %]
170 [% CATCH file %]
171 Error: [% error.type %] - [% error.info.split(': ').1 %]
172 [% END %]
173 -- expect --
f4df3b6 @abw approach v2.03
authored
174 This is the foo file, a is Error: file - not found
6ddeb78 @abw added leak.t
authored
175
3ebfa07 @abw Initial revision
authored
176
177 -- test --
178 [% TRY %]
179 [% INCLUDE foo %]
180 [% INCLUDE $absfile %]
181 [% CATCH file %]
182 Error: [% error.type %] - [% error.info.split(': ').1 %]
183 [% END %]
184 -- expect --
f4df3b6 @abw approach v2.03
authored
185 This is the foo file, a is Error: file - not found
6ddeb78 @abw added leak.t
authored
186
3ebfa07 @abw Initial revision
authored
187
3e45060 @abw *** empty log message ***
authored
188 -- test --
189 [% TRY %]
f4df3b6 @abw approach v2.03
authored
190 [% INSERT foo +%]
3e45060 @abw *** empty log message ***
authored
191 [% INSERT $absfile %]
192 [% CATCH file %]
193 Error: [% error %]
194 [% END %]
195 -- expect --
196 -- process --
197 [% TAGS [* *] %]
f4df3b6 @abw approach v2.03
authored
198 This is the foo file, a is [% a -%]
3e45060 @abw *** empty log message ***
authored
199 Error: file error - [* absfile *]: not found
200
3ebfa07 @abw Initial revision
authored
201 #------------------------------------------------------------------------
202
203 -- test --
204 -- use ttrel --
205 [% TRY %]
206 [% INCLUDE $relfile %]
207 [% INCLUDE foo %]
6ddeb78 @abw added leak.t
authored
208 [% CATCH file -%]
3ebfa07 @abw Initial revision
authored
209 Error: [% error.type %] - [% error.info %]
210 [% END %]
211 -- expect --
f4df3b6 @abw approach v2.03
authored
212 This is the foo file, a is Error: file - foo: not found
3ebfa07 @abw Initial revision
authored
213
214 -- test --
215 [% TRY %]
6ddeb78 @abw added leak.t
authored
216 [% INCLUDE $relfile -%]
3ebfa07 @abw Initial revision
authored
217 [% INCLUDE $absfile %]
6ddeb78 @abw added leak.t
authored
218 [% CATCH file %]
3ebfa07 @abw Initial revision
authored
219 Error: [% error.type %] - [% error.info.split(': ').1 %]
220 [% END %]
221 -- expect --
f4df3b6 @abw approach v2.03
authored
222 This is the foo file, a is Error: file - absolute paths are not allowed (set ABSOLUTE option)
3ebfa07 @abw Initial revision
authored
223
6ddeb78 @abw added leak.t
authored
224
3e45060 @abw *** empty log message ***
authored
225 -- test --
226 foo: [% TRY; INSERT foo; CATCH; "$error\n"; END %]
f4df3b6 @abw approach v2.03
authored
227 rel: [% TRY; INSERT $relfile; CATCH; "$error\n"; END +%]
3e45060 @abw *** empty log message ***
authored
228 abs: [% TRY; INSERT $absfile; CATCH; "$error\n"; END %]
229 -- expect --
230 -- process --
231 [% TAGS [* *] %]
232 foo: file error - foo: not found
f4df3b6 @abw approach v2.03
authored
233 rel: This is the foo file, a is [% a -%]
3e45060 @abw *** empty log message ***
authored
234 abs: file error - [* absfile *]: absolute paths are not allowed (set ABSOLUTE option)
235
3ebfa07 @abw Initial revision
authored
236 #------------------------------------------------------------------------
237
238 -- test --
239 -- use ttabs --
240 [% TRY %]
241 [% INCLUDE $absfile %]
242 [% INCLUDE foo %]
6ddeb78 @abw added leak.t
authored
243 [% CATCH file %]
3ebfa07 @abw Initial revision
authored
244 Error: [% error.type %] - [% error.info %]
245 [% END %]
246 -- expect --
f4df3b6 @abw approach v2.03
authored
247 This is the foo file, a is Error: file - foo: not found
3ebfa07 @abw Initial revision
authored
248
249 -- test --
250 [% TRY %]
251 [% INCLUDE $absfile %]
252 [% INCLUDE $relfile %]
6ddeb78 @abw added leak.t
authored
253 [% CATCH file %]
3ebfa07 @abw Initial revision
authored
254 Error: [% error.type %] - [% error.info.split(': ').1 %]
255 [% END %]
256 -- expect --
f4df3b6 @abw approach v2.03
authored
257 This is the foo file, a is Error: file - relative paths are not allowed (set RELATIVE option)
3ebfa07 @abw Initial revision
authored
258
259
3e45060 @abw *** empty log message ***
authored
260 -- test --
261 foo: [% TRY; INSERT foo; CATCH; "$error\n"; END %]
262 rel: [% TRY; INSERT $relfile; CATCH; "$error\n"; END %]
263 abs: [% TRY; INSERT $absfile; CATCH; "$error\n"; END %]
264 -- expect --
265 -- process --
266 [% TAGS [* *] %]
267 foo: file error - foo: not found
268 rel: file error - [* relfile *]: relative paths are not allowed (set RELATIVE option)
f4df3b6 @abw approach v2.03
authored
269 abs: This is the foo file, a is [% a -%]
3e45060 @abw *** empty log message ***
authored
270
271
6ddeb78 @abw added leak.t
authored
272
3ebfa07 @abw Initial revision
authored
273 #------------------------------------------------------------------------
274 # test that files updated on disk are automatically reloaded.
275 #------------------------------------------------------------------------
276
277 -- test --
278 -- use ttinc --
1303d12 @abw *** empty log message ***
authored
279 [% INCLUDE foobar %]
3ebfa07 @abw Initial revision
authored
280 -- expect --
281 This is the old content
282
283 -- test --
284 [% CALL fixfile('This is the new content') %]
1303d12 @abw *** empty log message ***
authored
285 [% INCLUDE foobar %]
3ebfa07 @abw Initial revision
authored
286 -- expect --
287 This is the new content
288
289
Something went wrong with that request. Please try again.