[Templates-cvs] cvs commit: TT3/t context.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Wed, 01 Dec 2004 10:46:48 +0000
cvs 04/12/01 10:46:48
Modified: t context.t
Log:
* added tests for nested contexts
Revision Changes Path
1.3 +146 -234 TT3/t/context.t
Index: context.t
===================================================================
RCS file: /template-toolkit/TT3/t/context.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- context.t 2004/11/26 12:33:30 1.2
+++ context.t 2004/12/01 10:46:48 1.3
@@ -9,7 +9,7 @@
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
-# $Id: context.t,v 1.2 2004/11/26 12:33:30 abw Exp $
+# $Id: context.t,v 1.3 2004/12/01 10:46:48 abw Exp $
#
#========================================================================
@@ -18,7 +18,7 @@
use lib qw( ./lib ../lib );
use Template::Context;
-use Template::Test tests => 19;
+use Template::Test tests => 58;
my $DEBUG =
$Template::Context::DEBUG =
@@ -44,10 +44,8 @@
bar => 'The bar thing',
};
-$context = $pkg->new({
- name => 'outer',
- things => $things,
-}) || die $pkg->error();
+$context = $pkg->new( things => $things )
+ || die $pkg->error();
ok( $context, 'created a context with things' );
@@ -65,22 +63,121 @@
#------------------------------------------------------------------------
-# find()
+# find() from a hash
#------------------------------------------------------------------------
my $foo = $context->find( things => 'foo' )
|| die $context->error();
+
+is( $context->find( things => 'foo' ), $things->{ foo } );
+ok( ! defined $context->find( things => 'waz' ), 'no waz thing' );
+
+
+#------------------------------------------------------------------------
+# find() from provider subroutine
+#------------------------------------------------------------------------
+
+my $numbers = {
+ pi => 3.14,
+ e => 2.718,
+};
+
+sub numbers {
+ my ($self, $name, %opts) = @_;
+ return $self->error($opts{ barf }) if $opts{ barf };
+ return $numbers->{ $name }
+ || $self->decline("$name: not found");
+}
+
+$context = $pkg->new({
+ numbers => \&numbers,
+});
+
+ok( $context, 'created a provider sub context' );
+is( $context->find( numbers => 'pi' ), 3.14, 'got pi from sub' );
+is( $context->find( numbers => 'e' ), 2.718, 'got e from sub' );
+ok( ! defined $context->find( numbers => 'phi' ), 'no phi' );
+
+
+#------------------------------------------------------------------------
+# find() from a provider object
+#------------------------------------------------------------------------
+
+package My::Provider;
+use base qw( Template::Base );
+
+sub init {
+ my ($self, $config) = @_;
+ $self->{ items } = $config;
+ return $self;
+}
+
+sub fetch {
+ my ($self, $context, $name, %opts) = @_;
+ return $self->error($opts{ barf }) if $opts{ barf };
+ return $self->{ items }->{ $name }
+ || $self->decline("$name: not found");
+}
-is( $foo, $things->{ foo }, 'got foo thing back' );
+package main;
+my $animals = {
+ dog => 'canine',
+ cat => 'feline',
+};
+
+my $drinks = {
+ beer => 'guinness',
+ malt => 'laphroig',
+};
+
+my $zoologist = My::Provider->new($animals);
+my $bartender = My::Provider->new($drinks);
+
+$context = $pkg->new({
+ name => 'inner',
+ animals => $zoologist,
+ drinks => $bartender,
+}) || die $pkg->error();
+
+ok( $context, 'created a provider object context' );
+is( $context->find( animals => 'cat' ), 'feline', 'cat is feline' );
+is( $context->find( animals => 'dog' ), 'canine', 'dog is canine' );
+is( $context->find( drinks => 'beer' ), 'guinness', 'guinness is a tasty beer' );
+is( $context->find( drinks => 'malt' ), 'laphroig', 'laphroig is a malt whisky' );
+
+ok( ! defined $context->find( animals => 'camel' ), 'no camel (shame!)' );
+ok( ! defined $context->find( drinks => 'cider' ), 'no cider' );
+
+
#------------------------------------------------------------------------
-# TODO
-# test provider with fetch() method, provider with get() method, simple
-# subroutine, and lists of the above
+# find() from a list of all three of the above.
#------------------------------------------------------------------------
+$context = $pkg->new({
+ things => $things,
+ numbers => \&numbers,
+ animals => $zoologist,
+ drinks => $bartender,
+ anything => [ $things, \&numbers, $zoologist, $bartender ],
+}) || die $pkg->error();
+
+ok( $context, 'created a combined object context' );
+
+# fetch explicit resource
+is( $context->find( things => 'foo' ), 'The foo thing', 'got combo foo' );
+is( $context->find( numbers => 'pi' ), 3.14, 'got combo pi' );
+is( $context->find( animals => 'cat' ), 'feline', 'got combo cat' );
+is( $context->find( drinks => 'beer' ), 'guinness', 'got combo beer' );
+
+# fetch any resource
+is( $context->find( anything => 'foo' ), 'The foo thing', 'got anything foo' );
+is( $context->find( anything => 'pi' ), 3.14, 'got anything pi' );
+is( $context->find( anything => 'cat' ), 'feline', 'got anything cat' );
+is( $context->find( anything => 'beer' ), 'guinness', 'got anything beer' );
+
#------------------------------------------------------------------------
# resource()
#------------------------------------------------------------------------
@@ -145,256 +242,71 @@
# TODO: store()
#------------------------------------------------------------------------
+ok( 1, 'TODO: test store() method' );
-#------------------------------------------------------------------------
-# TODO: nested contexts
-#------------------------------------------------------------------------
-__END__
-my ($outer, $inner, $template);
-# outer context has simple hash of templates
+#------------------------------------------------------------------------
+# attach() and detach() test nested contexts
+#------------------------------------------------------------------------
-$outer = $pkg->new({
- name => 'outer',
- component => {
- templates => {
- foo => 'the foo template',
- bar => 'the bar template',
- },
- variables => {
- a => 10,
- b => 20,
- },
- resources => {
- templates => $templates,
- },
- compilers => {
- tt2 => $compiler,
- default => 'tt2',
- },
+my $outer = $pkg->new({
+ numbers => {
+ x => 10,
+ y => 20,
},
}) || die $pkg->error();
ok( $outer, 'created outer context' );
-
-$template = $outer->search( templates => 'foo' )
- || die $outer->error();
-
-is( $template, 'the foo template', 'got foo template' );
+is( $outer->find( numbers => 'x' ), 10, 'outer x is 10' );
-$template = $outer->search( templates => 'bar' )
- || die $outer->error();
-
-is( $template, 'the bar template', 'got bar template' );
-
-is( $outer->search( variables => 'a' ), 10, 'a is 10' );
-
-
-#------------------------------------------------------------------------
-# define some provider subroutines and an object
-#------------------------------------------------------------------------
-
-my $numbers = {
- pi => 3.14,
- e => 2.718,
-};
-
-my $words = {
- black => 'dark',
- white => 'light',
-};
-
-my $animals = {
- dog => 'canine',
- cat => 'feline',
-};
-
-my $drinks = {
- beer => 'guinness',
- malt => 'laphroig',
-};
-
-sub numbers {
- my ($self, $name, %opts) = @_;
- return $self->error($opts{ barf }) if $opts{ barf };
- return $numbers->{ $name }
- || $self->decline("$name: not found");
-}
-
-sub words {
- my ($self, $name, %opts) = @_;
- return $self->error($opts{ barf }) if $opts{ barf };
- return $words->{ $name }
- || $self->decline("$name: not found");
-}
-
-package My::Provider;
-use base qw( Template::Base );
-
-sub init {
- my ($self, $config) = @_;
- $self->{ items } = $config;
- return $self;
-}
-
-sub fetch {
- my ($self, $name, %opts) = @_;
- return $self->error($opts{ barf }) if $opts{ barf };
- return $self->{ items }->{ $name }
- || $self->decline("$name: not found");
-}
-
-sub decline {
- my $self = shift;
- $self->error(@_) if @_;
- return 0;
-}
-
-
-
-#------------------------------------------------------------------------
-# inner context has list of hashes and provider subroutines
-#------------------------------------------------------------------------
-
-package main;
-
-$inner = $pkg->new({
- caller => $outer,
- name => 'inner',
- component => {
- templates => [
- {
- wam => 'the wam template',
- bam => 'the bam template',
- },
- \&numbers,
- My::Provider->new($animals),
- {
- dizzy => 'delilah',
- wizzy => 'william',
- },
- \&words,
- My::Provider->new($drinks),
- ],
+my $inner = $pkg->new({
+ numbers => {
+ y => 30,
+ z => 40,
},
}) || die $pkg->error();
ok( $inner, 'created inner context' );
-
-
-#------------------------------------------------------------------------
-# check that we can search() everything
-#------------------------------------------------------------------------
-
-# these come from the outer context
-
-is( $inner->search( templates => 'foo' ),
- 'the foo template', 'got foo template' );
-
-is( $inner->search( templates => 'bar' ),
- 'the bar template', 'got bar template' );
-
-# these come from the first hash in the inner context
-
-is( $inner->search( templates => 'wam' ),
- 'the wam template', 'got wam template' );
-
-is( $inner->search( templates => 'bam' ),
- 'the bam template', 'got bam template' );
-# these come from the numbers() subroutine
+# this should cache the known locations of numbers...
+is( $inner->find( numbers => 'y' ), 30, 'y is 30' );
+ok( ! defined $inner->find( numbers => 'x' ), 'no x in inner' );
-is( $inner->search( templates => 'pi' ),
- '3.14', 'pi is 3.14' );
+# ...but the attach() should delete the locations cache...
+ok( $inner->attach($outer), 'attached inner to outer' );
+is( $inner->parent(), $outer, 'inner is attached to outer' );
-is( $inner->search( templates => 'e' ),
- '2.718', 'e is 2.718' );
+# ...and it should be rebuilt again here with the parent in scope
+is( $inner->find( numbers => 'x' ), 10, 'inner x is 10' );
+is( $inner->find( numbers => 'y' ), 30, 'inner y is 30' );
+is( $inner->find( numbers => 'z' ), 40, 'inner z is 40' );
-# these from the animals provider, woof! woof!
+# now detach and check it works as before
+ok( $inner->detach(), 'detached inner context from outer' );
+is( $inner->find( numbers => 'y' ), 30, 'y is 30' );
+ok( ! defined $inner->find( numbers => 'x' ), 'no x in inner after detach' );
-is( $inner->search( templates => 'cat' ),
- 'feline', 'a cat is feline' );
-is( $inner->search( templates => 'dog' ),
- 'canine', 'a dog is canine' );
-
-# these come from the second hash in the inner context
-
-is( $inner->search( templates => 'dizzy' ) || die($inner->error()),
- 'delilah', 'dizzy delilah template' );
-
-$template = $inner->template('dizzy') || die $inner->error();
-ok( $template, 'got dizzy template' );
-my $result = $template->run($context) || die "run failed: ", $template->error();
-
-is( $result, 'delilah', 'dizzy delilah result' );
-
-
-__END__
-is( $inner->template('wizzy'),
- 'william', 'wizzy william' );
-
-# The question is: "How much more black could this be?"...
-is( $inner->template('black'),
- 'dark', 'black is dark' );
-
-# ..and the answer is "None, none more black"
-is( $inner->template('white'),
- 'light', 'white is light' );
-
-# Phew! This is thirsy work. Anyone fancy a quick drink?
-
-is( $inner->template('beer'),
- 'guinness', 'guinness is a nice beer' );
-
-is( $inner->template('malt'),
- 'laphroig', 'laphroig is a nice malt' );
-
-
-#------------------------------------------------------------------------
-# check the locations() method
-#------------------------------------------------------------------------
-
-my $locations = $inner->collect('templates');
-ok( $locations, 'got locations' );
-is( $locations->[0]->{ wam }, 'the wam template',
- 'wam hash located' );
-is( $locations->[1]->(undef, 'pi'), 3.14,
- 'pi subroutine located' );
-is( $locations->[2]->fetch('dog'), 'canine',
- 'dog provider located - woof! woof!' );
-is( $locations->[3]->{ dizzy }, 'delilah',
- 'delilah hash located' );
-is( $locations->[4]->(undef, 'black'), 'dark',
- 'black subroutine located' );
-is( $locations->[5]->fetch('beer'), 'guinness',
- 'beer provider located - yay!' );
-
-
#------------------------------------------------------------------------
-# check the resource() method and local caching
+# child() method for creating a nested context
#------------------------------------------------------------------------
-my $foo = $pkg->new( resources => { wiz => 'wizzle' } );
-my $bar = $pkg->new( resources => { waz => 'wazzle' }, context => $foo );
-my $baz = $pkg->new( resources => { woz => 'wozzle' }, context => $bar );
+my $child = $outer->child({
+ numbers => {
+ y => 50,
+ z => 60,
+ },
+}) || die $pkg->error();
-ok( $foo, 'made foo' );
-ok( $bar, 'made bar' );
-ok( $baz, 'made baz' );
+ok( $child, 'created child context' );
-is( $baz->resource('woz'), 'wozzle', 'got woz resource' );
-is( $baz->{ local }->{ resources }->{ woz }, 'wozzle', 'used wozzle' );
-is( $baz->resource('woz'), 'wozzle', 'got cached woz resource' );
-$baz->{ local }->{ resources }->{ woz } = 'wazzle';
-is( $baz->resource('woz'), 'wazzle', 'got munged cached woz resource' );
+is( $child->find( numbers => 'x' ), 10, 'child x is 10' );
+is( $child->find( numbers => 'y' ), 50, 'child y is 50' );
+is( $child->find( numbers => 'z' ), 60, 'child z is 60' );
-is( $baz->resource('waz'), 'wazzle', 'got waz resource' );
-is( $baz->resource('waz'), 'wazzle', 'got cached waz resource' );
-#$baz->fetch( user => 'abw' ) || die $baz->error();
__END__