Permalink
Browse files

added Dumper plugin, fixed DOM, misc other fixes

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@50 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent 2e6e6de commit 7a871442a2dcbc99c02f97fbb36871bff49edee8 @abw committed Oct 3, 2000
Showing with 317 additions and 7 deletions.
  1. +1 −0 t/README
  2. +10 −0 t/block.t
  3. +5 −4 t/context.t
  4. +182 −1 t/dom.t
  5. +91 −0 t/dumper.t
  6. +28 −2 t/foreach.t
View
@@ -19,6 +19,7 @@ date.t Date plugin (Template::Plugin::Date)
directive.t Directive layout, chomping, comments, etc.
document.t Template::Document module
dom.t XML::DOM plugin (Template::Plugin::XML::DOM)
+dumper.t Data::Dumper plugin (Template::Plugin::Data::Dumper)
error.t Test that errors are reported back to caller as exceptions
evalperl.t Evaluation of PERL and RAWPERL blocks
exception.t Template::Exception module
View
@@ -25,6 +25,8 @@ use Template::Test;
$^W = 1;
$Template::Test::DEBUG = 0;
+$Template::Parser::DEBUG = 1;
+#$Template::Directive::PRETTY = 1;
my $ttcfg = {
INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
@@ -87,3 +89,11 @@ A long time ago in a galaxy far, far away...
<html><head><title>A New Beginning</title></head><body>
A long time ago in a galaxy far, far away...
</body></html>
+
+-- test --
+[% BLOCK foo:bar %]
+blah
+[% END %]
+[% PROCESS foo:bar %]
+-- expect --
+blah
View
@@ -22,7 +22,7 @@ use Template::Test;
$Template::Test::DEBUG = 0;
-ntests(55);
+ntests(54);
# script may be being run in distribution root or 't' directory
my $dir = -d 't' ? 't/test' : 'test';
@@ -130,9 +130,10 @@ 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' );
+eval {
+ $plugin = $context->plugin('no_such_plugin');
+};
+ok( "$@" eq 'plugin error - no_such_plugin: plugin not found' );
#------------------------------------------------------------------------
# filter()
View
183 t/dom.t
@@ -37,7 +37,96 @@ test_expect(\*DATA, undef, { 'xmlfile' => $file });
__END__
-- test --
-[% USE doc = XML.DOM(xmlfile) -%]
+[% TRY;
+ # old usage style is now deprecated (it was badly broken)
+ USE dom = XML.DOM(xmlfile);
+ CATCH XML;
+ error;
+ END
+%]
+-- expect --
+XML.DOM error - XML::DOM usage has changed - you must now call parse()
+
+-- test --
+[% TRY;
+ # specify a dummy encoding, just to make sure it gets passed as an option
+ USE dom = XML.DOM(ProtocolEncoding = 'ISO-666');
+ doc = dom.parse(xmlfile);
+ CATCH XML.DOM;
+ error.info.split(' /').0;
+ END;
+%]
+
+-- expect --
+failed to parse xml file
+
+-- test --
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(xmlfile) -%]
+[% FOREACH tag = doc.getElementsByTagName('page') -%]
+ * [% tag.href %] [% tag.title %]
+[% END %]
+-- expect --
+ * /foo/bar The Foo Page
+ * /bar/baz The Bar Page
+ * /baz/qux The Baz Page
+
+-- test --
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(file => xmlfile) -%]
+[% FOREACH tag = doc.getElementsByTagName('page') -%]
+ * [% tag.href %] [% tag.title %]
+[% END %]
+-- expect --
+ * /foo/bar The Foo Page
+ * /bar/baz The Bar Page
+ * /baz/qux The Baz Page
+
+-- test --
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(filename => xmlfile) -%]
+[% FOREACH tag = doc.getElementsByTagName('page') -%]
+ * [% tag.href %] [% tag.title %]
+[% END %]
+-- expect --
+ * /foo/bar The Foo Page
+ * /bar/baz The Bar Page
+ * /baz/qux The Baz Page
+
+-- test --
+[% global.xmltext = BLOCK %]
+<website id="webzone1">
+ <section name="alpha" title="The Alpha Zone">
+ <page href="/foo/bar" title="The Foo Page"><msg>Hello World!</msg></page>
+ <page href="/bar/baz" title="The Bar Page"/>
+ <page href="/baz/qux" title="The Baz Page"/>
+ </section>
+</website>
+[% END -%]
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(global.xmltext) -%]
+[% FOREACH tag = doc.getElementsByTagName('page') -%]
+ * [% tag.href %] [% tag.title %]
+[% END %]
+-- expect --
+ * /foo/bar The Foo Page
+ * /bar/baz The Bar Page
+ * /baz/qux The Baz Page
+
+-- test --
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(text => global.xmltext) -%]
+[% FOREACH tag = doc.getElementsByTagName('page') -%]
+ * [% tag.href %] [% tag.title %]
+[% END %]
+-- expect --
+ * /foo/bar The Foo Page
+ * /bar/baz The Bar Page
+ * /baz/qux The Baz Page
+
+-- test --
+[% USE dom = XML.DOM -%]
+[% doc = dom.parse(xml => global.xmltext) -%]
[% FOREACH tag = doc.getElementsByTagName('page') -%]
* [% tag.href %] [% tag.title %]
[% END %]
@@ -46,6 +135,98 @@ __END__
* /bar/baz The Bar Page
* /baz/qux The Baz Page
+-- test --
+[% USE parser = XML.DOM -%]
+[% doc = parser.parse(global.xmltext) -%]
+[% FOREACH node = doc.getElementsByTagName('section') -%]
+[% node.toTemplate %]
+[% END %]
+
+[% BLOCK section -%]
+Section name: [% node.name %] title: [% node.title %]
+[% node.childrenToTemplate -%]
+[% END %]
+
+[% BLOCK page -%]
+<a href="[% node.href %]">[% node.title %]</a>
+[% node.childrenToTemplate -%]
+[% END %]
+
+[% BLOCK msg -%]
+<b>[% node.childrenToTemplate(verbose=1) %]</b>
+[% END %]
+
+
+-- expect --
+Section name: alpha title: The Alpha Zone
+<a href="/foo/bar">The Foo Page</a>
+<b>Hello World!</b>
+<a href="/bar/baz">The Bar Page</a>
+<a href="/baz/qux">The Baz Page</a>
+
+-- test --
+[% xmltext = BLOCK %]
+<xml>
+<section id="a" title="First Section">>
+ <page id="a1" title="page 1">
+ <head><author>Andy Wardley</author></head>
+ <body>
+ This is the first page
+ </body>
+ </page>
+ <page id="a2" title="page 2">
+ This is the second page
+ </page>
+</section>
+<section id="b" title="Second Section">
+ <page id="b1" title="page 1">
+ This is the first page in section b
+ </page>
+ <page id="b2" title="page 2">
+ This is the second page in section b
+ </page>
+</section>
+</xml>
+[% END -%]
+[% USE parser = XML.DOM -%]
+[% doc = parser.parse(xmltext) -%]
+[% node.allChildrenToTemplate(default='anynode')
+ FOREACH node = doc.getChildNodes %]
+[% BLOCK section -%]
+SECTION [% node.id %]: [% node.title %]
+[% children -%]
+END OF SECTION [% node.id %]
+[% END %]
+
+[% BLOCK page -%]
+PAGE: [% node.title %]
+[% node.children -%]
+END OF PAGE
+[% END %]
+[% BLOCK head -%]
+HEADER: [% node.toString; prune %]END_HEADER
+[% END %]
+
+[% BLOCK anynode -%]
+<any>[% node.toString; node.prune %]</any>
+[% END %]
+-- expect --
+SECTION a: First Section
+PAGE: page 1
+HEADER: <head><author>Andy Wardley</author></head>END_HEADER
+<any><body>
+ This is the first page
+ </body></any>
+END OF PAGE
+PAGE: page 2
+END OF PAGE
+END OF SECTION a
+SECTION b: Second Section
+PAGE: page 1
+END OF PAGE
+PAGE: page 2
+END OF PAGE
+END OF SECTION b
View
@@ -0,0 +1,91 @@
+#============================================================= -*-perl-*-
+#
+# t/dumper.t
+#
+# Test the Dumper plugin.
+#
+# Written by Simon Matthews <sam@knowledgepool.com>
+#
+# 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 vars qw( $DEBUG );
+use Template::Test;
+$^W = 1;
+
+my $params = {
+ 'foo' => 'bar',
+ 'baz' => 'boo',
+};
+
+$DEBUG = 0;
+
+test_expect(\*DATA, undef, { params => $params });
+
+#------------------------------------------------------------------------
+
+__DATA__
+[% USE Dumper -%]
+Dumper
+
+-- expect --
+Dumper
+
+-- test --
+[% USE Dumper -%]
+[% Dumper.dump({ foo = 'bar', bar = 'baz'}, 'hello' ) -%]
+
+-- expect --
+$VAR1 = {
+ 'foo' => 'bar',
+ 'bar' => 'baz'
+ };
+$VAR2 = 'hello';
+
+
+-- test --
+[% USE Dumper -%]
+[% Dumper.dump(params) -%]
+
+-- expect --
+$VAR1 = {
+ 'foo' => 'bar',
+ 'baz' => 'boo'
+ };
+
+-- test --
+[% USE Dumper -%]
+[% Dumper.dump_html(params) -%]
+
+-- expect --
+$VAR1 = {<br>
+ 'foo' =&gt; 'bar',<br>
+ 'baz' =&gt; 'boo'<br>
+ };<br>
+
+-- test --
+[% USE dumper(indent=1, pad='> ', varname="frank") -%]
+[% dumper.dump(params) -%]
+
+-- expect --
+> $frank1 = {
+> 'foo' => 'bar',
+> 'baz' => 'boo'
+> };
+
+-- test --
+[% USE dumper(Pad='>> ', Varname="bob") -%]
+[% dumper.dump(params) -%]
+
+-- expect --
+>> $bob1 = {
+>> 'foo' => 'bar',
+>> 'baz' => 'boo'
+>> };
+
View
@@ -67,9 +67,12 @@ my $params = {
one => 'Section One',
two => 'Section Two',
three => 'Section Three',
- four => 'Section Four'
+ four => 'Section Four',
},
-
+ nested => [
+ [ qw( a b c ) ],
+ [ qw( x y z ) ],
+ ],
};
sub format {
@@ -476,3 +479,26 @@ count: 10
-- expect --
<blip>..<blip>..<blip>..
+-- test --
+[% FOREACH outer = nested -%]
+outer start
+[% FOREACH inner = outer -%]
+inner [% inner +%]
+[% "last inner\n" IF loop.last -%]
+[% END %]
+[% "last outer\n" IF loop.last -%]
+[% END %]
+-- expect --
+outer start
+inner a
+inner b
+inner c
+last inner
+outer start
+inner x
+inner y
+inner z
+last inner
+last outer
+
+

0 comments on commit 7a87144

Please sign in to comment.