Mercurial > pub > Impl
comparison _test/Test/Web/View.pm @ 367:608e74bc309f
form metadata, mostly done
author | cin |
---|---|
date | Tue, 03 Dec 2013 17:55:36 +0400 |
parents | aeeb57a12046 |
children | 010ceafd0c5a |
comparison
equal
deleted
inserted
replaced
366:935629bf80df | 367:608e74bc309f |
---|---|
2 use IMPL::Profiler::Memory; | 2 use IMPL::Profiler::Memory; |
3 use strict; | 3 use strict; |
4 use warnings; | 4 use warnings; |
5 use utf8; | 5 use utf8; |
6 | 6 |
7 use parent qw(IMPL::Test::Unit); | |
8 __PACKAGE__->PassThroughArgs; | |
9 | |
10 use File::Slurp; | 7 use File::Slurp; |
11 use Scalar::Util qw(weaken); | 8 use Scalar::Util qw(weaken); |
12 use Data::Dumper; | 9 use Data::Dumper; |
13 use IMPL::lang; | 10 use IMPL::lang; |
14 use IMPL::Test qw(assert assertarray test GetCallerSourceLine); | 11 use IMPL::Test qw(assert assertarray test GetCallerSourceLine); |
15 use IMPL::Web::View::TTLoader(); | |
16 | 12 |
17 use constant { | 13 use IMPL::declare { |
18 TTLoader => 'IMPL::Web::View::TTLoader', | 14 require => { |
19 MProfiler => 'IMPL::Profiler::Memory' | 15 FormMeta => 'IMPL::Web::View::Metadata::FormMeta', |
16 Schema => 'IMPL::DOM::Schema', | |
17 Builder => 'IMPL::DOM::Navigator::Builder', | |
18 Document => 'IMPL::DOM::Document', | |
19 XMLReader => 'IMPL::DOM::XMLReader' | |
20 }, | |
21 base => [ | |
22 'IMPL::Test::Unit' => '@_' | |
23 ] | |
20 }; | 24 }; |
25 | |
21 | 26 |
22 sub AssertMemoryLeak { | 27 sub AssertMemoryLeak { |
23 my $code = shift; | 28 my $code = shift; |
24 my $dump = shift; | 29 my $dump = shift; |
25 | 30 |
31 | 36 |
32 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); | 37 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); |
33 } | 38 } |
34 | 39 |
35 sub templatesDir { | 40 sub templatesDir { |
36 $_[0]->GetResourceDir('Resources','TTView'); | 41 shift->GetResourceDir('Resources','TTView'); |
37 } | 42 } |
38 | 43 |
39 sub CreateLoader { | 44 test TestFormMetadata => sub { |
40 my ($this) = @_; | 45 my ($this) = @_; |
41 | 46 |
42 my $loader = TTLoader->new( | 47 my $schema = Schema->LoadSchema($this->GetResourceFile('Resources','person.schema.xml')); |
43 { | 48 my $builder = Builder->new(Document, $schema); |
44 INCLUDE_PATH => [ | 49 |
45 $this->templatesDir | 50 my $reader = XMLReader->new( Navigator => $builder ); |
46 ], | 51 |
47 INTERPOLATE => 1, | 52 $reader->ParseFile("Resources/person_info.xml"); |
48 POST_CHOMP => 1, | 53 |
49 ENCODING => 'utf-8' | 54 my $doc = $builder->document; |
50 }, | 55 my @errors = $builder->buildErrors; |
51 ext => '.tt', | 56 |
52 initializer => 'global.tt', | 57 push @errors, $schema->Validate($doc); |
53 globals => { | 58 |
54 site => { | 59 my $meta = FormMeta->new( |
55 name => 'Test Site' | 60 $doc, |
56 }, | 61 $doc->schemaSource->type, |
57 date => { | 62 { |
58 now => sub { localtime(time); } | 63 decl => $doc->schemaSource, |
59 }, | 64 schema => $doc->schema, |
60 dynamic => sub { 'this is a dynamic value' }, | 65 errors => \@errors |
61 view => { | 66 } |
62 } | 67 ); |
63 }, | |
64 layoutBase => 'Layout' | |
65 ); | |
66 } | |
67 | 68 |
68 test TTLoaderTests => sub { | 69 my $props = $meta->GetProperties; |
69 my ($this) = @_; | 70 |
70 | 71 my $prop = $meta->GetProperty('address'); |
71 my $loader = $this->CreateLoader(); | 72 |
72 | 73 join ',', map $_->GetProperty('line')->name, @{$prop->GetItems}; |
73 # test the loader to be able to find a desired resource | |
74 assert( defined($loader->template('simple') ) ); | |
75 | |
76 # loader should be initialized on demand | |
77 assert( not $loader->isInitialized ); | |
78 | |
79 # loader should be able to load a document | |
80 my $doc = $loader->document('simple'); | |
81 assert(defined $doc); | |
82 | |
83 assert( $loader->isInitialized ); | |
84 assert( $loader->context->stash->get('user') eq 'test_user'); | |
85 | |
86 # document should inherit loader's context | |
87 assert( $doc->context->stash->get('user') eq 'test_user'); | |
88 | |
89 # document should not have 'this' template variable | |
90 | |
91 assert( $doc->context != $loader->context); # document should have an own context | |
92 }; | 74 }; |
93 | 75 |
94 test TTDocumentTests => sub { | |
95 my ($this) = @_; | |
96 my $loader = $this->CreateLoader(); | |
97 | |
98 my $doc = $loader->document('simple'); | |
99 | |
100 assert(defined $doc); | |
101 | |
102 $doc->title('test document'); | |
103 $doc->name('document'); | |
104 | |
105 assert($doc->name eq 'document'); | |
106 assert($doc->title eq 'test document'); | |
107 | |
108 assert(not $doc->can('notexists')); # autoloaded property should be ignored | |
109 assert(not defined $doc->notexists); # nonexisting property | |
110 assert($doc->template->version == 10); # static metadata | |
111 assert($doc->context->stash->get('user') eq 'test_user' ); # runtime context should be derived from documentContext | |
112 | |
113 my $text = $doc->Render(); | |
114 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8'); | |
115 | |
116 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected"); | |
117 | |
118 }; | |
119 | |
120 test TestDocumentLayout => sub { | |
121 my ($this) = @_; | |
122 | |
123 my $loader = $this->CreateLoader(); | |
124 | |
125 my $doc = $loader->document( | |
126 'complex', | |
127 { | |
128 data => [qw(one two three)], | |
129 site => { | |
130 name => 'Test Site' | |
131 } | |
132 } | |
133 ); | |
134 | |
135 assert($doc->layout eq 'default'); | |
136 | |
137 my $text = $doc->Render(); | |
138 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' ); | |
139 my ($text_raw,$expected_raw) = ($text, $expected); | |
140 $text_raw =~ s/\s+//g; | |
141 $expected_raw =~ s/\s+//g; | |
142 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
143 }; | |
144 | |
145 test TestControlInheritance => sub { | |
146 my ($this) = @_; | |
147 | |
148 my $loader = $this->CreateLoader(); | |
149 my $doc = $loader->document('derived'); | |
150 | |
151 my $text = $doc->Render(); | |
152 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'derived.txt'), binmode => ':utf8' ); | |
153 | |
154 my ($text_raw,$expected_raw) = ($text, $expected); | |
155 $text_raw =~ s/\s+//g; | |
156 $expected_raw =~ s/\s+//g; | |
157 | |
158 assert($text_raw eq $expected_raw, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected"); | |
159 }; | |
160 | |
161 test TestDocumentsIsolation => sub { | |
162 my $this = shift; | |
163 | |
164 my $loader = $this->CreateLoader(); | |
165 | |
166 my $doc = $loader->document('simple'); | |
167 | |
168 assert(ref $loader->context->stash->get([ 'dojo', 0, 'require', 0]) eq 'ARRAY'); | |
169 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
170 assert($loader->context->stash != $doc->context->stash); | |
171 | |
172 assert(defined $doc); | |
173 | |
174 # only root stash variables can be localized, to avoid modifying dojo we | |
175 # need to replace it completely | |
176 $doc->context->process(\q{ | |
177 [% SET dojo = { require => [] } %] | |
178 [% dojo.require.push('dijit/form/TextBox') %] | |
179 [% SET user = 'dummy guy' %] | |
180 }); | |
181 | |
182 assert($doc->context->stash->get('user') eq 'dummy guy'); | |
183 assert($loader->context->stash->get('user') eq 'test_user'); | |
184 assertarray($doc->context->stash->get([ 'dojo', 0, 'require', 0]),['dijit/form/TextBox']); | |
185 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
186 | |
187 my $text = $doc->Render(); | |
188 | |
189 my $doc2 = $loader->document('complex'); | |
190 | |
191 assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
192 | |
193 # This document has a layout ehich will replace 'dojo' global variable. | |
194 # The layout contains INIT block which runs first in the context of the | |
195 # document, then RenderContent is called and then the layout is applied | |
196 $doc2->Render(); | |
197 | |
198 assertarray($loader->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
199 | |
200 # TODO: to be able to rendered multiple times, Render shouldn't affect the context of the document | |
201 #assertarray($doc2->context->stash->get([ 'dojo', 0, 'require', 0]),[]); | |
202 }; | |
203 | |
204 test TestMemoryLeaks => sub { | |
205 my ($this) = @_; | |
206 | |
207 my $loader = $this->CreateLoader(); | |
208 $loader->document('simple'); # force loader initialization | |
209 | |
210 AssertMemoryLeak(sub { | |
211 my $doc = $loader->document('simple'); | |
212 },'dump'); | |
213 | |
214 AssertMemoryLeak(sub { | |
215 my $doc = $loader->document('simple'); | |
216 $doc->Render( { self => $doc } ); | |
217 },'dump'); | |
218 | |
219 $loader->template('Layout/default'); | |
220 $loader->template('My/Org/Panel'); | |
221 $loader->template('My/Org/TextPreview'); | |
222 | |
223 $loader->template('complex'); | |
224 AssertMemoryLeak(sub { | |
225 my $doc = $loader->document('complex'); | |
226 $doc->Render(); | |
227 },'dump'); | |
228 | |
229 }; | |
230 | 76 |
231 1; | 77 1; |