Debian Codegraph @

t/08findnodes.t

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use strict;
2 use warnings;
3
4 # Should be 45.
5 use Test::More tests => 45;
6
7 use XML::LibXML;
8
9 # to test if findnodes works.
10 # i added findnodes to the node class, so a query can be started
11 # everywhere.
12
13 my $file = "example/dromeds.xml";
14
15 # init the file parser
16 my $parser = XML::LibXML->new();
17 my $dom = $parser->parse_file( $file );
18
19 if ( defined $dom ) {
20 # get the root document
21 my $elem = $dom->getDocumentElement();
22
23 # first very simple path starting at root
24 my @list = $elem->findnodes( "species" );
25 # TEST
26 is( scalar(@list), 3, ' TODO : Add test name' );
27
28 # a simple query starting somewhere ...
29 my $node = $list[0];
30 my @slist = $node->findnodes( "humps" );
31 # TEST
32 is( scalar(@slist), 1, ' TODO : Add test name' );
33
34 # find a single node
35 @list = $elem->findnodes( "species[\@name='Llama']" );
36 # TEST
37 is( scalar( @list ), 1, ' TODO : Add test name' );
38
39 # find with not conditions
40 @list = $elem->findnodes( "species[\@name!='Llama']/disposition" );
41 # TEST
42 is( scalar(@list), 2, ' TODO : Add test name' );
43
44
45 @list = $elem->findnodes( 'species/@name' );
46 # warn $elem->toString();
47
48 # TEST
49
50 ok( scalar @list && $list[0]->toString() eq ' name="Camel"', ' TODO : Add test name' );
51
52 my $x = XML::LibXML::Text->new( 1234 );
53 if( defined $x ) {
54 # TEST
55 is( $x->getData(), "1234", ' TODO : Add test name' );
56 }
57
58 my $telem = $dom->createElement('test');
59 $telem->appendWellBalancedChunk('<b>c</b>');
60
61 finddoc($dom);
62 # TEST
63 ok(1, ' TODO : Add test name');
64 }
65 # TEST
66
67 ok( $dom, ' TODO : Add test name' );
68
69 # test to make sure that multiple array findnodes() returns
70 # don't segfault perl; it'll happen after the second one if it does
71 for (0..3) {
72 my $doc = XML::LibXML->new->parse_string(
73 '<?xml version="1.0" encoding="UTF-8"?>
74 <?xsl-stylesheet type="text/xsl" href="a.xsl"?>
75 <a />');
76 my @nds = $doc->findnodes("processing-instruction('xsl-stylesheet')");
77 }
78
79 my $doc = $parser->parse_string(<<'EOT');
80 <a:foo xmlns:a="http://foo.com" xmlns:b="http://bar.com">
81 <b:bar>
82 <a:foo xmlns:a="http://other.com"/>
83 </b:bar>
84 </a:foo>
85 EOT
86
87 my $root = $doc->getDocumentElement;
88 my @a = $root->findnodes('//a:foo');
89 # TEST
90
91 is(@a, 1, ' TODO : Add test name');
92
93 my @b = $root->findnodes('//b:bar');
94 # TEST
95
96 is(@b, 1, ' TODO : Add test name');
97
98 my @none = $root->findnodes('//b:foo');
99 @none = (@none, $root->findnodes('//foo'));
100 # TEST
101
102 is(@none, 0, ' TODO : Add test name');
103
104 my @doc = $root->findnodes('document("example/test.xml")');
105 # TEST
106
107 ok(@doc, ' TODO : Add test name');
108 # warn($doc[0]->toString);
109
110 # this query should result an empty array!
111 my @nodes = $root->findnodes( "/humpty/dumpty" );
112 # TEST
113
114 is( scalar(@nodes), 0, ' TODO : Add test name' );
115
116
117 my $docstring = q{
118 <foo xmlns="http://kungfoo" xmlns:bar="http://foo"/>
119 };
120 $doc = $parser->parse_string( $docstring );
121 $root = $doc->documentElement;
122
123 my @ns = $root->findnodes('namespace::*');
124 # TEST
125
126 is(scalar(@ns), 2, ' TODO : Add test name' );
127
128 # bad xpaths
129 # TEST:$badxpath=4;
130 my @badxpath = (
131 'abc:::def',
132 'foo///bar',
133 '...',
134 '/-',
135 );
136
137 foreach my $xp ( @badxpath ) {
138 my $res;
139 eval { $res = $root->findnodes( $xp ); };
140 # TEST*$badxpath
141 ok($@, ' TODO : Add test name');
142 eval { $res = $root->find( $xp ); };
143 # TEST*$badxpath
144 ok($@, ' TODO : Add test name');
145 eval { $res = $root->findvalue( $xp ); };
146 # TEST*$badxpath
147 ok($@, ' TODO : Add test name');
148 eval { $res = $root->findnodes( encodeToUTF8( "iso-8859-1", $xp ) ); };
149 # TEST*$badxpath
150 ok($@, ' TODO : Add test name');
151 eval { $res = $root->find( encodeToUTF8( "iso-8859-1", $xp ) );};
152 # TEST*$badxpath
153 ok($@, ' TODO : Add test name');
154 }
155
156
157 {
158 # as reported by jian lou:
159 # 1. getElementByTagName("myTag") is not working is
160 # "myTag" is a node directly under root. Same problem
161 # for findNodes("//myTag")
162 # 2. When I add new nodes into DOM tree by
163 # appendChild(). Then try to find them by
164 # getElementByTagName("newNodeTag"), the newly created
165 # nodes are not returned. ...
166 #
167 # this seems not to be a problem by XML::LibXML itself, but newer versions
168 # of libxml2 (newer is 2.4.27 or later)
169 #
170 my $doc = XML::LibXML->createDocument();
171 my $root= $doc->createElement( "A" );
172 $doc->setDocumentElement($root);
173
174 my $b= $doc->createElement( "B" );
175 $root->appendChild( $b );
176
177 my @list = $doc->findnodes( '//A' );
178 # TEST
179 ok( scalar @list, ' TODO : Add test name' );
180 # TEST
181 ok( $list[0]->isSameNode( $root ), ' TODO : Add test name' );
182
183 @list = $doc->findnodes( '//B' );
184 # TEST
185 ok( scalar @list, ' TODO : Add test name' );
186 # TEST
187 ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' );
188
189
190 # @list = $doc->getElementsByTagName( "A" );
191 # ok( scalar @list );
192 # ok( $list[0]->isSameNode( $root ) );
193
194 @list = $root->getElementsByTagName( 'B' );
195 # TEST
196 ok( scalar @list, ' TODO : Add test name' );
197 # TEST
198 ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' );
199 }
200
201 {
202 # test potential unbinding-segfault-problem
203 my $doc = XML::LibXML->createDocument();
204 my $root= $doc->createElement( "A" );
205 $doc->setDocumentElement($root);
206
207 my $b= $doc->createElement( "B" );
208 $root->appendChild( $b );
209 my $c= $doc->createElement( "C" );
210 $b->appendChild( $c );
211 $b= $doc->createElement( "B" );
212 $root->appendChild( $b );
213 $c= $doc->createElement( "C" );
214 $b->appendChild( $c );
215
216 my @list = $root->findnodes( "B" );
217 # TEST
218 is( scalar(@list) , 2, ' TODO : Add test name' );
219 foreach my $node ( @list ) {
220 my @subnodes = $node->findnodes( "C" );
221 $node->unbindNode() if ( scalar( @subnodes ) );
222 # TEST*2
223 ok(1, ' TODO : Add test name');
224 }
225 }
226
227 {
228 # findnode remove problem
229
230 my $xmlstr = "<a><b><c>1</c><c>2</c></b></a>";
231
232 my $doc = $parser->parse_string( $xmlstr );
233 my $root = $doc->documentElement;
234 my ( $lastc ) = $root->findnodes( 'b/c[last()]' );
235 # TEST
236 ok( $lastc, ' TODO : Add test name' );
237
238 $root->removeChild( $lastc );
239 # TEST
240 is( $root->toString(), $xmlstr, ' TODO : Add test name' );
241 }
242
243 # --------------------------------------------------------------------------- #
244 sub finddoc {
245 my $doc = shift;
246 return unless defined $doc;
247 my $rn = $doc->documentElement;
248 $rn->findnodes("/");
249 }