.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Template::Test 3pm" .TH Template::Test 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Template::Test \- Module for automating TT2 test scripts .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Template::Test; \& \& $Template::Test::DEBUG = 0; # set this true to see each test running \& $Template::Test::EXTRA = 2; # 2 extra tests follow test_expect()... \& \& # ok() can be called any number of times before test_expect \& ok( $true_or_false ) \& \& # test_expect() splits $input into individual tests, processes each \& # and compares generated output against expected output \& test_expect($input, $template, \e%replace ); \& \& # $input is text or filehandle (e.g. DATA section after _\|_END_\|_) \& test_expect( $text ); \& test_expect( \e*DATA ); \& \& # $template is a Template object or configuration hash \& my $template_cfg = { ... }; \& test_expect( $input, $template_cfg ); \& my $template_obj = Template\->new($template_cfg); \& test_expect( $input, $template_obj ); \& \& # $replace is a hash reference of template variables \& my $replace = { \& a => \*(Aqalpha\*(Aq, \& b => \*(Aqbravo\*(Aq \& }; \& test_expect( $input, $template, $replace ); \& \& # ok() called after test_expect should be declared in $EXTRA (2) \& ok( $true_or_false ) \& ok( $true_or_false ) .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" The \f(CW\*(C`Template::Test\*(C'\fR module defines the \fBtest_expect()\fR and other related subroutines which can be used to automate test scripts for the Template Toolkit. See the numerous tests in the \fIt\fR sub-directory of the distribution for examples of use. .SH "PACKAGE SUBROUTINES" .IX Header "PACKAGE SUBROUTINES" .SS \fBtext_expect()\fP .IX Subsection "text_expect()" The \f(CWtest_expect()\fR subroutine splits an input document into a number of separate tests, processes each one using the Template Toolkit and then compares the generated output against an expected output, also specified in the input document. It generates the familiar \&\f(CW\*(C`ok\*(C'\fR/\f(CW\*(C`not ok\*(C'\fR output compatible with \f(CW\*(C`Test::Harness\*(C'\fR. .PP The test input should be specified as a text string or a reference to a filehandle (e.g. \f(CW\*(C`GLOB\*(C'\fR or \f(CW\*(C`IO::Handle\*(C'\fR) from which it can be read. In particular, this allows the test input to be placed after the \f(CW\*(C`_\|_END_\|_\*(C'\fR marker and read via the \f(CW\*(C`DATA\*(C'\fR filehandle. .PP .Vb 1 \& use Template::Test; \& \& test_expect(\e*DATA); \& \& _\|_END_\|_ \& # this is the first test (this is a comment) \& \-\- test \-\- \& blah blah blah [% foo %] \& \-\- expect \-\- \& blah blah blah value_of_foo \& \& # here\*(Aqs the second test (no surprise, so is this) \& \-\- test \-\- \& more blah blah [% bar %] \& \-\- expect \-\- \& more blah blah value_of_bar .Ve .PP Blank lines between test sections are generally ignored. Any line starting with \f(CW\*(C`#\*(C'\fR is treated as a comment and is ignored. .PP The second and third parameters to \f(CWtest_expect()\fR are optional. The second may be either a reference to a Template object which should be used to process the template fragments, or a reference to a hash array containing configuration values which should be used to instantiate a new Template object. .PP .Vb 6 \& # pass reference to config hash \& my $config = { \& INCLUDE_PATH => \*(Aq/here/there:/every/where\*(Aq, \& POST_CHOMP => 1, \& }; \& test_expect(\e*DATA, $config); \& \& # or create Template object explicitly \& my $template = Template\->new($config); \& test_expect(\e*DATA, $template); .Ve .PP The third parameter may be used to reference a hash array of template variable which should be defined when processing the tests. This is passed to the Template \fBprocess()\fR method. .PP .Vb 4 \& my $replace = { \& a => \*(Aqalpha\*(Aq, \& b => \*(Aqbravo\*(Aq, \& }; \& \& test_expect(\e*DATA, $config, $replace); .Ve .PP The second parameter may be left undefined to specify a default Template configuration. .PP .Vb 1 \& test_expect(\e*DATA, undef, $replace); .Ve .PP For testing the output of different Template configurations, a reference to a list of named Template objects also may be passed as the second parameter. .PP .Vb 3 \& my $tt1 = Template\->new({ ... }); \& my $tt2 = Template\->new({ ... }); \& my @tts = [ one => $tt1, two => $tt1 ]; .Ve .PP The first object in the list is used by default. Other objects may be switched in with a '\f(CW\*(C`\-\- use $name \-\-\*(C'\fR' marker. This should immediately follow a '\f(CW\*(C`\-\- test \-\-\*(C'\fR' line. That object will then be used for the rest of the test, or until a different object is selected. .PP .Vb 5 \& \-\- test \-\- \& \-\- use one \-\- \& [% blah %] \& \-\- expect \-\- \& blah, blah \& \& \-\- test \-\- \& still using one... \& \-\- expect \-\- \& ... \& \& \-\- test \-\- \& \-\- use two \-\- \& [% blah %] \& \-\- expect \-\- \& blah, blah, more blah .Ve .PP The \f(CWtest_expect()\fR sub counts the number of tests, and then calls \fBntests()\fR to generate the familiar "\f(CW\*(C`1..$ntests\en\*(C'\fR" test harness line. Each test defined generates two test numbers. The first indicates that the input was processed without error, and the second that the output matches that expected. .PP Additional test may be run before \f(CWtest_expect()\fR by calling \fBok()\fR. These test results are cached until \fBntests()\fR is called and the final number of tests can be calculated. Then, the "\f(CW\*(C`1..$ntests\*(C'\fR" line is output, along with "\f(CW\*(C`ok $n\*(C'\fR" / "\f(CW\*(C`not ok $n\*(C'\fR" lines for each of the cached test result. Subsequent calls to \fBok()\fR then generate an output line immediately. .PP .Vb 2 \& my $something = SomeObject\->new(); \& ok( $something ); \& \& my $other = AnotherThing\->new(); \& ok( $other ); \& \& test_expect(\e*DATA); .Ve .PP If any tests are to follow after \f(CWtest_expect()\fR is called then these should be pre-declared by setting the \f(CW$EXTRA\fR package variable. This value (default: \f(CW0\fR) is added to the grand total calculated by \fBntests()\fR. The results of the additional tests are also registered by calling \fBok()\fR. .PP .Vb 1 \& $Template::Test::EXTRA = 2; \& \& # can call ok() any number of times before test_expect() \& ok( $did_that_work ); \& ok( $make_sure ); \& ok( $dead_certain ); \& \& # number of tests... \& test_expect(\e*DATA, $config, $replace); \& \& # here\*(Aqs those $EXTRA tests \& ok( defined $some_result && ref $some_result eq \*(AqARRAY\*(Aq ); \& ok( $some_result\->[0] eq \*(Aqsome expected value\*(Aq ); .Ve .PP If you don't want to call \f(CWtest_expect()\fR at all then you can call \&\f(CWntests($n)\fR to declare the number of tests and generate the test header line. After that, simply call \fBok()\fR for each test passing a true or false values to indicate that the test passed or failed. .PP .Vb 3 \& ntests(2); \& ok(1); \& ok(0); .Ve .PP If you're really lazy, you can just call \fBok()\fR and not bother declaring the number of tests at all. All tests results will be cached until the end of the script and then printed in one go before the program exits. .PP .Vb 2 \& ok( $x ); \& ok( $y ); .Ve .PP You can identify only a specific part of the input file for testing using the '\f(CW\*(C`\-\- start \-\-\*(C'\fR' and '\f(CW\*(C`\-\- stop \-\-\*(C'\fR' markers. Anything before the first '\f(CW\*(C`\-\- start \-\-\*(C'\fR' is ignored, along with anything after the next \&'\f(CW\*(C`\-\- stop \-\-\*(C'\fR' marker. .PP .Vb 4 \& \-\- test \-\- \& this is test 1 (not performed) \& \-\- expect \-\- \& this is test 1 (not performed) \& \& \-\- start \-\- \& \& \-\- test \-\- \& this is test 2 \& \-\- expect \-\- \& this is test 2 \& \& \-\- stop \-\- \& \& ... .Ve .SS \fBntests()\fP .IX Subsection "ntests()" Subroutine used to specify how many tests you're expecting to run. .SS ok($test) .IX Subsection "ok($test)" Generates an "\f(CW\*(C`ok $n\*(C'\fR" or "\f(CW\*(C`not ok $n\*(C'\fR" message if \f(CW$test\fR is true or false. .SS not_ok($test) .IX Subsection "not_ok($test)" The logical inverse of \fBok()\fR. Prints an "\f(CW\*(C`ok $n\*(C'\fR" message is \f(CW$test\fR is \&\fIfalse\fR and vice-versa. .SS \fBcallsign()\fP .IX Subsection "callsign()" For historical reasons and general utility, the module also defines a \&\f(CWcallsign()\fR subroutine which returns a hash mapping the letters \f(CW\*(C`a\*(C'\fR to \f(CW\*(C`z\*(C'\fR to their phonetic alphabet equivalent (e.g. radio callsigns). This is used by many of the test scripts as a known source of variable values. .PP .Vb 1 \& test_expect(\e*DATA, $config, callsign()); .Ve .SS \fBbanner()\fP .IX Subsection "banner()" This subroutine prints a simple banner including any text passed as parameters. The \f(CW$DEBUG\fR variable must be set for it to generate any output. .PP .Vb 1 \& banner(\*(AqTesting something\-or\-other\*(Aq); .Ve .PP example output: .PP .Vb 3 \& #\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # Testing something\-or\-other (27 tests completed) \& #\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .SH "PACKAGE VARIABLES" .IX Header "PACKAGE VARIABLES" .ie n .SS $DEBUG .el .SS \f(CW$DEBUG\fP .IX Subsection "$DEBUG" The \f(CW$DEBUG\fR package variable can be set to enable debugging mode. .ie n .SS $PRESERVE .el .SS \f(CW$PRESERVE\fP .IX Subsection "$PRESERVE" The \f(CW$PRESERVE\fR package variable can be set to stop the \fBtest_expect()\fR from converting newlines in the output and expected output into the literal strings '\en'. .SH HISTORY .IX Header "HISTORY" This module started its butt-ugly life as the \f(CW\*(C`t/texpect.pl\*(C'\fR script. It was cleaned up to became the \f(CW\*(C`Template::Test\*(C'\fR module some time around version 0.29. It underwent further cosmetic surgery for version 2.00 but still retains some remarkable rear-end resemblances. .PP Since then the \f(CW\*(C`Test::More\*(C'\fR and related modules have appeared on CPAN making this module mostly, but not entirely, redundant. .SH "BUGS / KNOWN ""FEATURES""" .IX Header "BUGS / KNOWN ""FEATURES""" Imports all methods by default. This is generally a Bad Thing, but this module is only used in test scripts (i.e. at build time) so a) we don't really care and b) it saves typing. .PP The line splitter may be a bit dumb, especially if it sees lines like \&\f(CW\*(C`\-\- this \-\-\*(C'\fR that aren't supposed to be special markers. So don't do that. .SH AUTHOR .IX Header "AUTHOR" Andy Wardley .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (C) 1996\-2007 Andy Wardley. All Rights Reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" Template