Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 191:78a18a2b6266
IMPL::Web::View improvements (unstable)
author | cin |
---|---|
date | Thu, 05 Apr 2012 17:51:51 +0400 |
parents | cd1ff7029a63 |
children | 4d0e1962161c |
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 { | |
188 | 17 TTLoader => typeof IMPL::Web::View::TTLoader, |
18 MProfiler => 'IMPL::Profiler::Memory' | |
183 | 19 }; |
20 | |
188 | 21 sub AssertMemoryLeak { |
22 my $code = shift; | |
23 my $dump = shift; | |
24 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
25 my $data = MProfiler->Monitor($code); |
188 | 26 |
189 | 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 { |
35 $_[0]->GetResourceDir('Resources','TTView'); | |
36 } | |
37 | |
188 | 38 sub CreateLoader { |
183 | 39 my ($this) = @_; |
40 | |
41 my $loader = TTLoader->new( | |
42 { | |
43 INCLUDE_PATH => [ | |
185 | 44 $this->templatesDir |
188 | 45 ], |
46 INTERPOLATE => 1, | |
47 POST_CHOMP => 1, | |
48 ENCODING => 'utf-8' | |
183 | 49 }, |
184 | 50 ext => '.tt', |
51 initializer => 'global.tt' | |
183 | 52 ); |
188 | 53 } |
54 | |
55 test TTLoaderTests => sub { | |
56 my ($this) = @_; | |
57 | |
58 my $loader = $this->CreateLoader(); | |
183 | 59 |
184 | 60 # test the loader to be able to find a desired resource |
61 assert( defined($loader->template('simple') ) ); | |
62 | |
63 # loader should be initialized on demand | |
64 assert( not $loader->isInitialized ); | |
183 | 65 |
184 | 66 # loader should be able to load a document |
67 my $doc = $loader->document('simple'); | |
68 assert(defined $doc); | |
69 | |
70 assert( $loader->isInitialized ); | |
71 assert( $loader->context->stash->get('user') eq 'test_user'); | |
72 | |
73 # document should inherit loader's context | |
74 assert( $doc->context->stash->get('user') eq 'test_user'); | |
188 | 75 |
76 # document should not have 'this' template variable | |
77 assert( not $doc->templateVars('this') ); | |
78 | |
79 assert( $doc->context != $loader->context); # document should have an own context | |
183 | 80 }; |
81 | |
185 | 82 test TTDocumentTests => sub { |
83 my ($this) = @_; | |
188 | 84 my $loader = $this->CreateLoader(); |
185 | 85 |
86 my $doc = $loader->document('simple'); | |
87 | |
88 assert(defined $doc); | |
89 | |
90 assert($doc->nodeName eq 'document'); | |
91 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
188 | 92 assert(not defined $doc->notexists); # nonexisting property |
93 assert($doc->template->version == 10); # static metadata | |
94 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable | |
95 assert($doc->templateVars('user') eq 'test_user'); # global data | |
96 assert($doc->templateVars('templateVar') eq 'initialized by the constructor'); # defined in CTOR block | |
185 | 97 |
98 my $text = $doc->Render(); | |
99 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
100 | |
101 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
102 | |
103 }; | |
104 | |
186 | 105 test TTControlTests => sub { |
106 my ($this) = @_; | |
107 | |
188 | 108 my $loader = $this->CreateLoader(); |
186 | 109 |
110 my $doc = $loader->document('simple'); | |
111 | |
112 assert(defined $doc); | |
113 | |
114 my $factory = $doc->require('My/Org/Panel'); | |
115 | |
116 assert(defined $factory); | |
117 | |
188 | 118 |
119 assert($factory->context->stash != $doc->context->stash); | |
120 | |
186 | 121 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once"); |
122 | |
191 | 123 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } ); |
186 | 124 |
188 | 125 assert(defined $ctl); |
186 | 126 |
127 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information"); | |
128 | |
129 assert($ctl->nodeProperty('visualClass') eq 'simple'); | |
130 | |
131 assert($factory->instances == 1); | |
132 | |
189 | 133 $doc->appendChild($ctl); |
186 | 134 |
191 | 135 assert($doc->templateVars('dojo.require')); |
136 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY'); | |
137 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' ); | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
138 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
139 my $text = $ctl->Render(); |
186 | 140 |
191 | 141 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); |
186 | 142 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); |
143 | |
191 | 144 |
145 | |
146 }; | |
147 | |
148 test TestDocumentLayout => sub { | |
149 my ($this) = @_; | |
150 | |
151 my $loader = $this->CreateLoader(); | |
152 | |
153 my $doc = $loader->document('complex'); | |
154 assert($doc->layout eq 'Layout/default'); | |
155 | |
156 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
157 | |
158 assert($doc->templateVars('dojo.require')->[0]); | |
159 | |
160 my $text = $doc->Render({ data => [qw(one two three)] }); | |
161 | |
162 assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
186 | 163 }; |
164 | |
188 | 165 test TestMemoryLeaks => sub { |
166 my ($this) = @_; | |
167 | |
168 my $loader = $this->CreateLoader(); | |
169 $loader->document('simple'); # force loader initialization | |
170 | |
171 AssertMemoryLeak(sub { | |
172 my $doc = $loader->document('simple'); | |
173 }); | |
174 | |
175 AssertMemoryLeak(sub { | |
176 my $doc = $loader->document('simple'); | |
177 $doc->Render( { self => $doc } ); | |
178 }); | |
179 | |
191 | 180 $loader->template('Layout/default'); |
189 | 181 $loader->template('My/Org/Panel'); |
182 $loader->template('My/Org/TextPreview'); | |
183 AssertMemoryLeak(sub { | |
188 | 184 my $doc = $loader->document('simple'); |
185 my $factory = $doc->require('My/Org/Panel'); | |
189 | 186 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) ); |
188 | 187 }); |
188 | |
189 | 189 $loader->template('complex'); |
190 AssertMemoryLeak(sub { | |
191 my $doc = $loader->document('complex'); | |
192 $doc->Render(); | |
193 },'dump'); | |
194 | |
188 | 195 }; |
196 | |
183 | 197 1; |