1Template::Test(3)     User Contributed Perl Documentation    Template::Test(3)
2
3
4

NAME

6       Template::Test - Module for automating TT2 test scripts
7

SYNOPSIS

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

DESCRIPTION

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 dis‐
46       tribution for examples of use.
47
48       The test_expect() subroutine splits an input document into a number of
49       separate tests, processes each one using the Template Toolkit and then
50       compares the generated output against an expected output, also speci‐
51       fied in the input document.  It generates the familiar "ok/not ok" out‐
52       put compatible with Test::Harness.
53
54       The test input should be specified as a text string or a reference to a
55       filehandle (e.g. GLOB or IO::Handle) from which it can be read.  In
56       particular, this allows the test input to be placed after the __END__
57       marker and read via the DATA filehandle.
58
59           use Template::Test;
60
61           test_expect(\*DATA);
62
63           __END__
64           # this is the first test (this is a comment)
65           -- test --
66           blah blah blah [% foo %]
67           -- expect --
68           blah blah blah value_of_foo
69
70           # here's the second test (no surprise, so is this)
71           -- test --
72           more blah blah [% bar %]
73           -- expect --
74           more blah blah value_of_bar
75
76       Blank lines between test sections are generally ignored.  Any line
77       starting with '#' is treated as a comment and is ignored.
78
79       The second and third parameters to test_expect() are optional.  The
80       second may be either a reference to a Template object which should be
81       used to process the template fragments, or a reference to a hash array
82       containing configuration values which should be used to instantiate a
83       new Template object.
84
85           # pass reference to config hash
86           my $config = {
87               INCLUDE_PATH => '/here/there:/every/where',
88               POST_CHOMP   => 1,
89           };
90           test_expect(\*DATA, $config);
91
92           # or create Template object explicitly
93           my $template = Template->new($config);
94           test_expect(\*DATA, $template);
95
96       The third parameter may be used to reference a hash array of template
97       variable which should be defined when processing the tests.  This is
98       passed to the Template process() method.
99
100           my $replace = {
101               a => 'alpha',
102               b => 'bravo',
103           };
104
105           test_expect(\*DATA, $config, $replace);
106
107       The second parameter may be left undefined to specify a default Tem‐
108       plate configuration.
109
110           test_expect(\*DATA, undef, $replace);
111
112       For testing the output of different Template configurations, a refer‐
113       ence to a list of named Template objects also may be passed as the sec‐
114       ond parameter.
115
116           my $tt1 = Template->new({ ... });
117           my $tt2 = Template->new({ ... });
118           my @tts = [ one => $tt1, two => $tt1 ];
119
120       The first object in the list is used by default.  Other objects may be
121       switched in with the '-- use $name --' marker.  This should immediately
122       follow a '-- test --' line.  That object will then be used for the rest
123       of the test, or until a different object is selected.
124
125           -- test --
126           -- use one --
127           [% blah %]
128           -- expect --
129           blah, blah
130
131           -- test --
132           still using one...
133           -- expect --
134           ...
135
136           -- test --
137           -- use two --
138           [% blah %]
139           -- expect --
140           blah, blah, more blah
141
142       The test_expect() sub counts the number of tests, and then calls
143       ntests() to generate the familiar "1..$ntests\n" test harness line.
144       Each test defined generates two test numbers.  The first indicates that
145       the input was processed without error, and the second that the output
146       matches that expected.
147
148       Additional test may be run before test_expect() by calling ok().  These
149       test results are cached until ntests() is called and the final number
150       of tests can be calculated.  Then, the "1..$ntests" line is output,
151       along with "ok $n" / "not ok $n" lines for each of the cached test
152       result.  Subsequent calls to ok() then generate an output line immedi‐
153       ately.
154
155           my $something = SomeObject->new();
156           ok( $something );
157
158           my $other = AnotherThing->new();
159           ok( $other );
160
161           test_expect(\*DATA);
162
163       If any tests are to follow after test_expect() is called then these
164       should be pre-declared by setting the $EXTRA package variable.  This
165       value (default: 0) is added to the grand total calculated by ntests().
166       The results of the additional tests are also registered by calling
167       ok().
168
169           $Template::Test::EXTRA = 2;
170
171           # can call ok() any number of times before test_expect()
172           ok( $did_that_work );
173           ok( $make_sure );
174           ok( $dead_certain );
175
176           # <some> number of tests...
177           test_expect(\*DATA, $config, $replace);
178
179           # here's those $EXTRA tests
180           ok( defined $some_result && ref $some_result eq 'ARRAY' );
181           ok( $some_result->[0] eq 'some expected value' );
182
183       If you don't want to call test_expect() at all then you can call
184       ntests($n) to declare the number of tests and generate the test header
185       line.  After that, simply call ok() for each test passing a true or
186       false values to indicate that the test passed or failed.
187
188           ntests(2);
189           ok(1);
190           ok(0);
191
192       If you're really lazy, you can just call ok() and not bother declaring
193       the number of tests at all.  All tests results will be cached until the
194       end of the script and then printed in one go before the program exits.
195
196           ok( $x );
197           ok( $y );
198
199       You can identify only a specific part of the input file for testing
200       using the '-- start --' and '-- stop --' markers.  Anything before the
201       first '-- start --' is ignored, along with anything after the next '--
202       stop --' marker.
203
204           -- test --
205           this is test 1 (not performed)
206           -- expect --
207           this is test 1 (not performed)
208
209           -- start --
210
211           -- test --
212           this is test 2
213           -- expect --
214           this is test 2
215
216           -- stop --
217
218           ...
219
220       For historical reasons and general utility, the module also defines a
221       'callsign' subroutine which returns a hash mapping a..z to their pho‐
222       netic alphabet equivalent (e.g. radio callsigns).  This is used by many
223       of the test scripts as a "known source" of variable values.
224
225           test_expect(\*DATA, $config, callsign());
226
227       A banner() subroutine is also provided which prints a simple banner
228       including any text passed as parameters, if $DEBUG is set.
229
230           banner('Testing something-or-other');
231
232       example output:
233
234           #------------------------------------------------------------
235           # Testing something-or-other (27 tests completed)
236           #------------------------------------------------------------
237
238       The $DEBUG package variable can be set to enable debugging mode.
239
240       The $PRESERVE package variable can be set to stop the test_expect()
241       from converting newlines in the output and expected output into the
242       literal strings '\n'.
243

HISTORY

245       This module started its butt-ugly life as the t/texpect.pl script.  It
246       was cleaned up to became the Template::Test module some time around
247       version 0.29.  It underwent further cosmetic surgery for version 2.00
248       but still retains some rear-end resemblances.
249

BUGS / KNOWN "FEATURES"

251       Imports all methods by default.  This is generally a Bad Thing, but
252       this module is only used in test scripts (i.e. at build time) so a) we
253       don't really care and b) it saves typing.
254
255       The line splitter may be a bit dumb, especially if it sees lines like
256       -- this -- that aren't supposed to be special markers.  So don't do
257       that.
258

AUTHOR

260       Andy Wardley <abw@wardley.org>
261
262       <http://wardley.org/http://wardley.org/>
263

VERSION

265       2.75, distributed as part of the Template Toolkit version 2.18,
266       released on 09 February 2007.
267
269         Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.
270
271       This module is free software; you can redistribute it and/or modify it
272       under the same terms as Perl itself.
273

SEE ALSO

275       Template
276
277
278
279perl v5.8.8                       2007-02-09                 Template::Test(3)
Impressum