NAME

Top Close Open

Template::TT3::Test - useful functions for writing test scripts

SYNOPSIS

Top Close Open
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

DESCRIPTION

Top Close Open

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.

EXPORTED SUBROUTINES

Top Close Open

The following subroutine is exported by default, in addition to those exported by the Badger::Test base class.

test_expect(\%params)

Top Close Open

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

vars

Top Close Open

A reference to a hash array of template variables.

config

Top Close Open

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.

engine

Top Close Open

A reference to a Template engine which will be used to process the source templates in the tests.

engines

Top Close Open

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.

handler

Top Close Open

A reference to a subroutine responsible for running each tests. This defaults to the test_handler() subroutine.

tests

Top Close Open

A reference to a list of tests. This defaults to the tests returned by the data_tests() subroutine.

step

Top Close Open

When set to a true value this will cause the test handler to pause before each test and prompt the user to hit RETURN.

EXPORTABLE SUBROUTINES

Top Close Open

The following subroutine are exported on demand, in addition to those exported by the Badger::Test base class.

data_text()

Top Close Open

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.

data_tests()

Top Close Open

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

callsign()

Top Close Open

Returns a reference to a hash array mapping letters of the alphabet to their corresponding words in the phonetic alphabet.

print callsign->{a};        # alpha

INTERNAL SUBROUTINES

Top Close Open

test_handler()

Top Close Open

This subroutine is the default test handler used by test_expect() .

TEST DIRECTIVES

Top Close Open

test

Top Close Open

Used to mark the start of a new test. An option test name may follow.

-- test --
input text...
...

-- test hello world --
input text...
...

expect

Top Close Open

Used to mark the start of the expected output for a test.

-- test hello world --
Hello [% name or 'World' %]
-- expect --
Hello World

skip

Top Close Open

Used to skip over a test. Should appear after the opening test directive.

-- test hello world --
-- skip --
Hello [% name or 'World' %]
-- expect --
Hello World

only

Top Close Open

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

process

Top Close Open

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 %]

AUTHOR

Top Close Open

Andy Wardley http://wardley.org/

COPYRIGHT

Top Close Open

Copyright (C) 1996-2008 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


http://tt3.template-toolkit.org/docs/Template/TT3/Test.pm last modified 2009-12-23 14:56:09