Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 281:a8dbddf491dd
refactoring
*IMPL::declare reset abstractProps static property after implementing these props
*IMPL::Serialization: fixed bug with multiple text parts inside one node
*IMPL::Web::Handler::TTView: removed obsolete property 'location' from 'view' template variable
author | cin |
---|---|
date | Mon, 11 Feb 2013 00:58:22 +0400 |
parents | c6d0f889ef87 |
children | d357b5d85d25 |
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; |
188 | 14 use IMPL::Test qw(assert 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 | |
188 | 189 test TestMemoryLeaks => sub { |
194 | 190 my ($this) = @_; |
191 | |
192 my $loader = $this->CreateLoader(); | |
193 $loader->document('simple'); # force loader initialization | |
194 | |
195 AssertMemoryLeak(sub { | |
196 my $doc = $loader->document('simple'); | |
197 }); | |
198 | |
199 AssertMemoryLeak(sub { | |
200 my $doc = $loader->document('simple'); | |
201 $doc->Render( { self => $doc } ); | |
202 }); | |
203 | |
204 $loader->template('Layout/default'); | |
205 $loader->template('My/Org/Panel'); | |
206 $loader->template('My/Org/TextPreview'); | |
207 AssertMemoryLeak(sub { | |
208 my $doc = $loader->document('simple'); | |
238 | 209 my $factory = $doc->RequireControl('My/Org/Panel'); |
210 my $ctl = $doc->childNodes($factory->new('information', { visualClass => 'complex' }) ); | |
263 | 211 },'dump'); |
194 | 212 |
213 $loader->template('complex'); | |
214 AssertMemoryLeak(sub { | |
215 my $doc = $loader->document('complex'); | |
216 $doc->Render(); | |
217 },'dump'); | |
218 | |
188 | 219 }; |
220 | |
183 | 221 1; |