Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 287:2d253e6e4a88
*TTView refactoring
| author | cin |
|---|---|
| date | Tue, 19 Feb 2013 03:05:10 +0400 |
| parents | d357b5d85d25 |
| children | 3a9cfea098dd |
| 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); | |
| 287 | 102 |
| 194 | 103 $doc->title('test document'); |
| 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 | |
| 111 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable | |
| 112 assert($doc->templateVars('user') eq 'test_user'); # global data | |
| 113 assert($doc->templateVars('templateVar') eq ''); # defined in CTOR block, should be local | |
| 114 assert($doc->templateVars('dynamic') eq 'this is a dynamic value'); | |
| 115 | |
| 116 my $text = $doc->Render(); | |
| 117 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
| 118 | |
| 119 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
| 120 | |
| 185 | 121 }; |
| 122 | |
| 186 | 123 test TTControlTests => sub { |
| 194 | 124 my ($this) = @_; |
| 125 | |
| 126 my $loader = $this->CreateLoader(); | |
| 127 | |
| 128 my $doc = $loader->document('simple'); | |
| 129 | |
| 130 assert(defined $doc); | |
| 131 | |
| 238 | 132 my $factory = $doc->RequireControl('My/Org/Panel'); |
| 194 | 133 |
| 134 assert(defined $factory); | |
| 135 | |
| 263 | 136 # control factory shares document scope to perform an initialization on demand |
| 137 assert($factory->context->stash == $doc->context->stash); | |
| 194 | 138 |
| 238 | 139 assert($factory == $doc->RequireControl('My/Org/Panel'), "Control should be loaded only once"); |
| 194 | 140 |
| 141 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } ); | |
| 142 | |
| 143 assert(defined $ctl); | |
| 144 | |
| 238 | 145 assert($ctl->name eq 'information', "Created control should have a name", "Got: ".$ctl->name, "Expected: information"); |
| 194 | 146 |
| 238 | 147 assert($ctl->GetAttribute('visualClass') eq 'simple'); |
| 194 | 148 |
| 149 assert($factory->instances == 1); | |
| 150 | |
| 238 | 151 $doc->childNodes([$ctl]); |
| 194 | 152 |
| 153 assert($doc->templateVars('dojo.require')); | |
| 154 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY'); | |
| 155 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' ); | |
| 156 | |
| 157 my $text = $ctl->Render(); | |
| 158 | |
| 159 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); | |
| 160 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
| 161 | |
| 162 | |
| 163 | |
| 191 | 164 }; |
| 165 | |
| 166 test TestDocumentLayout => sub { | |
| 194 | 167 my ($this) = @_; |
| 168 | |
| 169 my $loader = $this->CreateLoader(); | |
| 170 | |
| 171 my $doc = $loader->document( | |
| 172 'complex', | |
| 173 { | |
| 174 data => [qw(one two three)], | |
| 175 site => { | |
| 176 name => 'Test Site' | |
| 177 } | |
| 178 } | |
| 179 ); | |
| 180 | |
|
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
181 assert($doc->layout eq 'default'); |
| 194 | 182 |
| 183 my $text = $doc->Render(); | |
| 184 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
| 287 | 185 my ($text_raw,$expected_raw) = ($text, $expected); |
| 186 $text_raw =~ s/\s+//g; | |
| 187 $expected_raw =~ s/\s+//g; | |
| 188 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
| 186 | 189 }; |
| 190 | |
| 286 | 191 test TestDocumentsIsolation => sub { |
| 192 my $this = shift; | |
| 193 | |
| 194 my $loader = $this->CreateLoader(); | |
| 195 | |
| 196 my $doc = $loader->document('simple'); | |
| 197 | |
| 198 assert(ref $loader->context->stash->get([ 'dojo', 0, 'require', 0]) eq 'ARRAY'); | |
| 199 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
| 200 assert($loader->context->stash != $doc->stash); | |
| 201 | |
| 202 assert(defined $doc); | |
| 203 | |
| 204 # only root stash variables can be localized, to avoid modifying dojo we | |
| 205 # need to replace it completely | |
| 206 $doc->context->process(\q{ | |
| 207 [% SET dojo = { require => [] } %] | |
| 208 [% dojo.require.push('dijit/form/TextBox') %] | |
| 209 [% SET user = 'dummy guy' %] | |
| 210 }); | |
| 211 | |
| 212 assert($doc->context->stash->get('user') eq 'dummy guy'); | |
| 213 assert($loader->context->stash->get('user') eq 'test_user'); | |
| 214 assertarray($doc->context->stash->get([ 'dojo', 0, 'require', 0]),['dijit/form/TextBox']); | |
| 215 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
| 216 | |
| 217 my $text = $doc->Render(); | |
| 218 | |
| 287 | 219 my $doc2 = $loader->document('complex'); |
| 286 | 220 |
| 221 assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
| 287 | 222 |
| 223 # This document has a layout ehich will replace 'dojo' global variable. | |
| 224 # The layout contains INIT block which runs first in the context of the | |
| 225 # document, then RenderContent is called and then the layout is applied | |
| 226 $doc2->Render(); | |
| 227 | |
| 228 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
| 229 | |
| 230 # TODO: to be able to rendered multiple times, Render shouldn't affect the context of the document | |
| 231 #assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
| 286 | 232 }; |
| 233 | |
| 188 | 234 test TestMemoryLeaks => sub { |
| 194 | 235 my ($this) = @_; |
| 236 | |
| 237 my $loader = $this->CreateLoader(); | |
| 238 $loader->document('simple'); # force loader initialization | |
| 239 | |
| 240 AssertMemoryLeak(sub { | |
| 241 my $doc = $loader->document('simple'); | |
| 242 }); | |
| 243 | |
| 244 AssertMemoryLeak(sub { | |
| 245 my $doc = $loader->document('simple'); | |
| 246 $doc->Render( { self => $doc } ); | |
| 247 }); | |
| 248 | |
| 249 $loader->template('Layout/default'); | |
| 250 $loader->template('My/Org/Panel'); | |
| 251 $loader->template('My/Org/TextPreview'); | |
| 252 AssertMemoryLeak(sub { | |
| 253 my $doc = $loader->document('simple'); | |
| 238 | 254 my $factory = $doc->RequireControl('My/Org/Panel'); |
| 255 my $ctl = $doc->childNodes($factory->new('information', { visualClass => 'complex' }) ); | |
| 263 | 256 },'dump'); |
| 194 | 257 |
| 258 $loader->template('complex'); | |
| 259 AssertMemoryLeak(sub { | |
| 260 my $doc = $loader->document('complex'); | |
| 261 $doc->Render(); | |
| 262 },'dump'); | |
| 263 | |
| 188 | 264 }; |
| 265 | |
| 183 | 266 1; |
