Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 219:c477f24f1980
sync
author | sergey |
---|---|
date | Tue, 21 Aug 2012 17:13:47 +0400 |
parents | 7a920771fd8e |
children | b8c724f6de36 |
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 |
188 | 13 use IMPL::Test qw(assert test GetCallerSourceLine); |
183 | 14 use IMPL::Web::View::TTLoader(); |
15 | |
16 use constant { | |
194 | 17 TTLoader => typeof IMPL::Web::View::TTLoader, |
18 MProfiler => 'IMPL::Profiler::Memory' | |
183 | 19 }; |
20 | |
188 | 21 sub AssertMemoryLeak { |
194 | 22 my $code = shift; |
23 my $dump = shift; | |
24 | |
25 my $data = MProfiler->Monitor($code); | |
26 | |
27 if ($data->isLeak and $dump) { | |
28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() ); | |
29 } | |
30 | |
31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); | |
188 | 32 } |
33 | |
185 | 34 sub templatesDir { |
194 | 35 $_[0]->GetResourceDir('Resources','TTView'); |
185 | 36 } |
37 | |
188 | 38 sub CreateLoader { |
194 | 39 my ($this) = @_; |
40 | |
41 my $loader = TTLoader->new( | |
42 { | |
43 INCLUDE_PATH => [ | |
44 $this->templatesDir | |
45 ], | |
46 INTERPOLATE => 1, | |
47 POST_CHOMP => 1, | |
48 ENCODING => 'utf-8' | |
49 }, | |
50 ext => '.tt', | |
51 initializer => 'global.tt', | |
52 globals => { | |
53 site => { | |
54 name => 'Test Site' | |
55 }, | |
56 date => { | |
57 now => sub { localtime(time); } | |
58 }, | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
59 dynamic => sub { 'this is a dynamic value' }, |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
60 view => { |
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
61 } |
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 layoutBase => 'Layout' |
194 | 64 ); |
188 | 65 } |
66 | |
67 test TTLoaderTests => sub { | |
194 | 68 my ($this) = @_; |
69 | |
70 my $loader = $this->CreateLoader(); | |
71 | |
72 # test the loader to be able to find a desired resource | |
73 assert( defined($loader->template('simple') ) ); | |
74 | |
75 # loader should be initialized on demand | |
76 assert( not $loader->isInitialized ); | |
77 | |
78 # loader should be able to load a document | |
79 my $doc = $loader->document('simple'); | |
80 assert(defined $doc); | |
81 | |
82 assert( $loader->isInitialized ); | |
83 assert( $loader->context->stash->get('user') eq 'test_user'); | |
84 | |
85 # document should inherit loader's context | |
86 assert( $doc->context->stash->get('user') eq 'test_user'); | |
87 | |
88 # document should not have 'this' template variable | |
89 assert( not $doc->templateVars('this') ); | |
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); | |
101 $doc->title('test document'); | |
102 | |
103 assert($doc->nodeName eq 'document'); | |
104 assert($doc->title eq 'test document'); | |
105 | |
106 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
107 assert(not defined $doc->notexists); # nonexisting property | |
108 assert($doc->template->version == 10); # static metadata | |
109 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable | |
110 assert($doc->templateVars('user') eq 'test_user'); # global data | |
111 assert($doc->templateVars('templateVar') eq ''); # defined in CTOR block, should be local | |
112 assert($doc->templateVars('dynamic') eq 'this is a dynamic value'); | |
113 | |
114 my $text = $doc->Render(); | |
115 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
116 | |
117 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
118 | |
185 | 119 }; |
120 | |
186 | 121 test TTControlTests => sub { |
194 | 122 my ($this) = @_; |
123 | |
124 my $loader = $this->CreateLoader(); | |
125 | |
126 my $doc = $loader->document('simple'); | |
127 | |
128 assert(defined $doc); | |
129 | |
130 my $factory = $doc->require('My/Org/Panel'); | |
131 | |
132 assert(defined $factory); | |
133 | |
134 | |
135 assert($factory->context->stash != $doc->context->stash); | |
136 | |
137 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once"); | |
138 | |
139 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } ); | |
140 | |
141 assert(defined $ctl); | |
142 | |
143 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information"); | |
144 | |
145 assert($ctl->nodeProperty('visualClass') eq 'simple'); | |
146 | |
147 assert($factory->instances == 1); | |
148 | |
149 $doc->appendChild($ctl); | |
150 | |
151 assert($doc->templateVars('dojo.require')); | |
152 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY'); | |
153 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' ); | |
154 | |
155 my $text = $ctl->Render(); | |
156 | |
157 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); | |
158 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
159 | |
160 | |
161 | |
191 | 162 }; |
163 | |
164 test TestDocumentLayout => sub { | |
194 | 165 my ($this) = @_; |
166 | |
167 my $loader = $this->CreateLoader(); | |
168 | |
169 my $doc = $loader->document( | |
170 'complex', | |
171 { | |
172 data => [qw(one two three)], | |
173 site => { | |
174 name => 'Test Site' | |
175 } | |
176 } | |
177 ); | |
178 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
179 assert($doc->layout eq 'default'); |
194 | 180 |
181 assert($doc->templateVars('dojo.require')->[0]); | |
182 | |
183 my $text = $doc->Render(); | |
184 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
|
185 #assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); |
186 | 186 }; |
187 | |
188 | 188 test TestMemoryLeaks => sub { |
194 | 189 my ($this) = @_; |
190 | |
191 my $loader = $this->CreateLoader(); | |
192 $loader->document('simple'); # force loader initialization | |
193 | |
194 AssertMemoryLeak(sub { | |
195 my $doc = $loader->document('simple'); | |
196 }); | |
197 | |
198 AssertMemoryLeak(sub { | |
199 my $doc = $loader->document('simple'); | |
200 $doc->Render( { self => $doc } ); | |
201 }); | |
202 | |
203 $loader->template('Layout/default'); | |
204 $loader->template('My/Org/Panel'); | |
205 $loader->template('My/Org/TextPreview'); | |
206 AssertMemoryLeak(sub { | |
207 my $doc = $loader->document('simple'); | |
208 my $factory = $doc->require('My/Org/Panel'); | |
209 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) ); | |
210 }); | |
211 | |
212 $loader->template('complex'); | |
213 AssertMemoryLeak(sub { | |
214 my $doc = $loader->document('complex'); | |
215 $doc->Render(); | |
216 },'dump'); | |
217 | |
188 | 218 }; |
219 | |
183 | 220 1; |