[Templates-cvs] cvs commit: TT3/t/directive interpolate.t tags.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Fri, 12 Nov 2004 18:42:21 +0000
cvs 04/11/12 18:42:21
Added: t/directive interpolate.t tags.t
Log:
* added tests for TAGS and INTERPOLATE directives
Revision Changes Path
1.1 TT3/t/directive/interpolate.t
Index: interpolate.t
===================================================================
#============================================================= -*-perl-*-
#
# t/directive/interpolate.t
#
# Test the Template::Directive::Interpolate module
#
# Written by Andy Wardley <abw@wardley.org>
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id: interpolate.t,v 1.1 2004/11/12 18:42:21 abw Exp $
#
#========================================================================
use strict;
use warnings;
use lib qw( ./lib ../lib ../../lib );
use Template::Handler;
use Template::Parser;
use Template::Scanner;
use Template::Tagset::TT;
use Template::Generator::Debug;
use Template::Directive::Tags;
use Template::Directive::Interpolate;
use Template::Test qw( :all );
plan(32);
our $DEBUG =
$Template::Directive::Tags::DEBUG =
$Template::Directive::Interpolate::DEBUG =
#$Template::Tag::Directive::DEBUG =
$Template::Tag::DEBUG =
$Template::Scanner::DEBUG =
grep(/^--?d(ebug)?/, @ARGV);
my $directives = {
TAGS => 'Template::Directive::Tags',
INTERPOLATE => 'Template::Directive::Interpolate',
};
my $parser = Template::Parser->new({
directives => $directives,
});
my $tagset = Template::Tagset::TT->new({
parser => $parser,
});
my $scanner = Template::Scanner->new({
tags => [ tt => $tagset ],
});
my $handler = Template::Handler->new();
my $text =<<'EOF';
$foo and ${bar} and \$baz
[% INTERPOLATE on %]
$wiz and ${waz} and \$woz
[% INTERPOLATE off %]
$baz and ${buz} and \$bam
[% INTERPOLATE on; INTERPOLATE off INTERPOLATE 1 %]$foo
[% INTERPOLATE 0 %]$bar
EOF
$scanner->scan(\$text, $handler)
|| die $scanner->error();
ok( 1, 'scanned text' );
my $body = $handler->content();
ok( 1, 'returned content' );
print $handler->dump_item($body) if $DEBUG;
is_text_expr( $body->[0], "\$foo and \${bar} and \\\$baz\n",
'interpolate initially off' );
is_text_expr( $body->[1], "\n", 'newline' );
is_get_expr( $body->[2], 'wiz', 'wiz var' );
is_text_expr( $body->[3], ' and ', 'and 1' );
is_get_expr( $body->[4], 'waz', 'waz var' );
is_text_expr( $body->[5], ' and ', 'and 2' );
is_text_expr( $body->[6], '$', 'dollar' );
is_text_expr( $body->[7], "woz\n", 'woz' );
is_text_expr( $body->[8], "\n\$baz and \${buz} and \\\$bam\n", 'baz buz bam' );
is_get_expr( $body->[9], 'foo', 'foo' );
is_text_expr( $body->[10], "\n", 'newline' );
is_text_expr( $body->[11], "\$bar\n", '$bar' );
#is_text_expr( $body->[2], "\nhello\n",
# 'hello' );
#is_text_expr( $body->[3], "\nmore text\n",
# 'more text' );
#is_text_expr( $body->[4], " [% nothing %] [* nada *] <* zip *>\nthe end\n",
# "trailing tags ignored" );
sub is_text_expr {
my ($expr, $text, $message) = @_;
$message ||= $text;
is( $expr->[0], 'text', "$message (text expr)" );
is( ${$expr->[1]}, $text, "$message (text matches)" );
}
sub is_get_expr {
my ($expr, $var, $message) = @_;
$message ||= $text;
is( $expr->[0], 'get', "$message (get)" );
is( $expr->[1]->[0], 'variable', "$message (variable)" );
is( $expr->[1]->[1]->[0]->[0], 'ident', "$message (ident)" );
is( $expr->[1]->[1]->[0]->[1], $var, "$message ($var)" );
}
__END__
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
1.1 TT3/t/directive/tags.t
Index: tags.t
===================================================================
#============================================================= -*-perl-*-
#
# t/directive/tags.t
#
# Test the Template::Directive::Tags module
#
# Written by Andy Wardley <abw@wardley.org>
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id: tags.t,v 1.1 2004/11/12 18:42:21 abw Exp $
#
#========================================================================
use strict;
use warnings;
use lib qw( ./lib ../lib ../../lib );
use Template::Handler;
use Template::Parser;
use Template::Scanner;
use Template::Tagset::TT;
use Template::Generator::Debug;
use Template::Directive::Tags;
use Template::Test qw( :all );
plan(12);
our $DEBUG =
$Template::Directive::Tags::DEBUG =
#$Template::Tag::Directive::DEBUG =
#$Template::Tag::DEBUG =
#$Template::Scanner::DEBUG =
grep(/^--?d(ebug)?/, @ARGV);
my $directives = {
TAGS => 'Template::Directive::Tags',
};
my $parser = Template::Parser->new({
directives => $directives,
});
my $tagset = Template::Tagset::TT->new({
parser => $parser,
});
my $scanner = Template::Scanner->new({
tags => [ tt => $tagset ],
});
my $handler = Template::Handler->new();
my $text =<<EOF;
The cat sat on the mat
[% TAGS star -%]
and shat
[% ignored %]
[* TAGS default *]
hello
[% TAGS <* *> %]
more text
<* TAGS default;
TAGS << >>
TAGS off
=*>
[% nothing %] [* nada *] <* zip *>
the end
EOF
$scanner->scan(\$text, $handler)
|| die $scanner->error();
ok( 1, 'scanned text' );
my $body = $handler->content();
ok( 1, 'returned content' );
print $handler->dump_item($body) if $DEBUG;
is_text_expr( $body->[0], "The cat sat on the mat\n",
'cat sat' );
is_text_expr( $body->[1], "and shat\n[% ignored %]\n",
'and shat' );
is_text_expr( $body->[2], "\nhello\n",
'hello' );
is_text_expr( $body->[3], "\nmore text\n",
'more text' );
is_text_expr( $body->[4], " [% nothing %] [* nada *] <* zip *>\nthe end\n",
"trailing tags ignored" );
sub is_text_expr {
my ($expr, $text, $message) = @_;
$message ||= $text;
is( $expr->[0], 'text', "$message (text expr)" );
is( ${$expr->[1]}, $text, "$message (text matches)" );
}
__END__
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: