Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 247 lines (216 sloc) 5.189 kb
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/service.t
4 #
5 # Test the Template::Service 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::Service;
23 use Template::Document;
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
24 use Template::Constants qw( :debug );
25
26 my $DEBUG = grep(/^--?d(debug)?$/, @ARGV);
3ebfa07 @abw Initial revision
authored
27
ea725fe @abw *** empty log message ***
authored
28 my $dir = -d 't' ? 't/test' : 'test';
29 my $config = {
30 INCLUDE_PATH => "$dir/src:$dir/lib",
31 PRE_PROCESS => [ 'config', 'header' ],
32 POST_PROCESS => 'footer',
33 BLOCKS => {
34 demo => sub { return 'This is a demo' },
35 astext => "Another template block, a is '[% a %]'",
36 },
3ebfa07 @abw Initial revision
authored
37 ERROR => {
ea725fe @abw *** empty log message ***
authored
38 barf => 'barfed',
39 default => 'error',
3ebfa07 @abw Initial revision
authored
40 },
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
41 DEBUG => $DEBUG ? DEBUG_SERVICE : 0,
ea725fe @abw *** empty log message ***
authored
42 };
43 my $tt1 = Template->new($config);
44
45 $config->{ AUTO_RESET } = 0;
46 my $tt2 = Template->new($config);
47
48 $config->{ ERROR } = 'barfed';
49 my $tt3 = Template->new($config);
3ebfa07 @abw Initial revision
authored
50
cf8065a @abw * added tests for WRAPPER configuration item
authored
51 $config->{ PRE_PROCESS } = 'before';
52 $config->{ POST_PROCESS } = 'after';
53 $config->{ PROCESS } = 'process';
54 $config->{ WRAPPER } = 'outer';
55 my $tt4 = Template->new($config);
56
57 $config->{ WRAPPER } = [ 'outer', 'inner' ];
58 my $tt5 = Template->new($config);
59
ea725fe @abw *** empty log message ***
authored
60 my $replace = {
61 title => 'Joe Random Title',
62 };
63
64
cf8065a @abw * added tests for WRAPPER configuration item
authored
65 test_expect(\*DATA, [
66 tt1 => $tt1,
67 tt2 => $tt2,
68 tt3 => $tt3,
69 wrapper => $tt4,
70 nested => $tt5,
71 ], $replace);
3ebfa07 @abw Initial revision
authored
72
49d1e32 @abw *** empty log message ***
authored
73 __END__
ea725fe @abw *** empty log message ***
authored
74 # test that headers and footers get added
75 -- test --
76 This is some text
77 -- expect --
78 header:
79 title: Joe Random Title
80 menu: This is the menu, defined in 'config'
81 This is some text
82 footer
83
84 # test that the 'demo' block (template sub) is defined
85 -- test --
86 [% INCLUDE demo %]
87 -- expect --
88 header:
89 title: Joe Random Title
90 menu: This is the menu, defined in 'config'
91 This is a demo
92 footer
93
94 # and also the 'astext' block (template text)
95 -- test --
96 [% INCLUDE astext a = 'artifact' %]
97 -- expect --
98 header:
99 title: Joe Random Title
100 menu: This is the menu, defined in 'config'
101 Another template block, a is 'artifact'
102 footer
103
104 # test that 'barf' exception gets redirected to the correct error template
105 -- test --
106 [% THROW barf 'Not feeling too good' %]
107 -- expect --
108 header:
109 title: Joe Random Title
110 menu: This is the menu, defined in 'config'
111 barfed: [barf] [Not feeling too good]
112 footer
113
114 # test all other errors get redirected correctly
115 -- test --
116 [% INCLUDE no_such_file %]
117 -- expect --
118 header:
119 title: Joe Random Title
120 menu: This is the menu, defined in 'config'
121 error: [file] [no_such_file: not found]
122 footer
123
124 # import some block definitions from 'blockdef'...
125 -- test --
126 [% PROCESS blockdef -%]
127 [% INCLUDE block1
128 a = 'alpha'
129 %]
130 -- expect --
131 header:
132 title: Joe Random Title
133 menu: This is the menu, defined in 'config'
134 start of blockdef
135
136 end of blockdef
137 This is block 1, defined in blockdef, a is alpha
138
139 footer
140
141 # ...and make sure they go away for the next service
49d1e32 @abw *** empty log message ***
authored
142 -- test --
ea725fe @abw *** empty log message ***
authored
143 [% INCLUDE block1 %]
49d1e32 @abw *** empty log message ***
authored
144 -- expect --
ea725fe @abw *** empty log message ***
authored
145 header:
146 title: Joe Random Title
147 menu: This is the menu, defined in 'config'
148 error: [file] [block1: not found]
149 footer
3ebfa07 @abw Initial revision
authored
150
3e45060 @abw *** empty log message ***
authored
151 # now try it again with AUTO_RESET turned off...
ea725fe @abw *** empty log message ***
authored
152 -- test --
153 -- use tt2 --
154 [% PROCESS blockdef -%]
155 [% INCLUDE block1
156 a = 'alpha'
157 %]
158 -- expect --
159 header:
160 title: Joe Random Title
161 menu: This is the menu, defined in 'config'
162 start of blockdef
163
164 end of blockdef
165 This is block 1, defined in blockdef, a is alpha
166
167 footer
168
169 # ...and the block definitions should persist
170 -- test --
171 [% INCLUDE block1 a = 'alpha' %]
172 -- expect --
173 header:
174 title: Joe Random Title
175 menu: This is the menu, defined in 'config'
176 This is block 1, defined in blockdef, a is alpha
177
178 footer
179
180 # test that the 'demo' block is still defined
181 -- test --
182 [% INCLUDE demo %]
183 -- expect --
184 header:
185 title: Joe Random Title
186 menu: This is the menu, defined in 'config'
187 This is a demo
188 footer
189
190 # and also the 'astext' block
191 -- test --
192 [% INCLUDE astext a = 'artifact' %]
193 -- expect --
194 header:
195 title: Joe Random Title
196 menu: This is the menu, defined in 'config'
197 Another template block, a is 'artifact'
198 footer
199
200 # test that a single ERROR template can be specified
201 -- test --
202 -- use tt3 --
203 [% THROW food 'cabbages' %]
204 -- expect --
205 header:
206 title: Joe Random Title
207 menu: This is the menu, defined in 'config'
208 barfed: [food] [cabbages]
209 footer
cf8065a @abw * added tests for WRAPPER configuration item
authored
210
211 -- test --
212 -- use wrapper --
213 [% title = 'The Foo Page' -%]
214 begin page content
215 title is "[% title %]"
216 end page content
217 -- expect --
218 This comes before
219 <outer title="The Foo Page">
220 begin process
221 begin page content
222 title is "The Foo Page"
223 end page content
224 end process
225 </outer>
226 This comes after
227
228 -- test --
229 -- use nested --
230 [% title = 'The Bar Page' -%]
231 begin page content
232 title is "[% title %]"
233 end page content
234 -- expect --
235 This comes before
236 <outer title="inner The Bar Page">
237 <inner title="The Bar Page">
238 begin process
239 begin page content
240 title is "The Bar Page"
241 end page content
242 end process
243 </inner>
244
245 </outer>
246 This comes after
Something went wrong with that request. Please try again.