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
|
|
25 my $data = MProfiler->Monitor($code, sub { $_ =~ m/^IMPL::/} );
|
|
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
|
|
123 my $ctl = $factory->new('information', { visualClass => 'simple' } );
|
|
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
|
189
|
135 my $text = $ctl->Render({ data => ['one','two','hello world']});
|
186
|
136 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
|
|
137
|
|
138 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
|
|
139
|
|
140 };
|
|
141
|
188
|
142 test TestMemoryLeaks => sub {
|
|
143 my ($this) = @_;
|
|
144
|
|
145 my $loader = $this->CreateLoader();
|
|
146 $loader->document('simple'); # force loader initialization
|
|
147
|
|
148 AssertMemoryLeak(sub {
|
|
149 my $doc = $loader->document('simple');
|
|
150 });
|
|
151
|
|
152 AssertMemoryLeak(sub {
|
|
153 my $doc = $loader->document('simple');
|
|
154 $doc->Render( { self => $doc } );
|
|
155 });
|
|
156
|
189
|
157 $loader->template('My/Org/Panel');
|
|
158 $loader->template('My/Org/TextPreview');
|
|
159 AssertMemoryLeak(sub {
|
188
|
160 my $doc = $loader->document('simple');
|
|
161 my $factory = $doc->require('My/Org/Panel');
|
189
|
162 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
|
188
|
163 });
|
|
164
|
189
|
165 $loader->template('complex');
|
|
166 AssertMemoryLeak(sub {
|
|
167 my $doc = $loader->document('complex');
|
|
168 $doc->Render();
|
|
169 },'dump');
|
|
170
|
188
|
171 };
|
|
172
|
183
|
173 1; |