Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 292:6dc1c369eb71
sync
author | cin |
---|---|
date | Tue, 26 Feb 2013 02:21:03 +0400 |
parents | 85572f512abc |
children | aeeb57a12046 |
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'); |
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 | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
110 assert($doc->context->stash->get('user') eq 'test_user' ); # runtime context should be derived from documentContext |
194 | 111 |
112 my $text = $doc->Render(); | |
113 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
114 | |
115 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
116 | |
185 | 117 }; |
118 | |
191 | 119 test TestDocumentLayout => sub { |
194 | 120 my ($this) = @_; |
121 | |
122 my $loader = $this->CreateLoader(); | |
123 | |
124 my $doc = $loader->document( | |
125 'complex', | |
126 { | |
127 data => [qw(one two three)], | |
128 site => { | |
129 name => 'Test Site' | |
130 } | |
131 } | |
132 ); | |
133 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
134 assert($doc->layout eq 'default'); |
194 | 135 |
136 my $text = $doc->Render(); | |
137 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
287 | 138 my ($text_raw,$expected_raw) = ($text, $expected); |
139 $text_raw =~ s/\s+//g; | |
140 $expected_raw =~ s/\s+//g; | |
141 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
186 | 142 }; |
143 | |
286 | 144 test TestDocumentsIsolation => sub { |
145 my $this = shift; | |
146 | |
147 my $loader = $this->CreateLoader(); | |
148 | |
149 my $doc = $loader->document('simple'); | |
150 | |
151 assert(ref $loader->context->stash->get([ 'dojo', 0, 'require', 0]) eq 'ARRAY'); | |
152 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
153 assert($loader->context->stash != $doc->context->stash); |
286 | 154 |
155 assert(defined $doc); | |
156 | |
157 # only root stash variables can be localized, to avoid modifying dojo we | |
158 # need to replace it completely | |
159 $doc->context->process(\q{ | |
160 [% SET dojo = { require => [] } %] | |
161 [% dojo.require.push('dijit/form/TextBox') %] | |
162 [% SET user = 'dummy guy' %] | |
163 }); | |
164 | |
165 assert($doc->context->stash->get('user') eq 'dummy guy'); | |
166 assert($loader->context->stash->get('user') eq 'test_user'); | |
167 assertarray($doc->context->stash->get([ 'dojo', 0, 'require', 0]),['dijit/form/TextBox']); | |
168 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
169 | |
170 my $text = $doc->Render(); | |
171 | |
287 | 172 my $doc2 = $loader->document('complex'); |
286 | 173 |
174 assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
287 | 175 |
176 # This document has a layout ehich will replace 'dojo' global variable. | |
177 # The layout contains INIT block which runs first in the context of the | |
178 # document, then RenderContent is called and then the layout is applied | |
179 $doc2->Render(); | |
180 | |
181 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
182 | |
183 # TODO: to be able to rendered multiple times, Render shouldn't affect the context of the document | |
184 #assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
286 | 185 }; |
186 | |
188 | 187 test TestMemoryLeaks => sub { |
194 | 188 my ($this) = @_; |
189 | |
190 my $loader = $this->CreateLoader(); | |
191 $loader->document('simple'); # force loader initialization | |
192 | |
193 AssertMemoryLeak(sub { | |
194 my $doc = $loader->document('simple'); | |
289 | 195 },'dump'); |
194 | 196 |
197 AssertMemoryLeak(sub { | |
198 my $doc = $loader->document('simple'); | |
199 $doc->Render( { self => $doc } ); | |
289 | 200 },'dump'); |
194 | 201 |
202 $loader->template('Layout/default'); | |
203 $loader->template('My/Org/Panel'); | |
204 $loader->template('My/Org/TextPreview'); | |
205 | |
206 $loader->template('complex'); | |
207 AssertMemoryLeak(sub { | |
208 my $doc = $loader->document('complex'); | |
209 $doc->Render(); | |
210 },'dump'); | |
211 | |
188 | 212 }; |
213 | |
183 | 214 1; |