Permalink
Browse files

*** empty log message ***

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@7 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent 67b2613 commit b4f721cdaf4fd6cf92b0b8d5b985d16af9ae5f1a @abw committed Jul 4, 2000
Showing with 404 additions and 66 deletions.
  1. +23 −0 t/README
  2. +10 −1 t/block.t
  3. +3 −4 t/cgi.t
  4. +15 −8 t/{compile.t → compile1.t}
  5. +68 −0 t/compile2.t
  6. +64 −0 t/compile3.t
  7. +7 −2 t/config.t
  8. +92 −17 t/context.t
  9. +0 −5 t/document.t
  10. +13 −3 t/evalperl.t
  11. +47 −0 t/exception.t
  12. +1 −3 t/filter.t
  13. +2 −2 t/foreach.t
  14. +47 −18 t/include.t
  15. +2 −2 t/iterator.t
  16. +1 −1 t/provider.t
  17. +3 −0 t/test/src/bar/baz
  18. +3 −0 t/test/src/bar/baz.txt
  19. +3 −0 t/test/src/complex
View
23 t/README
@@ -1,3 +1,26 @@
+base.t Test the Template::Base.pm module.
+binop.t Test binary operators
+block.t Test BLOCK definition
+capture.t Capture directive output and assign to a variable
+cgi.t Test the CGI script
+compile1.t Compile templates to Perl code and save to file
+compile2.t Reload above compiled templates without re-parsing
+compile3.t Test that touching source template causes re-compilation
+config.t Test the Template::Config factory module
+context.t Test the Template::Context module
+datafile.t Test the Datafile plugin (Template::Plugin::Datafile)
+document.t Test the Template::Document module
+evalperl.t Test the evaluation of PERL and RAWPERL blocks
+except.t Test the Template::Exception module
+filter.t Test the FILTER directive and various filters
+foreach.t Test the FOREACH directive
+format.t Test the Format plugin (Template::Plugin::Format)
+include.t Test the INCLUDE directive
+iterator.t
+TODO:
+
+plugins.t to test PLUGIN_BASE, etc.
+
NOTE: this is an older list. Bit of a mess, don't trust anything here.
merged literal.t into vars.t
View
11 t/block.t
@@ -29,6 +29,10 @@ $Template::Test::DEBUG = 0;
my $ttcfg = {
INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
POST_CHOMP => 1,
+ BLOCKS => {
+ block_a => sub { return 'this is block a' },
+ block_b => sub { return 'this is block b' },
+ },
};
test_expect(\*DATA, $ttcfg, &callsign);
@@ -67,5 +71,10 @@ start of blockdef
end of blockdef
This is block 1, defined in blockdef, a is alpha
-
+-- test --
+[% INCLUDE block_a +%]
+[% INCLUDE block_b %]
+-- expect --
+this is block a
+this is block b
View
7 t/cgi.t
@@ -20,25 +20,22 @@ use strict;
use lib qw( ../lib );
use Template;
use Template::Test;
-use Template::Parser;
$^W = 1;
-$Template::Parser::DEBUG = 1;
+#$Template::Parser::DEBUG = 0;
eval "use CGI";
if ($@) {
print "1..0\n";
exit(0);
}
-
test_expect(\*DATA);
__END__
-- test --
[% USE cgi = CGI('id=abw&name=Andy+Wardley') -%]
name: [% cgi.param('name') %]
-name: [% cgi.param('name') %]
[% FOREACH x = cgi.checkbox_group(
name => 'words'
@@ -49,6 +46,8 @@ name: [% cgi.param('name') %]
name: [% cgi.param('name') %]
-- expect --
+name: Andy Wardley
+
<INPUT TYPE="checkbox" NAME="words" VALUE="eenie" CHECKED>eenie
<INPUT TYPE="checkbox" NAME="words" VALUE="meenie" CHECKED>meenie
<INPUT TYPE="checkbox" NAME="words" VALUE="minie">minie
View
23 t/compile.t → t/compile1.t
@@ -1,6 +1,6 @@
#============================================================= -*-perl-*-
#
-# t/compile.t
+# t/compile1.t
#
# Test the facility for the Template::Provider to maintain a persistance
# cache of compiled templates by writing generated Perl code to files.
@@ -20,23 +20,30 @@
use strict;
use lib qw( ./lib ../lib );
use Template::Test;
-
$^W = 1;
-$Template::Test::DEBUG = 0;
-$Template::Config::DEBUG = 0;
-$Template::Provider::DEBUG = 0;
-my $DEBUG = 0;
+
+# declare extra tests to follow test_expect();
+$Template::Test::EXTRA = 2;
# script may be being run in distribution root or 't' directory
-my $dir = -d 't' ? 't/test/src' : 'test/src';
+my $dir = -d 't' ? 't/test/src' : 'test/src';
my $ttcfg = {
POST_CHOMP => 1,
INCLUDE_PATH => $dir,
COMPILE_EXT => '.ttc',
};
+# delete any existing files
+foreach my $f ( "$dir/foo.ttc", "$dir/complex.ttc" ) {
+ ok( unlink($f) ) if -f $f;
+}
+
test_expect(\*DATA, $ttcfg);
+# $EXTRA tests
+ok( -f "$dir/foo.ttc" );
+ok( -f "$dir/complex.ttc" );
+
__DATA__
-- test --
@@ -46,7 +53,7 @@ __DATA__
Error: [% error.type %] - [% error.info %]
[% END %]
-- expect --
-This is the foo file
+This is the foo file, a is
-- test --
[% META author => 'abw' version => 3.14 %]
View
68 t/compile2.t
@@ -0,0 +1,68 @@
+#============================================================= -*-perl-*-
+#
+# t/compile2.t
+#
+# Test that the compiled template files written by compile1.t can be
+# loaded and used.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( ./lib ../lib );
+use Template::Test;
+$^W = 1;
+
+# script may be being run in distribution root or 't' directory
+my $dir = -d 't' ? 't/test/src' : 'test/src';
+my $ttcfg = {
+ POST_CHOMP => 1,
+ INCLUDE_PATH => $dir,
+ COMPILE_EXT => '.ttc',
+};
+
+# check compiled template files exist
+ok( -f "$dir/foo.ttc" );
+ok( -f "$dir/complex.ttc" );
+
+# we're going to hack on the foo.ttc file to change some key text.
+# this way we can tell that the template was loaded from the compiled
+# version and not the source.
+
+open(FOO, "$dir/foo.ttc") || die "$dir/foo.ttc: $!\n";
+local $/ = undef;
+my $foo = <FOO>;
+close(FOO);
+
+$foo =~ s/the foo file/the hacked foo file/;
+open(FOO, "> $dir/foo.ttc") || die "$dir/foo.ttc: $!\n";
+print FOO $foo;
+close(FOO);
+
+test_expect(\*DATA, $ttcfg);
+
+
+__DATA__
+-- test --
+[% INCLUDE foo a = 'any value' %]
+-- expect --
+This is the hacked foo file, a is any value
+
+-- test --
+[% META author => 'billg' version => 6.66 %]
+[% INCLUDE complex %]
+-- expect --
+This is the header, title: Yet Another Template Test
+This is a more complex file which includes some BLOCK definitions
+This is the footer, author: billg, version: 6.66
+
+
View
64 t/compile3.t
@@ -0,0 +1,64 @@
+#============================================================= -*-perl-*-
+#
+# t/compile3.t
+#
+# Last test in the compile<n>.t trilogy. Checks that modifications
+# to a source template result in a re-compilation of the template.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( ./lib ../lib );
+use Template::Test;
+$^W = 1;
+
+# declare extra test to follow test_expect();
+$Template::Test::EXTRA = 1;
+
+# script may be being run in distribution root or 't' directory
+my $dir = -d 't' ? 't/test/src' : 'test/src';
+my $ttcfg = {
+ POST_CHOMP => 1,
+ INCLUDE_PATH => $dir,
+ COMPILE_EXT => '.ttc',
+};
+
+my $file = "$dir/complex";
+
+# check compiled template file exists and save modification time
+ok( -f "$file.ttc" );
+my $mod = (stat(_))[9];
+
+# sleep for a couple of seconds to ensure clock has ticked
+sleep(2);
+
+# append a harmless newline to the end of the source file to change
+# its modification time
+open(FOO, ">>$file") || die "$file: $!\n";
+print FOO "\n";
+close(FOO);
+
+test_expect(\*DATA, $ttcfg);
+
+ok( (stat($file))[9] > $mod );
+
+__DATA__
+-- test --
+[% META author => 'albert' version => 'emc2' %]
+[% INCLUDE complex %]
+-- expect --
+This is the header, title: Yet Another Template Test
+This is a more complex file which includes some BLOCK definitions
+This is the footer, author: albert, version: emc2
+
+
View
9 t/config.t
@@ -22,9 +22,9 @@ use vars qw( $DEBUG );
use Template::Test;
use Template::Config;
-ntests(37);
+ntests(38);
$DEBUG = 0;
-$Template::Config::DEBUG = 1;
+$Template::Config::DEBUG = 0;
my $factory = 'Template::Config';
@@ -72,6 +72,11 @@ $provider = $factory->provider({
ok( $provider );
ok( join('...', @{ $provider->{ INCLUDE_PATH } }) eq 'cat...mat' );
+
+# force provider to instantiate a parser and check it uses the correct
+# parameters.
+my $text = 'The cat sat on the mat';
+ok( $provider->fetch(\$text) );
ok( $provider->{ PARSER }->{ CASE } == 1);
ok( $provider->{ PARSER }->{ INTERPOLATE } == 1);
View
109 t/context.t
@@ -20,31 +20,46 @@ use strict;
use lib qw( ./lib ../lib );
use Template::Test;
-#ntests(3);
+$Template::Test::DEBUG = 0;
+ntests(55);
+
+# script may be being run in distribution root or 't' directory
+my $dir = -d 't' ? 't/test' : 'test';
my $tt = Template->new({
- INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
+ INCLUDE_PATH => "$dir/src:$dir/lib",
TRIM => 1,
POST_CHOMP => 1,
});
my $ttperl = Template->new({
- INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
+ INCLUDE_PATH => "$dir/src:$dir/lib",
TRIM => 1,
EVAL_PERL => 1,
POST_CHOMP => 1,
});
+#------------------------------------------------------------------------
+# misc
+#------------------------------------------------------------------------
+
# test we created a context object and check internal values
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() );
+#------------------------------------------------------------------------
+# template()
+#------------------------------------------------------------------------
+
+banner('testing template()');
+
# test we can fetch a template via template()
my $template = $context->template('header');
ok( $template );
@@ -53,7 +68,7 @@ ok( UNIVERSAL::isa($template, 'Template::Document') );
# test that non-existance of a template is reported
$template = $context->template('no_such_template');
ok( ! $template );
-ok( $context->error() eq 'no_such_template: template not found' );
+ok( $context->error() eq 'no_such_template: not found' );
# check that template() returns CODE and Template::Document refs intact
my $code = sub { return "this is a hard-coded template" };
@@ -100,20 +115,80 @@ $context->reset();
ok( ! $context->template('some_block_1') );
ok( ! $context->template('some_block_2') );
-#test_expect(\*DATA, $tt, &callsign);
+#------------------------------------------------------------------------
+# plugin()
+#------------------------------------------------------------------------
+
+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 );
+
+$plugin = $context->plugin('no_such_plugin');
+ok( ! $plugin );
+ok( $context->error() eq 'no_such_plugin: plugin not found' );
+
+#------------------------------------------------------------------------
+# filter()
+#------------------------------------------------------------------------
+
+banner('testing filter()');
+
+my $filter = $context->filter('html');
+ok( $filter );
+ok( ref $filter eq 'CODE' );
+ok( &$filter('<input/>') eq '&lt;input/&gt;' );
+
+$filter = $context->filter('replace', [ 'foo', 'bar' ]);
+ok( $filter );
+ok( ref $filter eq 'CODE' );
+ok( &$filter('this is foo, so it is') eq 'this is bar, so it is' );
+
+# check filter got cached
+$filter = $context->filter('replace');
+ok( $filter );
+ok( ref $filter eq 'CODE' );
+ok( &$filter('this is foo, so it is') eq 'this is bar, so it is' );
+
+
+#------------------------------------------------------------------------
+# include() and process()
+#------------------------------------------------------------------------
+
+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' );
+
+# check stash hasn't been altered
+ok( $stash->get('a') eq 'alpha' );
+
+$text = $context->process('baz');
+ok( $text eq 'This is the baz file, a: alpha' );
-__DATA__
+# check stash *has* been altered
+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' );
-__END__
-Methods:
- template() - ok
- plugin()
- filter()
- process()
- include()
- throw()
- catch()
- localise()/delocalise()
- visit()/leave()/reset - ok
View
5 t/document.t
@@ -66,11 +66,6 @@ ok( &{ $doc->block } eq 'some output' );
ok( &{ $doc->blocks->{ foo } } eq 'the foo block' );
ok( &{ $doc->blocks->{ bar } } eq 'the bar block' );
-
-#------------------------------------------------------------------------
-# test metadata access via 'template' variable
-#------------------------------------------------------------------------
-
test_expect(\*DATA);
__END__
View
16 t/evalperl.t
@@ -2,7 +2,17 @@
#
# t/evalperl.t
#
-# Test templates compiled to perl code.
+# Test the evaluation of PERL and RAWPERL blocks.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
#
#========================================================================
@@ -11,8 +21,8 @@ use lib qw( ./lib ../lib );
use Template::Test;
$^W = 1;
-$Template::Parser::DEBUG = 1;
-$Template::Context::DEBUG = 1;
+#$Template::Parser::DEBUG = 0;
+#$Template::Context::DEBUG = 0;
my $tt_no_perl = Template->new({
INTERPOLATE => 1,
View
47 t/exception.t
@@ -0,0 +1,47 @@
+#============================================================= -*-perl-*-
+#
+# t/except.t
+#
+# Test the Template::Exception module.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( ./lib ../lib );
+use Template::Test;
+use Template::Exception;
+
+my $text = 'the current output buffer';
+
+my $e1 = Template::Exception->new('e1.type', 'e1.info');
+my $e2 = Template::Exception->new('e2.type', 'e2.info', \$text);
+
+ok( $e1 );
+ok( $e2 );
+ok( $e1->type() eq 'e1.type' );
+ok( $e2->info() eq 'e2.info' );
+
+my @ti = $e1->type_info();
+ok( $ti[0] eq 'e1.type' );
+ok( $ti[1] eq 'e1.info' );
+
+ok( $e2->as_string() eq 'e2.type error - e2.info' );
+ok( $e2->text() eq 'the current output buffer' );
+
+my $prepend = 'text to prepend ';
+$e2->text(\$prepend);
+ok( $e2->text() eq 'text to prepend the current output buffer' );
+
+my @handlers = ('something', 'e2', 'e1.type');
+ok( $e1->select_handler(@handlers) eq 'e1.type' );
+ok( $e2->select_handler(@handlers) eq 'e2' );
View
4 t/filter.t
@@ -252,9 +252,7 @@ The cat...
[% global.blocktext FILTER truncate %]
-- expect --
-The cat sat on the mat
-
-Mary ...
+The cat...
-- test --
[% "foo..." FILTER repeat(5) %]
View
4 t/foreach.t
@@ -22,8 +22,8 @@ use Template qw( :status );
use Template::Test;
$^W = 1;
-$Template::Test::DEBUG = 0;
-$Template::Parser::DEBUG = 0;
+#$Template::Test::DEBUG = 0;
+#$Template::Parser::DEBUG = 0;
my ($a, $b, $c, $d, $l, $o, $r, $u, $w ) =
qw( alpha bravo charlie delta lima oscar romeo uncle whisky );
View
65 t/include.t
@@ -23,8 +23,8 @@ use Template;
use Template::Test;
$^W = 1;
-$Template::Test::DEBUG = 0;
-$Template::Context::DEBUG = 0;
+#$Template::Test::DEBUG = 0;
+#$Template::Context::DEBUG = 0;
# sample data
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m,
@@ -33,7 +33,7 @@ my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m,
juliet kilo lima mike november oscar papa quebec romeo
sierra tango umbrella victor whisky x-ray yankee zulu );
-my $params = {
+my $replace = {
'a' => $a,
'b' => $b,
'c' => {
@@ -49,23 +49,27 @@ my $params = {
't' => $t,
};
+# script may be being run in distribution root or 't' directory
+my $dir = -d 't' ? 't/test' : 'test';
my $tproc = Template->new({
- INTERPOLATE => 1,
- CACHE_DIR => '/tmp/tt',
- INCLUDE_PATH => [ qw( t/test/src test/src ) ],
- RESET_BLOCKS => 0,
+ INTERPOLATE => 1,
+ INCLUDE_PATH => "$dir/src:$dir/lib",
+ TRIM => 1,
+ AUTO_RESET => 0,
});
-test_expect(\*DATA, $tproc, $params);
+
+my $tt_reset = Template->new({
+ INTERPOLATE => 1,
+ INCLUDE_PATH => "$dir/src:$dir/lib",
+ TRIM => 1,
+});
+
+test_expect(\*DATA, [ default => $tproc, reset => $tt_reset ], $replace);
__DATA__
+-- test --
[% a %]
-[% BLOCK first_block -%]
-this is my first block, a is set to '[% a %]'
-[%- END -%]
-[% BLOCK second_block; DEFAULT b = 99 m = 98 -%]
-this is my second block, a is initially set to '[% a %]' and
-then set to [% a = s %]'[% a %]' b is $b m is $m
-[%- END -%]
+[% PROCESS incblock -%]
[% b %]
-- expect --
alpha
@@ -105,11 +109,11 @@ then set to 'sierra' b is golf m is 97
alpha
-- test --
-FOO: [% INCLUDE foo -%]
+FOO: [% INCLUDE foo +%]
FOO: [% INCLUDE foo a = b -%]
-- expect --
-FOO: This is foo a is alpha
-FOO: This is foo a is bravo
+FOO: This is the foo file, a is alpha
+FOO: This is the foo file, a is bravo
-- test --
GOLF: [% INCLUDE $c.f.g %]
@@ -156,3 +160,28 @@ End
Block defined...
This is bamboozle
End
+
+
+# test that BLOCK definitions get AUTO_RESET (i.e. cleared) by default
+-- test --
+-- use reset --
+[% a %]
+[% PROCESS incblock -%]
+[% INCLUDE first_block %]
+[% INCLUDE second_block %]
+[% b %]
+-- expect --
+alpha
+this is my first block, a is set to 'alpha'
+this is my second block, a is initially set to 'alpha' and
+then set to 'sierra' b is bravo m is 98
+bravo
+
+-- test --
+[% TRY %]
+[% INCLUDE first_block %]
+[% CATCH file %]
+ERROR: [% error.info %]
+[% END %]
+-- expect --
+ERROR: first_block: not found
View
4 t/iterator.t
@@ -23,8 +23,8 @@ use Template::Test;
use Template::Iterator;
$^W = 1;
-$Template::Parser::DEBUG = 0;
-$Template::Test::DEBUG = 0;
+#$Template::Parser::DEBUG = 0;
+#$Template::Test::DEBUG = 0;
my $data = [ qw( foo bar baz qux wiz woz waz ) ];
my $vars = {
View
2 t/provider.t
@@ -36,7 +36,7 @@ my $dir = -d 't' ? 't/test/src' : 'test/src';
my $file = 'foo';
my $relfile = "./$dir/$file";
my $absfile = abs_path($dir) . '/' . $file;
-my $newfile = "$dir/bar";
+my $newfile = "$dir/foobar";
my $vars = {
file => $file,
relfile => $relfile,
View
3 t/test/src/bar/baz
@@ -0,0 +1,3 @@
+[% DEFAULT word = 'qux' -%]
+This is file baz
+The word is '[% word %]'
View
3 t/test/src/bar/baz.txt
@@ -0,0 +1,3 @@
+[% DEFAULT time = 'now' -%]
+[% INCLUDE bar/baz %]
+The time is $time
View
3 t/test/src/complex
@@ -19,3 +19,6 @@ This is the footer, author: [% template.author %], version: [% template.version
+
+
+

0 comments on commit b4f721c

Please sign in to comment.