Mercurial > pub > Impl
annotate _test/Test/Web/View.pm @ 404:9ef75f2029be default
sync
author | cin |
---|---|
date | Fri, 28 Aug 2015 19:54:53 +0300 |
parents | 5aff94ba842f |
children |
rev | line source |
---|---|
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 |
185 | 7 use File::Slurp; |
188 | 8 use Scalar::Util qw(weaken); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
9 use Data::Dumper; |
280 | 10 use IMPL::lang; |
286 | 11 use IMPL::Test qw(assert assertarray test GetCallerSourceLine); |
183 | 12 |
367 | 13 use IMPL::declare { |
14 require => { | |
15 FormMeta => 'IMPL::Web::View::Metadata::FormMeta', | |
369 | 16 ObjectMeta => 'IMPL::Web::View::Metadata::ObjectMeta', |
367 | 17 Schema => 'IMPL::DOM::Schema', |
18 Document => 'IMPL::DOM::Document', | |
368 | 19 XMLReader => 'IMPL::DOM::XMLReader', |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
20 MProfiler => '-IMPL::Profiler::Memory', |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
21 PersonModel => '-Test::Web::View::Model::Person' |
367 | 22 }, |
23 base => [ | |
24 'IMPL::Test::Unit' => '@_' | |
25 ] | |
183 | 26 }; |
27 | |
367 | 28 |
188 | 29 sub AssertMemoryLeak { |
194 | 30 my $code = shift; |
31 my $dump = shift; | |
32 | |
33 my $data = MProfiler->Monitor($code); | |
34 | |
35 if ($data->isLeak and $dump) { | |
36 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() ); | |
37 } | |
38 | |
39 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} ); | |
188 | 40 } |
41 | |
185 | 42 sub templatesDir { |
367 | 43 shift->GetResourceDir('Resources','TTView'); |
188 | 44 } |
45 | |
367 | 46 test TestFormMetadata => sub { |
47 my ($this) = @_; | |
48 | |
368 | 49 # preload to avoid the false memory leak dection |
50 Schema->MetaSchema->Validate(Schema->MetaSchema); | |
51 Document->Empty(); | |
52 require IMPL::DOM::Schema::Validator::RegExp; | |
367 | 53 |
368 | 54 AssertMemoryLeak(sub{ |
55 my ($doc,$errors) = XMLReader->LoadDocument( | |
56 $this->GetResourceFile('Resources','person_info2.xml'), | |
57 $this->GetResourceFile('Resources','person.schema.xml') | |
58 ); | |
59 | |
60 my $meta = FormMeta->new( | |
61 $doc, | |
389 | 62 $doc->schemaNode->type, |
368 | 63 { |
389 | 64 decl => $doc->schemaNode, |
65 schema => $doc->schemaType, | |
368 | 66 errors => $errors |
67 } | |
68 ); | |
69 | |
70 assert(@{$errors||[]} == 2); | |
367 | 71 |
368 | 72 my $props = $meta->GetProperties; |
73 | |
74 assertarray( | |
75 [map $_->name, @$props], | |
76 [qw(firstName lastName age address)] | |
77 ); | |
78 assertarray( | |
79 [map $_->modelType, @$props], | |
80 [undef,undef,'number','ARRAY'] | |
81 ); | |
82 | |
83 my $prop = $meta->GetProperty('address'); | |
84 | |
85 assert($prop); | |
86 assert($prop->schema->type eq 'address'); | |
87 assert($prop->isMultiple); | |
88 | |
89 assert(@{$prop->errors || []} == 1); | |
90 assertarray( | |
91 [map scalar(@{$_->errors || []}), @{$prop->GetItems}], | |
92 [0,0,1] | |
93 ); | |
94 | |
95 my $item = $prop->GetItem(2); | |
96 | |
97 assert(@{$item->errors} == 1); | |
98 assert(@{$item->GetOwnErrors()} == 0); | |
99 | |
100 assert($item->GetProperty('street')); | |
101 | |
102 assert(@{$item->GetProperty('street')->errors} == 1); | |
103 assert(@{$item->GetProperty('street')->GetOwnErrors()} == 1); | |
104 | |
105 #join ',', map $_->GetProperty('line')->name, @{$prop->GetItems}; | |
106 }); | |
185 | 107 }; |
369 | 108 test TestObjectMetadata => sub { |
109 my ($this) = @_; | |
110 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
111 my $meta = ObjectMeta->new( |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
112 PersonModel->new( |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
113 name => 'alex', |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
114 age => 29, |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
115 address => [ |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
116 { city => 'city 17', street => 'doomed' }, |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
117 { city => 'gropher', street => 'caveroad' } |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
118 ] |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
119 ) |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
120 ); |
369 | 121 |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
122 assert($meta->modelType eq PersonModel); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
123 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
124 my $prop = $meta->GetProperty('name'); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
125 assert($prop); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
126 assert($prop->model eq 'alex'); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
127 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
128 $prop = $meta->GetProperty('address'); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
129 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
130 assert($prop->isMultiple); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
131 assert($prop->name eq 'address'); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
132 assert($prop->modelType eq 'ARRAY'); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
133 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
134 my $items = $prop->GetItems(); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
135 assert(@$items == 2); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
136 assertarray( |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
137 [map $_->GetProperty('city')->model, @$items], |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
138 ['city 17', 'gropher'] |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
139 ); |
369 | 140 }; |
185 | 141 |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
142 package Test::Web::View::Model::Person; |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
143 use IMPL::Const qw(:prop); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
144 use IMPL::declare { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
145 base => [ |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
146 'IMPL::Object' => undef, |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
147 'IMPL::Object::Autofill' => '@_' |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
148 ], |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
149 props => [ |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
150 name => PROP_RW, |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
151 age => PROP_RW, |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
152 address => PROP_RW | PROP_LIST |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
153 ] |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
369
diff
changeset
|
154 }; |
188 | 155 |
183 | 156 1; |