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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download) (as text)
Sat Jun 16 08:49:00 2007 UTC (17 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +138 -1 lines
File MIME type: application/x-troff
++ manakai/t/ChangeLog	16 Jun 2007 08:48:42 -0000
	* DOM-NodeList.t: Tests for tied arrays and empty node lists
	are added.

2007-06-16  Wakaba  <wakaba@suika.fam.cx>

++ manakai/lib/Message/DOM/ChangeLog	16 Jun 2007 08:48:03 -0000
	* DOMException.pm (Message::IF::DOMException): Extends
	the |Message::Util::Error| class.

	* NodeList.pm (Message::DOM::NodeList): Extends the |Tie::Array| class.
	(CLEAR): Not all items were removed.

2007-06-16  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3     use Test;
4     BEGIN { plan tests => 10 }
5    
6     require Message::DOM::DOMImplementation;
7 wakaba 1.2 use Message::Util::Error;
8 wakaba 1.1
9     my $dom = Message::DOM::DOMImplementation->____new;
10     my $doc = $dom->create_document;
11    
12     for my $parent (create_parent_nodes ()) {
13     my $node1;
14     my $node2;
15     my $node1a;
16     my $node2a;
17     if ($parent->node_type == $parent->DOCUMENT_TYPE_NODE) {
18     $node1 = $doc->create_processing_instruction ('pi1', 'data1');
19     $node2 = $doc->create_processing_instruction ('pi2', 'data2');
20     $node1a = $doc->create_processing_instruction ('pi1', 'data1');
21     $node2a = $doc->create_processing_instruction ('pi2', 'data2');
22     } elsif ($parent->node_type == $parent->DOCUMENT_NODE) {
23     $node1 = $doc->create_comment ('comment1');
24     $node2 = $doc->create_comment ('comment2');
25     $node1a = $doc->create_comment ('comment1');
26     $node2a = $doc->create_comment ('comment2');
27     } else {
28     $node1 = $doc->create_text_node ('text1');
29     $node2 = $doc->create_text_node ('text2');
30     $node1a = $doc->create_text_node ('text1');
31     $node2a = $doc->create_text_node ('text2');
32     }
33    
34     my $cn = $parent->child_nodes;
35     ok UNIVERSAL::isa ($cn, 'Message::IF::NodeList') ? 1 : 0, 1,
36     'childNodes.interface';
37     ok $cn ? 1 : 0, 1, 'bool';
38    
39     ok $cn->can ('manakai_read_only') ? 1 : 0, 1, 'can childNodes.manakaiReadOnly';
40     $parent->manakai_set_read_only (1);
41     ok $cn->manakai_read_only ? 1 : 0, 1, 'childNodes.manakaiReadOnly (1)';
42     $parent->manakai_set_read_only (0);
43     ok $cn->manakai_read_only ? 1 : 0, 0, 'childNodes.manakaiReadOnly (0)';
44    
45     ok $cn->can ('length') ? 1 : 0, 1, 'can childNodes.length [0]';
46     ok $cn->length, 0, 'childNodes.length [0]';
47 wakaba 1.2 ok 0+@$cn, 0, '@{child_nodes} [0]';
48 wakaba 1.1
49     ok $cn->can ('item') ? 1 : 0, 1, 'can childNodes.item';
50     ok $cn->item (0), undef, 'childNodes.item (0) [0]';
51     ok $cn->item (1), undef, 'childNodes.item (1) [0]';
52     ok $cn->item (-1), undef, 'childNodes.item (-1) [0]';
53     ok $cn->item (undef), undef, 'childNodes.item (undef) [0]';
54 wakaba 1.2 ok $cn->[0], undef, 'child_nodes->[0] [0]';
55     ok $cn->[1], undef, 'child_nodes->[1] [0]';
56 wakaba 1.1
57     $parent->append_child ($node1);
58    
59     ok $cn->length, 1, 'childNodes.length [1]';
60    
61     ok $cn->item (0), $node1, 'childNodes.item (0) [1]';
62     ok $cn->item (1), undef, 'childNodes.item (1) [1]';
63     ok $cn->item (-1), undef, 'childNodes.item (-1) [1]';
64     ok $cn->item (undef), $node1, 'childNodes.item (undef) [1]';
65 wakaba 1.2 ok $cn->[0], $node1, 'child_nodes->[0] [1]';
66     ok $cn->[1], undef, 'child_nodes->[1] [1]';
67     ok exists $cn->[0] ? 1 : 0, 1, 'exists child_nodes->[0] [1]';
68     ok exists $cn->[1] ? 1 : 0, 0, 'exists child_nodes->[1] [1]';
69 wakaba 1.1
70     $parent->append_child ($node2);
71    
72     ok $cn->length, 2, 'childNodes.length [2]';
73    
74     ok $cn->item (0), $node1, 'childNodes.item (0) [2]';
75     ok $cn->item (1), $node2, 'childNodes.item (1) [2]';
76     ok $cn->item (-1), undef, 'childNodes.item (-1) [2]';
77     ok $cn->item (undef), $node1, 'childNodes.item (undef) [2]';
78 wakaba 1.2 ok $cn->[0], $node1, 'child_nodes->[0] [2]';
79     ok $cn->[1], $node2, 'child_nodes->[1] [2]';
80 wakaba 1.1
81     ok $cn eq $cn ? 1 : 0, 1, 'A eq A';
82     ok $cn ne $cn ? 1 : 0, 0, 'A ne A';
83     ok $cn == $cn ? 1 : 0, 1, 'A == A';
84     ok $cn != $cn ? 1 : 0, 0, 'A != A';
85    
86     my $cn2 = $parent->child_nodes;
87     ok $cn eq $cn2 ? 1 : 0, 1, "A eq A'";
88     ok $cn ne $cn2 ? 1 : 0, 0, "A ne A'";
89     ok $cn == $cn2 ? 1 : 0, 1, "A == A'";
90     ok $cn != $cn2 ? 1 : 0, 0, "A != A'";
91    
92     my $parenta = $doc->create_element ('element');
93     my $cn3 = $parenta->child_nodes;
94     ok $cn eq $cn3 ? 1 : 0, 0, 'A eq B (A != B)';
95     ok $cn ne $cn3 ? 1 : 0, 1, 'A ne B (A != B)';
96     ok $cn == $cn3 ? 1 : 0, 0, 'A == B (A != B)';
97     ok $cn != $cn3 ? 1 : 0, 1, 'A != B (A != B)';
98    
99     $parenta->append_child ($node1a);
100     $parenta->append_child ($node2a);
101     ok $cn eq $cn3 ? 1 : 0, 0, 'A eq B (A == B)';
102     ok $cn ne $cn3 ? 1 : 0, 1, 'A ne B (A == B)';
103     ok $cn == $cn3 ? 1 : 0, 1, 'A == B (A == B)';
104     ok $cn != $cn3 ? 1 : 0, 0, 'A != B (A == B)';
105 wakaba 1.2
106     $cn->[2] = $node1a;
107     ok $cn->[2], $node1a, 'child_nodes->[2] setter (item)';
108     ok 0+@$cn, 3, 'child_nodes->[2] setter (length)';
109    
110     $cn->[4] = $node2a;
111     ok $cn->[3], $node2a, 'child_nodes->[3] setter (item)';
112     ok $cn->[4], undef, 'child_nodes->[4] setter (item)';
113     ok 0+@$cn, 4, 'child_nodes->[4] setter (length)';
114     ## TODO: Add replaceChild test
115    
116     my $deleted = delete $cn->[4];
117     ok $deleted, undef, 'delete child_nodes->[4]';
118     ok $cn->[3], $node2a, 'delete child_nodes->[4] item(3)';
119     ok $cn->[4], undef, 'delete child_nodes->[4] item(4)';
120    
121     $deleted = delete $cn->[0];
122     ok $deleted, $node1, 'delete child_nodes->[0]';
123     ok $cn->[0], $node2, 'delete child_nodes->[0] item(0)';
124     ok $cn->[1], $node1a, 'delete child_nodes->[0] item(1)';
125     ok $cn->[2], $node2a, 'delete child_nodes->[0] item(2)';
126     ok $cn->[3], undef, 'delete child_nodes->[0] item(3)';
127     ok $cn->[4], undef, 'delete child_nodes->[0] item(4)';
128    
129     my @a = @$cn;
130     ok 0+@a, 3;
131     ok $a[0], $node2, '@ = @{child_nodes} item(0)';
132     ok $a[1], $node1a, '@ = @{child_nodes} item(1)';
133     ok $a[2], $node2a, '@ = @{child_nodes} item(2)';
134     ok $a[3], undef, '@ = @{child_nodes} item(3)';
135    
136     @$cn = ();
137     ok 0+@$cn, 0, 'child_nodes->clear';
138 wakaba 1.1 }
139    
140 wakaba 1.2 for my $parent (create_leaf_nodes ()) {
141     my $node1;
142     my $node2;
143     my $node1a;
144     my $node2a;
145     $node1 = $doc->create_text_node ('text1');
146     $node2 = $doc->create_text_node ('text2');
147     $node1a = $doc->create_text_node ('text1');
148     $node2a = $doc->create_text_node ('text2');
149    
150     my $cn = $parent->child_nodes;
151     ok UNIVERSAL::isa ($cn, 'Message::IF::NodeList') ? 1 : 0, 1,
152     'childNodes.interface';
153     ok $cn ? 1 : 0, 1, 'bool';
154    
155     ok $cn->can ('manakai_read_only') ? 1 : 0, 1, 'can childNodes.manakaiReadOnly';
156     $parent->manakai_set_read_only (1);
157     ok $cn->manakai_read_only ? 1 : 0, 1, 'childNodes.manakaiReadOnly (1)';
158     $parent->manakai_set_read_only (0);
159     ok $cn->manakai_read_only ? 1 : 0, 1, 'childNodes.manakaiReadOnly (0)';
160    
161     ok $cn->can ('length') ? 1 : 0, 1, 'can childNodes.length [0]';
162     ok $cn->length, 0, 'childNodes.length [0]';
163     ok 0+@$cn, 0, '@{child_nodes} [0]';
164    
165     ok $cn->can ('item') ? 1 : 0, 1, 'can childNodes.item';
166     ok $cn->item (0), undef, 'childNodes.item (0) [0]';
167     ok $cn->item (1), undef, 'childNodes.item (1) [0]';
168     ok $cn->item (-1), undef, 'childNodes.item (-1) [0]';
169     ok $cn->item (undef), undef, 'childNodes.item (undef) [0]';
170     ok $cn->[0], undef, 'child_nodes->[0] [0]';
171     ok $cn->[1], undef, 'child_nodes->[1] [0]';
172     ok exists $cn->[0] ? 1 : 0, 0, 'exists child_nodes->[0] [1]';
173     ok exists $cn->[1] ? 1 : 0, 0, 'exists child_nodes->[1] [1]';
174    
175     ok $cn eq $cn ? 1 : 0, 1, 'A eq A';
176     ok $cn ne $cn ? 1 : 0, 0, 'A ne A';
177     ok $cn == $cn ? 1 : 0, 1, 'A == A';
178     ok $cn != $cn ? 1 : 0, 0, 'A != A';
179    
180     my $cn2 = $parent->child_nodes;
181     ok $cn eq $cn2 ? 1 : 0, 1, "A eq A'";
182     ok $cn ne $cn2 ? 1 : 0, 0, "A ne A'";
183     ok $cn == $cn2 ? 1 : 0, 1, "A == A'";
184     ok $cn != $cn2 ? 1 : 0, 0, "A != A'";
185    
186     my $parenta = $doc->create_element ('element');
187     my $cn3 = $parenta->child_nodes;
188     ok $cn eq $cn3 ? 1 : 0, 0, 'A eq B (A != B)';
189     ok $cn ne $cn3 ? 1 : 0, 1, 'A ne B (A != B)';
190     ok $cn == $cn3 ? 1 : 0, 1, 'A == B (A != B)';
191     ok $cn != $cn3 ? 1 : 0, 0, 'A != B (A != B)';
192    
193     try {
194     $cn->[2] = $node1a;
195     ok 0, 1, $parent->node_name . '->child_nodes->[2] = $node';
196     } catch Message::IF::DOMException with {
197     my $err = shift;
198     ok $err->type, 'NO_MODIFICATION_ALLOWED_ERR',
199     $parent->node_name . '->child_nodes->[2] = $node';
200     };
201     ok 0+@$cn, 0, $parent->node_name . '->child_nodes->[2] (length)';
202    
203     try {
204     delete $cn->[4];
205     ok 0, 1, $parent->node_name . '->child_nodes->[2] delete';
206     } catch Message::IF::DOMException with {
207     my $err = shift;
208     ok $err->type, 'NO_MODIFICATION_ALLOWED_ERR',
209     $parent->node_name . '->child_nodes->[2] delete';
210     };
211    
212     my @a = @$cn;
213     ok 0+@a, 0, $parent->node_type . '->child_nodes @{} = 0+';
214    
215     try {
216     @$cn = ();
217     ok 0, 1, $parent->node_name . '->child_nodes->[2] delete';
218     } catch Message::IF::DOMException with {
219     my $err = shift;
220     ok $err->type, 'NO_MODIFICATION_ALLOWED_ERR',
221     $parent->node_name . '->child_nodes @{} CLEAR';
222     };
223     }
224 wakaba 1.1
225     sub create_leaf_nodes () {
226     (
227     $doc->create_cdata_section ('cdata1'),
228     $doc->create_comment ('comment1'),
229     $doc->create_notation ('notation1'),
230     $doc->create_processing_instruction ('pi1', 'pi1data'),
231     $doc->create_text_node ('text1'),
232     );
233     } # create_leaf_nodes
234    
235     sub create_parent_nodes () {
236     (
237     $doc->create_attribute ('attr1'),
238     $doc->create_attribute_definition ('at1'),
239     $doc->create_element ('element1'),
240     $doc->create_general_entity ('entity1'),
241     $doc->create_entity_reference ('entity-reference1'),
242     $doc->implementation->create_document,
243     $doc->create_document_fragment,
244     $doc->create_document_type_definition ('dt1'),
245     );
246     } # create_parent_nodes
247 wakaba 1.2
248     =head1 LICENSE
249    
250     Copyright 2007 Wakaba <w@suika.fam.cx>
251    
252     This program is free software; you can redistribute it and/or
253     modify it under the same terms as Perl itself.
254    
255     =cut
256    
257     ## $Date: 2007/06/15 16:12:28 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24