# HG changeset patch # User cin # Date 1333030935 -14400 # Node ID ae8072f2f2a33f0c01a9d1ed77ca9b5fe8d9e352 # Parent 7525ea9a071a01028409ef4adaf6d4a6e64c89de IMPL::Web::View::TTDocument tests, fixes diff -r 7525ea9a071a -r ae8072f2f2a3 Lib/IMPL/Web/View/TTControl.pm --- a/Lib/IMPL/Web/View/TTControl.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/Lib/IMPL/Web/View/TTControl.pm Thu Mar 29 18:22:15 2012 +0400 @@ -47,7 +47,19 @@ return $this->context->process( $body, { this => $this } ); } else { return ""; - } + } +} + +sub AUTOLOAD { + our $AUTOLOAD; + + my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; + + return if $method eq 'DESTROY'; + + my $res = $_[0]->template->$method(); + + return defined($res) ? $res : $_[0]->context->stash->get($method); } diff -r 7525ea9a071a -r ae8072f2f2a3 Lib/IMPL/Web/View/TTDocument.pm --- a/Lib/IMPL/Web/View/TTDocument.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/Lib/IMPL/Web/View/TTDocument.pm Thu Mar 29 18:22:15 2012 +0400 @@ -74,7 +74,7 @@ sub Render { my ($this,$param) = @_; - my $output = $this->context->process($this->template, {this => $this} ); + my $output = $this->context->process($this->template, {this => $this, document => $this} ); if ($this->layout) { $output = $this->context->include($this->layout,{ content => $output }); @@ -131,12 +131,24 @@ Каждый документ имеет свое собственное пространство имен, которое может быть вложенным в некоторое внешнее, указанное при создании документа. +=head2 Порядок обработки документа + +=over + +=item 1 Создается документ при помощи метода C + +=item 1 При создании документа (в конструкторе), происходит выполнение блока C + +=item 1 При вызове метода C устанавливаются переменные C, C +и шаблон обрабатывается при помощи метода C контекста документа. + +=back =head2 Загрузка элемента управления =over -=item 1 C +=item 1 C =item 1 Загружает шаблон C diff -r 7525ea9a071a -r ae8072f2f2a3 Lib/IMPL/Web/View/TTLoader.pm --- a/Lib/IMPL/Web/View/TTLoader.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/Lib/IMPL/Web/View/TTLoader.pm Thu Mar 29 18:22:15 2012 +0400 @@ -77,6 +77,8 @@ sub _appendExt { my ($this,$name) = @_; + return $name unless $this->ext; + if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { return $name; } else { diff -r 7525ea9a071a -r ae8072f2f2a3 Lib/IMPL/template.pm --- a/Lib/IMPL/template.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/Lib/IMPL/template.pm Thu Mar 29 18:22:15 2012 +0400 @@ -106,6 +106,15 @@ $this->items->AddLast( $this->ItemType->new( key => $key, value => $value ) ); } +package main; + +use IMPL::require { + TFoo => 'Some::Package::Foo', + TBar => 'Some::Package::Bar' +}; + +my $TCol = spec MyCollection(TFoo, TBar); + =end code =head1 DESCRIPTION diff -r 7525ea9a071a -r ae8072f2f2a3 _test/Resources/TTView.Output/simple.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/_test/Resources/TTView.Output/simple.txt Thu Mar 29 18:22:15 2012 +0400 @@ -0,0 +1,1 @@ +test_user - Документ 1 \ No newline at end of file diff -r 7525ea9a071a -r ae8072f2f2a3 _test/Resources/TTView/simple.tt --- a/_test/Resources/TTView/simple.tt Thu Mar 29 01:54:20 2012 +0400 +++ b/_test/Resources/TTView/simple.tt Thu Mar 29 18:22:15 2012 +0400 @@ -1,2 +1,5 @@ -[% META title = "Документ 1" %] -Текст докуметна $document.title +[% META title = "Документ 1", version = 10 %] +[% BLOCK CTOR; + templateVar = "initialized by the constructor"; +END; %] +$user - $document.title \ No newline at end of file diff -r 7525ea9a071a -r ae8072f2f2a3 _test/Test/Web/TT.pm --- a/_test/Test/Web/TT.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/_test/Test/Web/TT.pm Thu Mar 29 18:22:15 2012 +0400 @@ -6,6 +6,7 @@ use parent qw(IMPL::Test::Unit); use IMPL::Test qw(test failed); use IMPL::Web::TT::Document; + __PACKAGE__->PassThroughArgs; test Creation => sub { diff -r 7525ea9a071a -r ae8072f2f2a3 _test/Test/Web/View.pm --- a/_test/Test/Web/View.pm Thu Mar 29 01:54:20 2012 +0400 +++ b/_test/Test/Web/View.pm Thu Mar 29 18:22:15 2012 +0400 @@ -1,10 +1,13 @@ package Test::Web::View; use strict; use warnings; +use utf8; use parent qw(IMPL::Test::Unit); __PACKAGE__->PassThroughArgs; +use File::Slurp; + use IMPL::Test qw(assert test); use IMPL::Web::View::TTLoader(); @@ -12,13 +15,17 @@ TTLoader => typeof IMPL::Web::View::TTLoader }; -test TemplateLoaderTests => sub { +sub templatesDir { + $_[0]->GetResourceDir('Resources','TTView'); +} + +test TTLoaderTests => sub { my ($this) = @_; my $loader = TTLoader->new( { INCLUDE_PATH => [ - $this->GetResourceDir('Resources','TTView') + $this->templatesDir ] }, ext => '.tt', @@ -42,4 +49,37 @@ assert( $doc->context->stash->get('user') eq 'test_user'); }; +test TTDocumentTests => sub { + my ($this) = @_; + my $loader = TTLoader->new( + { + INCLUDE_PATH => [ + $this->templatesDir + ], + INTERPOLATE => 1, + POST_CHOMP => 1, + ENCODING => 'utf-8' + }, + ext => '.tt', + initializer => 'global.tt' + ); + + my $doc = $loader->document('simple'); + + assert(defined $doc); + + assert($doc->nodeName eq 'document'); + assert(not $doc->can('notexists')); # autoloaded property should be ignored + assert($doc->notexists eq ''); # nonexisting property + assert($doc->version == 10); # static metadata + assert($doc->user eq 'test_user'); # global data + assert($doc->templateVar eq 'initialized by the constructor'); # defined in CTOR block + + my $text = $doc->Render(); + my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); + + assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); + +}; + 1; \ No newline at end of file diff -r 7525ea9a071a -r ae8072f2f2a3 _test/Web.t --- a/_test/Web.t Thu Mar 29 01:54:20 2012 +0400 +++ b/_test/Web.t Thu Mar 29 18:22:15 2012 +0400 @@ -2,6 +2,7 @@ use strict; use lib '../Lib'; use lib '.'; +use utf8; use IMPL::Test qw(run_plan);