Debian Codegraph @

t/30xpathcontext.t

raw
1
2 use strict;
3 use warnings;
4
5 use Test::More tests => 82;
6
7 use XML::LibXML;
8 use XML::LibXML::XPathContext;
9
10 my $doc = XML::LibXML->new->parse_string(<<'XML');
11 <foo><bar a="b"></bar></foo>
12 XML
13
14 # test findnodes() in list context
15 my $xpath = '/*';
16 # TEST:$exp=2;
17 for my $exp ($xpath, XML::LibXML::XPathExpression->new($xpath)) {
18 my @nodes = XML::LibXML::XPathContext->new($doc)->findnodes($exp);
19 # TEST*$exp
20 ok(@nodes == 1, ' TODO : Add test name');
21 # TEST*$exp
22 ok($nodes[0]->nodeName eq 'foo', ' TODO : Add test name');
23 # TEST*$exp
24 is(
25 (XML::LibXML::XPathContext->new($nodes[0])->findnodes('bar'))[0]->nodeName(),
26 'bar',
27 ' TODO : Add test list',
28 );
29 }
30
31
32 # test findnodes() in scalar context
33 # TEST:$exp=2;
34 for my $exp ($xpath, XML::LibXML::XPathExpression->new($xpath)) {
35 my $nl = XML::LibXML::XPathContext->new($doc)->findnodes($exp);
36 # TEST*$exp
37 ok($nl->pop->nodeName eq 'foo', ' TODO : Add test name');
38 # TEST*$exp
39 ok(!defined($nl->pop), ' TODO : Add test name');
40 }
41
42 # test findvalue()
43 # TEST
44 ok(XML::LibXML::XPathContext->new($doc)->findvalue('1+1') == 2, ' TODO : Add test name');
45 # TEST
46
47 ok(XML::LibXML::XPathContext->new($doc)->findvalue(XML::LibXML::XPathExpression->new('1+1')) == 2, ' TODO : Add test name');
48 # TEST
49
50 ok(XML::LibXML::XPathContext->new($doc)->findvalue('1=2') eq 'false', ' TODO : Add test name');
51 # TEST
52
53 ok(XML::LibXML::XPathContext->new($doc)->findvalue(XML::LibXML::XPathExpression->new('1=2')) eq 'false', ' TODO : Add test name');
54
55 # test find()
56 # TEST
57 ok(XML::LibXML::XPathContext->new($doc)->find('/foo/bar')->pop->nodeName eq 'bar', ' TODO : Add test name');
58 # TEST
59
60 ok(XML::LibXML::XPathContext->new($doc)->find(XML::LibXML::XPathExpression->new('/foo/bar'))->pop->nodeName eq 'bar', ' TODO : Add test name');
61
62 # TEST
63
64 ok(XML::LibXML::XPathContext->new($doc)->find('1*3')->value == '3', ' TODO : Add test name');
65 # TEST
66
67 ok(XML::LibXML::XPathContext->new($doc)->find('1=1')->to_literal eq 'true', ' TODO : Add test name');
68
69 my $doc1 = XML::LibXML->new->parse_string(<<'XML');
70 <foo xmlns="http://example.com/foobar"><bar a="b"></bar></foo>
71 XML
72
73 # test registerNs()
74 my $compiled = XML::LibXML::XPathExpression->new('/xxx:foo');
75 my $xc = XML::LibXML::XPathContext->new($doc1);
76 $xc->registerNs('xxx', 'http://example.com/foobar');
77 # TEST
78
79 ok($xc->findnodes('/xxx:foo')->pop->nodeName eq 'foo', ' TODO : Add test name');
80 # TEST
81
82 ok($xc->findnodes($compiled)->pop->nodeName eq 'foo', ' TODO : Add test name');
83 # TEST
84
85 ok($xc->lookupNs('xxx') eq 'http://example.com/foobar', ' TODO : Add test name');
86 # TEST
87
88 ok($xc->exists('//xxx:bar/@a'), ' TODO : Add test name');
89 # TEST
90
91 is($xc->exists('//xxx:bar/@b'),0, ' TODO : Add test name');
92 # TEST
93
94 ok($xc->exists('xxx:bar',$doc1->getDocumentElement), ' TODO : Add test name');
95
96 # test unregisterNs()
97 $xc->unregisterNs('xxx');
98 eval { $xc->findnodes('/xxx:foo') };
99 # TEST
100
101 ok($@, ' TODO : Add test name');
102 # TEST
103
104 ok(!defined($xc->lookupNs('xxx')), ' TODO : Add test name');
105
106 eval { $xc->findnodes($compiled) };
107 # TEST
108
109 ok($@, ' TODO : Add test name');
110 # TEST
111
112 ok(!defined($xc->lookupNs('xxx')), ' TODO : Add test name');
113
114 # test getContextNode and setContextNode
115 # TEST
116 ok($xc->getContextNode->isSameNode($doc1), ' TODO : Add test name');
117 $xc->setContextNode($doc1->getDocumentElement);
118 # TEST
119
120 ok($xc->getContextNode->isSameNode($doc1->getDocumentElement), ' TODO : Add test name');
121 # TEST
122
123 ok($xc->findnodes('.')->pop->isSameNode($doc1->getDocumentElement), ' TODO : Add test name');
124
125 # test xpath context preserves the document
126 my $xc2 = XML::LibXML::XPathContext->new(
127 XML::LibXML->new->parse_string(<<'XML'));
128 <foo/>
129 XML
130 # TEST
131
132 ok($xc2->findnodes('*')->pop->nodeName eq 'foo', ' TODO : Add test name');
133
134 # test xpath context preserves context node
135 my $doc2 = XML::LibXML->new->parse_string(<<'XML');
136 <foo><bar/></foo>
137 XML
138 my $xc3 = XML::LibXML::XPathContext->new($doc2->getDocumentElement);
139 $xc3->find('/');
140 # TEST
141
142 ok($xc3->getContextNode->toString() eq '<foo><bar/></foo>', ' TODO : Add test name');
143
144 # check starting with empty context
145 my $xc4 = XML::LibXML::XPathContext->new();
146 # TEST
147
148 ok(!defined($xc4->getContextNode), ' TODO : Add test name');
149 eval { $xc4->find('/') };
150 # TEST
151
152 ok($@, ' TODO : Add test name');
153 my $cn=$doc2->getDocumentElement;
154 $xc4->setContextNode($cn);
155 # TEST
156
157 ok($xc4->find('/'), ' TODO : Add test name');
158 # TEST
159
160 ok($xc4->getContextNode->isSameNode($doc2->getDocumentElement), ' TODO : Add test name');
161 $cn=undef;
162 # TEST
163
164 ok($xc4->getContextNode, ' TODO : Add test name');
165 # TEST
166
167 ok($xc4->getContextNode->isSameNode($doc2->getDocumentElement), ' TODO : Add test name');
168
169 # check temporarily changed context node
170 my ($bar)=$xc4->findnodes('foo/bar',$doc2);
171 # TEST
172
173 ok($bar->nodeName eq 'bar', ' TODO : Add test name');
174 # TEST
175
176 ok($xc4->getContextNode->isSameNode($doc2->getDocumentElement), ' TODO : Add test name');
177
178 # TEST
179
180 ok($xc4->findnodes('parent::*',$bar)->pop->nodeName eq 'foo', ' TODO : Add test name');
181 # TEST
182
183 ok($xc4->getContextNode->isSameNode($doc2->getDocumentElement), ' TODO : Add test name');
184
185 # testcase for segfault found by Steve Hay
186 my $xc5 = XML::LibXML::XPathContext->new();
187 $xc5->registerNs('pfx', 'http://www.foo.com');
188 $doc = XML::LibXML->new->parse_string('<foo xmlns="http://www.foo.com" />');
189 $xc5->setContextNode($doc);
190 $xc5->findnodes('/');
191 $xc5->setContextNode(undef);
192 $xc5->getContextNode();
193 $xc5->setContextNode($doc);
194 $xc5->findnodes('/');
195 # TEST
196
197 ok(1, ' TODO : Add test name');
198
199 # check setting context position and size
200 # TEST
201 ok($xc4->getContextPosition() == -1, ' TODO : Add test name');
202 # TEST
203
204 ok($xc4->getContextSize() == -1, ' TODO : Add test name');
205 eval { $xc4->setContextPosition(4); };
206 # TEST
207
208 ok($@, ' TODO : Add test name');
209 eval { $xc4->setContextPosition(-4); };
210 # TEST
211
212 ok($@, ' TODO : Add test name');
213 eval { $xc4->setContextSize(-4); };
214 # TEST
215
216 ok($@, ' TODO : Add test name');
217 eval { $xc4->findvalue('position()') };
218 # TEST
219
220 ok($@, ' TODO : Add test name');
221 eval { $xc4->findvalue('last()') };
222 # TEST
223
224 ok($@, ' TODO : Add test name');
225
226 $xc4->setContextSize(0);
227 # TEST
228
229 ok($xc4->getContextSize() == 0, ' TODO : Add test name');
230 # TEST
231
232 ok($xc4->getContextPosition() == 0, ' TODO : Add test name');
233 # TEST
234
235 ok($xc4->findvalue('position()')==0, ' TODO : Add test name');
236 # TEST
237
238 ok($xc4->findvalue('last()')==0, ' TODO : Add test name');
239
240 $xc4->setContextSize(4);
241 # TEST
242
243 ok($xc4->getContextSize() == 4, ' TODO : Add test name');
244 # TEST
245
246 ok($xc4->getContextPosition() == 1, ' TODO : Add test name');
247 # TEST
248
249 ok($xc4->findvalue('last()')==4, ' TODO : Add test name');
250 # TEST
251
252 ok($xc4->findvalue('position()')==1, ' TODO : Add test name');
253 eval { $xc4->setContextPosition(5); };
254 # TEST
255
256 ok($@, ' TODO : Add test name');
257 # TEST
258
259 ok($xc4->findvalue('position()')==1, ' TODO : Add test name');
260 # TEST
261
262 ok($xc4->getContextSize() == 4, ' TODO : Add test name');
263 $xc4->setContextPosition(4);
264 # TEST
265
266 ok($xc4->findvalue('position()')==4, ' TODO : Add test name');
267 # TEST
268
269 ok($xc4->findvalue('position()=last()'), ' TODO : Add test name');
270
271 $xc4->setContextSize(-1);
272 # TEST
273
274 ok($xc4->getContextPosition() == -1, ' TODO : Add test name');
275 # TEST
276
277 ok($xc4->getContextSize() == -1, ' TODO : Add test name');
278 eval { $xc4->findvalue('position()') };
279 # TEST
280
281 ok($@, ' TODO : Add test name');
282 eval { $xc4->findvalue('last()') };
283 # TEST
284
285 ok($@, ' TODO : Add test name');
286
287 {
288 my $d = XML::LibXML->new()->parse_string(q~<x:a xmlns:x="http://x.com" xmlns:y="http://x1.com"><x1:a xmlns:x1="http://x1.com"/></x:a>~);
289 {
290 my $x = XML::LibXML::XPathContext->new;
291
292 # use the document's declaration
293 # TEST
294 ok( $x->findvalue('count(/x:a/y:a)',$d->documentElement)==1, ' TODO : Add test name' );
295
296 $x->registerNs('x', 'http://x1.com');
297 # x now maps to http://x1.com, so it won't match the top-level element
298 # TEST
299 ok( $x->findvalue('count(/x:a)',$d->documentElement)==0, ' TODO : Add test name' );
300
301 $x->registerNs('x1', 'http://x.com');
302 # x1 now maps to http://x.com
303 # x1:a will match the first element
304 # TEST
305 ok( $x->findvalue('count(/x1:a)',$d->documentElement)==1, ' TODO : Add test name' );
306 # but not the second
307 # TEST
308 ok( $x->findvalue('count(/x1:a/x1:a)',$d->documentElement)==0, ' TODO : Add test name' );
309 # this will work, though
310 # TEST
311 ok( $x->findvalue('count(/x1:a/x:a)',$d->documentElement)==1, ' TODO : Add test name' );
312 # the same using y for http://x1.com
313 # TEST
314 ok( $x->findvalue('count(/x1:a/y:a)',$d->documentElement)==1, ' TODO : Add test name' );
315 $x->registerNs('y', 'http://x.com');
316 # y prefix remapped
317 # TEST
318 ok( $x->findvalue('count(/x1:a/y:a)',$d->documentElement)==0, ' TODO : Add test name' );
319 # TEST
320 ok( $x->findvalue('count(/y:a/x:a)',$d->documentElement)==1, ' TODO : Add test name' );
321 $x->registerNs('y', 'http://x1.com');
322 # y prefix remapped back
323 # TEST
324 ok( $x->findvalue('count(/x1:a/y:a)',$d->documentElement)==1, ' TODO : Add test name' );
325 $x->unregisterNs('x');
326 # TEST
327 ok( $x->findvalue('count(/x:a)',$d->documentElement)==1, ' TODO : Add test name' );
328 $x->unregisterNs('y');
329 # TEST
330 ok( $x->findvalue('count(/x:a/y:a)',$d->documentElement)==1, ' TODO : Add test name' );
331 }
332 }
333
334 SKIP:
335 {
336 # 37332
337 if (XML::LibXML::LIBXML_VERSION() < 20617) {
338 skip(
339 'xpath does not work on nodes without a document in libxml2 < 2.6.17',
340 3
341 );
342 }
343 my $frag = XML::LibXML::DocumentFragment->new;
344 my $foo = XML::LibXML::Element->new('foo');
345 my $xpc = XML::LibXML::XPathContext->new;
346 $frag->appendChild($foo);
347 $foo->appendTextChild('bar', 'quux');
348 {
349 my @n = $xpc->findnodes('./foo', $frag);
350 # TEST
351 ok ( @n == 1, ' TODO : Add test name' );
352 }
353 {
354 my @n = $xpc->findnodes('./foo/bar', $frag);
355 # TEST
356 ok ( @n == 1, ' TODO : Add test name' );
357 }
358 {
359 my @n = $xpc->findnodes('./bar', $foo);
360 # TEST
361 ok ( @n == 1, ' TODO : Add test name' );
362 }
363 }