Mercurial > pub > Impl
annotate _test/Test/DOM/Node.pm @ 148:e6447ad85cb4
DOM objects now have a schema and schemaSource properties
RegExp now can launder data
Improved post to DOM transformation (multiple values a now supported)
Added new axes to navigation queries: ancestor and descendant
minor changes and bug fixes
author | wizard |
---|---|
date | Mon, 16 Aug 2010 08:26:44 +0400 |
parents | e30bdd040fe3 |
children | 4267a2ac3d46 |
rev | line source |
---|---|
49 | 1 package Test::DOM::Node; |
2 use strict; | |
3 use warnings; | |
4 | |
5 use base qw(IMPL::Test::Unit); | |
6 use IMPL::Test qw(test shared failed cmparray); | |
7 use IMPL::Class::Property; | |
8 | |
9 require IMPL::DOM::Node; | |
10 | |
11 __PACKAGE__->PassThroughArgs; | |
12 | |
13 BEGIN { | |
14 shared public property Root => prop_all; | |
15 } | |
16 | |
17 test Create => sub { | |
18 my ($this) = @_; | |
19 | |
20 $this->Root(new IMPL::DOM::Document(nodeName => 'Root')) or failed "Failed to create a document"; | |
21 }; | |
22 | |
23 test InsertNode => sub { | |
24 my ($this) = @_; | |
25 my $child = $this->Root->insertNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to insert a child node"; | |
26 failed "fiestChild returned incorrect results" unless ($this->Root->firstChild || 0) == $child; | |
27 }; | |
28 | |
29 test AppendNode => sub { | |
30 my ($this) = @_; | |
31 | |
32 my $child = $this->Root->appendNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to append a child node"; | |
33 | |
34 my $lastChild = $this->Root->removeLast; | |
35 | |
36 failed "removeLast returned incorrect results" unless $lastChild == $child; | |
37 }; | |
38 | |
39 test GetDocumentNode => sub { | |
40 my ($this) = @_; | |
41 | |
42 my $child = $this->Root->firstChild->appendNode(new IMPL::DOM::Node(nodeName => 'GrandChild')) or failed "Failed to append a child node"; | |
43 | |
44 failed "document property is undef" unless $child->document; | |
45 failed "document property returned incorrect value" unless $child->document == $this->Root; | |
46 }; | |
47 | |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
48 test DocumentCreateNode => sub { |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
49 my ($this) = @_; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
50 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
51 my $child = $this->Root->firstChild->appendNode($this->Root->Create(Info => { uuid => '77f9-9a-6d58' } )) or failed "Failed to append a child node"; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
52 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
53 failed "document property is undef" unless $child->document; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
54 failed "document property returned incorrect value" unless $child->document == $this->Root; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
55 }; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
56 |
49 | 57 test MoveNode => sub { |
58 my ($this) = @_; | |
59 | |
60 my $grandChild = $this->Root->firstChild->firstChild; | |
61 $this->Root->appendNode($grandChild); | |
62 | |
63 failed "incorrect new parentNode value" unless ($grandChild->parentNode || 0) == $this->Root; | |
64 failed "incorrect new document value" unless ($grandChild->document || 0) == $this->Root; | |
65 }; | |
66 | |
67 test AppendRange => sub { | |
68 my ($this) = @_; | |
69 | |
70 my $count = $this->Root->childNodes->Count; | |
71 | |
72 $this->Root->appendRange( | |
73 map IMPL::DOM::Node->new(nodeName => "Item", nodeValue => $_),1..10 | |
74 ); | |
75 | |
76 failed | |
77 "Wrong number of a child nodes", | |
78 "Expected: ".($count+10), | |
79 "Actual: ".$this->Root->childNodes->Count | |
80 unless $count + 10 == $this->Root->childNodes->Count; | |
81 }; | |
82 | |
83 test SelectNodes => sub { | |
84 my ($this) = @_; | |
85 | |
86 my @result = $this->Root->selectNodes("Item"); | |
87 | |
88 failed | |
89 "Wrong number of a selected nodes", | |
90 "Expected: 10", | |
91 "Actual: ".scalar(@result) | |
92 unless @result == 10; | |
93 }; | |
94 | |
95 test SelectNodesByQuery => sub { | |
96 my ($this) = @_; | |
97 | |
98 my @result = $this->Root->selectNodes(sub { $_->nodeName =~ /child/i } ); | |
99 failed | |
100 "Wrong number of a selected nodes", | |
101 "Expected: 2", | |
102 "Actual: ".scalar(@result) | |
103 unless @result == 2; | |
104 }; | |
105 | |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
106 test SelectNodesPath => sub { |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
107 my ($this) = @_; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
108 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
109 my @result = $this->Root->selectNodes('Child','Info'); |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
110 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
111 failed "Failed to select a node by path 'Child/Info'" unless @result; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
112 }; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
113 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
114 test SelectByAxisDescendant => sub { |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
115 my ($this) = @_; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
116 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
117 my @result = $this->Root->selectNodes( { descendant => ['GrandChild','Info']} ); |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
118 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
119 failed "Failed to select a node by path '//(GrandChild|Info)/'" unless @result == 2; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
120 }; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
121 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
122 test SelectByAxisAncestor => sub { |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
123 my ($this) = @_; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
124 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
125 my @result = $this->Root->selectSingleNode( { descendant => 'Info'} )->selectNodes( { ancestor => undef } ) ; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
126 |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
127 failed "Failed to select a node by path '//Info/ancestor:*'" unless @result == 2; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
128 }; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
129 |
49 | 130 test CheckNodesValues => sub { |
131 my ($this) = @_; | |
132 | |
133 my @expected = (1..10); | |
134 | |
135 my @result = map $_->nodeValue, grep $_->nodeValue, $this->Root->selectNodes("Item"); | |
136 | |
137 failed | |
138 "Some nodes returned wrong node values or in a wrong order", | |
139 "Expected: ".join(', ',@expected), | |
140 "Recieved: ".join(', ',@result) | |
141 unless cmparray(\@expected,\@result); | |
142 | |
143 failed | |
144 "a text property of a root node returned a wrong value", | |
145 "Expected: @expected", | |
146 "Recieved: ". $this->Root->text | |
147 unless $this->Root->text eq join '',@expected; | |
148 }; | |
149 | |
150 test isComplex => sub { | |
151 my ($this) = @_; | |
152 | |
153 failed "property isComplex returned false for the root node" unless $this->Root->isComplex; | |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
124
diff
changeset
|
154 failed "property isComplex returned true for a simple node", $this->Root->selectSingleNode('Item')->childNodes->Count if $this->Root->selectSingleNode('Item')->isComplex; |
49 | 155 }; |
156 | |
123 | 157 test setObjectProperty => sub { |
158 my ($this) = @_; | |
159 | |
160 my $node = Test::DOM::TypedNode->new(nodeName => 'TestNode'); | |
161 | |
162 my $name = 'Vergon 6'; | |
163 | |
164 $node->nodeProperty(name => $name); | |
165 failed "Failed to set a property 'name'", "Expected: $name", "Got: ".$node->name unless $node->name eq $name; | |
166 | |
167 $name = 'entity_vergon_6'; | |
168 $node->systemName($name); | |
169 failed "Failed to set a property 'systemName'", "Expected: $name", "Got: ".$node->nodeProperty('systemName') unless $node->nodeProperty('systemName') eq $name; | |
170 }; | |
171 | |
172 test setDynamicProperty => sub { | |
173 my $node = Test::DOM::TypedNode->new(nodeName => 'TestNode'); | |
174 | |
175 my $uuid = 'entity_76fd98b9e7a'; | |
176 $node->nodeProperty(uuid => $uuid); | |
177 failed "Failed to set a dynamic property 'uuid'", "Expected: $uuid", "Got: ".$node->nodeProperty('uuid') unless $node->nodeProperty('uuid') eq $uuid; | |
178 }; | |
179 | |
180 test setPrivateProperty => sub { | |
181 my $node = Test::DOM::TypedNode->new(nodeName => 'TestNode'); | |
182 | |
183 eval { | |
184 $node->nodeProperty(_private => 'failed'); | |
185 1; | |
186 } and failed "Setting a private property successfull"; | |
187 }; | |
188 | |
124 | 189 test createNodeWithProps => sub { |
190 my $uuid = 'entity_76fd98b9e7a'; | |
191 my $name = 'Vergon 6'; | |
192 my $systemName = 'entity_vergon_6'; | |
193 | |
194 my $node = Test::DOM::TypedNode->new( | |
195 nodeName => 'TestNode', | |
196 uuid => $uuid, | |
197 name => $name, | |
198 systemName => $systemName | |
199 ); | |
200 | |
201 failed "Failed to get dynamic property 'uuid'" unless $node->nodeProperty('uuid') eq $uuid; | |
202 failed "Failed to get property 'name' through nodeProperty method" unless $node->nodeProperty('name') eq $name; | |
203 failed "Failed to get property name directly" unless $node->name eq $name; | |
204 }; | |
205 | |
206 test listNodePredefinedProps => sub { | |
207 my $node = Test::DOM::TypedNode->new(nodeName => 'TestNode'); | |
208 | |
209 my @props = $node->listProperties; | |
210 my @expected = qw(name _private); | |
211 | |
212 failed "Got wrong list of props", @props unless cmparray(\@props,\@expected); | |
213 }; | |
214 | |
215 test listNodeAllProps => sub { | |
216 my $node = Test::DOM::TypedNode->new( | |
217 nodeName => 'TestNode', | |
218 uuid => 'ade58f98b', # dynamic | |
219 name => 'noname', # predefined | |
220 systemName => 'no sys' # not visible to DOM | |
221 ); | |
222 | |
223 my @props = $node->listProperties; | |
224 my @expected = qw(name _private uuid); # systemName is not a DOM prop | |
225 | |
226 failed "Got wrong list of props", @props unless cmparray(\@props,\@expected); | |
227 }; | |
228 | |
122 | 229 package Test::DOM::TypedNode; |
230 use base qw(IMPL::DOM::Node); | |
231 use IMPL::Class::Property; | |
123 | 232 use IMPL::DOM::Property qw(_dom); |
233 | |
234 __PACKAGE__->PassThroughArgs; | |
122 | 235 |
123 | 236 BEGIN { |
237 public _dom property name => prop_all; | |
238 public property systemName => prop_all; | |
239 private _dom property _private => prop_all; | |
240 } | |
122 | 241 |
242 | |
49 | 243 1; |