Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 190:cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
author | cin |
---|---|
date | Wed, 04 Apr 2012 17:51:27 +0400 |
parents | 08015e2803f1 |
children | 78a18a2b6266 |
rev | line source |
---|---|
183 | 1 package Test::Web::View; |
188 | 2 use IMPL::Profiler::Memory; |
183 | 3 use strict; |
4 use warnings; | |
185 | 5 use utf8; |
183 | 6 |
7 use parent qw(IMPL::Test::Unit); | |
8 __PACKAGE__->PassThroughArgs; | |
9 | |
185 | 10 use File::Slurp; |
188 | 11 use Scalar::Util qw(weaken); |
185 | 12 |
188 | 13 use IMPL::Test qw(assert test GetCallerSourceLine); |
183 | 14 use IMPL::Web::View::TTLoader(); |
15 | |
16 use constant { | |
188 | 17 TTLoader => typeof IMPL::Web::View::TTLoader, |
18 MProfiler => 'IMPL::Profiler::Memory' | |
183 | 19 }; |
20 | |
188 | 21 sub AssertMemoryLeak { |
22 my $code = shift; | |
23 my $dump = shift; | |
24 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
25 my $data = MProfiler->Monitor($code); |
188 | 26 |
189 | 27 if ($data->isLeak and $dump) { |
28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() ); | |
29 } | |
30 | |
31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); | |
188 | 32 } |
33 | |
185 | 34 sub templatesDir { |
35 $_[0]->GetResourceDir('Resources','TTView'); | |
36 } | |
37 | |
188 | 38 sub CreateLoader { |
183 | 39 my ($this) = @_; |
40 | |
41 my $loader = TTLoader->new( | |
42 { | |
43 INCLUDE_PATH => [ | |
185 | 44 $this->templatesDir |
188 | 45 ], |
46 INTERPOLATE => 1, | |
47 POST_CHOMP => 1, | |
48 ENCODING => 'utf-8' | |
183 | 49 }, |
184 | 50 ext => '.tt', |
51 initializer => 'global.tt' | |
183 | 52 ); |
188 | 53 } |
54 | |
55 test TTLoaderTests => sub { | |
56 my ($this) = @_; | |
57 | |
58 my $loader = $this->CreateLoader(); | |
183 | 59 |
184 | 60 # test the loader to be able to find a desired resource |
61 assert( defined($loader->template('simple') ) ); | |
62 | |
63 # loader should be initialized on demand | |
64 assert( not $loader->isInitialized ); | |
183 | 65 |
184 | 66 # loader should be able to load a document |
67 my $doc = $loader->document('simple'); | |
68 assert(defined $doc); | |
69 | |
70 assert( $loader->isInitialized ); | |
71 assert( $loader->context->stash->get('user') eq 'test_user'); | |
72 | |
73 # document should inherit loader's context | |
74 assert( $doc->context->stash->get('user') eq 'test_user'); | |
188 | 75 |
76 # document should not have 'this' template variable | |
77 assert( not $doc->templateVars('this') ); | |
78 | |
79 assert( $doc->context != $loader->context); # document should have an own context | |
183 | 80 }; |
81 | |
185 | 82 test TTDocumentTests => sub { |
83 my ($this) = @_; | |
188 | 84 my $loader = $this->CreateLoader(); |
185 | 85 |
86 my $doc = $loader->document('simple'); | |
87 | |
88 assert(defined $doc); | |
89 | |
90 assert($doc->nodeName eq 'document'); | |
91 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
188 | 92 assert(not defined $doc->notexists); # nonexisting property |
93 assert($doc->template->version == 10); # static metadata | |
94 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable | |
95 assert($doc->templateVars('user') eq 'test_user'); # global data | |
96 assert($doc->templateVars('templateVar') eq 'initialized by the constructor'); # defined in CTOR block | |
185 | 97 |
98 my $text = $doc->Render(); | |
99 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
100 | |
101 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
102 | |
103 }; | |
104 | |
186 | 105 test TTControlTests => sub { |
106 my ($this) = @_; | |
107 | |
188 | 108 my $loader = $this->CreateLoader(); |
186 | 109 |
110 my $doc = $loader->document('simple'); | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
111 $doc->templateVars(data => ['one','two','hello world']); |
186 | 112 |
113 assert(defined $doc); | |
114 | |
115 my $factory = $doc->require('My/Org/Panel'); | |
116 | |
117 assert(defined $factory); | |
118 | |
188 | 119 |
120 assert($factory->context->stash != $doc->context->stash); | |
121 | |
186 | 122 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once"); |
123 | |
124 my $ctl = $factory->new('information', { visualClass => 'simple' } ); | |
125 | |
188 | 126 assert(defined $ctl); |
186 | 127 |
128 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information"); | |
129 | |
130 assert($ctl->nodeProperty('visualClass') eq 'simple'); | |
131 | |
132 assert($factory->instances == 1); | |
133 | |
189 | 134 $doc->appendChild($ctl); |
186 | 135 |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
136 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
137 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
138 my $text = $ctl->Render(); |
186 | 139 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); |
140 | |
141 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
142 | |
143 }; | |
144 | |
188 | 145 test TestMemoryLeaks => sub { |
146 my ($this) = @_; | |
147 | |
148 my $loader = $this->CreateLoader(); | |
149 $loader->document('simple'); # force loader initialization | |
150 | |
151 AssertMemoryLeak(sub { | |
152 my $doc = $loader->document('simple'); | |
153 }); | |
154 | |
155 AssertMemoryLeak(sub { | |
156 my $doc = $loader->document('simple'); | |
157 $doc->Render( { self => $doc } ); | |
158 }); | |
159 | |
189 | 160 $loader->template('My/Org/Panel'); |
161 $loader->template('My/Org/TextPreview'); | |
162 AssertMemoryLeak(sub { | |
188 | 163 my $doc = $loader->document('simple'); |
164 my $factory = $doc->require('My/Org/Panel'); | |
189 | 165 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) ); |
188 | 166 }); |
167 | |
189 | 168 $loader->template('complex'); |
169 AssertMemoryLeak(sub { | |
170 my $doc = $loader->document('complex'); | |
171 $doc->Render(); | |
172 },'dump'); | |
173 | |
188 | 174 }; |
175 | |
183 | 176 1; |