annotate _test/Test/Web/View.pm @ 188:029c9610528c

Memory leak tests in IMPL::Web::View
author cin
date Tue, 03 Apr 2012 20:08:42 +0400
parents 6c0fee769b0c
children 08015e2803f1
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
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
25 my $data = MProfiler->Monitor($code, sub { $_ =~ m/^IMPL::/} );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
26
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
27 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}}, $dump ? $data->Dump : () );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
28 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
29
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
30 sub templatesDir {
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
31 $_[0]->GetResourceDir('Resources','TTView');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
32 }
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
33
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
34 sub CreateLoader {
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
35 my ($this) = @_;
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
36
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
37 my $loader = TTLoader->new(
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
38 {
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
39 INCLUDE_PATH => [
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
40 $this->templatesDir
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
41 ],
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
42 INTERPOLATE => 1,
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
43 POST_CHOMP => 1,
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
44 ENCODING => 'utf-8'
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
45 },
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
46 ext => '.tt',
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
47 initializer => 'global.tt'
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
48 );
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
49 }
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
50
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
51 test TTLoaderTests => sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
52 my ($this) = @_;
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 my $loader = $this->CreateLoader();
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
55
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
56 # test the loader to be able to find a desired resource
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
57 assert( defined($loader->template('simple') ) );
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
58
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
59 # loader should be initialized on demand
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
60 assert( not $loader->isInitialized );
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
61
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
62 # loader should be able to load a document
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
63 my $doc = $loader->document('simple');
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
64 assert(defined $doc);
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
65
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
66 assert( $loader->isInitialized );
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
67 assert( $loader->context->stash->get('user') eq 'test_user');
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
68
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
69 # document should inherit loader's context
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 183
diff changeset
70 assert( $doc->context->stash->get('user') eq 'test_user');
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
71
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
72 # document should not have 'this' template variable
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
73 assert( not $doc->templateVars('this') );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
74
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
75 assert( $doc->context != $loader->context); # document should have an own context
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
76 };
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
77
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
78 test TTDocumentTests => sub {
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
79 my ($this) = @_;
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
80 my $loader = $this->CreateLoader();
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
81
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
82 my $doc = $loader->document('simple');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
83
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
84 assert(defined $doc);
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 assert($doc->nodeName eq 'document');
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
87 assert(not $doc->can('notexists')); # autoloaded property should be ignored
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
88 assert(not defined $doc->notexists); # nonexisting property
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
89 assert($doc->template->version == 10); # static metadata
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
90 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
91 assert($doc->templateVars('user') eq 'test_user'); # global data
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
92 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
93
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
94 my $text = $doc->Render();
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
95 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
96
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
97 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected");
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
98
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
99 };
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
100
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
101 test TTControlTests => sub {
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
102 my ($this) = @_;
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
103
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
104 my $loader = $this->CreateLoader();
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
105
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
106 my $doc = $loader->document('simple');
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
107
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
108 assert(defined $doc);
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 $factory = $doc->require('My/Org/Panel');
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
111
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
112 assert(defined $factory);
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
113
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
114 assert(not $loader->context->stash->get('My.Org.Panel'));
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
115
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
116 assert($factory->context->stash != $doc->context->stash);
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
117
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
118 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
119
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
120 my $ctl = $factory->new('information', { visualClass => 'simple' } );
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
121
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
122 assert(defined $ctl);
186
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 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
125
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
126 assert($ctl->nodeProperty('visualClass') eq 'simple');
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($factory->instances == 1);
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
129
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
130 assert($doc->templateVars('My.Org.Panel') == $factory);
186
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 my $text = $ctl->Render();
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
133 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
134
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
135 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
136
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
137 };
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
138
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
139 test TestMemoryLeaks => sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
140 my ($this) = @_;
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
141
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
142 my $loader = $this->CreateLoader();
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
143 $loader->document('simple'); # force loader initialization
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
144
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
145 AssertMemoryLeak(sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
146 my $doc = $loader->document('simple');
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
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
149 AssertMemoryLeak(sub {
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
150 my $doc = $loader->document('simple');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
151 $doc->Render( { self => $doc } );
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
152 });
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 AssertMemoryLeak(sub{
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
155 my $doc = $loader->document('simple');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
156 my $factory = $doc->require('My/Org/Panel');
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
157 #my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
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
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
160 };
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 186
diff changeset
161
183
2184fa28b49e IMPL::Web::View::TTLoader tests
cin
parents:
diff changeset
162 1;