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
|
|
27 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}}, $dump ? $data->Dump : () );
|
|
28 }
|
|
29
|
185
|
30 sub templatesDir {
|
|
31 $_[0]->GetResourceDir('Resources','TTView');
|
|
32 }
|
|
33
|
188
|
34 sub CreateLoader {
|
183
|
35 my ($this) = @_;
|
|
36
|
|
37 my $loader = TTLoader->new(
|
|
38 {
|
|
39 INCLUDE_PATH => [
|
185
|
40 $this->templatesDir
|
188
|
41 ],
|
|
42 INTERPOLATE => 1,
|
|
43 POST_CHOMP => 1,
|
|
44 ENCODING => 'utf-8'
|
183
|
45 },
|
184
|
46 ext => '.tt',
|
|
47 initializer => 'global.tt'
|
183
|
48 );
|
188
|
49 }
|
|
50
|
|
51 test TTLoaderTests => sub {
|
|
52 my ($this) = @_;
|
|
53
|
|
54 my $loader = $this->CreateLoader();
|
183
|
55
|
184
|
56 # test the loader to be able to find a desired resource
|
|
57 assert( defined($loader->template('simple') ) );
|
|
58
|
|
59 # loader should be initialized on demand
|
|
60 assert( not $loader->isInitialized );
|
183
|
61
|
184
|
62 # loader should be able to load a document
|
|
63 my $doc = $loader->document('simple');
|
|
64 assert(defined $doc);
|
|
65
|
|
66 assert( $loader->isInitialized );
|
|
67 assert( $loader->context->stash->get('user') eq 'test_user');
|
|
68
|
|
69 # document should inherit loader's context
|
|
70 assert( $doc->context->stash->get('user') eq 'test_user');
|
188
|
71
|
|
72 # document should not have 'this' template variable
|
|
73 assert( not $doc->templateVars('this') );
|
|
74
|
|
75 assert( $doc->context != $loader->context); # document should have an own context
|
183
|
76 };
|
|
77
|
185
|
78 test TTDocumentTests => sub {
|
|
79 my ($this) = @_;
|
188
|
80 my $loader = $this->CreateLoader();
|
185
|
81
|
|
82 my $doc = $loader->document('simple');
|
|
83
|
|
84 assert(defined $doc);
|
|
85
|
|
86 assert($doc->nodeName eq 'document');
|
|
87 assert(not $doc->can('notexists')); # autoloaded property should be ignored
|
188
|
88 assert(not defined $doc->notexists); # nonexisting property
|
|
89 assert($doc->template->version == 10); # static metadata
|
|
90 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable
|
|
91 assert($doc->templateVars('user') eq 'test_user'); # global data
|
|
92 assert($doc->templateVars('templateVar') eq 'initialized by the constructor'); # defined in CTOR block
|
185
|
93
|
|
94 my $text = $doc->Render();
|
|
95 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8');
|
|
96
|
|
97 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected");
|
|
98
|
|
99 };
|
|
100
|
186
|
101 test TTControlTests => sub {
|
|
102 my ($this) = @_;
|
|
103
|
188
|
104 my $loader = $this->CreateLoader();
|
186
|
105
|
|
106 my $doc = $loader->document('simple');
|
|
107
|
|
108 assert(defined $doc);
|
|
109
|
|
110 my $factory = $doc->require('My/Org/Panel');
|
|
111
|
|
112 assert(defined $factory);
|
|
113
|
188
|
114 assert(not $loader->context->stash->get('My.Org.Panel'));
|
|
115
|
|
116 assert($factory->context->stash != $doc->context->stash);
|
|
117
|
186
|
118 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once");
|
|
119
|
|
120 my $ctl = $factory->new('information', { visualClass => 'simple' } );
|
|
121
|
188
|
122 assert(defined $ctl);
|
186
|
123
|
|
124 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information");
|
|
125
|
|
126 assert($ctl->nodeProperty('visualClass') eq 'simple');
|
|
127
|
|
128 assert($factory->instances == 1);
|
|
129
|
188
|
130 assert($doc->templateVars('My.Org.Panel') == $factory);
|
186
|
131
|
|
132 my $text = $ctl->Render();
|
|
133 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
|
|
134
|
|
135 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
|
|
136
|
|
137 };
|
|
138
|
188
|
139 test TestMemoryLeaks => sub {
|
|
140 my ($this) = @_;
|
|
141
|
|
142 my $loader = $this->CreateLoader();
|
|
143 $loader->document('simple'); # force loader initialization
|
|
144
|
|
145 AssertMemoryLeak(sub {
|
|
146 my $doc = $loader->document('simple');
|
|
147 });
|
|
148
|
|
149 AssertMemoryLeak(sub {
|
|
150 my $doc = $loader->document('simple');
|
|
151 $doc->Render( { self => $doc } );
|
|
152 });
|
|
153
|
|
154 AssertMemoryLeak(sub{
|
|
155 my $doc = $loader->document('simple');
|
|
156 my $factory = $doc->require('My/Org/Panel');
|
|
157 #my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
|
|
158 });
|
|
159
|
|
160 };
|
|
161
|
183
|
162 1; |