Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 286:d357b5d85d25
*TTView refactoring
author | sergey |
---|---|
date | Mon, 18 Feb 2013 14:46:06 +0400 |
parents | c6d0f889ef87 |
children | 2d253e6e4a88 |
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 |
280 | 13 use IMPL::lang; |
286 | 14 use IMPL::Test qw(assert assertarray test GetCallerSourceLine); |
183 | 15 use IMPL::Web::View::TTLoader(); |
16 | |
17 use constant { | |
280 | 18 TTLoader => 'IMPL::Web::View::TTLoader', |
194 | 19 MProfiler => 'IMPL::Profiler::Memory' |
183 | 20 }; |
21 | |
188 | 22 sub AssertMemoryLeak { |
194 | 23 my $code = shift; |
24 my $dump = shift; | |
25 | |
26 my $data = MProfiler->Monitor($code); | |
27 | |
28 if ($data->isLeak and $dump) { | |
29 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() ); | |
30 } | |
31 | |
32 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); | |
188 | 33 } |
34 | |
185 | 35 sub templatesDir { |
194 | 36 $_[0]->GetResourceDir('Resources','TTView'); |
185 | 37 } |
38 | |
188 | 39 sub CreateLoader { |
194 | 40 my ($this) = @_; |
41 | |
42 my $loader = TTLoader->new( | |
43 { | |
44 INCLUDE_PATH => [ | |
45 $this->templatesDir | |
46 ], | |
47 INTERPOLATE => 1, | |
48 POST_CHOMP => 1, | |
49 ENCODING => 'utf-8' | |
50 }, | |
51 ext => '.tt', | |
52 initializer => 'global.tt', | |
53 globals => { | |
54 site => { | |
55 name => 'Test Site' | |
56 }, | |
57 date => { | |
58 now => sub { localtime(time); } | |
59 }, | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
60 dynamic => sub { 'this is a dynamic value' }, |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
61 view => { |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
62 } |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
63 }, |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
64 layoutBase => 'Layout' |
194 | 65 ); |
188 | 66 } |
67 | |
68 test TTLoaderTests => sub { | |
194 | 69 my ($this) = @_; |
70 | |
71 my $loader = $this->CreateLoader(); | |
72 | |
73 # test the loader to be able to find a desired resource | |
74 assert( defined($loader->template('simple') ) ); | |
75 | |
76 # loader should be initialized on demand | |
77 assert( not $loader->isInitialized ); | |
78 | |
79 # loader should be able to load a document | |
80 my $doc = $loader->document('simple'); | |
81 assert(defined $doc); | |
82 | |
83 assert( $loader->isInitialized ); | |
84 assert( $loader->context->stash->get('user') eq 'test_user'); | |
85 | |
86 # document should inherit loader's context | |
87 assert( $doc->context->stash->get('user') eq 'test_user'); | |
88 | |
89 # document should not have 'this' template variable | |
90 assert( not $doc->templateVars('this') ); | |
91 | |
92 assert( $doc->context != $loader->context); # document should have an own context | |
183 | 93 }; |
94 | |
185 | 95 test TTDocumentTests => sub { |
194 | 96 my ($this) = @_; |
97 my $loader = $this->CreateLoader(); | |
98 | |
99 my $doc = $loader->document('simple'); | |
100 | |
101 assert(defined $doc); | |
102 $doc->title('test document'); | |
103 | |
238 | 104 assert($doc->name eq 'document'); |
194 | 105 assert($doc->title eq 'test document'); |
106 | |
107 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
108 assert(not defined $doc->notexists); # nonexisting property | |
109 assert($doc->template->version == 10); # static metadata | |
110 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable | |
111 assert($doc->templateVars('user') eq 'test_user'); # global data | |
112 assert($doc->templateVars('templateVar') eq ''); # defined in CTOR block, should be local | |
113 assert($doc->templateVars('dynamic') eq 'this is a dynamic value'); | |
114 | |
115 my $text = $doc->Render(); | |
116 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
117 | |
118 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
119 | |
185 | 120 }; |
121 | |
186 | 122 test TTControlTests => sub { |
194 | 123 my ($this) = @_; |
124 | |
125 my $loader = $this->CreateLoader(); | |
126 | |
127 my $doc = $loader->document('simple'); | |
128 | |
129 assert(defined $doc); | |
130 | |
238 | 131 my $factory = $doc->RequireControl('My/Org/Panel'); |
194 | 132 |
133 assert(defined $factory); | |
134 | |
263 | 135 # control factory shares document scope to perform an initialization on demand |
136 assert($factory->context->stash == $doc->context->stash); | |
194 | 137 |
238 | 138 assert($factory == $doc->RequireControl('My/Org/Panel'), "Control should be loaded only once"); |
194 | 139 |
140 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } ); | |
141 | |
142 assert(defined $ctl); | |
143 | |
238 | 144 assert($ctl->name eq 'information', "Created control should have a name", "Got: ".$ctl->name, "Expected: information"); |
194 | 145 |
238 | 146 assert($ctl->GetAttribute('visualClass') eq 'simple'); |
194 | 147 |
148 assert($factory->instances == 1); | |
149 | |
238 | 150 $doc->childNodes([$ctl]); |
194 | 151 |
152 assert($doc->templateVars('dojo.require')); | |
153 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY'); | |
154 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' ); | |
155 | |
156 my $text = $ctl->Render(); | |
157 | |
158 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); | |
159 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
160 | |
161 | |
162 | |
191 | 163 }; |
164 | |
165 test TestDocumentLayout => sub { | |
194 | 166 my ($this) = @_; |
167 | |
168 my $loader = $this->CreateLoader(); | |
169 | |
170 my $doc = $loader->document( | |
171 'complex', | |
172 { | |
173 data => [qw(one two three)], | |
174 site => { | |
175 name => 'Test Site' | |
176 } | |
177 } | |
178 ); | |
179 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
180 assert($doc->layout eq 'default'); |
194 | 181 |
182 assert($doc->templateVars('dojo.require')->[0]); | |
183 | |
184 my $text = $doc->Render(); | |
185 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
186 #assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); |
186 | 187 }; |
188 | |
286 | 189 test TestDocumentsIsolation => sub { |
190 my $this = shift; | |
191 | |
192 my $loader = $this->CreateLoader(); | |
193 | |
194 my $doc = $loader->document('simple'); | |
195 | |
196 assert(ref $loader->context->stash->get([ 'dojo', 0, 'require', 0]) eq 'ARRAY'); | |
197 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
198 assert($loader->context->stash != $doc->stash); | |
199 | |
200 assert(defined $doc); | |
201 | |
202 # only root stash variables can be localized, to avoid modifying dojo we | |
203 # need to replace it completely | |
204 $doc->context->process(\q{ | |
205 [% SET dojo = { require => [] } %] | |
206 [% dojo.require.push('dijit/form/TextBox') %] | |
207 [% SET user = 'dummy guy' %] | |
208 }); | |
209 | |
210 assert($doc->context->stash->get('user') eq 'dummy guy'); | |
211 assert($loader->context->stash->get('user') eq 'test_user'); | |
212 assertarray($doc->context->stash->get([ 'dojo', 0, 'require', 0]),['dijit/form/TextBox']); | |
213 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
214 | |
215 my $text = $doc->Render(); | |
216 | |
217 my $doc2 = $loader->document('simple'); | |
218 | |
219 assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
220 }; | |
221 | |
188 | 222 test TestMemoryLeaks => sub { |
194 | 223 my ($this) = @_; |
224 | |
225 my $loader = $this->CreateLoader(); | |
226 $loader->document('simple'); # force loader initialization | |
227 | |
228 AssertMemoryLeak(sub { | |
229 my $doc = $loader->document('simple'); | |
230 }); | |
231 | |
232 AssertMemoryLeak(sub { | |
233 my $doc = $loader->document('simple'); | |
234 $doc->Render( { self => $doc } ); | |
235 }); | |
236 | |
237 $loader->template('Layout/default'); | |
238 $loader->template('My/Org/Panel'); | |
239 $loader->template('My/Org/TextPreview'); | |
240 AssertMemoryLeak(sub { | |
241 my $doc = $loader->document('simple'); | |
238 | 242 my $factory = $doc->RequireControl('My/Org/Panel'); |
243 my $ctl = $doc->childNodes($factory->new('information', { visualClass => 'complex' }) ); | |
263 | 244 },'dump'); |
194 | 245 |
246 $loader->template('complex'); | |
247 AssertMemoryLeak(sub { | |
248 my $doc = $loader->document('complex'); | |
249 $doc->Render(); | |
250 },'dump'); | |
251 | |
188 | 252 }; |
253 | |
183 | 254 1; |