[Templates-cvs] cvs commit: TT3/t base.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Tue, 23 Mar 2004 10:46:46 +0000
cvs 04/03/23 10:46:46
Modified: t base.t
Log:
* updated base.t for Template::Base module
Revision Changes Path
1.8 +90 -15 TT3/t/base.t
Index: base.t
===================================================================
RCS file: /template-toolkit/TT3/t/base.t,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- base.t 2004/01/30 13:59:51 1.7
+++ base.t 2004/03/23 10:46:46 1.8
@@ -2,14 +2,14 @@
#
# t/base.t
#
-# Test the Template::TT3::Base.pm module.
+# Test the Template::Base.pm 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: base.t,v 1.7 2004/01/30 13:59:51 abw Exp $
+# $Id: base.t,v 1.8 2004/03/23 10:46:46 abw Exp $
#
#========================================================================
@@ -17,10 +17,10 @@
use warnings;
use lib qw( ./lib ../lib );
-use Template::TT3::Base;
-use Test::More tests => 62;
+use Template::Base;
+use Test::More tests => 76;
-$Template::TT3::Base::DEBUG = grep(/^--?d(ebug)?/, @ARGV);
+$Template::Base::DEBUG = grep(/^--?d(ebug)?/, @ARGV);
my ($pkg, $obj);
@@ -30,7 +30,7 @@
#------------------------------------------------------------------------
# instantiate a base class object and test error reporting/returning
-$pkg = 'Template::TT3::Base';
+$pkg = 'Template::Base';
$obj = $pkg->new() || die $pkg->error();
ok( $obj, 'created a base class object' );
@@ -87,7 +87,7 @@
#------------------------------------------------------------------------
package Template::Test::Warning;
-use base qw( Template::TT3::Base );
+use base qw( Template::Base );
use vars qw( $WARNING );
package main;
@@ -116,7 +116,7 @@
#------------------------------------------------------------------------
package Template::Test::Fail;
-use base qw( Template::TT3::Base );
+use base qw( Template::Base );
use vars qw( $ERROR $WARNING );
sub init {
@@ -139,7 +139,7 @@
#------------------------------------------------------------------------
package Template::Test::Name;
-use base qw( Template::TT3::Base );
+use base qw( Template::Base );
use vars qw( $ERROR );
sub init {
@@ -180,7 +180,7 @@
#------------------------------------------------------------------------
package Foo;
-use base qw( Template::TT3::Base );
+use base qw( Template::Base );
use vars qw( $FOO $BAR );
$FOO = 'Foo Var';
$BAR = 'Foo Bar';
@@ -224,12 +224,14 @@
#------------------------------------------------------------------------
package Base;
-use base qw( Template::TT3::Base );
+use base qw( Template::Base );
sub init {
my ($self, $config) = @_;
- $self->{ name } = $config->{ name } || '<anon>';
- $self->{ text } = $config->{ text } || '<none>';
+ return $self->error($config->{ error })
+ if $config->{ error };
+ $self->{ name } = $config->{ name } || '<anon>';
+ $self->{ text } ||= $config->{ text } || '<none>';
return $self;
}
@@ -237,16 +239,89 @@
sub text { return $_[0]->{ text } }
package main;
+
my $baz = Base->new( name => 'baz', text => 'hello' )
|| die Base->error();
ok( $baz, 'created base baz object' );
is( $baz->name(), 'baz', 'baz name correct' );
is( $baz->text(), 'hello', 'baz text correct' );
-my $bam = $baz->new( name => 'bam', text => 'goodbye' );
+my $bam = $baz->new( name => 'bam' );
ok( $bam, 'created base bam object' );
is( $bam->name(), 'bam', 'bam name correct' );
-is( $bam->text(), 'goodbye', 'bam text correct' );
+is( $bam->text(), '<none>', 'no bam text' );
+
+my $waz = $baz->clone( name => 'waz' );
+ok( $waz, 'created waz object' );
+is( $waz->name(), 'waz', 'waz name correct' );
+is( $waz->text(), 'hello', 'waz cloned bam text' );
+
+
+#------------------------------------------------------------------------
+# test module() and object() method
+#------------------------------------------------------------------------
+
+package My::Loader;
+use base qw( Template::Base );
+
+sub init {
+ my ($self, $config) = @_;
+ @$self{ keys %$config } = values %$config;
+ return $self;
+}
+
+package My::Wibble;
+use base qw( Template::Base );
+sub wobble { return 'wobble' }
+
+package main;
+
+my $loader = My::Loader->new({
+ exception => 'Template::Exception',
+ wibble => 'My::Wibble',
+ load_wibble => 0,
+ dribble => 'My::Dribble',
+});
+ok( $loader, 'created loader' );
+
+# this should load the Template::Exception module
+is( $loader->module('exception'), 'Template::Exception',
+ 'exception module' );
+
+no warnings;
+ok( defined $Template::Exception::ERROR,
+ 'Template::Exception loaded' );
+use warnings;
+
+# but this won't try and load the My::Wibble module
+is( $loader->module('wibble'), 'My::Wibble',
+ 'wibble module' );
+
+# make sure we get errors reported
+ok( ! defined $loader->module('dribble'), 'no dribble' );
+
+like( $loader->error(), qr[^failed to load My/Dribble.pm: ],
+ 'dribble error' );
+
+my $exception = $loader->object( exception => food => 'no breakfast' )
+ || die $loader->error();
+ok( $exception, 'created exception object' );
+is( $exception->type(), 'food', 'food exception' );
+is( $exception->info(), 'no breakfast', 'no breakfast' );
+
+my $dribble = $loader->object( dribble => 'drobble' );
+ok( ! defined $dribble, 'no dribble object' );
+like( $loader->error(), qr[^failed to load My/Dribble.pm: ],
+ 'no dribble object error' );
+
__END__
+
+# Local Variables:
+# mode: perl
+# perl-indent-level: 4
+# indent-tabs-mode: nil
+# End:
+#
+# vim: expandtab shiftwidth=4: