1Template::Test(3) User Contributed Perl Documentation Template::Test(3)
2
3
4
6 Template::Test - Module for automating TT2 test scripts
7
9 use Template::Test;
10
11 $Template::Test::DEBUG = 0; # set this true to see each test running
12 $Template::Test::EXTRA = 2; # 2 extra tests follow test_expect()...
13
14 # ok() can be called any number of times before test_expect
15 ok( $true_or_false )
16
17 # test_expect() splits $input into individual tests, processes each
18 # and compares generated output against expected output
19 test_expect($input, $template, \%replace );
20
21 # $input is text or filehandle (e.g. DATA section after __END__)
22 test_expect( $text );
23 test_expect( \*DATA );
24
25 # $template is a Template object or configuration hash
26 my $template_cfg = { ... };
27 test_expect( $input, $template_cfg );
28 my $template_obj = Template->new($template_cfg);
29 test_expect( $input, $template_obj );
30
31 # $replace is a hash reference of template variables
32 my $replace = {
33 a => 'alpha',
34 b => 'bravo'
35 };
36 test_expect( $input, $template, $replace );
37
38 # ok() called after test_expect should be declared in $EXTRA (2)
39 ok( $true_or_false )
40 ok( $true_or_false )
41
43 The "Template::Test" module defines the test_expect() and other related
44 subroutines which can be used to automate test scripts for the Template
45 Toolkit. See the numerous tests in the t sub-directory of the
46 distribution for examples of use.
47
49 text_expect()
50 The "test_expect()" subroutine splits an input document into a number
51 of separate tests, processes each one using the Template Toolkit and
52 then compares the generated output against an expected output, also
53 specified in the input document. It generates the familiar "ok"/"not
54 ok" output compatible with "Test::Harness".
55
56 The test input should be specified as a text string or a reference to a
57 filehandle (e.g. "GLOB" or "IO::Handle") from which it can be read. In
58 particular, this allows the test input to be placed after the "__END__"
59 marker and read via the "DATA" filehandle.
60
61 use Template::Test;
62
63 test_expect(\*DATA);
64
65 __END__
66 # this is the first test (this is a comment)
67 -- test --
68 blah blah blah [% foo %]
69 -- expect --
70 blah blah blah value_of_foo
71
72 # here's the second test (no surprise, so is this)
73 -- test --
74 more blah blah [% bar %]
75 -- expect --
76 more blah blah value_of_bar
77
78 Blank lines between test sections are generally ignored. Any line
79 starting with "#" is treated as a comment and is ignored.
80
81 The second and third parameters to "test_expect()" are optional. The
82 second may be either a reference to a Template object which should be
83 used to process the template fragments, or a reference to a hash array
84 containing configuration values which should be used to instantiate a
85 new Template object.
86
87 # pass reference to config hash
88 my $config = {
89 INCLUDE_PATH => '/here/there:/every/where',
90 POST_CHOMP => 1,
91 };
92 test_expect(\*DATA, $config);
93
94 # or create Template object explicitly
95 my $template = Template->new($config);
96 test_expect(\*DATA, $template);
97
98 The third parameter may be used to reference a hash array of template
99 variable which should be defined when processing the tests. This is
100 passed to the Template process() method.
101
102 my $replace = {
103 a => 'alpha',
104 b => 'bravo',
105 };
106
107 test_expect(\*DATA, $config, $replace);
108
109 The second parameter may be left undefined to specify a default
110 Template configuration.
111
112 test_expect(\*DATA, undef, $replace);
113
114 For testing the output of different Template configurations, a
115 reference to a list of named Template objects also may be passed as the
116 second parameter.
117
118 my $tt1 = Template->new({ ... });
119 my $tt2 = Template->new({ ... });
120 my @tts = [ one => $tt1, two => $tt1 ];
121
122 The first object in the list is used by default. Other objects may be
123 switched in with a '"-- use $name --"' marker. This should immediately
124 follow a '"-- test --"' line. That object will then be used for the
125 rest of the test, or until a different object is selected.
126
127 -- test --
128 -- use one --
129 [% blah %]
130 -- expect --
131 blah, blah
132
133 -- test --
134 still using one...
135 -- expect --
136 ...
137
138 -- test --
139 -- use two --
140 [% blah %]
141 -- expect --
142 blah, blah, more blah
143
144 The "test_expect()" sub counts the number of tests, and then calls
145 ntests() to generate the familiar ""1..$ntests\n"" test harness line.
146 Each test defined generates two test numbers. The first indicates that
147 the input was processed without error, and the second that the output
148 matches that expected.
149
150 Additional test may be run before "test_expect()" by calling ok().
151 These test results are cached until ntests() is called and the final
152 number of tests can be calculated. Then, the ""1..$ntests"" line is
153 output, along with ""ok $n"" / ""not ok $n"" lines for each of the
154 cached test result. Subsequent calls to ok() then generate an output
155 line immediately.
156
157 my $something = SomeObject->new();
158 ok( $something );
159
160 my $other = AnotherThing->new();
161 ok( $other );
162
163 test_expect(\*DATA);
164
165 If any tests are to follow after "test_expect()" is called then these
166 should be pre-declared by setting the $EXTRA package variable. This
167 value (default: 0) is added to the grand total calculated by ntests().
168 The results of the additional tests are also registered by calling
169 ok().
170
171 $Template::Test::EXTRA = 2;
172
173 # can call ok() any number of times before test_expect()
174 ok( $did_that_work );
175 ok( $make_sure );
176 ok( $dead_certain );
177
178 # <some> number of tests...
179 test_expect(\*DATA, $config, $replace);
180
181 # here's those $EXTRA tests
182 ok( defined $some_result && ref $some_result eq 'ARRAY' );
183 ok( $some_result->[0] eq 'some expected value' );
184
185 If you don't want to call "test_expect()" at all then you can call
186 "ntests($n)" to declare the number of tests and generate the test
187 header line. After that, simply call ok() for each test passing a true
188 or false values to indicate that the test passed or failed.
189
190 ntests(2);
191 ok(1);
192 ok(0);
193
194 If you're really lazy, you can just call ok() and not bother declaring
195 the number of tests at all. All tests results will be cached until the
196 end of the script and then printed in one go before the program exits.
197
198 ok( $x );
199 ok( $y );
200
201 You can identify only a specific part of the input file for testing
202 using the '"-- start --"' and '"-- stop --"' markers. Anything before
203 the first '"-- start --"' is ignored, along with anything after the
204 next '"-- stop --"' marker.
205
206 -- test --
207 this is test 1 (not performed)
208 -- expect --
209 this is test 1 (not performed)
210
211 -- start --
212
213 -- test --
214 this is test 2
215 -- expect --
216 this is test 2
217
218 -- stop --
219
220 ...
221
222 ntests()
223 Subroutine used to specify how many tests you're expecting to run.
224
225 ok($test)
226 Generates an ""ok $n"" or ""not ok $n"" message if $test is true or
227 false.
228
229 not_ok($test)
230 The logical inverse of ok(). Prints an ""ok $n"" message is $test is
231 false and vice-versa.
232
233 callsign()
234 For historical reasons and general utility, the module also defines a
235 "callsign()" subroutine which returns a hash mapping the letters "a" to
236 "z" to their phonetic alphabet equivalent (e.g. radio callsigns). This
237 is used by many of the test scripts as a known source of variable
238 values.
239
240 test_expect(\*DATA, $config, callsign());
241
242 banner()
243 This subroutine prints a simple banner including any text passed as
244 parameters. The $DEBUG variable must be set for it to generate any
245 output.
246
247 banner('Testing something-or-other');
248
249 example output:
250
251 #------------------------------------------------------------
252 # Testing something-or-other (27 tests completed)
253 #------------------------------------------------------------
254
256 $DEBUG
257 The $DEBUG package variable can be set to enable debugging mode.
258
259 $PRESERVE
260 The $PRESERVE package variable can be set to stop the test_expect()
261 from converting newlines in the output and expected output into the
262 literal strings '\n'.
263
265 This module started its butt-ugly life as the "t/texpect.pl" script.
266 It was cleaned up to became the "Template::Test" module some time
267 around version 0.29. It underwent further cosmetic surgery for version
268 2.00 but still retains some remarkable rear-end resemblances.
269
270 Since then the "Test::More" and related modules have appeared on CPAN
271 making this module mostly, but not entirely, redundant.
272
274 Imports all methods by default. This is generally a Bad Thing, but
275 this module is only used in test scripts (i.e. at build time) so a) we
276 don't really care and b) it saves typing.
277
278 The line splitter may be a bit dumb, especially if it sees lines like
279 "-- this --" that aren't supposed to be special markers. So don't do
280 that.
281
283 Andy Wardley <abw@wardley.org> <http://wardley.org/>
284
286 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
287
288 This module is free software; you can redistribute it and/or modify it
289 under the same terms as Perl itself.
290
292 Template
293
294
295
296perl v5.30.0 2019-07-26 Template::Test(3)