File: 08findnodes.t

package info (click to toggle)
libxml-libxml-perl 2.0207%2Bdfsg%2Breally%2B2.0134-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,572 kB
  • sloc: perl: 6,310; ansic: 3,851; xml: 182; sh: 21; makefile: 16
file content (249 lines) | stat: -rw-r--r-- 6,498 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use strict;
use warnings;

# Should be 45.
use Test::More tests => 45;

use XML::LibXML;

# to test if findnodes works.
# i added findnodes to the node class, so a query can be started
# everywhere.

my $file    = "example/dromeds.xml";

# init the file parser
my $parser = XML::LibXML->new();
my $dom    = $parser->parse_file( $file );

if ( defined $dom ) {
    # get the root document
    my $elem   = $dom->getDocumentElement();

    # first very simple path starting at root
    my @list   = $elem->findnodes( "species" );
    # TEST
    is( scalar(@list), 3, ' TODO : Add test name' );

    # a simple query starting somewhere ...
    my $node = $list[0];
    my @slist = $node->findnodes( "humps" );
    # TEST
    is( scalar(@slist), 1, ' TODO : Add test name' );

    # find a single node
    @list   = $elem->findnodes( "species[\@name='Llama']" );
    # TEST
    is( scalar( @list ), 1, ' TODO : Add test name' );

    # find with not conditions
    @list   = $elem->findnodes( "species[\@name!='Llama']/disposition" );
    # TEST
    is( scalar(@list), 2, ' TODO : Add test name' );


    @list   = $elem->findnodes( 'species/@name' );
    # warn $elem->toString();

    # TEST

    ok( scalar @list && $list[0]->toString() eq ' name="Camel"', ' TODO : Add test name' );

    my $x = XML::LibXML::Text->new( 1234 );
    if( defined $x ) {
        # TEST
        is( $x->getData(), "1234", ' TODO : Add test name' );
    }

    my $telem = $dom->createElement('test');
    $telem->appendWellBalancedChunk('<b>c</b>');

    finddoc($dom);
    # TEST
    ok(1, ' TODO : Add test name');
}
# TEST

ok( $dom, ' TODO : Add test name' );

# test to make sure that multiple array findnodes() returns
# don't segfault perl; it'll happen after the second one if it does
for (0..3) {
    my $doc = XML::LibXML->new->parse_string(
'<?xml version="1.0" encoding="UTF-8"?>
<?xsl-stylesheet type="text/xsl" href="a.xsl"?>
<a />');
    my @nds = $doc->findnodes("processing-instruction('xsl-stylesheet')");
}

my $doc = $parser->parse_string(<<'EOT');
<a:foo xmlns:a="http://foo.com" xmlns:b="http://bar.com">
 <b:bar>
  <a:foo xmlns:a="http://other.com"/>
 </b:bar>
</a:foo>
EOT

my $root = $doc->getDocumentElement;
my @a = $root->findnodes('//a:foo');
# TEST

is(@a, 1, ' TODO : Add test name');

my @b = $root->findnodes('//b:bar');
# TEST

is(@b, 1, ' TODO : Add test name');

my @none = $root->findnodes('//b:foo');
@none = (@none, $root->findnodes('//foo'));
# TEST

is(@none, 0, ' TODO : Add test name');

my @doc = $root->findnodes('document("example/test.xml")');
# TEST

ok(@doc, ' TODO : Add test name');
# warn($doc[0]->toString);

# this query should result an empty array!
my @nodes = $root->findnodes( "/humpty/dumpty" );
# TEST

is( scalar(@nodes), 0, ' TODO : Add test name' );


my $docstring = q{
<foo xmlns="http://kungfoo" xmlns:bar="http://foo"/>
};
 $doc = $parser->parse_string( $docstring );
 $root = $doc->documentElement;

my @ns = $root->findnodes('namespace::*');
# TEST

is(scalar(@ns), 2, ' TODO : Add test name' );

# bad xpaths
# TEST:$badxpath=4;
my @badxpath = (
    'abc:::def',
    'foo///bar',
    '...',
    '/-',
               );

foreach my $xp ( @badxpath ) {
    my $res;
    eval { $res = $root->findnodes( $xp ); };
    # TEST*$badxpath
    ok($@, ' TODO : Add test name');
    eval { $res = $root->find( $xp ); };
    # TEST*$badxpath
    ok($@, ' TODO : Add test name');
    eval { $res = $root->findvalue( $xp ); };
    # TEST*$badxpath
    ok($@, ' TODO : Add test name');
    eval { $res = $root->findnodes( encodeToUTF8( "iso-8859-1", $xp ) ); };
    # TEST*$badxpath
    ok($@, ' TODO : Add test name');
    eval { $res = $root->find( encodeToUTF8( "iso-8859-1", $xp ) );};
    # TEST*$badxpath
    ok($@, ' TODO : Add test name');
}


{
    # as reported by jian lou:
    # 1. getElementByTagName("myTag") is not working is
    # "myTag" is a node directly under root. Same problem
    # for findNodes("//myTag")
    # 2. When I add new nodes into DOM tree by
    # appendChild(). Then try to find them by
    # getElementByTagName("newNodeTag"), the newly created
    # nodes are not returned. ...
    #
    # this seems not to be a problem by XML::LibXML itself, but newer versions
    # of libxml2 (newer is 2.4.27 or later)
    #
    my $doc = XML::LibXML->createDocument();
    my $root= $doc->createElement( "A" );
    $doc->setDocumentElement($root);

    my $b= $doc->createElement( "B" );
    $root->appendChild( $b );

    my @list = $doc->findnodes( '//A' );
    # TEST
    ok( scalar @list, ' TODO : Add test name' );
    # TEST
    ok( $list[0]->isSameNode( $root ), ' TODO : Add test name' );

    @list = $doc->findnodes( '//B' );
    # TEST
    ok( scalar @list, ' TODO : Add test name' );
    # TEST
    ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' );


    # @list = $doc->getElementsByTagName( "A" );
    # ok( scalar @list );
    # ok( $list[0]->isSameNode( $root ) );

    @list = $root->getElementsByTagName( 'B' );
    # TEST
    ok( scalar @list, ' TODO : Add test name' );
    # TEST
    ok( $list[0]->isSameNode( $b ), ' TODO : Add test name' );
}

{
    # test potential unbinding-segfault-problem
    my $doc = XML::LibXML->createDocument();
    my $root= $doc->createElement( "A" );
    $doc->setDocumentElement($root);

    my $b= $doc->createElement( "B" );
    $root->appendChild( $b );
    my $c= $doc->createElement( "C" );
    $b->appendChild( $c );
    $b= $doc->createElement( "B" );
    $root->appendChild( $b );
    $c= $doc->createElement( "C" );
    $b->appendChild( $c );

    my @list = $root->findnodes( "B" );
    # TEST
    is( scalar(@list) , 2, ' TODO : Add test name' );
    foreach my $node ( @list ) {
        my @subnodes = $node->findnodes( "C" );
        $node->unbindNode() if ( scalar( @subnodes ) );
        # TEST*2
        ok(1, ' TODO : Add test name');
    }
}

{
    # findnode remove problem

    my $xmlstr = "<a><b><c>1</c><c>2</c></b></a>";

    my $doc       = $parser->parse_string( $xmlstr );
    my $root      = $doc->documentElement;
    my ( $lastc ) = $root->findnodes( 'b/c[last()]' );
    # TEST
    ok( $lastc, ' TODO : Add test name' );

    $root->removeChild( $lastc );
    # TEST
    is( $root->toString(), $xmlstr, ' TODO : Add test name' );
}

# --------------------------------------------------------------------------- #
sub finddoc {
    my $doc = shift;
    return unless defined $doc;
    my $rn = $doc->documentElement;
    $rn->findnodes("/");
}