Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 332:04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
author | cin |
---|---|
date | Sun, 09 Jun 2013 21:48:57 +0400 |
parents | aeeb57a12046 |
children | 608e74bc309f |
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); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
12 use Data::Dumper; |
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 | |
91 assert( $doc->context != $loader->context); # document should have an own context | |
183 | 92 }; |
93 | |
185 | 94 test TTDocumentTests => sub { |
194 | 95 my ($this) = @_; |
96 my $loader = $this->CreateLoader(); | |
97 | |
98 my $doc = $loader->document('simple'); | |
99 | |
100 assert(defined $doc); | |
287 | 101 |
194 | 102 $doc->title('test document'); |
301 | 103 $doc->name('document'); |
194 | 104 |
238 | 105 assert($doc->name eq 'document'); |
194 | 106 assert($doc->title eq 'test document'); |
107 | |
108 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
109 assert(not defined $doc->notexists); # nonexisting property | |
110 assert($doc->template->version == 10); # static metadata | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
111 assert($doc->context->stash->get('user') eq 'test_user' ); # runtime context should be derived from documentContext |
194 | 112 |
113 my $text = $doc->Render(); | |
114 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
115 | |
116 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
117 | |
185 | 118 }; |
119 | |
191 | 120 test TestDocumentLayout => sub { |
194 | 121 my ($this) = @_; |
122 | |
123 my $loader = $this->CreateLoader(); | |
124 | |
125 my $doc = $loader->document( | |
126 'complex', | |
127 { | |
128 data => [qw(one two three)], | |
129 site => { | |
130 name => 'Test Site' | |
131 } | |
132 } | |
133 ); | |
134 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
135 assert($doc->layout eq 'default'); |
194 | 136 |
137 my $text = $doc->Render(); | |
138 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
287 | 139 my ($text_raw,$expected_raw) = ($text, $expected); |
140 $text_raw =~ s/\s+//g; | |
141 $expected_raw =~ s/\s+//g; | |
142 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
186 | 143 }; |
144 | |
301 | 145 test TestControlInheritance => sub { |
146 my ($this) = @_; | |
147 | |
148 my $loader = $this->CreateLoader(); | |
149 my $doc = $loader->document('derived'); | |
150 | |
151 my $text = $doc->Render(); | |
152 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'derived.txt'), binmode => ':utf8' ); | |
153 | |
154 my ($text_raw,$expected_raw) = ($text, $expected); | |
155 $text_raw =~ s/\s+//g; | |
156 $expected_raw =~ s/\s+//g; | |
157 | |
158 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
159 }; | |
160 | |
286 | 161 test TestDocumentsIsolation => sub { |
162 my $this = shift; | |
163 | |
164 my $loader = $this->CreateLoader(); | |
165 | |
166 my $doc = $loader->document('simple'); | |
167 | |
168 assert(ref $loader->context->stash->get([ 'dojo', 0, 'require', 0]) eq 'ARRAY'); | |
169 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
170 assert($loader->context->stash != $doc->context->stash); |
286 | 171 |
172 assert(defined $doc); | |
173 | |
174 # only root stash variables can be localized, to avoid modifying dojo we | |
175 # need to replace it completely | |
176 $doc->context->process(\q{ | |
177 [% SET dojo = { require => [] } %] | |
178 [% dojo.require.push('dijit/form/TextBox') %] | |
179 [% SET user = 'dummy guy' %] | |
180 }); | |
181 | |
182 assert($doc->context->stash->get('user') eq 'dummy guy'); | |
183 assert($loader->context->stash->get('user') eq 'test_user'); | |
184 assertarray($doc->context->stash->get([ 'dojo', 0, 'require', 0]),['dijit/form/TextBox']); | |
185 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
186 | |
187 my $text = $doc->Render(); | |
188 | |
287 | 189 my $doc2 = $loader->document('complex'); |
286 | 190 |
191 assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
287 | 192 |
193 # This document has a layout ehich will replace 'dojo' global variable. | |
194 # The layout contains INIT block which runs first in the context of the | |
195 # document, then RenderContent is called and then the layout is applied | |
196 $doc2->Render(); | |
197 | |
198 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
199 | |
200 # TODO: to be able to rendered multiple times, Render shouldn't affect the context of the document | |
201 #assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
286 | 202 }; |
203 | |
188 | 204 test TestMemoryLeaks => sub { |
194 | 205 my ($this) = @_; |
206 | |
207 my $loader = $this->CreateLoader(); | |
208 $loader->document('simple'); # force loader initialization | |
209 | |
210 AssertMemoryLeak(sub { | |
211 my $doc = $loader->document('simple'); | |
289 | 212 },'dump'); |
194 | 213 |
214 AssertMemoryLeak(sub { | |
215 my $doc = $loader->document('simple'); | |
216 $doc->Render( { self => $doc } ); | |
289 | 217 },'dump'); |
194 | 218 |
219 $loader->template('Layout/default'); | |
220 $loader->template('My/Org/Panel'); | |
221 $loader->template('My/Org/TextPreview'); | |
222 | |
223 $loader->template('complex'); | |
224 AssertMemoryLeak(sub { | |
225 my $doc = $loader->document('complex'); | |
226 $doc->Render(); | |
227 },'dump'); | |
228 | |
188 | 229 }; |
230 | |
183 | 231 1; |