comparison _test/Test/Web/View.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents 78a18a2b6266
children 7a920771fd8e
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
12 12
13 use IMPL::Test qw(assert test GetCallerSourceLine); 13 use IMPL::Test qw(assert test GetCallerSourceLine);
14 use IMPL::Web::View::TTLoader(); 14 use IMPL::Web::View::TTLoader();
15 15
16 use constant { 16 use constant {
17 TTLoader => typeof IMPL::Web::View::TTLoader, 17 TTLoader => typeof IMPL::Web::View::TTLoader,
18 MProfiler => 'IMPL::Profiler::Memory' 18 MProfiler => 'IMPL::Profiler::Memory'
19 }; 19 };
20 20
21 sub AssertMemoryLeak { 21 sub AssertMemoryLeak {
22 my $code = shift; 22 my $code = shift;
23 my $dump = shift; 23 my $dump = shift;
24 24
25 my $data = MProfiler->Monitor($code); 25 my $data = MProfiler->Monitor($code);
26 26
27 if ($data->isLeak and $dump) { 27 if ($data->isLeak and $dump) {
28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() ); 28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() );
29 } 29 }
30 30
31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); 31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} );
32 } 32 }
33 33
34 sub templatesDir { 34 sub templatesDir {
35 $_[0]->GetResourceDir('Resources','TTView'); 35 $_[0]->GetResourceDir('Resources','TTView');
36 } 36 }
37 37
38 sub CreateLoader { 38 sub CreateLoader {
39 my ($this) = @_; 39 my ($this) = @_;
40 40
41 my $loader = TTLoader->new( 41 my $loader = TTLoader->new(
42 { 42 {
43 INCLUDE_PATH => [ 43 INCLUDE_PATH => [
44 $this->templatesDir 44 $this->templatesDir
45 ], 45 ],
46 INTERPOLATE => 1, 46 INTERPOLATE => 1,
47 POST_CHOMP => 1, 47 POST_CHOMP => 1,
48 ENCODING => 'utf-8' 48 ENCODING => 'utf-8'
49 }, 49 },
50 ext => '.tt', 50 ext => '.tt',
51 initializer => 'global.tt' 51 initializer => 'global.tt',
52 ); 52 globals => {
53 site => {
54 name => 'Test Site'
55 },
56 date => {
57 now => sub { localtime(time); }
58 },
59 dynamic => sub { 'this is a dynamic value' }
60 }
61 );
53 } 62 }
54 63
55 test TTLoaderTests => sub { 64 test TTLoaderTests => sub {
56 my ($this) = @_; 65 my ($this) = @_;
57 66
58 my $loader = $this->CreateLoader(); 67 my $loader = $this->CreateLoader();
59 68
60 # test the loader to be able to find a desired resource 69 # test the loader to be able to find a desired resource
61 assert( defined($loader->template('simple') ) ); 70 assert( defined($loader->template('simple') ) );
62 71
63 # loader should be initialized on demand 72 # loader should be initialized on demand
64 assert( not $loader->isInitialized ); 73 assert( not $loader->isInitialized );
65 74
66 # loader should be able to load a document 75 # loader should be able to load a document
67 my $doc = $loader->document('simple'); 76 my $doc = $loader->document('simple');
68 assert(defined $doc); 77 assert(defined $doc);
69 78
70 assert( $loader->isInitialized ); 79 assert( $loader->isInitialized );
71 assert( $loader->context->stash->get('user') eq 'test_user'); 80 assert( $loader->context->stash->get('user') eq 'test_user');
72 81
73 # document should inherit loader's context 82 # document should inherit loader's context
74 assert( $doc->context->stash->get('user') eq 'test_user'); 83 assert( $doc->context->stash->get('user') eq 'test_user');
75 84
76 # document should not have 'this' template variable 85 # document should not have 'this' template variable
77 assert( not $doc->templateVars('this') ); 86 assert( not $doc->templateVars('this') );
78 87
79 assert( $doc->context != $loader->context); # document should have an own context 88 assert( $doc->context != $loader->context); # document should have an own context
80 }; 89 };
81 90
82 test TTDocumentTests => sub { 91 test TTDocumentTests => sub {
83 my ($this) = @_; 92 my ($this) = @_;
84 my $loader = $this->CreateLoader(); 93 my $loader = $this->CreateLoader();
85 94
86 my $doc = $loader->document('simple'); 95 my $doc = $loader->document('simple');
87 96
88 assert(defined $doc); 97 assert(defined $doc);
89 98 $doc->title('test document');
90 assert($doc->nodeName eq 'document'); 99
91 assert(not $doc->can('notexists')); # autoloaded property should be ignored 100 assert($doc->nodeName eq 'document');
92 assert(not defined $doc->notexists); # nonexisting property 101 assert($doc->title eq 'test document');
93 assert($doc->template->version == 10); # static metadata 102
94 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable 103 assert(not $doc->can('notexists')); # autoloaded property should be ignored
95 assert($doc->templateVars('user') eq 'test_user'); # global data 104 assert(not defined $doc->notexists); # nonexisting property
96 assert($doc->templateVars('templateVar') eq 'initialized by the constructor'); # defined in CTOR block 105 assert($doc->template->version == 10); # static metadata
97 106 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable
98 my $text = $doc->Render(); 107 assert($doc->templateVars('user') eq 'test_user'); # global data
99 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); 108 assert($doc->templateVars('templateVar') eq ''); # defined in CTOR block, should be local
100 109 assert($doc->templateVars('dynamic') eq 'this is a dynamic value');
101 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); 110
102 111 my $text = $doc->Render();
112 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8');
113
114 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected");
115
103 }; 116 };
104 117
105 test TTControlTests => sub { 118 test TTControlTests => sub {
106 my ($this) = @_; 119 my ($this) = @_;
107 120
108 my $loader = $this->CreateLoader(); 121 my $loader = $this->CreateLoader();
109 122
110 my $doc = $loader->document('simple'); 123 my $doc = $loader->document('simple');
111 124
112 assert(defined $doc); 125 assert(defined $doc);
113 126
114 my $factory = $doc->require('My/Org/Panel'); 127 my $factory = $doc->require('My/Org/Panel');
115 128
116 assert(defined $factory); 129 assert(defined $factory);
117 130
118 131
119 assert($factory->context->stash != $doc->context->stash); 132 assert($factory->context->stash != $doc->context->stash);
120 133
121 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once"); 134 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once");
122 135
123 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } ); 136 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } );
124 137
125 assert(defined $ctl); 138 assert(defined $ctl);
126 139
127 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information"); 140 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information");
128 141
129 assert($ctl->nodeProperty('visualClass') eq 'simple'); 142 assert($ctl->nodeProperty('visualClass') eq 'simple');
130 143
131 assert($factory->instances == 1); 144 assert($factory->instances == 1);
132 145
133 $doc->appendChild($ctl); 146 $doc->appendChild($ctl);
134 147
135 assert($doc->templateVars('dojo.require')); 148 assert($doc->templateVars('dojo.require'));
136 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY'); 149 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY');
137 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' ); 150 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' );
138 151
139 my $text = $ctl->Render(); 152 my $text = $ctl->Render();
140 153
141 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8'); 154 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
142 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected"); 155 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
143 156
144 157
145 158
146 }; 159 };
147 160
148 test TestDocumentLayout => sub { 161 test TestDocumentLayout => sub {
149 my ($this) = @_; 162 my ($this) = @_;
150 163
151 my $loader = $this->CreateLoader(); 164 my $loader = $this->CreateLoader();
152 165
153 my $doc = $loader->document('complex'); 166 my $doc = $loader->document(
154 assert($doc->layout eq 'Layout/default'); 167 'complex',
155 168 {
156 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); 169 data => [qw(one two three)],
157 170 site => {
158 assert($doc->templateVars('dojo.require')->[0]); 171 name => 'Test Site'
159 172 }
160 my $text = $doc->Render({ data => [qw(one two three)] }); 173 }
161 174 );
162 assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); 175
176 assert($doc->layout eq 'Layout/default');
177
178 assert($doc->templateVars('dojo.require')->[0]);
179
180 my $text = $doc->Render();
181
182 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' );
183 assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected");
163 }; 184 };
164 185
165 test TestMemoryLeaks => sub { 186 test TestMemoryLeaks => sub {
166 my ($this) = @_; 187 my ($this) = @_;
167 188
168 my $loader = $this->CreateLoader(); 189 my $loader = $this->CreateLoader();
169 $loader->document('simple'); # force loader initialization 190 $loader->document('simple'); # force loader initialization
170 191
171 AssertMemoryLeak(sub { 192 AssertMemoryLeak(sub {
172 my $doc = $loader->document('simple'); 193 my $doc = $loader->document('simple');
173 }); 194 });
174 195
175 AssertMemoryLeak(sub { 196 AssertMemoryLeak(sub {
176 my $doc = $loader->document('simple'); 197 my $doc = $loader->document('simple');
177 $doc->Render( { self => $doc } ); 198 $doc->Render( { self => $doc } );
178 }); 199 });
179 200
180 $loader->template('Layout/default'); 201 $loader->template('Layout/default');
181 $loader->template('My/Org/Panel'); 202 $loader->template('My/Org/Panel');
182 $loader->template('My/Org/TextPreview'); 203 $loader->template('My/Org/TextPreview');
183 AssertMemoryLeak(sub { 204 AssertMemoryLeak(sub {
184 my $doc = $loader->document('simple'); 205 my $doc = $loader->document('simple');
185 my $factory = $doc->require('My/Org/Panel'); 206 my $factory = $doc->require('My/Org/Panel');
186 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) ); 207 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
187 }); 208 });
188 209
189 $loader->template('complex'); 210 $loader->template('complex');
190 AssertMemoryLeak(sub { 211 AssertMemoryLeak(sub {
191 my $doc = $loader->document('complex'); 212 my $doc = $loader->document('complex');
192 $doc->Render(); 213 $doc->Render();
193 },'dump'); 214 },'dump');
194 215
195 }; 216 };
196 217
197 1; 218 1;