use strict;
use lib qw( ./lib ../lib );
use Template::Test;
use Template::Constants qw( :debug );
my $DEBUG = grep(/^--?d(debug)?$/, @ARGV);
ntests(54);
my $dir = -d 't' ? 't/test' : 'test';
my $tt = Template->new({
INCLUDE_PATH => "$dir/src:$dir/lib",
TRIM => 1,
POST_CHOMP => 1,
DEBUG => $DEBUG ? DEBUG_CONTEXT : 0,
});
my $ttperl = Template->new({
INCLUDE_PATH => "$dir/src:$dir/lib",
TRIM => 1,
EVAL_PERL => 1,
POST_CHOMP => 1,
DEBUG => $DEBUG ? DEBUG_CONTEXT : 0,
});
my $context = $tt->service->context();
ok( $context );
ok( $context eq $tt->context() );
ok( $context->trim() );
ok( ! $context->eval_perl() );
ok( $context = $ttperl->service->context() );
ok( $context->trim() );
ok( $context->eval_perl() );
banner('testing template()');
my $template = $context->template('header');
ok( $template );
ok( UNIVERSAL::isa($template, 'Template::Document') );
eval { $template = $context->template('no_such_template') };
ok( $@ );
ok( "$@" eq 'file error - no_such_template: not found' );
my $code = sub { return "this is a hard-coded template" };
$template = $context->template($code);
ok( $template eq $code );
my $doc = "this is a document";
$doc = bless \$doc, 'Template::Document';
$template = $context->template($doc);
ok( $template eq $doc );
ok( $$doc = 'this is a document' );
my $blocks1 = {
some_block_1 => 'hello',
};
my $blocks2 = {
some_block_2 => 'world',
};
eval { $context->template('some_block_1') };
ok( $@ );
$context->visit('no doc', $blocks1);
ok( $context->template('some_block_1') eq 'hello' );
eval { $context->template('some_block_2') };
ok( $@ );
$context->visit('no doc', $blocks2);
ok( $context->template('some_block_1') eq 'hello' );
ok( $context->template('some_block_2') eq 'world' );
$context->leave();
ok( $context->template('some_block_1') eq 'hello' );
eval { $context->template('some_block_2') };
ok( $@ );
$context->leave();
eval { $context->template('some_block_1') };
ok( $@ );
eval { $context->template('some_block_2') };
ok( $@ );
$context->visit('no doc', $blocks1);
ok( $context->template('some_block_1') eq 'hello' );
eval { $context->template('some_block_2') };
ok( $@ );
$context->visit('no doc', $blocks2);
ok( $context->template('some_block_1') eq 'hello' );
ok( $context->template('some_block_2') eq 'world' );
$context->reset();
eval { $context->template('some_block_1') };
ok( $@ );
eval { $context->template('some_block_2') };
ok( $@ );
banner('testing plugin()');
my $plugin = $context->plugin('Table', [ [1,2,3,4], { rows => 2 } ]);
ok( $plugin );
ok( ref $plugin eq 'Template::Plugin::Table' );
my $row = $plugin->row(0);
ok( $row && ref $row eq 'ARRAY' );
ok( $row->[0] == 1 );
ok( $row->[1] == 3 );
eval {
$plugin = $context->plugin('no_such_plugin');
};
ok( "$@" eq 'plugin error - no_such_plugin: plugin not found' );
banner('testing filter()');
my $filter = $context->filter('html');
ok( $filter );
ok( ref $filter eq 'CODE' );
ok( &$filter('<input/>') eq '<input/>' );
$filter = $context->filter('replace', [ 'foo', 'bar' ], 'repsave');
ok( $filter );
ok( ref $filter eq 'CODE' );
ok( &$filter('this is foo, so it is') eq 'this is bar, so it is' );
$filter = $context->filter('repsave');
ok( $filter );
ok( ref $filter eq 'CODE' );
match( &$filter('this is foo, so it is'), 'this is bar, so it is' );
banner('testing include()');
$context = $tt->context();
ok( $context );
my $stash = $context->stash();
ok( $stash );
$stash->set('a', 'alpha');
ok( $stash->get('a') eq 'alpha' );
my $text = $context->include('baz');
ok( $text eq 'This is the baz file, a: alpha' );
$text = $context->include('baz', { a => 'bravo' });
ok( $text eq 'This is the baz file, a: bravo' );
ok( $stash->get('a') eq 'alpha' );
$text = $context->process('baz');
ok( $text eq 'This is the baz file, a: alpha' );
ok( $stash->get('a') eq 'charlie' );
$text = $context->process('baz', { a => 'bravo' });
ok( $text eq 'This is the baz file, a: bravo' );
ok( $stash->get('a') eq 'charlie' );