changeset 185:ae8072f2f2a3

IMPL::Web::View::TTDocument tests, fixes
author cin
date Thu, 29 Mar 2012 18:22:15 +0400
parents 7525ea9a071a
children 6c0fee769b0c
files Lib/IMPL/Web/View/TTControl.pm Lib/IMPL/Web/View/TTDocument.pm Lib/IMPL/Web/View/TTLoader.pm Lib/IMPL/template.pm _test/Resources/TTView.Output/simple.txt _test/Resources/TTView/simple.tt _test/Test/Web/TT.pm _test/Test/Web/View.pm _test/Web.t
diffstat 9 files changed, 88 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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);
 	
 }
 
--- 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<TTLoader::document()>
+
+=item 1 При создании документа (в конструкторе), происходит выполнение блока C<CTOR>
+
+=item 1 При вызове метода C<TTDocument::Render()> устанавливаются переменные C<this>, C<document>
+и шаблон обрабатывается при помощи метода C<process()> контекста документа.
+
+=back
 
 =head2 Загрузка элемента управления
 
 =over
 
-=item 1 C<document.require(my.org.input)>
+=item 1 C<document.require('my/org/input')>
 
 =item 1 Загружает шаблон C<my/org/input.tt>
 
--- 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 {
--- 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
--- /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
--- 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
--- 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 {
--- 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
--- 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);