| 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 |
# https://gitlab.gnome.org/GNOME/libxml2/-/commit/aca16fb3 |
| 127 |
# fixed xmlCopyNamespace with XML namespace. |
| 128 |
if (XML::LibXML::LIBXML_RUNTIME_VERSION() < 21300) { |
| 129 |
is(scalar(@ns), 2, ' TODO : Add test name' ); |
| 130 |
} else { |
| 131 |
is(scalar(@ns), 3, ' TODO : Add test name' ); |
| 132 |
} |
| 133 |
|
| 134 |
# bad xpaths |
| 135 |
# TEST:$badxpath=4; |
| 136 |
my @badxpath = ( |
| 137 |
'abc:::def', |
| 138 |
'foo///bar', |
| 139 |
'...', |
| 140 |
'/-', |
| 141 |
); |
| 142 |
|
| 143 |
foreach my $xp ( @badxpath ) { |
| 144 |
my $res; |
| 145 |
eval { $res = $root->findnodes( $xp ); }; |
| 146 |
# TEST*$badxpath |
| 147 |
ok($@, ' TODO : Add test name'); |
| 148 |
eval { $res = $root->find( $xp ); }; |
| 149 |
# TEST*$badxpath |
| 150 |
ok($@, ' TODO : Add test name'); |
| 151 |
eval { $res = $root->findvalue( $xp ); }; |
| 152 |
# TEST*$badxpath |
| 153 |
ok($@, ' TODO : Add test name'); |
| 154 |
eval { $res = $root->findnodes( encodeToUTF8( "iso-8859-1", $xp ) ); }; |
| 155 |
# TEST*$badxpath |
| 156 |
ok($@, ' TODO : Add test name'); |
| 157 |
eval { $res = $root->find( encodeToUTF8( "iso-8859-1", $xp ) );}; |
| 158 |
# TEST*$badxpath |
| 159 |
ok($@, ' TODO : Add test name'); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
{ |
| 164 |
# as reported by jian lou: |
| 165 |
# 1. getElementByTagName("myTag") is not working is |
| 166 |
# "myTag" is a node directly under root. Same problem |
| 167 |
# for findNodes("//myTag") |
| 168 |
# 2. When I add new nodes into DOM tree by |
| 169 |
# appendChild(). Then try to find them by |
| 170 |
# getElementByTagName("newNodeTag"), the newly created |
| 171 |
# nodes are not returned. ... |
| 172 |
# |
| 173 |
# this seems not to be a problem by XML::LibXML itself, but newer versions |
| 174 |
# of libxml2 (newer is 2.4.27 or later) |
| 175 |
# |
| 176 |
my $doc = XML::LibXML->createDocument(); |
| 177 |
my $root= $doc->createElement( "A" ); |
| 178 |
$doc->setDocumentElement($root); |
| 179 |
|
| 180 |
my $b= $doc->createElement( "B" ); |
| 181 |
$root->appendChild( $b ); |
| 182 |
|
| 183 |
my @list = $doc->findnodes( '//A' ); |
| 184 |
# TEST |
| 185 |
ok( scalar @list, ' TODO : Add test name' ); |
| 186 |
# TEST |
| 187 |
ok( $list[0]->isSameNode( $root ), ' TODO : Add test name' ); |
| 188 |
|
| 189 |
@list = $doc->findnodes( '//B' ); |
| 190 |
# TEST |
| 191 |
ok( scalar @list, ' TODO : Add test name' ); |
| 192 |
# TEST |
| 193 |
ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' ); |
| 194 |
|
| 195 |
|
| 196 |
# @list = $doc->getElementsByTagName( "A" ); |
| 197 |
# ok( scalar @list ); |
| 198 |
# ok( $list[0]->isSameNode( $root ) ); |
| 199 |
|
| 200 |
@list = $root->getElementsByTagName( 'B' ); |
| 201 |
# TEST |
| 202 |
ok( scalar @list, ' TODO : Add test name' ); |
| 203 |
# TEST |
| 204 |
ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' ); |
| 205 |
} |
| 206 |
|
| 207 |
{ |
| 208 |
# test potential unbinding-segfault-problem |
| 209 |
my $doc = XML::LibXML->createDocument(); |
| 210 |
my $root= $doc->createElement( "A" ); |
| 211 |
$doc->setDocumentElement($root); |
| 212 |
|
| 213 |
my $b= $doc->createElement( "B" ); |
| 214 |
$root->appendChild( $b ); |
| 215 |
my $c= $doc->createElement( "C" ); |
| 216 |
$b->appendChild( $c ); |
| 217 |
$b= $doc->createElement( "B" ); |
| 218 |
$root->appendChild( $b ); |
| 219 |
$c= $doc->createElement( "C" ); |
| 220 |
$b->appendChild( $c ); |
| 221 |
|
| 222 |
my @list = $root->findnodes( "B" ); |
| 223 |
# TEST |
| 224 |
is( scalar(@list) , 2, ' TODO : Add test name' ); |
| 225 |
foreach my $node ( @list ) { |
| 226 |
my @subnodes = $node->findnodes( "C" ); |
| 227 |
$node->unbindNode() if ( scalar( @subnodes ) ); |
| 228 |
# TEST*2 |
| 229 |
ok(1, ' TODO : Add test name'); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
{ |
| 234 |
# findnode remove problem |
| 235 |
|
| 236 |
my $xmlstr = "<a><b><c>1</c><c>2</c></b></a>"; |
| 237 |
|
| 238 |
my $doc = $parser->parse_string( $xmlstr ); |
| 239 |
my $root = $doc->documentElement; |
| 240 |
my ( $lastc ) = $root->findnodes( 'b/c[last()]' ); |
| 241 |
# TEST |
| 242 |
ok( $lastc, ' TODO : Add test name' ); |
| 243 |
|
| 244 |
$root->removeChild( $lastc ); |
| 245 |
# TEST |
| 246 |
is( $root->toString(), $xmlstr, ' TODO : Add test name' ); |
| 247 |
} |
| 248 |
|
| 249 |
# --------------------------------------------------------------------------- # |
| 250 |
sub finddoc { |
| 251 |
my $doc = shift; |
| 252 |
return unless defined $doc; |
| 253 |
my $rn = $doc->documentElement; |
| 254 |
$rn->findnodes("/"); |
| 255 |
} |