| 1 |
# $Id$ |
| 2 |
|
| 3 |
## |
| 4 |
# this test checks the DOM Document interface of XML::LibXML |
| 5 |
# it relies on the success of t/01basic.t and t/02parse.t |
| 6 |
|
| 7 |
# it will ONLY test the DOM capabilities as specified in DOM Level3 |
| 8 |
# XPath tests should be done in another test file |
| 9 |
|
| 10 |
# since all tests are run on a preparsed |
| 11 |
|
| 12 |
use strict; |
| 13 |
use warnings; |
| 14 |
|
| 15 |
# Should be 168. |
| 16 |
use Test::More tests => 193; |
| 17 |
|
| 18 |
use XML::LibXML; |
| 19 |
use XML::LibXML::Common qw(:libxml); |
| 20 |
|
| 21 |
use IO::Handle; |
| 22 |
|
| 23 |
sub is_empty_str |
| 24 |
{ |
| 25 |
my $s = shift; |
| 26 |
return (!defined($s) or (length($s) == 0)); |
| 27 |
} |
| 28 |
|
| 29 |
# TEST:$c=0; |
| 30 |
sub _check_element_node |
| 31 |
{ |
| 32 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 33 |
|
| 34 |
my ($node, $name, $blurb) = @_; |
| 35 |
|
| 36 |
# TEST:$c++; |
| 37 |
ok($node, "$blurb - node was initialised"); |
| 38 |
# TEST:$c++; |
| 39 |
is($node->nodeType, XML_ELEMENT_NODE, "$blurb - node is an element node"); |
| 40 |
# TEST:$c++; |
| 41 |
is($node->nodeName, $name, "$blurb - node has the right name."); |
| 42 |
} |
| 43 |
|
| 44 |
# TEST:$_check_element_node=$c; |
| 45 |
|
| 46 |
sub _check_created_element |
| 47 |
{ |
| 48 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 49 |
my ($doc, $given_name, $name, $blurb) = @_; |
| 50 |
|
| 51 |
return _check_element_node( |
| 52 |
$doc->createElement($given_name), |
| 53 |
$name, |
| 54 |
$blurb |
| 55 |
); |
| 56 |
} |
| 57 |
# TEST:$_check_created_element=$_check_element_node; |
| 58 |
|
| 59 |
sub _multi_arg_generic_count |
| 60 |
{ |
| 61 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 62 |
|
| 63 |
my ($doc, $method, $params) = @_; |
| 64 |
|
| 65 |
my ($meth_params, $want_count, $blurb) = @$params; |
| 66 |
|
| 67 |
my @elems = $doc->$method( @$meth_params ); |
| 68 |
|
| 69 |
return is (scalar(@elems), $want_count, $blurb); |
| 70 |
} |
| 71 |
|
| 72 |
sub _generic_count |
| 73 |
{ |
| 74 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 75 |
|
| 76 |
my ($doc, $method, $params) = @_; |
| 77 |
|
| 78 |
my ($name, $want_count, $blurb) = @$params; |
| 79 |
|
| 80 |
return _multi_arg_generic_count( |
| 81 |
$doc, $method, [[$name], $want_count, $blurb, ], |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
sub _count_local_name |
| 86 |
{ |
| 87 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 88 |
my $doc = shift; |
| 89 |
|
| 90 |
return _generic_count($doc, 'getElementsByLocalName', [@_]); |
| 91 |
} |
| 92 |
|
| 93 |
sub _count_tag_name |
| 94 |
{ |
| 95 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 96 |
my $doc = shift; |
| 97 |
|
| 98 |
return _generic_count($doc, 'getElementsByTagName', [@_]); |
| 99 |
} |
| 100 |
|
| 101 |
sub _count_children_by_local_name |
| 102 |
{ |
| 103 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 104 |
my $doc = shift; |
| 105 |
|
| 106 |
return _generic_count($doc, 'getChildrenByLocalName', [@_]); |
| 107 |
} |
| 108 |
|
| 109 |
sub _count_children_by_name |
| 110 |
{ |
| 111 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 112 |
my $doc = shift; |
| 113 |
|
| 114 |
return _generic_count($doc, 'getChildrenByTagName', [@_]); |
| 115 |
} |
| 116 |
|
| 117 |
sub _count_elements_by_name_ns |
| 118 |
{ |
| 119 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 120 |
my ($doc, $ns_and_name, $want_count, $blurb) = @_; |
| 121 |
|
| 122 |
return _multi_arg_generic_count($doc, 'getElementsByTagNameNS', |
| 123 |
[$ns_and_name, $want_count, $blurb] |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
sub _count_children_by_name_ns |
| 128 |
{ |
| 129 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 130 |
my ($doc, $ns_and_name, $want_count, $blurb) = @_; |
| 131 |
|
| 132 |
return _multi_arg_generic_count($doc, 'getChildrenByTagNameNS', |
| 133 |
[$ns_and_name, $want_count, $blurb] |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
{ |
| 138 |
# Document Attributes |
| 139 |
|
| 140 |
my $doc = XML::LibXML::Document->createDocument(); |
| 141 |
# TEST |
| 142 |
ok($doc, ' TODO : Add test name'); |
| 143 |
# TEST |
| 144 |
ok( ! defined($doc->encoding), ' TODO : Add test name'); |
| 145 |
# TEST |
| 146 |
is( $doc->version, "1.0", ' TODO : Add test name' ); |
| 147 |
# TEST |
| 148 |
is( $doc->standalone, -1, ' TODO : Add test name' ); # is the value we get for undefined, |
| 149 |
# actually the same as 0 but just not set. |
| 150 |
# TEST |
| 151 |
ok( !defined($doc->URI), ' TODO : Add test name'); # should be set by default. |
| 152 |
# TEST |
| 153 |
is( $doc->compression, -1, ' TODO : Add test name' ); # -1 indicates NO compression at all! |
| 154 |
# while 0 indicates just no zip compression |
| 155 |
# (big difference huh?) |
| 156 |
|
| 157 |
$doc->setEncoding( "iso-8859-1" ); |
| 158 |
# TEST |
| 159 |
is( $doc->encoding, "iso-8859-1", 'Encoding was set.' ); |
| 160 |
|
| 161 |
$doc->setVersion(12.5); |
| 162 |
# TEST |
| 163 |
is( $doc->version, "12.5", 'Version was set.' ); |
| 164 |
|
| 165 |
$doc->setStandalone(1); |
| 166 |
# TEST |
| 167 |
is( $doc->standalone, 1, 'Standalone was set.' ); |
| 168 |
|
| 169 |
$doc->setBaseURI( "localhost/here.xml" ); |
| 170 |
# TEST |
| 171 |
is( $doc->URI, "localhost/here.xml", 'URI is set.' ); |
| 172 |
|
| 173 |
my $doc2 = XML::LibXML::Document->createDocument("1.1", "iso-8859-2"); |
| 174 |
# TEST |
| 175 |
is( $doc2->encoding, "iso-8859-2", 'doc2 encoding was set.' ); |
| 176 |
# TEST |
| 177 |
is( $doc2->version, "1.1", 'doc2 version was set.' ); |
| 178 |
# TEST |
| 179 |
is( $doc2->standalone, -1, 'doc2 standalone' ); |
| 180 |
} |
| 181 |
|
| 182 |
{ |
| 183 |
# 2. Creating Elements |
| 184 |
my $doc = XML::LibXML::Document->new(); |
| 185 |
{ |
| 186 |
my $node = $doc->createDocumentFragment(); |
| 187 |
# TEST |
| 188 |
ok($node, ' TODO : Add test name'); |
| 189 |
# TEST |
| 190 |
is($node->nodeType, XML_DOCUMENT_FRAG_NODE, ' TODO : Add test name'); |
| 191 |
} |
| 192 |
|
| 193 |
# TEST*$_check_created_element |
| 194 |
_check_created_element($doc, 'foo', 'foo', 'Simple Element'); |
| 195 |
|
| 196 |
{ |
| 197 |
# document with encoding |
| 198 |
my $encdoc = XML::LibXML::Document->new( "1.0" ); |
| 199 |
$encdoc->setEncoding( "iso-8859-1" ); |
| 200 |
|
| 201 |
# TEST*$_check_created_element |
| 202 |
_check_created_element( |
| 203 |
$encdoc, 'foo', 'foo', 'Encdoc Element creation' |
| 204 |
); |
| 205 |
|
| 206 |
# SAX style document with encoding |
| 207 |
my $node_def = { |
| 208 |
Name => "object", |
| 209 |
LocalName => "object", |
| 210 |
Prefix => "", |
| 211 |
NamespaceURI => "", |
| 212 |
}; |
| 213 |
|
| 214 |
# TEST*$_check_created_element |
| 215 |
_check_created_element( |
| 216 |
$encdoc, $node_def->{Name}, 'object', |
| 217 |
'Encdoc element creation based on node_def->{name}', |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
{ |
| 222 |
# namespaced element test |
| 223 |
my $node = $doc->createElementNS( "http://kungfoo", "foo:bar" ); |
| 224 |
# TEST |
| 225 |
ok($node, ' TODO : Add test name'); |
| 226 |
# TEST |
| 227 |
is($node->nodeType, XML_ELEMENT_NODE, ' TODO : Add test name'); |
| 228 |
# TEST |
| 229 |
is($node->nodeName, "foo:bar", ' TODO : Add test name'); |
| 230 |
# TEST |
| 231 |
is($node->prefix, "foo", ' TODO : Add test name'); |
| 232 |
# TEST |
| 233 |
is($node->localname, "bar", ' TODO : Add test name'); |
| 234 |
# TEST |
| 235 |
is($node->namespaceURI, "http://kungfoo", ' TODO : Add test name'); |
| 236 |
} |
| 237 |
|
| 238 |
{ |
| 239 |
# bad element creation |
| 240 |
# TEST:$badnames_count=5; |
| 241 |
my @badnames = ( ";", "&", "<><", "/", "1A"); |
| 242 |
|
| 243 |
foreach my $name ( @badnames ) { |
| 244 |
my $node = eval {$doc->createElement( $name );}; |
| 245 |
# TEST*$badnames_count |
| 246 |
ok( !(defined $node), ' TODO : Add test name' ); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
{ |
| 252 |
my $node = $doc->createTextNode( "foo" ); |
| 253 |
# TEST |
| 254 |
ok($node, ' TODO : Add test name'); |
| 255 |
# TEST |
| 256 |
is($node->nodeType, XML_TEXT_NODE, ' TODO : Add test name' ); |
| 257 |
# TEST |
| 258 |
is($node->nodeValue, "foo", ' TODO : Add test name' ); |
| 259 |
} |
| 260 |
|
| 261 |
{ |
| 262 |
my $node = $doc->createComment( "foo" ); |
| 263 |
# TEST |
| 264 |
ok($node, ' TODO : Add test name'); |
| 265 |
# TEST |
| 266 |
is($node->nodeType, XML_COMMENT_NODE, ' TODO : Add test name' ); |
| 267 |
# TEST |
| 268 |
is($node->nodeValue, "foo", ' TODO : Add test name' ); |
| 269 |
# TEST |
| 270 |
is($node->toString, "<!--foo-->", ' TODO : Add test name'); |
| 271 |
} |
| 272 |
|
| 273 |
{ |
| 274 |
my $node = $doc->createCDATASection( "foo" ); |
| 275 |
# TEST |
| 276 |
ok($node, ' TODO : Add test name'); |
| 277 |
# TEST |
| 278 |
is($node->nodeType, XML_CDATA_SECTION_NODE, ' TODO : Add test name' ); |
| 279 |
# TEST |
| 280 |
is($node->nodeValue, "foo", ' TODO : Add test name' ); |
| 281 |
# TEST |
| 282 |
is($node->toString, "<![CDATA[foo]]>", ' TODO : Add test name'); |
| 283 |
} |
| 284 |
|
| 285 |
# -> Create Attributes |
| 286 |
{ |
| 287 |
my $attr = $doc->createAttribute("foo", "bar"); |
| 288 |
# TEST |
| 289 |
ok($attr, ' TODO : Add test name'); |
| 290 |
# TEST |
| 291 |
is($attr->nodeType, XML_ATTRIBUTE_NODE, ' TODO : Add test name' ); |
| 292 |
# TEST |
| 293 |
is($attr->name, "foo", ' TODO : Add test name'); |
| 294 |
# TEST |
| 295 |
is($attr->value, "bar", ' TODO : Add test name' ); |
| 296 |
# TEST |
| 297 |
is($attr->hasChildNodes, 0, ' TODO : Add test name'); |
| 298 |
my $content = $attr->firstChild; |
| 299 |
# TEST |
| 300 |
ok( $content, ' TODO : Add test name' ); |
| 301 |
# TEST |
| 302 |
is( $content->nodeType, XML_TEXT_NODE, ' TODO : Add test name' ); |
| 303 |
} |
| 304 |
{ |
| 305 |
# bad attribute creation |
| 306 |
# TEST:$badnames_count=5; |
| 307 |
my @badnames = ( ";", "&", "<><", "/", "1A"); |
| 308 |
|
| 309 |
foreach my $name ( @badnames ) { |
| 310 |
my $node = eval {$doc->createAttribute( $name, "bar" );}; |
| 311 |
# TEST*$badnames_count |
| 312 |
ok( !defined($node), ' TODO : Add test name' ); |
| 313 |
} |
| 314 |
|
| 315 |
} |
| 316 |
{ |
| 317 |
my $elem = $doc->createElement('foo'); |
| 318 |
my $attr = $doc->createAttribute(attr => 'e & f'); |
| 319 |
$elem->addChild($attr); |
| 320 |
# TEST |
| 321 |
ok ($elem->toString() eq '<foo attr="e & f"/>', ' TODO : Add test name'); |
| 322 |
$elem->removeAttribute('attr'); |
| 323 |
$attr = $doc->createAttributeNS(undef,'attr2' => 'a & b'); |
| 324 |
$elem->addChild($attr); |
| 325 |
# TEST |
| 326 |
ok ($elem->toString() eq '<foo attr2="a & b"/>', ' TODO : Add test name'); |
| 327 |
} |
| 328 |
{ |
| 329 |
eval { |
| 330 |
my $attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar"); |
| 331 |
}; |
| 332 |
# TEST |
| 333 |
ok($@, ' TODO : Add test name'); |
| 334 |
|
| 335 |
my $root = $doc->createElement( "foo" ); |
| 336 |
$doc->setDocumentElement( $root ); |
| 337 |
|
| 338 |
my $attr; |
| 339 |
eval { |
| 340 |
$attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar"); |
| 341 |
}; |
| 342 |
# TEST |
| 343 |
ok($attr, ' TODO : Add test name'); |
| 344 |
# TEST |
| 345 |
is($attr->nodeName, "kung:foo", ' TODO : Add test name'); |
| 346 |
# TEST |
| 347 |
is($attr->name,"foo", ' TODO : Add test name' ); |
| 348 |
# TEST |
| 349 |
is($attr->value, "bar", ' TODO : Add test name' ); |
| 350 |
|
| 351 |
$attr->setValue( q(bar&) ); |
| 352 |
# TEST |
| 353 |
is($attr->getValue, q(bar&), ' TODO : Add test name' ); |
| 354 |
} |
| 355 |
{ |
| 356 |
# bad attribute creation |
| 357 |
# TEST:$badnames_count=5; |
| 358 |
my @badnames = ( ";", "&", "<><", "/", "1A"); |
| 359 |
|
| 360 |
foreach my $name ( @badnames ) { |
| 361 |
my $node = eval {$doc->createAttributeNS( undef, $name, "bar" );}; |
| 362 |
# TEST*$badnames_count |
| 363 |
ok( (!defined $node), ' TODO : Add test name' ); |
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
# -> Create PIs |
| 369 |
{ |
| 370 |
my $pi = $doc->createProcessingInstruction( "foo", "bar" ); |
| 371 |
# TEST |
| 372 |
ok($pi, ' TODO : Add test name'); |
| 373 |
# TEST |
| 374 |
is($pi->nodeType, XML_PI_NODE, ' TODO : Add test name'); |
| 375 |
# TEST |
| 376 |
is($pi->nodeName, "foo", ' TODO : Add test name'); |
| 377 |
# TEST |
| 378 |
is($pi->textContent, "bar", ' TODO : Add test name'); |
| 379 |
# TEST |
| 380 |
is($pi->getData, "bar", ' TODO : Add test name'); |
| 381 |
} |
| 382 |
|
| 383 |
{ |
| 384 |
my $pi = $doc->createProcessingInstruction( "foo" ); |
| 385 |
# TEST |
| 386 |
ok($pi, ' TODO : Add test name'); |
| 387 |
# TEST |
| 388 |
is($pi->nodeType, XML_PI_NODE, ' TODO : Add test name'); |
| 389 |
# TEST |
| 390 |
is($pi->nodeName, "foo", ' TODO : Add test name'); |
| 391 |
my $data = $pi->textContent; |
| 392 |
# undef or "" depending on libxml2 version |
| 393 |
# TEST |
| 394 |
ok( is_empty_str($data), ' TODO : Add test name' ); |
| 395 |
$data = $pi->getData; |
| 396 |
# TEST |
| 397 |
ok( is_empty_str($data), ' TODO : Add test name' ); |
| 398 |
$pi->setData(q(bar&)); |
| 399 |
# TEST |
| 400 |
is( $pi->getData, q(bar&), ' TODO : Add test name'); |
| 401 |
# TEST |
| 402 |
is($pi->textContent, q(bar&), ' TODO : Add test name'); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
{ |
| 407 |
# Document Manipulation |
| 408 |
# -> Document Elements |
| 409 |
|
| 410 |
my $doc = XML::LibXML::Document->new(); |
| 411 |
my $node = $doc->createElement( "foo" ); |
| 412 |
$doc->setDocumentElement( $node ); |
| 413 |
my $tn = $doc->documentElement; |
| 414 |
# TEST |
| 415 |
ok($tn, ' TODO : Add test name'); |
| 416 |
# TEST |
| 417 |
ok($node->isSameNode($tn), ' TODO : Add test name'); |
| 418 |
|
| 419 |
my $node2 = $doc->createElement( "bar" ); |
| 420 |
{ my $warn; |
| 421 |
eval { |
| 422 |
local $SIG{__WARN__} = sub { $warn = 1 }; |
| 423 |
# TEST |
| 424 |
ok( !defined($doc->appendChild($node2)), ' TODO : Add test name' ); |
| 425 |
}; |
| 426 |
# TEST |
| 427 |
ok(($@ or $warn), ' TODO : Add test name'); |
| 428 |
} |
| 429 |
my @cn = $doc->childNodes; |
| 430 |
# TEST |
| 431 |
is( scalar(@cn) , 1, ' TODO : Add test name'); |
| 432 |
# TEST |
| 433 |
ok($cn[0]->isSameNode($node), ' TODO : Add test name'); |
| 434 |
|
| 435 |
eval { |
| 436 |
$doc->insertBefore($node2, $node); |
| 437 |
}; |
| 438 |
# TEST |
| 439 |
ok ($@, ' TODO : Add test name'); |
| 440 |
@cn = $doc->childNodes; |
| 441 |
# TEST |
| 442 |
is( scalar(@cn) , 1, ' TODO : Add test name'); |
| 443 |
# TEST |
| 444 |
ok($cn[0]->isSameNode($node), ' TODO : Add test name'); |
| 445 |
|
| 446 |
$doc->removeChild($node); |
| 447 |
@cn = $doc->childNodes; |
| 448 |
# TEST |
| 449 |
is( scalar(@cn) , 0, ' TODO : Add test name'); |
| 450 |
|
| 451 |
for ( 1..2 ) { |
| 452 |
my $nodeA = $doc->createElement( "x" ); |
| 453 |
$doc->setDocumentElement( $nodeA ); |
| 454 |
} |
| 455 |
# TEST |
| 456 |
ok(1, ' TODO : Add test name'); # must not segfault here :) |
| 457 |
|
| 458 |
$doc->setDocumentElement( $node2 ); |
| 459 |
@cn = $doc->childNodes; |
| 460 |
# TEST |
| 461 |
is( scalar(@cn) , 1, ' TODO : Add test name'); |
| 462 |
# TEST |
| 463 |
ok($cn[0]->isSameNode($node2), ' TODO : Add test name'); |
| 464 |
|
| 465 |
my $node3 = $doc->createElementNS( "http://foo", "bar" ); |
| 466 |
# TEST |
| 467 |
ok($node3, ' TODO : Add test name'); |
| 468 |
|
| 469 |
# -> Processing Instructions |
| 470 |
{ |
| 471 |
my $pi = $doc->createProcessingInstruction( "foo", "bar" ); |
| 472 |
$doc->appendChild( $pi ); |
| 473 |
@cn = $doc->childNodes; |
| 474 |
# TEST |
| 475 |
ok( $pi->isSameNode($cn[-1]), ' TODO : Add test name' ); |
| 476 |
$pi->setData( 'bar="foo"' ); |
| 477 |
# TEST |
| 478 |
is( $pi->textContent, 'bar="foo"', ' TODO : Add test name'); |
| 479 |
$pi->setData( foo=>"foo" ); |
| 480 |
# TEST |
| 481 |
is( $pi->textContent, 'foo="foo"', ' TODO : Add test name'); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
package Stringify; |
| 486 |
|
| 487 |
use overload q[""] => sub { return '<A xmlns:C="xml://D"><C:A>foo<A/>bar</C:A><A><C:B/>X</A>baz</A>'; }; |
| 488 |
|
| 489 |
sub new |
| 490 |
{ |
| 491 |
return bless \(my $x); |
| 492 |
} |
| 493 |
|
| 494 |
package main; |
| 495 |
|
| 496 |
{ |
| 497 |
# Document Storing |
| 498 |
my $parser = XML::LibXML->new; |
| 499 |
my $doc = $parser->parse_string("<foo>bar</foo>"); |
| 500 |
|
| 501 |
# TEST |
| 502 |
|
| 503 |
ok( $doc, ' TODO : Add test name' ); |
| 504 |
|
| 505 |
# -> to file handle |
| 506 |
|
| 507 |
{ |
| 508 |
open my $fh, '>', 'example/testrun.xml' |
| 509 |
or die "Cannot open example/testrun.xml for writing - $!."; |
| 510 |
|
| 511 |
$doc->toFH( $fh ); |
| 512 |
$fh->close; |
| 513 |
# TEST |
| 514 |
ok(1, ' TODO : Add test name'); |
| 515 |
# now parse the file to check, if succeeded |
| 516 |
my $tdoc = $parser->parse_file( "example/testrun.xml" ); |
| 517 |
# TEST |
| 518 |
ok( $tdoc, ' TODO : Add test name' ); |
| 519 |
# TEST |
| 520 |
ok( $tdoc->documentElement, ' TODO : Add test name' ); |
| 521 |
# TEST |
| 522 |
is( $tdoc->documentElement->nodeName, "foo", ' TODO : Add test name' ); |
| 523 |
# TEST |
| 524 |
is( $tdoc->documentElement->textContent, "bar", ' TODO : Add test name' ); |
| 525 |
unlink "example/testrun.xml" ; |
| 526 |
} |
| 527 |
|
| 528 |
# -> to named file |
| 529 |
{ |
| 530 |
$doc->toFile( "example/testrun.xml" ); |
| 531 |
# TEST |
| 532 |
ok(1, ' TODO : Add test name'); |
| 533 |
# now parse the file to check, if succeeded |
| 534 |
my $tdoc = $parser->parse_file( "example/testrun.xml" ); |
| 535 |
# TEST |
| 536 |
ok( $tdoc, ' TODO : Add test name' ); |
| 537 |
# TEST |
| 538 |
ok( $tdoc->documentElement, ' TODO : Add test name' ); |
| 539 |
# TEST |
| 540 |
is( $tdoc->documentElement->nodeName, "foo", ' TODO : Add test name' ); |
| 541 |
# TEST |
| 542 |
is( $tdoc->documentElement->textContent, "bar", ' TODO : Add test name' ); |
| 543 |
unlink "example/testrun.xml" ; |
| 544 |
} |
| 545 |
|
| 546 |
# ELEMENT LIKE FUNCTIONS |
| 547 |
{ |
| 548 |
my $parser2 = XML::LibXML->new(); |
| 549 |
my $string1 = "<A><A><B/></A><A><B/></A></A>"; |
| 550 |
my $string2 = '<C:A xmlns:C="xml://D"><C:A><C:B/></C:A><C:A><C:B/></C:A></C:A>'; |
| 551 |
my $string3 = '<A xmlns="xml://D"><A><B/></A><A><B/></A></A>'; |
| 552 |
my $string4 = '<C:A><C:A><C:B/></C:A><C:A><C:B/></C:A></C:A>'; |
| 553 |
my $string5 = '<A xmlns:C="xml://D"><C:A>foo<A/>bar</C:A><A><C:B/>X</A>baz</A>'; |
| 554 |
{ |
| 555 |
my $doc2 = $parser2->parse_string($string1); |
| 556 |
# TEST |
| 557 |
_count_tag_name($doc2, 'A', 3, q{3 As}); |
| 558 |
# TEST |
| 559 |
_count_tag_name($doc2, '*', 5, q{5 elements of all names}); |
| 560 |
|
| 561 |
# TEST |
| 562 |
_count_elements_by_name_ns($doc2, ['*', 'B'], 2, |
| 563 |
'2 Bs of any namespace' |
| 564 |
); |
| 565 |
|
| 566 |
# TEST |
| 567 |
_count_local_name($doc2, 'A', 3, q{3 A's}); |
| 568 |
|
| 569 |
# TEST |
| 570 |
_count_local_name($doc2, '*', 5, q{5 Sub-elements}); |
| 571 |
} |
| 572 |
{ |
| 573 |
my $doc2 = $parser2->parse_string($string2); |
| 574 |
# TEST |
| 575 |
_count_tag_name( $doc2, 'C:A', 3, q{C:A count}); |
| 576 |
# TEST |
| 577 |
_count_elements_by_name_ns($doc2, [ "xml://D", "A" ], 3, |
| 578 |
q{3 elements of namespace xml://D and A}, |
| 579 |
); |
| 580 |
# TEST |
| 581 |
_count_elements_by_name_ns($doc2, ['*', 'A'], 3, |
| 582 |
q{3 Elements A of any namespace} |
| 583 |
); |
| 584 |
# TEST |
| 585 |
_count_local_name($doc2, 'A', 3, q{3 As}); |
| 586 |
} |
| 587 |
{ |
| 588 |
my $doc2 = $parser2->parse_string($string3); |
| 589 |
# TEST |
| 590 |
_count_elements_by_name_ns($doc2, ["xml://D", "A"], 3, |
| 591 |
q{3 Elements A of any namespace} |
| 592 |
); |
| 593 |
# TEST |
| 594 |
_count_local_name($doc2, 'A', 3, q{3 As}); |
| 595 |
} |
| 596 |
=begin taken_out |
| 597 |
# This was taken out because the XML uses an undefined namespace. |
| 598 |
# I don't know why this test was introduced in the first place, |
| 599 |
# but it fails now |
| 600 |
# |
| 601 |
# This test fails in this bug report - |
| 602 |
# https://rt.cpan.org/Ticket/Display.html?id=75403 |
| 603 |
# -- Shlomi Fish |
| 604 |
{ |
| 605 |
$parser2->recover(1); |
| 606 |
local $SIG{'__WARN__'} = sub { |
| 607 |
print "warning caught: @_\n"; |
| 608 |
}; |
| 609 |
# my $doc2 = $parser2->parse_string($string4); |
| 610 |
#-TEST |
| 611 |
# _count_local_name( $doc2, 'A', 3, q{3 As}); |
| 612 |
} |
| 613 |
=end taken_out |
| 614 |
|
| 615 |
=cut |
| 616 |
# TEST:$count=3; |
| 617 |
# Also test that we can parse from scalar references: |
| 618 |
# See RT #64051 ( https://rt.cpan.org/Ticket/Display.html?id=64051 ) |
| 619 |
# Also test that we can parse from references to scalars with |
| 620 |
# overloaded strings: |
| 621 |
# See RT #77864 ( https://rt.cpan.org/Public/Bug/Display.html?id=77864 ) |
| 622 |
|
| 623 |
my $obj = Stringify->new; |
| 624 |
|
| 625 |
foreach my $input ( $string5, (\$string5), $obj ) |
| 626 |
{ |
| 627 |
my $doc2 = $parser2->parse_string($input); |
| 628 |
# TEST*$count |
| 629 |
_count_tag_name($doc2, 'C:A', 1, q{3 C:As}); |
| 630 |
# TEST*$count |
| 631 |
_count_tag_name($doc2, 'A', 3, q{3 As}); |
| 632 |
# TEST*$count |
| 633 |
_count_elements_by_name_ns($doc2, ["*", "A"], 4, |
| 634 |
q{4 Elements of A of any namespace} |
| 635 |
); |
| 636 |
# TEST*$count |
| 637 |
_count_elements_by_name_ns($doc2, ['*', '*'], 5, |
| 638 |
q{4 Elements of any namespace}, |
| 639 |
); |
| 640 |
# TEST*$count |
| 641 |
_count_elements_by_name_ns( $doc2, ["xml://D", "*" ], 2, |
| 642 |
q{2 elements of any name in D} |
| 643 |
); |
| 644 |
|
| 645 |
my $A = $doc2->getDocumentElement; |
| 646 |
# TEST*$count |
| 647 |
_count_children_by_name($A, 'A', 1, q{1 A}); |
| 648 |
# TEST*$count |
| 649 |
_count_children_by_name($A, 'C:A', 1, q{C:A}); |
| 650 |
# TEST*$count |
| 651 |
_count_children_by_name($A, 'C:B', 0, q{No C:B children}); |
| 652 |
# TEST*$count |
| 653 |
_count_children_by_name($A, "*", 2, q{2 Childern in $A in total}); |
| 654 |
# TEST*$count |
| 655 |
_count_children_by_name_ns($A, ['*', 'A'], 2, |
| 656 |
q{2 As of any namespace}); |
| 657 |
# TEST*$count |
| 658 |
_count_children_by_name_ns($A, [ "xml://D", "*" ], 1, |
| 659 |
q{1 Child of D}, |
| 660 |
); |
| 661 |
# TEST*$count |
| 662 |
_count_children_by_name_ns($A, [ "*", "*" ], 2, |
| 663 |
q{2 Children in total}, |
| 664 |
); |
| 665 |
# TEST*$count |
| 666 |
_count_children_by_local_name($A, 'A', 2, q{2 As}); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
{ |
| 672 |
# Bug fixes (to be used with valgrind) |
| 673 |
{ |
| 674 |
my $doc=XML::LibXML->createDocument(); # create a doc |
| 675 |
my $x=$doc->createPI(foo=>"bar"); # create a PI |
| 676 |
undef $doc; # should not free |
| 677 |
undef $x; # free the PI |
| 678 |
# TEST |
| 679 |
ok(1, ' TODO : Add test name'); |
| 680 |
} |
| 681 |
{ |
| 682 |
my $doc=XML::LibXML->createDocument(); # create a doc |
| 683 |
my $x=$doc->createAttribute(foo=>"bar"); # create an attribute |
| 684 |
undef $doc; # should not free |
| 685 |
undef $x; # free the attribute |
| 686 |
# TEST |
| 687 |
ok(1, ' TODO : Add test name'); |
| 688 |
} |
| 689 |
{ |
| 690 |
my $doc=XML::LibXML->createDocument(); # create a doc |
| 691 |
my $x=$doc->createAttributeNS(undef,foo=>"bar"); # create an attribute |
| 692 |
undef $doc; # should not free |
| 693 |
undef $x; # free the attribute |
| 694 |
# TEST |
| 695 |
ok(1, ' TODO : Add test name'); |
| 696 |
} |
| 697 |
{ |
| 698 |
my $doc=XML::LibXML->new->parse_string('<foo xmlns:x="http://foo.bar"/>'); |
| 699 |
my $x=$doc->createAttributeNS('http://foo.bar','x:foo'=>"bar"); # create an attribute |
| 700 |
undef $doc; # should not free |
| 701 |
undef $x; # free the attribute |
| 702 |
# TEST |
| 703 |
ok(1, ' TODO : Add test name'); |
| 704 |
} |
| 705 |
{ |
| 706 |
# rt.cpan.org #30610 |
| 707 |
# valgrind this |
| 708 |
my $object=XML::LibXML::Element->new( 'object' ); |
| 709 |
my $xml = qq(<?xml version="1.0" encoding="UTF-8"?>\n<lom/>); |
| 710 |
my $lom_doc=XML::LibXML->new->parse_string($xml); |
| 711 |
my $lom_root=$lom_doc->getDocumentElement(); |
| 712 |
$object->appendChild( $lom_root ); |
| 713 |
# TEST |
| 714 |
ok(!defined($object->firstChild->ownerDocument), ' TODO : Add test name'); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
{ |
| 720 |
my $xml = q{<?xml version="1.0" encoding="UTF-8"?> |
| 721 |
<test/> |
| 722 |
}; |
| 723 |
my $out = q{<?xml version="1.0"?> |
| 724 |
<test/> |
| 725 |
}; |
| 726 |
my $dom = XML::LibXML->new->parse_string($xml); |
| 727 |
# TEST |
| 728 |
is($dom->getEncoding, "UTF-8", ' TODO : Add test name'); |
| 729 |
$dom->setEncoding(); |
| 730 |
# TEST |
| 731 |
is($dom->getEncoding, undef, ' TODO : Add test name'); |
| 732 |
# TEST |
| 733 |
is($dom->toString, $out, ' TODO : Add test name'); |
| 734 |
} |
| 735 |
|
| 736 |
# the following tests were added for #33810 |
| 737 |
SKIP: |
| 738 |
{ |
| 739 |
if (! eval { require Encode; }) |
| 740 |
{ |
| 741 |
skip "Encoding related tests require Encode", (3*8); |
| 742 |
} |
| 743 |
# TEST:$num_encs=3; |
| 744 |
# The count. |
| 745 |
# TEST:$c=0; |
| 746 |
for my $enc (qw(UTF-16 UTF-16LE UTF-16BE)) { |
| 747 |
my $xml = Encode::encode($enc,qq{<?xml version="1.0" encoding="$enc"?> |
| 748 |
<test foo="bar"/> |
| 749 |
}); |
| 750 |
my $dom = XML::LibXML->new->parse_string($xml); |
| 751 |
# TEST:$c++; |
| 752 |
is($dom->getEncoding,$enc, ' TODO : Add test name'); |
| 753 |
# TEST:$c++; |
| 754 |
is($dom->actualEncoding,$enc, ' TODO : Add test name'); |
| 755 |
# TEST:$c++; |
| 756 |
is($dom->getDocumentElement->getAttribute('foo'),'bar', ' TODO : Add test name'); |
| 757 |
# TEST:$c++; |
| 758 |
is($dom->getDocumentElement->getAttribute(Encode::encode('UTF-16','foo')), 'bar', ' TODO : Add test name'); |
| 759 |
# TEST:$c++; |
| 760 |
is($dom->getDocumentElement->getAttribute(Encode::encode($enc,'foo')), 'bar', ' TODO : Add test name'); |
| 761 |
my $exp_enc = $enc eq 'UTF-16' ? 'UTF-16LE' : $enc; |
| 762 |
# TEST:$c++; |
| 763 |
is($dom->getDocumentElement->getAttribute('foo',1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name'); |
| 764 |
# TEST:$c++; |
| 765 |
is($dom->getDocumentElement->getAttribute(Encode::encode('UTF-16','foo'),1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name'); |
| 766 |
# TEST:$c++; |
| 767 |
is($dom->getDocumentElement->getAttribute(Encode::encode($enc,'foo'),1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name'); |
| 768 |
} |
| 769 |
# TEST*$num_encs*$c |
| 770 |
} |