Skip to content
Browse files

added leak.t

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@84 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent 3519d37 commit 6ddeb78c2369ca2f9c6ef810d1463fde830bf4a4 @abw committed Mar 22, 2001
Showing with 618 additions and 23 deletions.
  1. +2 −0 t/cgi.t
  2. +126 −0 t/chomp.t
  3. +5 −1 t/datafile.t
  4. +0 −5 t/directive.t
  5. +10 −7 t/directry.t
  6. +13 −0 t/filter.t
  7. +28 −0 t/include.t
  8. +216 −0 t/leak.t
  9. +49 −1 t/object.t
  10. +105 −0 t/plusfile.t
  11. +18 −9 t/provider.t
  12. +29 −0 t/stash.t
  13. +11 −0 t/vars.t
  14. +6 −0 t/vmeth.t
View
2 t/cgi.t
@@ -32,6 +32,7 @@ if ($@) {
exit(0);
}
+
my $cgi = CGI->new('');
$cgi = join("\n", $cgi->checkbox_group(
-name => 'words',
@@ -46,6 +47,7 @@ sub barf {
carp('failed');
}
+
__END__
-- test --
[% USE cgi = CGI('id=abw&name=Andy+Wardley'); global.cgi = cgi -%]
View
126 t/chomp.t
@@ -0,0 +1,126 @@
+#============================================================= -*-perl-*-
+#
+# t/chomp.t
+#
+# Test the PRE_CHOMP and POST_CHOMP options.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2001 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2001 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::Constants qw( :chomp );
+
+$^W = 1;
+
+
+match( CHOMP_NONE, 0 );
+match( CHOMP_ALL, 1 );
+match( CHOMP_COLLAPSE, 2 );
+
+my $tt = [
+ tt_pre_none => Template->new(PRE_CHOMP => CHOMP_NONE),
+ tt_pre_all => Template->new(PRE_CHOMP => CHOMP_ALL),
+ tt_pre_coll => Template->new(PRE_CHOMP => CHOMP_COLLAPSE),
+ tt_post_none => Template->new(POST_CHOMP => CHOMP_NONE),
+ tt_post_all => Template->new(POST_CHOMP => CHOMP_ALL),
+ tt_post_coll => Template->new(POST_CHOMP => CHOMP_COLLAPSE),
+];
+
+test_expect(\*DATA, $tt);
+
+__DATA__
+#------------------------------------------------------------------------
+# tt_pre_none
+#------------------------------------------------------------------------
+-- test --
+begin[% a = 10; b = 20 %]
+ [% a %]
+ [% b %]
+end
+-- expect --
+begin
+ 10
+ 20
+end
+
+#------------------------------------------------------------------------
+# tt_pre_all
+#------------------------------------------------------------------------
+-- test --
+-- use tt_pre_all --
+-- test --
+begin[% a = 10; b = 20 %]
+ [% a %]
+ [% b %]
+end
+-- expect --
+begin1020
+end
+
+#------------------------------------------------------------------------
+# tt_pre_coll
+#------------------------------------------------------------------------
+-- test --
+-- use tt_pre_coll --
+-- test --
+begin[% a = 10; b = 20 %]
+ [% a %]
+ [% b %]
+end
+-- expect --
+begin 10 20
+end
+
+
+#------------------------------------------------------------------------
+# tt_post_none
+#------------------------------------------------------------------------
+-- test --
+-- use tt_post_none --
+begin[% a = 10; b = 20 %]
+ [% a %]
+ [% b %]
+end
+-- expect --
+begin
+ 10
+ 20
+end
+
+#------------------------------------------------------------------------
+# tt_post_all
+#------------------------------------------------------------------------
+-- test --
+-- use tt_post_all --
+-- test --
+begin[% a = 10; b = 20 %]
+ [% a %]
+ [% b %]
+end
+-- expect --
+begin 10 20end
+
+#------------------------------------------------------------------------
+# tt_post_coll
+#------------------------------------------------------------------------
+-- test --
+-- use tt_post_coll --
+-- test --
+begin[% a = 10; b = 20 %]
+[% a %]
+[% b %]
+end
+-- expect --
+begin 10 20 end
+
View
6 t/datafile.t
@@ -63,6 +63,10 @@ Users:
* mop: Marty Proton <mop@cre.canon.co.uk>
* nellb: Nell Browser <nellb@cre.canon.co.uk>
-
+-- test --
+[% USE userlist = datafile(datafile.1, delim = '|') -%]
+size: [% userlist.size %]
+-- expect --
+size: 3
View
5 t/directive.t
@@ -376,8 +376,3 @@ romeo-sierra.
-- expect --
bravo
-
-
-
-
-
View
17 t/directry.t
@@ -76,7 +76,7 @@ Directory error on /no/such/place
[% FOREACH f = d.files -%]
- [% f.name %]
[% END -%]
-[% FOREACH f = d.dirs -%]
+[% FOREACH f = d.dirs; NEXT IF f.name == 'CVS'; -%]
* [% f.name %]
[% END %]
-- expect --
@@ -94,7 +94,7 @@ Directory error on /no/such/place
[% FOREACH f = dir.files -%]
- [% f.name %]
[% END -%]
-[% FOREACH f = dir.dirs -%]
+[% FOREACH f = dir.dirs; NEXT IF f.name == 'CVS'; -%]
[% f.scan -%]
[% INCLUDE dir dir=f FILTER indent(4) -%]
[% END -%]
@@ -117,6 +117,7 @@ Directory error on /no/such/place
[% INCLUDE dir %]
[% BLOCK dir;
FOREACH f = dir.list ;
+ NEXT IF f.name == 'CVS';
IF f.isdir ; -%]
* [% f.name %]
[% f.scan ;
@@ -144,7 +145,7 @@ Directory error on /no/such/place
[% FOREACH f = d.files -%]
- [% f.name %]
[% END -%]
-[% FOREACH f = d.dirs -%]
+[% FOREACH f = d.dirs; NEXT IF f.name == 'CVS'; -%]
* [% f.name %]
[% END %]
-- expect --
@@ -154,13 +155,13 @@ Directory error on /no/such/place
* sub_one
* sub_two
-
-- test --
[% USE dir = Directory(dir, recurse=1, root=cwd) -%]
* [% dir.path %]
[% INCLUDE dir %]
[% BLOCK dir;
FOREACH f = dir.list ;
+ NEXT IF f.name == 'CVS';
IF f.isdir ; -%]
* [% f.name %] => [% f.path %] => [% f.abs %]
[% INCLUDE dir dir=f FILTER indent(4) ;
@@ -188,7 +189,8 @@ Directory error on /no/such/place
[% INCLUDE dir %]
[% BLOCK dir;
FOREACH f = dir.list ;
- IF f.isdir ; -%]
+ NEXT IF f.name == 'CVS';
+ IF f.isdir ; -%]
* [% f.name %] => [% f.home %]
[% INCLUDE dir dir=f FILTER indent(4) ;
ELSE -%]
@@ -237,9 +239,9 @@ xyzfile
f [% item.name %] => [% item.path %]
[% END -%]
-[% BLOCK directory -%]
+[% BLOCK directory; NEXT IF item.name == 'CVS'; -%]
d [% item.name %] => [% item.path %]
-[% item.content(my) | indent -%]
+[% item.content(view) | indent -%]
[% END -%]
[% END -%]
@@ -260,3 +262,4 @@ d dir => [% dir %]
+
View
13 t/filter.t
@@ -266,6 +266,13 @@ All the &lt;tags&gt; should be escaped &amp; protected
The &lt;cat&gt; sat on the &lt;mat&gt;
-- test --
+[% FILTER html %]
+"It isn't what I expected", he replied.
+[% END %]
+-- expect --
+&quot;It isn't what I expected&quot;, he replied.
+
+-- test --
[% FILTER format %]
Hello World!
[% END %]
@@ -806,3 +813,9 @@ on the mat
>> The cat sat
>> on the mat
+-- test --
+[% text = 'The cat sat on the mat';
+ text | indent('> ') | indent('+') %]
+-- expect --
++> The cat sat on the mat
+
View
28 t/include.t
@@ -268,3 +268,31 @@ name: c item: f
-- expect --
title: Hello World
bgcol: #ffffff
+
+-- test --
+[% myhash = {
+ name = 'Tom'
+ item = 'teacup'
+ }
+-%]
+[% INCLUDE myblock
+ name = 'Fred'
+ item = 'fish'
+%]
+[% INCLUDE myblock
+ import=myhash
+%]
+import([% import %])
+[% PROCESS myblock
+ import={ name = 'Tim', item = 'teapot' }
+%]
+import([% import %])
+[% BLOCK myblock %][% name %] has a [% item %][% END %]
+-- expect --
+Fred has a fish
+Tom has a teacup
+import()
+Tim has a teapot
+import()
+
+-- test --
View
216 t/leak.t
@@ -0,0 +1,216 @@
+#============================================================= -*-perl-*-
+#
+# t/leak.t
+#
+# Attempts to detect memory leaks... but fails. That's a Good Thing
+# if it means there are no memory leaks (in this particular aspect)
+# or a Bad Thing if it there are, but we're not smart enough to detect
+# them. :-)
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2001 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2001 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 );
+use Template::Test;
+$^W = 1;
+
+$Template::Test::PRESERVE = 1;
+#$Template::Parser::DEBUG = 1;
+#$Template::Directive::PRETTY = 1;
+
+#------------------------------------------------------------------------
+package Holler;
+use vars qw( $TRACE $PREFIX );
+$TRACE = '';
+$PREFIX = 'Holler:';
+
+sub new {
+ my $class = shift;
+ my $id = shift || '<anon>';
+ my $self = bless \$id, $class;
+ $self->trace("created");
+ return $self;
+}
+
+sub trace {
+ my $self = shift;
+ $TRACE .= "$$self @_\n";
+}
+
+sub clear {
+ $TRACE = '';
+ return '';
+}
+
+sub DESTROY {
+ my $self = shift;
+ $self->trace("destroyed");
+}
+
+#------------------------------------------------------------------------
+package Plugin::Holler;
+use base qw( Template::Plugin );
+
+sub new {
+ my ($class, $context, @args) = @_;
+ bless {
+ context => $context,
+ holler => Holler->new(@args),
+ }, $class;
+}
+
+sub trace {
+ my $self = shift;
+ $self->{ context }->process('trace');
+}
+
+#------------------------------------------------------------------------
+package main;
+
+my $ttcfg = {
+ INCLUDE_PATH => -d 't' ? 't/test/src' : 'test/src',
+ PLUGIN_FACTORY => { holler => 'Plugin::Holler' },
+ EVAL_PERL => 1,
+ BLOCKS => {
+ trace => "TRACE ==[% trace %]==",
+ },
+};
+
+my $ttvars = {
+ holler => sub { Holler->new(@_) },
+ trace => sub { $Holler::TRACE },
+ clear => \&Holler::clear,
+};
+
+test_expect(\*DATA, $ttcfg, $ttvars);
+
+__DATA__
+
+-- test --
+[% a = holler('first'); trace %]
+-- expect --
+first created
+
+-- test --
+[% trace %]
+-- expect --
+first created
+first destroyed
+
+-- test --
+[% BLOCK shout; a = holler('second'); END -%]
+[% clear; PROCESS shout; trace %]
+-- expect --
+second created
+
+-- test --
+[% BLOCK shout; a = holler('third'); END -%]
+[% clear; INCLUDE shout; trace %]
+-- expect --
+third created
+third destroyed
+
+-- test --
+[% MACRO shout BLOCK; a = holler('fourth'); END -%]
+[% clear; shout; trace %]
+-- expect --
+fourth created
+fourth destroyed
+
+-- test --
+[% clear; USE holler('holler plugin'); trace %]
+-- expect --
+holler plugin created
+
+-- test --
+[% BLOCK shout; USE holler('process plugin'); END -%]
+[% clear; PROCESS shout; holler.trace %]
+-- expect --
+TRACE ==process plugin created
+==
+
+-- test --
+[% BLOCK shout; USE holler('include plugin'); END -%]
+[% clear; INCLUDE shout; trace %]
+-- expect --
+include plugin created
+include plugin destroyed
+
+-- test --
+[% MACRO shout BLOCK; USE holler('macro plugin'); END -%]
+[% clear; shout; trace %]
+-- expect --
+macro plugin created
+macro plugin destroyed
+
+-- test --
+[% MACRO shout BLOCK;
+ USE holler('macro plugin');
+ holler.trace;
+ END
+-%]
+[% clear; shout; trace %]
+-- expect --
+TRACE ==macro plugin created
+==macro plugin created
+macro plugin destroyed
+
+-- test --
+[% clear; PROCESS leak1; trace %]
+-- expect --
+<leak1>
+</leak1>
+Hello created
+
+-- test --
+[% clear; INCLUDE leak1; trace %]
+-- expect --
+<leak1>
+</leak1>
+Hello created
+Hello destroyed
+
+-- test --
+[% clear; PROCESS leak2; trace %]
+-- expect --
+<leak2>
+</leak2>
+Goodbye created
+
+-- test --
+[% clear; INCLUDE leak2; trace %]
+-- expect --
+<leak2>
+</leak2>
+Goodbye created
+Goodbye destroyed
+
+-- test --
+[% MACRO leak BLOCK;
+ PROCESS leak1 + leak2;
+ USE holler('macro plugin');
+ END
+-%]
+[% clear; leak; trace %]
+-- expect --
+<leak1>
+</leak1>
+<leak2>
+</leak2>
+Hello created
+Goodbye created
+macro plugin created
+Hello destroyed
+Goodbye destroyed
+macro plugin destroyed
+
View
50 t/object.t
@@ -102,6 +102,24 @@ sub AUTOLOAD {
}
}
+#------------------------------------------------------------------------
+# another object for testing auto-stringification
+#------------------------------------------------------------------------
+
+package Stringy;
+
+use overload '""' => 'stringify';
+
+sub new {
+ my ($class, $text) = @_;
+ bless \$text, $class;
+}
+
+sub stringify {
+ my $self = shift;
+ return "stringified '$$self'";
+}
+
#------------------------------------------------------------------------
# main
@@ -116,7 +134,8 @@ my $objconf = {
};
my $replace = {
- thing => TestObject->new($objconf),
+ thing => TestObject->new($objconf),
+ string => Stringy->new('Test String'),
%{ callsign() },
};
@@ -224,3 +243,32 @@ before mid after
[]
[]
+#------------------------------------------------------------------------
+# test auto-stringification
+#------------------------------------------------------------------------
+
+-- test --
+[% string.stringify %]
+-- expect --
+stringified 'Test String'
+
+-- test --
+[% string %]
+-- expect --
+stringified 'Test String'
+
+-- test --
+[% "-> $string <-" %]
+-- expect --
+-> stringified 'Test String' <-
+
+-- test --
+[% "$string" %]
+-- expect --
+stringified 'Test String'
+
+-- test --
+foo $string bar
+-- expect --
+foo stringified 'Test String' bar
+
View
105 t/plusfile.t
@@ -0,0 +1,105 @@
+#============================================================= -*-perl-*-
+#
+# t/plufile.t
+#
+# Test ability to specify INCLUDE/PROCESS/WRAPPER files in the
+# form "foo+bar+baz".
+#
+# 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;
+use Template::Test;
+use Template::Context;
+$^W = 1;
+
+#$Template::Parser::DEBUG = 1;
+#$Template::Directive::PRETTY = 1;
+$Template::Test::PRESERVE = 1;
+
+my $dir = -d 't' ? 't/test/src' : 'test/src';
+
+test_expect(\*DATA, { INCLUDE_PATH => $dir });
+
+__DATA__
+-- test --
+[% INCLUDE foo %]
+[% BLOCK foo; "This is foo!"; END %]
+-- expect --
+This is foo!
+
+-- test --
+[% INCLUDE foo+bar -%]
+[% BLOCK foo; "This is foo!\n"; END %]
+[% BLOCK bar; "This is bar!\n"; END %]
+-- expect --
+This is foo!
+This is bar!
+
+-- test --
+[% PROCESS foo+bar -%]
+[% BLOCK foo; "This is foo!\n"; END %]
+[% BLOCK bar; "This is bar!\n"; END %]
+-- expect --
+This is foo!
+This is bar!
+
+-- test --
+[% WRAPPER edge + box + indent
+ title = "The Title" -%]
+My content
+[% END -%]
+[% BLOCK indent -%]
+<indent>
+[% content -%]
+</indent>
+[% END -%]
+[% BLOCK box -%]
+<box>
+[% content -%]
+</box>
+[% END -%]
+[% BLOCK edge -%]
+<edge>
+[% content -%]
+</edge>
+[% END -%]
+-- expect --
+<edge>
+<box>
+<indent>
+My content
+</indent>
+</box>
+</edge>
+
+
+-- test --
+[% INSERT foo+bar/baz %]
+-- expect --
+This is the foo file, a is [% a %]
+[% DEFAULT word = 'qux' -%]
+This is file baz
+The word is '[% word %]'
+
+-- test --
+[% file1 = 'foo'
+ file2 = 'bar/baz'
+-%]
+[% INSERT "$file1" + "$file2" %]
+-- expect --
+This is the foo file, a is [% a %]
+[% DEFAULT word = 'qux' -%]
+This is file baz
+The word is '[% word %]'
View
27 t/provider.t
@@ -29,6 +29,10 @@ $Template::Test::DEBUG = 0;
#$Template::Parser::DEBUG = 1;
#$Template::Directive::PRETTY = 1;
+# uncommenting the next line should cause test 43 to fail because
+# the provider doesn't stat the file.
+# $Template::Provider::STAT_TTL = 10;
+
my $DEBUG = 0;
my $factory = 'Template::Config';
@@ -167,7 +171,9 @@ __DATA__
Error: [% error.type %] - [% error.info.split(': ').1 %]
[% END %]
-- expect --
-This is the foo file, a is Error: file - not found
+This is the foo file, a is
+Error: file - not found
+
-- test --
[% TRY %]
@@ -177,11 +183,13 @@ This is the foo file, a is Error: file - not found
Error: [% error.type %] - [% error.info.split(': ').1 %]
[% END %]
-- expect --
-This is the foo file, a is Error: file - not found
+This is the foo file, a is
+Error: file - not found
+
-- test --
[% TRY %]
-[% INSERT foo %]
+[% INSERT foo -%]
[% INSERT $absfile %]
[% CATCH file %]
Error: [% error %]
@@ -192,15 +200,14 @@ Error: [% error %]
This is the foo file, a is [% a %]
Error: file error - [* absfile *]: not found
-
#------------------------------------------------------------------------
-- test --
-- use ttrel --
[% TRY %]
[% INCLUDE $relfile %]
[% INCLUDE foo %]
-[% CATCH file +%]
+[% CATCH file -%]
Error: [% error.type %] - [% error.info %]
[% END %]
-- expect --
@@ -209,15 +216,16 @@ Error: file - foo: not found
-- test --
[% TRY %]
-[% INCLUDE $relfile %]
+[% INCLUDE $relfile -%]
[% INCLUDE $absfile %]
-[% CATCH file +%]
+[% CATCH file %]
Error: [% error.type %] - [% error.info.split(': ').1 %]
[% END %]
-- expect --
This is the foo file, a is
Error: file - absolute paths are not allowed (set ABSOLUTE option)
+
-- test --
foo: [% TRY; INSERT foo; CATCH; "$error\n"; END %]
rel: [% TRY; INSERT $relfile; CATCH; "$error\n"; END %]
@@ -236,7 +244,7 @@ abs: file error - [* absfile *]: absolute paths are not allowed (set ABSOLUTE op
[% TRY %]
[% INCLUDE $absfile %]
[% INCLUDE foo %]
-[% CATCH file +%]
+[% CATCH file %]
Error: [% error.type %] - [% error.info %]
[% END %]
-- expect --
@@ -247,7 +255,7 @@ Error: file - foo: not found
[% TRY %]
[% INCLUDE $absfile %]
[% INCLUDE $relfile %]
-[% CATCH file +%]
+[% CATCH file %]
Error: [% error.type %] - [% error.info.split(': ').1 %]
[% END %]
-- expect --
@@ -267,6 +275,7 @@ rel: file error - [* relfile *]: relative paths are not allowed (set RELATIVE op
abs: This is the foo file, a is [% a %]
+
#------------------------------------------------------------------------
# test that files updated on disk are automatically reloaded.
#------------------------------------------------------------------------
View
29 t/stash.t
@@ -20,9 +20,38 @@ use strict;
use lib qw( ../lib );
use Template::Constants qw( :status );
use Template;
+use Template::Stash;
use Template::Test;
$^W = 1;
+my $count = 20;
+my $data = {
+ foo => 10,
+ bar => {
+ baz => 20,
+ },
+ baz => sub {
+ return {
+ boz => ($count += 10),
+ biz => (shift || '<undef>'),
+ };
+ },
+};
+
+my $stash = Template::Stash->new($data);
+
+match( $stash->get('foo'), 10 );
+match( $stash->get([ 'bar', 0, 'baz', 0 ]), 20 );
+match( $stash->get('bar.baz'), 20 );
+match( $stash->get('bar(10).baz'), 20 );
+match( $stash->get('baz.boz'), 30 );
+match( $stash->get('baz.boz'), 40 );
+match( $stash->get('baz.biz'), '<undef>' );
+match( $stash->get('baz(50).biz'), '<undef>' ); # args are ignored
+
+$stash->set( 'bar.buz' => 100 );
+match( $stash->get('bar.buz'), 100 );
+
my $ttlist = [
'default' => Template->new(),
'warn' => Template->new(DEBUG => 1),
View
11 t/vars.t
@@ -512,3 +512,14 @@ Mithrandir, Olorin, Incanus
-- expect --
[]
+-- stop --
+# Stas reported a problem with spacing in expressions but I can't
+# remember enough of the problem to reproduce it
+-- test --
+[% a = 4 -%]
+[% b=6 -%]
+[% c = a + b -%]
+[% d=a+b -%]
+[% c %]/[% d %]
+-- expect --
+10/10
View
6 t/vmeth.t
@@ -278,3 +278,9 @@ fooquxfooquxfoo
-- expect --
foo_bar_baz
+-- test --
+[% var = 'value99' ;
+ var.replace('value', '')
+%]
+-- expect --
+99

0 comments on commit 6ddeb78

Please sign in to comment.
Something went wrong with that request. Please try again.