Mercurial > pub > Impl
changeset 184:7525ea9a071a
IMPL::Web::View::TTLoader tests
author | sergey |
---|---|
date | Thu, 29 Mar 2012 01:54:20 +0400 |
parents | 2184fa28b49e |
children | ae8072f2f2a3 |
files | Lib/IMPL/Test/Unit.pm Lib/IMPL/Web/View/TTLoader.pm _test/Resources/TTView/global.tt _test/Resources/TTView/simple.tt _test/Test/Web/View.pm |
diffstat | 5 files changed, 38 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/Lib/IMPL/Test/Unit.pm Wed Mar 28 17:28:51 2012 +0400 +++ b/Lib/IMPL/Test/Unit.pm Thu Mar 29 01:54:20 2012 +0400 @@ -114,14 +114,14 @@ my ($this,@path) = @_; my ($cwd) = map m/(.*)/, File::Spec->rel2abs(File::Spec->curdir()); - return File::Spec->catfile(@path); + return File::Spec->catfile($cwd,@path); } sub GetResourceDir { my ($this,@path) = @_; my ($cwd) = map m/(.*)/, File::Spec->rel2abs(File::Spec->curdir()); - return File::Spec->catdir(@path); + return File::Spec->catdir($cwd,@path); } package IMPL::Test::Unit::TestInfo;
--- a/Lib/IMPL/Web/View/TTLoader.pm Wed Mar 28 17:28:51 2012 +0400 +++ b/Lib/IMPL/Web/View/TTLoader.pm Thu Mar 29 01:54:20 2012 +0400 @@ -26,12 +26,15 @@ sub CTOR { my ($this,$refOpts,%args) = @_; - $this->ext(delete $args{etx}); + $refOpts ||= {}; + + $this->ext(delete $args{ext}); $this->initializer(delete $args{initializer}); $this->options($refOpts); - $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider($refOpts)); + # to aviod cyclic references we need to do a copy of $refOpts + $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } )); $this->context(new Template::Context($refOpts)); } @@ -43,7 +46,7 @@ $this->_init(); - my $opts = { $this->options }; + my $opts = { %{ $this->options } }; $opts->{STASH} = $this->context->stash->clone(); $opts->{LOAD_TEMPLATES} = $this->provider; @@ -62,9 +65,9 @@ my ($tt,$error) = $this->provider->fetch($name); - if ($error == STATUS_DECLINED) { + if (defined $error and $error == STATUS_DECLINED) { die new IMPL::KeyNotFoundException($name); - } elsif ($error == STATUS_ERROR) { + } elsif (defined $error and $error == STATUS_ERROR) { die new IMPL::Exception("Failed to load a template", $name, $tt); } @@ -74,7 +77,6 @@ sub _appendExt { my ($this,$name) = @_; - if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { return $name; } else { @@ -86,13 +88,15 @@ my ($this) = @_; if (!$this->isInitialized) { - $this->isInitialized(1); - if ($this->initializer) { eval { $this->context->process($this->initializer); + }; + if (my $e = $@) { + die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e); } } + $this->isInitialized(1); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/_test/Resources/TTView/global.tt Thu Mar 29 01:54:20 2012 +0400 @@ -0,0 +1,5 @@ +[% META version = 1; + + user = 'test_user'; + +%] \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/_test/Resources/TTView/simple.tt Thu Mar 29 01:54:20 2012 +0400 @@ -0,0 +1,2 @@ +[% META title = "Документ 1" %] +Текст докуметна $document.title
--- a/_test/Test/Web/View.pm Wed Mar 28 17:28:51 2012 +0400 +++ b/_test/Test/Web/View.pm Thu Mar 29 01:54:20 2012 +0400 @@ -18,15 +18,28 @@ my $loader = TTLoader->new( { INCLUDE_PATH => [ - $this->GetResourceDir('Resources') + $this->GetResourceDir('Resources','TTView') ] }, - ext => '.tt' + ext => '.tt', + initializer => 'global.tt' ); - assert( defined(my $tt = $loader->template('simple') ) ); + # test the loader to be able to find a desired resource + assert( defined($loader->template('simple') ) ); + + # loader should be initialized on demand + assert( not $loader->isInitialized ); - $tt; + # loader should be able to load a document + my $doc = $loader->document('simple'); + assert(defined $doc); + + assert( $loader->isInitialized ); + assert( $loader->context->stash->get('user') eq 'test_user'); + + # document should inherit loader's context + assert( $doc->context->stash->get('user') eq 'test_user'); }; 1; \ No newline at end of file