annotate _test/Test/Web/View.pm @ 190:cd1ff7029a63

IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
author cin
date Wed, 04 Apr 2012 17:51:27 +0400
parents 08015e2803f1
children 78a18a2b6266
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
1 package Test::Web::View;
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
2 use IMPL::Profiler::Memory;
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
3 use strict;
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
4 use warnings;
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
5 use utf8;
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
6
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
7 use parent qw(IMPL::Test::Unit);
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
8 __PACKAGE__->PassThroughArgs;
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
9
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
10 use File::Slurp;
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
11 use Scalar::Util qw(weaken);
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
12
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
13 use IMPL::Test qw(assert test GetCallerSourceLine);
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
14 use IMPL::Web::View::TTLoader();
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
15
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
16 use constant {
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
17 TTLoader => typeof IMPL::Web::View::TTLoader,
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
18 MProfiler => 'IMPL::Profiler::Memory'
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
19 };
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
20
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
21 sub AssertMemoryLeak {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
22 my $code = shift;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
23 my $dump = shift;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
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
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
26
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
27 if ($data->isLeak and $dump) {
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() );
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
29 }
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
30
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} );
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
32 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
33
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
34 sub templatesDir {
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
35 $_[0]->GetResourceDir('Resources','TTView');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
36 }
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
37
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
38 sub CreateLoader {
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
39 my ($this) = @_;
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
40
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
41 my $loader = TTLoader->new(
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
42 {
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
43 INCLUDE_PATH => [
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
44 $this->templatesDir
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
45 ],
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
46 INTERPOLATE => 1,
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
47 POST_CHOMP => 1,
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
48 ENCODING => 'utf-8'
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
49 },
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
50 ext => '.tt',
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
51 initializer => 'global.tt'
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
52 );
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
53 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
54
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
55 test TTLoaderTests => sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
56 my ($this) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
57
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
58 my $loader = $this->CreateLoader();
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
59
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
60 # test the loader to be able to find a desired resource
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
61 assert( defined($loader->template('simple') ) );
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
62
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
63 # loader should be initialized on demand
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
64 assert( not $loader->isInitialized );
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
65
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
66 # loader should be able to load a document
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
67 my $doc = $loader->document('simple');
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
68 assert(defined $doc);
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
69
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
70 assert( $loader->isInitialized );
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
71 assert( $loader->context->stash->get('user') eq 'test_user');
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
72
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
73 # document should inherit loader's context
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
74 assert( $doc->context->stash->get('user') eq 'test_user');
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
75
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
76 # document should not have 'this' template variable
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
77 assert( not $doc->templateVars('this') );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
78
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
79 assert( $doc->context != $loader->context); # document should have an own context
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
80 };
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
81
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
82 test TTDocumentTests => sub {
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
83 my ($this) = @_;
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
84 my $loader = $this->CreateLoader();
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
85
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
86 my $doc = $loader->document('simple');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
87
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
88 assert(defined $doc);
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
89
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
90 assert($doc->nodeName eq 'document');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
91 assert(not $doc->can('notexists')); # autoloaded property should be ignored
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
92 assert(not defined $doc->notexists); # nonexisting property
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
93 assert($doc->template->version == 10); # static metadata
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
94 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
95 assert($doc->templateVars('user') eq 'test_user'); # global data
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
96 assert($doc->templateVars('templateVar') eq 'initialized by the constructor'); # defined in CTOR block
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
97
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
98 my $text = $doc->Render();
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
99 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
100
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
101 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected");
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
102
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
103 };
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
104
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
105 test TTControlTests => sub {
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
106 my ($this) = @_;
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
107
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
108 my $loader = $this->CreateLoader();
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
109
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
110 my $doc = $loader->document('simple');
190
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
111 $doc->templateVars(data => ['one','two','hello world']);
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
112
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
113 assert(defined $doc);
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
114
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
115 my $factory = $doc->require('My/Org/Panel');
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
116
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
117 assert(defined $factory);
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
118
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
119
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
120 assert($factory->context->stash != $doc->context->stash);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
121
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
122 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once");
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
123
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
124 my $ctl = $factory->new('information', { visualClass => 'simple' } );
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
125
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
126 assert(defined $ctl);
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
127
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
128 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information");
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
129
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
130 assert($ctl->nodeProperty('visualClass') eq 'simple');
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
131
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
132 assert($factory->instances == 1);
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
133
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
134 $doc->appendChild($ctl);
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
135
190
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
136
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
137
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
138 my $text = $ctl->Render();
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
139 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
140
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
141 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
142
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
143 };
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
144
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
145 test TestMemoryLeaks => sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
146 my ($this) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
147
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
148 my $loader = $this->CreateLoader();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
149 $loader->document('simple'); # force loader initialization
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
150
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
151 AssertMemoryLeak(sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
152 my $doc = $loader->document('simple');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
153 });
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
154
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
155 AssertMemoryLeak(sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
156 my $doc = $loader->document('simple');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
157 $doc->Render( { self => $doc } );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
158 });
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
159
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
160 $loader->template('My/Org/Panel');
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
161 $loader->template('My/Org/TextPreview');
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
162 AssertMemoryLeak(sub {
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
163 my $doc = $loader->document('simple');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
164 my $factory = $doc->require('My/Org/Panel');
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
165 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
166 });
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
167
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
168 $loader->template('complex');
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
169 AssertMemoryLeak(sub {
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
170 my $doc = $loader->document('complex');
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
171 $doc->Render();
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
172 },'dump');
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
173
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
174 };
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
175
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
176 1;