/[suikacvs]/messaging/manakai/t/DOM-Node.t
Suika

Contents of /messaging/manakai/t/DOM-Node.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download) (as text)
Fri Jun 15 16:12:28 2007 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +294 -1 lines
File MIME type: application/x-troff
++ manakai/t/ChangeLog	15 Jun 2007 16:12:22 -0000
2007-06-16  Wakaba  <wakaba@suika.fam.cx>

	* DOM-Node.t: Test data added.

++ manakai/lib/Message/DOM/ChangeLog	15 Jun 2007 16:11:56 -0000
2007-06-16  Wakaba  <wakaba@suika.fam.cx>

	* Node.pm: First alpha version of implementation of attributes.

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3     use Test;
4     BEGIN { plan tests => 4 }
5    
6     require Message::DOM::DOMImplementation;
7    
8     my $dom = Message::DOM::DOMImplementation->____new;
9     my $doc = $dom->create_document;
10    
11     ## Constants
12     my $constants = [
13     [ELEMENT_NODE => 1],
14     [ATTRIBUTE_NODE => 2],
15     [TEXT_NODE => 3],
16     [CDATA_SECTION_NODE => 4],
17     [ENTITY_REFERENCE_NODE => 5],
18     [ENTITY_NODE => 6],
19     [PROCESSING_INSTRUCTION_NODE => 7],
20     [COMMENT_NODE => 8],
21     [DOCUMENT_NODE => 9],
22     [DOCUMENT_TYPE_NODE => 10],
23     [DOCUMENT_FRAGMENT_NODE => 11],
24     [NOTATION_NODE => 12],
25     [ELEMENT_TYPE_DEFINITION_NODE => 81001],
26     [ATTRIBUTE_DEFINITION_NODE => 81002],
27     ];
28    
29     my $tests = {
30     attr => {
31     node => sub {
32     my $attr = $doc->create_attribute ('a');
33     $attr->value ('b');
34     return $attr;
35     },
36     attr_get => {
37 wakaba 1.2 first_child => undef,
38     last_child => undef,
39     local_name => 'a',
40     manakai_local_name => 'a',
41     namespace_uri => undef,
42     next_sibling => undef,
43 wakaba 1.1 node_name => 'a',
44     name => 'a',
45     node_value => 'b',
46 wakaba 1.2 owner_document => $doc,
47     parent_node => undef,
48     prefix => undef,
49     previous_sibling => undef,
50 wakaba 1.1 value => 'b',
51     attributes => undef,
52     },
53     },
54     attr_ns_default => {
55     node => sub {
56     my $attr = $doc->create_attribute_ns (undef, 'a');
57     $attr->value ('b');
58     return $attr;
59     },
60     attr_get => {
61 wakaba 1.2 first_child => undef,
62     last_child => undef,
63     local_name => 'a',
64     manakai_local_name => 'a',
65     namespace_uri => undef,
66     next_sibling => undef,
67 wakaba 1.1 node_name => 'a',
68     name => 'a',
69     node_value => 'b',
70 wakaba 1.2 owner_document => $doc,
71     parent_node => undef,
72     prefix => undef,
73 wakaba 1.1 value => 'b',
74     attributes => undef,
75 wakaba 1.2 previous_sibling => undef,
76 wakaba 1.1 },
77     },
78     attr_ns_prefixed => {
79     node => sub {
80     my $attr = $doc->create_attribute_ns ('http://test/', 'a:b');
81     $attr->value ('c');
82     return $attr;
83     },
84     attr_get => {
85 wakaba 1.2 first_child => undef,
86     last_child => undef,
87     local_name => 'b',
88     manakai_local_name => 'b',
89     namespace_uri => 'http://test/',
90     next_sibling => undef,
91 wakaba 1.1 node_name => 'a:b',
92     name => 'a:b',
93     node_value => 'c',
94 wakaba 1.2 owner_document => $doc,
95     parent_node => undef,
96     prefix => 'a',
97 wakaba 1.1 value => 'c',
98     attributes => undef,
99 wakaba 1.2 previous_sibling => undef,
100 wakaba 1.1 },
101     },
102     cdatasection => {
103     node => sub { return $doc->create_cdata_section ('cdatadata') },
104     attr_get => {
105 wakaba 1.2 first_child => undef,
106     last_child => undef,
107     local_name => undef,
108     manakai_local_name => undef,
109     namespace_uri => undef,
110     next_sibling => undef,
111 wakaba 1.1 node_name => '#cdata-section',
112     node_value => 'cdatadata',
113 wakaba 1.2 owner_document => $doc,
114     parent_node => undef,
115     prefix => undef,
116 wakaba 1.1 data => 'cdatadata',
117     attributes => undef,
118 wakaba 1.2 previous_sibling => undef,
119 wakaba 1.1 },
120     },
121     cdatasectionmde => {
122     node => sub { return $doc->create_cdata_section ('cdata]]>data') },
123     attr_get => {
124 wakaba 1.2 first_child => undef,
125     last_child => undef,
126     local_name => undef,
127     manakai_local_name => undef,
128     namespace_uri => undef,
129     next_sibling => undef,
130 wakaba 1.1 node_name => '#cdata-section',
131     node_value => 'cdata]]>data',
132 wakaba 1.2 owner_document => $doc,
133     parent_node => undef,
134     prefix => undef,
135 wakaba 1.1 data => 'cdata]]>data',
136     attributes => undef,
137 wakaba 1.2 previous_sibling => undef,
138 wakaba 1.1 },
139     },
140     comment => {
141     node => sub { return $doc->create_comment ('commentdata') },
142     attr_get => {
143 wakaba 1.2 first_child => undef,
144     last_child => undef,
145     local_name => undef,
146     manakai_local_name => undef,
147     namespace_uri => undef,
148     next_sibling => undef,
149 wakaba 1.1 node_name => '#comment',
150     node_value => 'commentdata',
151 wakaba 1.2 owner_document => $doc,
152     parent_node => undef,
153     prefix => undef,
154 wakaba 1.1 data => 'commentdata',
155     attributes => undef,
156 wakaba 1.2 previous_sibling => undef,
157 wakaba 1.1 },
158     },
159     commentcom1 => {
160     node => sub { return $doc->create_comment ('comment--data') },
161     attr_get => {
162 wakaba 1.2 first_child => undef,
163     last_child => undef,
164     local_name => undef,
165     manakai_local_name => undef,
166     namespace_uri => undef,
167     next_sibling => undef,
168 wakaba 1.1 node_name => '#comment',
169     node_value => 'comment--data',
170 wakaba 1.2 owner_document => $doc,
171     parent_node => undef,
172     prefix => undef,
173 wakaba 1.1 data => 'comment--data',
174     attributes => undef,
175 wakaba 1.2 previous_sibling => undef,
176 wakaba 1.1 },
177     },
178     commentcom2 => {
179     node => sub { return $doc->create_comment ('commentdata-') },
180     attr_get => {
181 wakaba 1.2 first_child => undef,
182     last_child => undef,
183     local_name => undef,
184     manakai_local_name => undef,
185     namespace_uri => undef,
186     next_sibling => undef,
187 wakaba 1.1 node_name => '#comment',
188     node_value => 'commentdata-',
189 wakaba 1.2 owner_document => $doc,
190     parent_node => undef,
191     prefix => undef,
192 wakaba 1.1 data => 'commentdata-',
193     attributes => undef,
194 wakaba 1.2 previous_sibling => undef,
195 wakaba 1.1 },
196     },
197     document => {
198     node => sub { return $doc },
199     attr_get => {
200     attributes => undef,
201 wakaba 1.2 first_child => undef,
202     implementation => $dom,
203     last_child => undef,
204 wakaba 1.1 local_name => undef,
205 wakaba 1.2 manakai_local_name => undef,
206 wakaba 1.1 namespace_uri => undef,
207 wakaba 1.2 next_sibling => undef,
208 wakaba 1.1 node_name => '#document',
209     node_value => undef,
210 wakaba 1.2 owner_document => undef,
211 wakaba 1.1 parent_node => undef,
212     prefix => undef,
213 wakaba 1.2 previous_sibling => undef,
214 wakaba 1.1 },
215     },
216     document_fragment => {
217     node => sub { return $doc->create_document_fragment },
218     attr_get => {
219     attributes => undef,
220 wakaba 1.2 first_child => undef,
221     last_child => undef,
222     local_name => undef,
223     manakai_local_name => undef,
224     namespace_uri => undef,
225     next_sibling => undef,
226 wakaba 1.1 node_name => '#document-fragment',
227     node_value => undef,
228 wakaba 1.2 owner_document => $doc,
229     parent_node => undef,
230     prefix => undef,
231     previous_sibling => undef,
232 wakaba 1.1 },
233     },
234     document_type => {
235     node => sub { return $doc->implementation->create_document_type ('n') },
236     attr_get => {
237     attributes => undef,
238 wakaba 1.2 first_child => undef,
239     implementation => $dom,
240     last_child => undef,
241     local_name => undef,
242     manakai_local_name => undef,
243     namespace_uri => undef,
244     next_sibling => undef,
245 wakaba 1.1 node_name => 'n',
246     node_value => undef,
247 wakaba 1.2 owner_document => undef,
248     parent_node => undef,
249     prefix => undef,
250     previous_sibling => undef,
251 wakaba 1.1 },
252     },
253     document_type_definition => {
254     node => sub { return $doc->create_document_type_definition ('n') },
255     attr_get => {
256     attributes => undef,
257 wakaba 1.2 first_child => undef,
258     implementation => $dom,
259     last_child => undef,
260     local_name => undef,
261     manakai_local_name => undef,
262     namespace_uri => undef,
263     next_sibling => undef,
264 wakaba 1.1 node_name => 'n',
265     node_value => undef,
266 wakaba 1.2 owner_document => $doc,
267     parent_node => undef,
268     prefix => undef,
269     previous_sibling => undef,
270 wakaba 1.1 },
271     },
272     element => {
273     node => sub { return $doc->create_element ('e') },
274     attr_get => {
275     ## TODO: attributes =>
276 wakaba 1.2 first_child => undef,
277     last_child => undef,
278     local_name => 'e',
279     manakai_local_name => 'e',
280     namespace_uri => undef,
281     next_sibling => undef,
282 wakaba 1.1 node_name => 'e',
283     node_value => undef,
284 wakaba 1.2 owner_document => $doc,
285     parent_node => undef,
286     prefix => undef,
287     previous_sibling => undef,
288 wakaba 1.1 },
289     },
290     element_ns_default => {
291     node => sub { return $doc->create_element_ns ('http://test/', 'f') },
292     attr_get => {
293     ## TODO: attributes =>
294 wakaba 1.2 first_child => undef,
295     last_child => undef,
296     local_name => 'f',
297     manakai_local_name => 'f',
298     namespace_uri => 'http://test/',
299     next_sibling => undef,
300 wakaba 1.1 node_name => 'f',
301     node_value => undef,
302 wakaba 1.2 owner_document => $doc,
303     parent_node => undef,
304     prefix => undef,
305     previous_sibling => undef,
306 wakaba 1.1 },
307     },
308     element_ns_prefiexed => {
309     node => sub { return $doc->create_element_ns ('http://test/', 'e:f') },
310     attr_get => {
311     ## TODO: attributes =>
312 wakaba 1.2 first_child => undef,
313     last_child => undef,
314     local_name => 'f',
315     manakai_local_name => 'f',
316     namespace_uri => 'http://test/',
317     next_sibling => undef,
318 wakaba 1.1 node_name => 'e:f',
319     node_value => undef,
320 wakaba 1.2 owner_document => $doc,
321     parent_node => undef,
322     prefix => 'e',
323     previous_sibling => undef,
324 wakaba 1.1 },
325     },
326     entity => {
327     node => sub { return $doc->create_general_entity ('e') },
328     attr_get => {
329     attributes => undef,
330 wakaba 1.2 first_child => undef,
331     last_child => undef,
332     next_sibling => undef,
333 wakaba 1.1 node_name => 'e',
334     node_value => undef,
335 wakaba 1.2 owner_document => $doc,
336     parent_node => undef,
337     previous_sibling => undef,
338 wakaba 1.1 },
339     },
340     entity_reference => {
341     node => sub { return $doc->create_entity_reference ('e') },
342     attr_get => {
343     attributes => undef,
344 wakaba 1.2 first_child => undef,
345     last_child => undef,
346     local_name => undef,
347     manakai_local_name => undef,
348     namespace_uri => undef,
349     next_sibling => undef,
350 wakaba 1.1 node_name => 'e',
351     node_value => undef,
352 wakaba 1.2 owner_document => $doc,
353     parent_node => undef,
354     prefix => undef,
355     previous_sibling => undef,
356 wakaba 1.1 },
357     },
358     notation => {
359     node => sub { return $doc->create_notation ('e') },
360     attr_get => {
361     attributes => undef,
362 wakaba 1.2 first_child => undef,
363     last_child => undef,
364     local_name => undef,
365     manakai_local_name => undef,
366     namespace_uri => undef,
367     next_sibling => undef,
368 wakaba 1.1 node_name => 'e',
369     node_value => undef,
370 wakaba 1.2 owner_document => $doc,
371     parent_node => undef,
372     prefix => undef,
373     previous_sibling => undef,
374 wakaba 1.1 },
375     },
376     processing_instruction => {
377     node => sub { return $doc->create_processing_instruction ('t', 'd') },
378     attr_get => {
379     attributes => undef,
380 wakaba 1.2 first_child => undef,
381     last_child => undef,
382     local_name => undef,
383     manakai_local_name => undef,
384     namespace_uri => undef,
385     next_sibling => undef,
386 wakaba 1.1 node_name => 't',
387     node_value => 'd',
388 wakaba 1.2 owner_document => $doc,
389     parent_node => undef,
390     prefix => undef,
391     previous_sibling => undef,
392 wakaba 1.1 },
393     },
394     text => {
395     node => sub { return $doc->create_text_node ('textdata') },
396     attr_get => {
397     attributes => undef,
398 wakaba 1.2 first_child => undef,
399     last_child => undef,
400     local_name => undef,
401     manakai_local_name => undef,
402     namespace_uri => undef,
403     next_sibling => undef,
404 wakaba 1.1 node_name => '#text',
405     node_value => 'textdata',
406 wakaba 1.2 owner_document => $doc,
407     parent_node => undef,
408     prefix => undef,
409     previous_sibling => undef,
410 wakaba 1.1 },
411     },
412     element_type_definition => {
413     node => sub { return $doc->create_element_type_definition ('e') },
414     attr_get => {
415     attributes => undef,
416 wakaba 1.2 first_child => undef,
417     last_child => undef,
418     local_name => undef,
419     manakai_local_name => undef,
420     namespace_uri => undef,
421     next_sibling => undef,
422 wakaba 1.1 node_name => 'e',
423     node_value => undef,
424 wakaba 1.2 owner_document => $doc,
425     parent_node => undef,
426     prefix => undef,
427     previous_sibling => undef,
428 wakaba 1.1 },
429     },
430     attribute_definition => {
431     node => sub { return $doc->create_attribute_definition ('e') },
432     attr_get => {
433     attributes => undef,
434 wakaba 1.2 first_child => undef,
435     last_child => undef,
436     local_name => undef,
437     manakai_local_name => undef,
438     namespace_uri => undef,
439     next_sibling => undef,
440 wakaba 1.1 node_name => 'e',
441     node_value => undef,
442 wakaba 1.2 owner_document => $doc,
443     parent_node => undef,
444     prefix => undef,
445     previous_sibling => undef,
446 wakaba 1.1 },
447     },
448     };
449    
450     for my $test_id (sort {$a cmp $b} keys %$tests) {
451     my $test_def = $tests->{$test_id};
452     my $node = $test_def->{node}->();
453    
454     for (@$constants) {
455     my $const_name = $_->[0];
456     ok $node->can ($const_name) ? 1 : 0, 1, "$test_id->can ($const_name)";
457     ok $node->$const_name, $_->[1], "$test_id.$const_name";
458     }
459    
460     for my $attr_name (sort {$a cmp $b} keys %{$test_def->{attr_get}}) {
461     my $expected = $test_def->{attr_get}->{$attr_name};
462     ok $node->can ($attr_name) ? 1 : 0, 1, "$test_id->can ($attr_name)";
463     my $actual = $node->$attr_name;
464     ok $actual, $expected, "$test_id.$attr_name.get";
465     }
466     }
467    
468 wakaba 1.2 ## Child node accessors' tests
469     for my $parent (create_parent_nodes ()) {
470     my $node1;
471     my $node2;
472     my $node3;
473     if ($parent->node_type == $parent->DOCUMENT_TYPE_NODE) {
474     $node1 = $doc->create_processing_instruction ('pi1', 'data1');
475     $node2 = $doc->create_processing_instruction ('pi2', 'data2');
476     $node3 = $doc->create_processing_instruction ('pi3', 'data3');
477     } elsif ($parent->node_type == $parent->DOCUMENT_NODE) {
478     $node1 = $doc->create_comment ('comment1');
479     $node2 = $doc->create_comment ('comment2');
480     $node3 = $doc->create_comment ('comment3');
481     } else {
482     $node1 = $doc->create_text_node ('text1');
483     $node2 = $doc->create_text_node ('text2');
484     $node3 = $doc->create_text_node ('text3');
485     }
486    
487     $parent->append_child ($node1);
488     ok $parent->first_child, $node1, $parent->node_name."->first_child [1]";
489     ok $parent->last_child, $node1, $parent->node_name."->last_child [1]";
490     ok $node1->next_sibling, undef, $parent->node_name."->next_sibling [1]";
491     ok $node1->previous_sibling, undef, $parent->node_name."->previous_sibling [1]";
492    
493     $parent->append_child ($node2);
494     ok $parent->first_child, $node1, $parent->node_name."->first_child [2]";
495     ok $parent->last_child, $node2, $parent->node_name."->last_child [2]";
496     ok $node1->next_sibling, $node2, $parent->node_name."1->next_sibling [2]";
497     ok $node1->previous_sibling, undef, $parent->node_name."1->previous_sibling [2]";
498     ok $node2->next_sibling, undef, $parent->node_name."2->next_sibling [2]";
499     ok $node2->previous_sibling, $node1, $parent->node_name."2->previous_sibling [2]";
500    
501     $parent->append_child ($node3);
502     ok $parent->first_child, $node1, $parent->node_name."->first_child [3]";
503     ok $parent->last_child, $node3, $parent->node_name."->last_child [3]";
504     ok $node1->next_sibling, $node2, $parent->node_name."1->next_sibling [3]";
505     ok $node1->previous_sibling, undef, $parent->node_name."1->previous_sibling [3]";
506     ok $node2->next_sibling, $node3, $parent->node_name."2->next_sibling [3]";
507     ok $node2->previous_sibling, $node1, $parent->node_name."2->previous_sibling [3]";
508     ok $node3->next_sibling, undef, $parent->node_name."3->next_sibling [3]";
509     ok $node3->previous_sibling, $node2, $parent->node_name."3->previous_sibling [3]";
510     }
511    
512     ## TODO: parent_node tests, as with append_child tests
513    
514     sub create_nodes () {
515     (
516     $doc->create_attribute ('attr1'),
517     $doc->create_attribute_definition ('at1'),
518     $doc->create_cdata_section ('cdata1'),
519     $doc->create_comment ('comment1'),
520     $doc->create_element ('element1'),
521     $doc->create_element_type_definition ('et1'),
522     $doc->create_general_entity ('entity1'),
523     $doc->create_entity_reference ('entity-reference1'),
524     $doc->implementation->create_document,
525     $doc->create_document_fragment,
526     $doc->create_document_type_definition ('dt1'),
527     $doc->create_notation ('notation1'),
528     $doc->create_processing_instruction ('pi1', 'pi1data'),
529     $doc->create_text_node ('text1'),
530     );
531     } # create_nodes
532    
533     sub create_parent_nodes () {
534     (
535     $doc->create_attribute ('attr1'),
536     $doc->create_attribute_definition ('at1'),
537     $doc->create_element ('element1'),
538     $doc->create_general_entity ('entity1'),
539     $doc->create_entity_reference ('entity-reference1'),
540     $doc->implementation->create_document,
541     $doc->create_document_fragment,
542     $doc->create_document_type_definition ('dt1'),
543     );
544     } # create_parent_nodes
545    
546 wakaba 1.1 ## License: Public Domain.
547 wakaba 1.2 ## $Date: 2007/06/15 14:32:50 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24