use Template::TT3::Test tests => 8, debug => 'Template::TT3::Context', args => \@ARGV; # use default configuration... test_expect(); # ...or custom config and/or vars test_expect( config => { # TT3 config }, vars => { # TT vars } ); # ...or multiple engines test_expect( engines => { engine1 => { # TT3 config for engine 1 }, engine2 => { # TT3 config for engine 2 }, }, ); __DATA__ # this is a comment -- test This is the first test Hello [% name or 'World' %] -- expect -- Hello World -- test This is the second test -- # use another engine configuration -- use engine1 -- Hello
This module implements a number of useful subroutines for testing the Template Toolkit. It is a subclass of Badger::Test which performs the same basic function as the Test::Simple and Test::More modules. It also adds some methods specific to the Template Toolkit.
The following subroutine is exported by default, in addition to those exported by the Badger::Test base class.
This subroutine can be used to run a number of template-based tests defined in
the data section of a program (i.e. following a __DATA__
or __END__
line).
use Template::TT3::Test tests => 1; test_expect( vars => { input => 'expected output' } ); __END__ -- test Number One -- This is the [% input %] -- expect -- This is the expected output
The subroutine accepts any of the following parameters
A reference to a hash array of Template configuration parameters which will be used to create a template engine for processing the source templates in the tests.
A reference to a Template engine which will be used to process the source templates in the tests.
A reference to a hash array of name Template engines. These can be selected for a particular test using the -- use engine_name -- test directive.
A reference to a subroutine responsible for running each tests. This defaults to the test_handler() subroutine.
A reference to a list of tests. This defaults to the tests returned by the data_tests() subroutine.
The following subroutine are exported on demand, in addition to those exported by the Badger::Test base class.
Returns the text from the DATA
section of the calling program, coming
after an __END__
or __DATA__
marker. If a second __END__
marker is found in the text then it and anything after it is removed.
use Template::TT3::Test 'data_text' print data_text(); # hello world __DATA__ hello world __END__ This part is ignored. We can put any editor variables/flags here # Local Variables: # mode: perl # perl-indent-level: 4 # indent-tabs-mode: nil # End: # # vim: expandtab shiftwidth=4:
The text is cached internally so that you can call data_text()
as many
times as you like and get the same text back each time.
Calls data_text() to read the text from the DATA section and then splits it into a number of tests.
The subroutine looks for special command lines embedded in the text,
appearing at the start of a line and surrounded by --
character
sequences. For example:
this is ignored -- start -- -- test number one -- This is the input -- expect -- This is the expected output -- end -- this is ignored
Anything coming before a -- start --
line or after an -- end --
line
is ignored. Each test begins with a -- test --
line which can also
contain a short name for the test, e.g. -- test number one --
. This is
followed by an -- expect --
line and the expected output of the test.
-- test number one -- This is the input -- expect -- This is the expected output -- test number two -- This is the input for test number two -- expect -- This is the expected output of test two
Each 'test' or 'expect' section can be followed any further flags, also
defined in the same way. The -- skip --
flag can be set, for example,
to temporarily skip a test.
-- test number one -- -- skip -- This is the input -- expect -- This is the expected output
This subroutine is the default test handler used by test_expect() .
Used to mark the start of a new test. An option test name may follow.
-- test -- input text... ... -- test hello world -- input text... ...
Used to mark the start of the expected output for a test.
-- test hello world -- Hello [% name or 'World' %] -- expect -- Hello World
Used to skip over a test. Should appear after the opening test directive.
-- test hello world -- -- skip -- Hello [% name or 'World' %] -- expect -- Hello World
Used to conditionally run a test if a variable is set to a true value.
For example, if a test depends on Some::Random::Module
being available
then you can set the has_srm
variable to 0
or 1
.
eval "use Some::Random::Module"; $HAS_SRM = $@ ? 0 : 1; test_expect( vars => { has_srm => $HAS_SRM, } );
Then your tests can use the only has_srm
guard clause:
-- test Some::Random::Module -- -- only has_srm -- Test input -- expect -- Test output
This can be added after the expect directive to indicate that the expected output should be template-processed before comparing it to the output generated from the test input. This can be used to expand values in the output that depend on certain conditions. For example, if the output generated from the test input depends on a language variable, then we can pre-process the expected output to insert the correct warning message.
-- test Example test -- [% warning_message %] -- expect -- -- process -- [% IF lang =='en' -%] DANGER! DANGER! [% ELSIF lang == 'de' -%] ACHTUNG! ACHTUNG! [% END %]
Andy Wardley http://wardley.org/