r43670 - in /branches/upstream/libxml-libxml-iterator-perl/current: Changes LICENSE MANIFEST META.yml lib/XML/LibXML/Iterator.pm lib/XML/LibXML/NodeList/Iterator.pm t/01basic.t t/02tree.t t/03list.t
dmn at users.alioth.debian.org
dmn at users.alioth.debian.org
Fri Sep 4 11:58:19 UTC 2009
Author: dmn
Date: Fri Sep 4 11:57:52 2009
New Revision: 43670
URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=43670
Log:
[svn-upgrade] Integrating new upstream version, libxml-libxml-iterator-perl (1.04)
Added:
branches/upstream/libxml-libxml-iterator-perl/current/LICENSE
branches/upstream/libxml-libxml-iterator-perl/current/META.yml
branches/upstream/libxml-libxml-iterator-perl/current/t/02tree.t
branches/upstream/libxml-libxml-iterator-perl/current/t/03list.t
Modified:
branches/upstream/libxml-libxml-iterator-perl/current/Changes
branches/upstream/libxml-libxml-iterator-perl/current/MANIFEST
branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/Iterator.pm
branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/NodeList/Iterator.pm
branches/upstream/libxml-libxml-iterator-perl/current/t/01basic.t
Modified: branches/upstream/libxml-libxml-iterator-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/Changes?rev=43670&op=diff
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/Changes (original)
+++ branches/upstream/libxml-libxml-iterator-perl/current/Changes Fri Sep 4 11:57:52 2009
@@ -1,4 +1,20 @@
Revision history for Perl extension XML::LibXML::Iterator.
+
+1.04 Fri Sep 28 2007
+ - fixed RT#29262
+ - minor documentation fixes
+
+1.03 Mon Aug 06 2007
+ - fixed RT#28688 by using the provided patch
+
+1.02 Sat Jun 23 2007
+ - fixed RT#6873
+ - cleaned up the tests
+ - more compliance with the traversal specification
+
+1.01 Sat Jun 23 2007
+ - fixed RT related bugs (though with a huge delay, sorry)
1.00 Fri Nov 8 2002
- extracted the modules from the main XML::LibXML bundle.
+
Added: branches/upstream/libxml-libxml-iterator-perl/current/LICENSE
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/LICENSE?rev=43670&op=file
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/LICENSE (added)
+++ branches/upstream/libxml-libxml-iterator-perl/current/LICENSE Fri Sep 4 11:57:52 2009
@@ -1,0 +1,15 @@
+LICENSE
+=======
+
+XML::LibXML::Iterator is free software, you may use it and distribute
+it under the same terms as Perl itself.
+
+Copyright 2002-2007, Christian Glahn, All rights reserved.
+
+DISCLAIMER
+==========
+
+THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY
+WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
+PARTICULAR PURPOSE.
+
Modified: branches/upstream/libxml-libxml-iterator-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/MANIFEST?rev=43670&op=diff
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/MANIFEST (original)
+++ branches/upstream/libxml-libxml-iterator-perl/current/MANIFEST Fri Sep 4 11:57:52 2009
@@ -3,6 +3,10 @@
lib/XML/LibXML/NodeList/Iterator.pm
Makefile.PL
MANIFEST
+LICENSE
README
t/01basic.t
+t/02tree.t
+t/03list.t
test.pl
+META.yml Module meta-data (added by MakeMaker)
Added: branches/upstream/libxml-libxml-iterator-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/META.yml?rev=43670&op=file
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/META.yml (added)
+++ branches/upstream/libxml-libxml-iterator-perl/current/META.yml Fri Sep 4 11:57:52 2009
@@ -1,0 +1,12 @@
+# http://module-build.sourceforge.net/META-spec.html
+#XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX#
+name: XML-LibXML-Iterator
+version: 1.04
+version_from: lib/XML/LibXML/Iterator.pm
+installdirs: site
+requires:
+ XML::LibXML: 1.52
+ XML::NodeFilter: 0
+
+distribution_type: module
+generated_by: ExtUtils::MakeMaker version 6.30_01
Modified: branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/Iterator.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/Iterator.pm?rev=43670&op=diff
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/Iterator.pm (original)
+++ branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/Iterator.pm Fri Sep 4 11:57:52 2009
@@ -1,4 +1,4 @@
-# $Id: Iterator.pm,v 1.1.1.1 2002/11/08 17:18:36 phish Exp $
+#
#
package XML::LibXML::Iterator;
@@ -8,22 +8,12 @@
use vars qw($VERSION);
-$VERSION = '1.00';
+$VERSION = '1.04';
use overload
- '++' => sub { $_[0]->next; $_[0]; },
- '--' => sub { $_[0]->previous; $_[0]; },
- '<>' => sub {
- if ( wantarray ) {
- my @rv = ();
- while ( $_[0]->next ){
- push @rv;
- }
- return @rv;
- } else {
- return $_[0]->next
- };
- },
+ '++' => sub { $_[0]->nextNode(); $_[0]; },
+ '--' => sub { $_[0]->previousNode(); $_[0]; },
+ '<>' => sub { return wantarray ? $_[0]->_get_all() : $_[0]->nextNode(); },
;
@@ -36,7 +26,11 @@
my $self = bless {}, $class;
$self->{FIRST} = $node;
- $self->first;
+ # $self->first;
+
+ $self->{CURRENT} = undef;
+ $self->{INDEX} = -1;
+
$self->{ITERATOR} = \&default_iterator;
$self->{FILTERS} = [];
@@ -73,33 +67,48 @@
sub current { return $_[0]->{CURRENT}; }
sub index { return $_[0]->{INDEX}; }
-sub next {
+sub next { return $_[0]->nextNode(); }
+sub previous { return $_[0]->previousNode(); }
+
+sub nextNode {
my $self = shift;
my @filters = @{$self->{FILTERS}};
my $node = undef;
- my $fv = FILTER_SKIP;
- unless ( scalar @filters > 0 ) {
- $fv = FILTER_DECLINED;
- }
- while ( 1 ) {
- $node = $self->{ITERATOR}->( $self, 1 );
- last unless defined $node;
- foreach my $f ( @filters ) {
- $fv = $f->accept_node( $node );
- last if $fv;
- }
- last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
- }
-
+
+ if ( $self->{INDEX} != -1 ) {
+ my $fv = FILTER_SKIP;
+ unless ( scalar @filters > 0 ) {
+ $fv = FILTER_DECLINED;
+ }
+
+ while ( 1 ) {
+ $node = $self->{ITERATOR}->( $self, 1 );
+ last unless defined $node;
+ foreach my $f ( @filters ) {
+ $fv = $f->accept_node( $node );
+ last if $fv;
+ }
+ last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
+ }
+ }
+ else {
+ $node = $self->first();
+ }
+
if ( defined $node ) {
$self->{CURRENT} = $node;
- $self->{INDEX}++;
+ if ( $node->isSameNode( $self->{FIRST} ) ) {
+ $self->{INDEX} = 0;
+ }
+ else {
+ $self->{INDEX}++;
+ }
}
return $node;
}
-sub previous {
+sub previousNode {
my $self = shift;
my @filters = @{$self->{FILTERS}};
my $node = undef;
@@ -189,24 +198,42 @@
}
}
else {
- return undef if $self->{CURRENT}->isSameNode( $self->{FIRST} )
- and $self->{INDEX} > 0;
-
- if ( $self->{CURRENT}->hasChildNodes ) {
- $node = $self->{CURRENT}->firstChild;
+ if ( defined $self->{CURRENT} ) {
+ return undef if $self->{CURRENT}->isSameNode( $self->{FIRST} )
+ and $self->{INDEX} > 0;
+
+ if ( $self->{CURRENT}->hasChildNodes ) {
+ $node = $self->{CURRENT}->firstChild;
+ }
+ else {
+ $node = $self->{CURRENT}->nextSibling;
+ my $pnode = $self->{CURRENT}->parentNode;
+ while ( not defined $node ) {
+ last unless defined $pnode;
+ $node = $pnode->nextSibling;
+ $pnode = $pnode->parentNode unless defined $node;
+ }
+ }
}
else {
- $node = $self->{CURRENT}->nextSibling;
- my $pnode = $self->{CURRENT}->parentNode;
- while ( not defined $node ) {
- last unless defined $pnode;
- $node = $pnode->nextSibling;
- $pnode = $pnode->parentNode unless defined $node;
- }
+ $self->{CURRENT} = $self->{FIRST};
+ $node = $self->{CURRENT};
}
}
return $node;
+}
+
+# helper function for the <> operator
+# returns all nodes that have not yet been accessed
+sub _get_all {
+ my $self = shift;
+ my @retval = ();
+ my $node;
+ while ( $node = $self->next() ) {
+ push @retval, $node;
+ }
+ return @retval;
}
1;
@@ -227,15 +254,15 @@
my $doc = XML::LibXML->new->parse_string( $somedata );
my $iter= XML::LibXML::Iterator->new( $doc );
- $iter->iterator_function( \&iterate );
+ $iter->iterator_function( \&iterator_function );
# more control on the flow
- while ( $iter->next ) {
+ while ( $iter->nextNode ) {
# do something
}
# operate on the entire tree
- $iter->iterate( \&operate );
+ $iter->iterate( \&callback_function );
=head1 DESCRIPTION
@@ -264,11 +291,11 @@
=over 4
-=item * next()
+=item * nextNode()
=item * current()
-=item * previous()
+=item * previousNode()
=back
@@ -302,7 +329,7 @@
my $iter = XML::LibXML::Iterator->new( $doc->documentElement );
# walk through the document
- while ( $iter->next() ) {
+ while ( $iter->nextNode() ) {
my $curnode = $iter->current();
print $curnode->nodeType();
}
@@ -312,13 +339,13 @@
my $curnode = $iter->current();
print $curnode->nodeType();
-Actually the functions next(), previous(), first(), last() and
+Actually the functions nextNode(), previousNode(), first(), last() and
current() do return the node which is current after the
-operation. E.g. next() moves to the next node if possible and then
+operation. E.g. nextNode() moves to the next node if possible and then
returns the node. Thus the while-loop in the example can be written
as
- while ( $iter->next() ) {
+ while ( $iter->nextNode() ) {
print $_->nodeType();
}
@@ -403,7 +430,7 @@
The following example will show how this works:
- $iter->iterate( sub {shift; map {$_->setNodeName( lc $_->nodeName ) if $_->nodeType != NAMESPACE_DECLARATION } ($_[0], $_[0]->attributes); } );
+ $iter->iterate( sub {my ($iter,$node)=@_; map {$iter->setNodeName( lc $iter->nodeName ) if $iter->nodeType != NAMESPACE_DECLARATION } ($node, $node->attributes); } );
This extra long line lowercases all tagnames and the names of the
attributes in a given subtree.
@@ -416,9 +443,9 @@
=item first()
-=item next()
-
-=item previous()
+=item nextNode()
+
+=item previousNode()
=item last()
@@ -442,11 +469,11 @@
=head1 AUTHOR
-Christian Glahn, E<lt>christian.glahn at uibk.ac.atE<gt>
+Christian Glahn, E<lt>phish at cpan.orgE<gt>
=head1 COPYRIGHT
-(c) 2002, Christian Glahn. All rights reserved.
+(c) 2002-2007, Christian Glahn. All rights reserved.
This package is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Modified: branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/NodeList/Iterator.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/NodeList/Iterator.pm?rev=43670&op=diff
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/NodeList/Iterator.pm (original)
+++ branches/upstream/libxml-libxml-iterator-perl/current/lib/XML/LibXML/NodeList/Iterator.pm Fri Sep 4 11:57:52 2009
@@ -6,21 +6,13 @@
use XML::NodeFilter qw(:results);
use vars qw($VERSION);
-$VERSION = "1.00";
+$VERSION = "1.03";
use overload
- '++' => sub { $_[0]->next; $_[0]; },
- '--' => sub { $_[0]->previous; $_[0] },
- '<>' => sub {
- if ( wantarray ) {
- my @rv = ();
- while ( $_[0]->next ){ push @rv,$_;}
- return @rv;
- } else {
- return $_[0]->next
- };
- },
-;
+ '++' => sub { $_[0]->nextNode(); $_[0]; },
+ '--' => sub { $_[0]->previousNode(); $_[0] },
+ '<>' => sub {return wantarray ? $_[0]->_get_all : $_[0]->nextNode(); },
+ ;
sub new {
my $class = shift;
@@ -28,10 +20,10 @@
my $self = undef;
if ( defined $list ) {
$self = bless [
- $list,
- 0,
- [],
- ], $class;
+ $list,
+ -1,
+ [],
+ ], $class;
}
return $self;
@@ -57,19 +49,21 @@
return FILTER_ACCEPT;
}
-sub first { $_[0][1]=0;
- my $s = scalar(@{$_[0][0]});
- while ( $_[0][1] < $s ) {
- last if $_[0]->accept_node($_[0][0][$_[0][1]]) == FILTER_ACCEPT;
- $_[0][1]++;
- }
- return undef if $_[0][1] == $s;
- return $_[0][0][$_[0][1]]; }
+sub first {
+ $_[0][1]=0;
+ my $s = scalar(@{$_[0][0]});
+ while ( $_[0][1] < $s ) {
+ last if $_[0]->accept_node($_[0][0][$_[0][1]]) == FILTER_ACCEPT;
+ $_[0][1]++;
+ }
+ return undef if $_[0][1] == $s;
+ return $_[0][0][$_[0][1]];
+}
sub last {
my $i = scalar(@{$_[0][0]})-1;
while($i >= 0){
- if ( $_[0]->accept_node($_[0][0][$i] == FILTER_ACCEPT) ) {
+ if ( $_[0]->accept_node($_[0][0][$i]) == FILTER_ACCEPT ) {
$_[0][1] = $i;
last;
}
@@ -83,17 +77,34 @@
return $_[0][0][$i];
}
-sub current { return $_[0][0][$_[0][1]]; }
-sub index { return $_[0][1]; }
-
-sub next {
- if ( (scalar @{$_[0][0]}) <= ($_[0][1] + 1)) {
+sub current {
+ if ( $_[0][1] >= 0 || $_[0][1] < scalar @{$_[0][0]} ) {
+ return $_[0][0][$_[0][1]];
+ }
+ return undef;
+}
+
+sub index {
+ if ( $_[0][1] >= 0 || $_[0][1] < scalar @{$_[0][0]} ) {
+ return $_[0][1];
+ }
+ return undef;
+}
+
+sub next { return $_[0]->nextNode(); }
+sub previous { return $_[0]->previousNode(); }
+
+sub nextNode {
+ my $nlen = scalar @{$_[0][0]};
+ if ( $nlen <= ($_[0][1] + 1)) {
return undef;
}
my $i = $_[0][1];
+ $i = -1 if $i < 0; # assure that we end up with the first
+ # element in the first iteration
while ( 1 ) {
$i++;
- return undef if $i >= scalar @{$_[0][0]};
+ return undef if $i >= $nlen;
if ( $_[0]->accept_node( $_[0][0]->[$i] ) == FILTER_ACCEPT ) {
$_[0][1] = $i;
last;
@@ -102,7 +113,7 @@
return $_[0][0]->[$_[0][1]];
}
-sub previous {
+sub previousNode {
if ( $_[0][1] <= 0 ) {
return undef;
}
@@ -121,13 +132,27 @@
sub iterate {
my $self = shift;
my $funcref = shift;
+ my $rv;
+
return unless defined $funcref && ref( $funcref ) eq 'CODE';
- $self->[1] = -1;
- my $rv;
- while ( $self->next ) {
- $rv = $funcref->( $self, $_ );
+
+ $self->[1] = -1; # first element
+ while ( my $node = $self->next ) {
+ $rv = $funcref->( $self, $node );
}
return $rv;
+}
+
+# helper function for the <> operator
+# returns all nodes that have not yet been accessed
+sub _get_all {
+ my $self = shift;
+ my @retval = ();
+ my $node;
+ while ( $node = $self->next() ) {
+ push @retval, $node;
+ }
+ return @retval;
}
1;
@@ -149,7 +174,7 @@
my $iter= XML::LibXML::NodeList::Iterator->new( $nodelist );
# more control on the flow
- while ( $iter->next ) {
+ while ( $iter->nextNode ) {
# do something
}
@@ -171,11 +196,11 @@
=head1 AUTHOR
-Christian Glahn, E<lt>christian.glahn at uibk.ac.atE<gt>
+Christian Glahn, E<lt>phish at cpan.orgE<gt>
=head1 COPYRIGHT
-(c) 2002, Christian Glahn. All rights reserved.
+(c) 2002-2007, Christian Glahn. All rights reserved.
This package is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Modified: branches/upstream/libxml-libxml-iterator-perl/current/t/01basic.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/t/01basic.t?rev=43670&op=diff
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/t/01basic.t (original)
+++ branches/upstream/libxml-libxml-iterator-perl/current/t/01basic.t Fri Sep 4 11:57:52 2009
@@ -1,49 +1,11 @@
use Test;
-BEGIN { plan tests => 32; }
+BEGIN { plan tests => 1; }
+END { if ($loaded) { ok(1);} }
-use XML::LibXML;
use XML::LibXML::Iterator;
use XML::LibXML::NodeList::Iterator;
-my $doc = XML::LibXML->new->parse_string( <<EOF );
-<test>
- text
- <foo/>
- <foo/>
- text
- <bar><kungfoo/></bar>
-</test>
-EOF
+$loaded = 1;
-print "# TREE ITERATION\n";
-my $iter = XML::LibXML::Iterator->new( $doc->documentElement );
-
-do {
- ok(1); # warn $iter->current->nodeName;
-}while ( $iter->next );
-
-do {
- ok(1); # warn $iter->current->nodeName;
-}while ( $iter->previous );
-
-
-$iter->iterate( sub { ok(1) } );
-
-$iter->first;
-ok( $iter->current->nodeName, "test" );
-
-my $n = $iter->last;
-ok( $iter->current->nodeName, "text" );
-
-
-print "# LIST ITERATION\n";
-my $nodelist = $doc->findnodes( '//foo' );
-my $nliter = XML::LibXML::NodeList::Iterator->new( $nodelist );
-
-while ( $nliter->next ) {
- ok(1);
-}
-
-$nliter->iterate( sub {ok(1)} );
Added: branches/upstream/libxml-libxml-iterator-perl/current/t/02tree.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/t/02tree.t?rev=43670&op=file
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/t/02tree.t (added)
+++ branches/upstream/libxml-libxml-iterator-perl/current/t/02tree.t Fri Sep 4 11:57:52 2009
@@ -1,0 +1,231 @@
+use Test;
+
+BEGIN { plan tests => 7; }
+
+use XML::LibXML;
+use XML::LibXML::Iterator;
+
+my $xmlstr = "<A><B/>T<B/><C><D/></C></A>";
+my $libversion;
+eval { $libversion = XML::LibXML::LIBXML_VERSION(); };
+
+sub t01_tree_first_element {
+ my $doc = XML::LibXML->new->parse_string( '<test><n1/><n2/></test>');
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ my $node = $iterator->nextNode();
+
+ unless ( defined $node ) {
+ print "# next did not return a node\n";
+ return 0;
+ }
+
+ unless ( $node->nodeName() eq 'test' ) {
+ print "# expected 'test' received '" . $node->nodeName() . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+ok(t01_tree_first_element());
+
+sub t06_set_first {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ $iterator->first();
+
+ unless ( defined $iterator->current() ) {
+ print "# there is no first node\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "A" ) {
+ print "# expected nodeName 'A' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t06_set_first());
+
+sub t07_set_last {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ $iterator->last();
+
+ unless ( defined $iterator->current() ) {
+ print "# there is no last node\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "D" ) {
+ print "# expected nodeName 'D' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+ok(t07_set_last());
+
+sub t02_loop_forward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ my $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 6 ) {
+ print "# expected 6 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ $iterator->first();
+ $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 5 ) {
+ print "# expected 5 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# wen out of scope\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "D" ) {
+ print "# expected nodeName 'D' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t02_loop_forward());
+
+sub t03_loop_backward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ my $i = 0;
+
+ $iterator->last();
+ while ( $iterator->previousNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 5 ) {
+ print "# expected 5 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t03_loop_backward());
+
+sub t04_loop_forward_backward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ my $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+ while ( $iterator->previousNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 11 ) {
+ print "# expected 11 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# went out of scope!\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "A" ) {
+ print "# expected nodeName 'A' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t04_loop_forward_backward());
+
+sub t05_run_iterate {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $iterator = XML::LibXML::Iterator->new( $doc->documentElement );
+
+ my $i = 0;
+ $iterator->iterate( sub { $i++; } );
+
+ unless ( $i == 6 ) {
+ print "# expected 6 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t05_run_iterate());
Added: branches/upstream/libxml-libxml-iterator-perl/current/t/03list.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libxml-libxml-iterator-perl/current/t/03list.t?rev=43670&op=file
==============================================================================
--- branches/upstream/libxml-libxml-iterator-perl/current/t/03list.t (added)
+++ branches/upstream/libxml-libxml-iterator-perl/current/t/03list.t Fri Sep 4 11:57:52 2009
@@ -1,0 +1,342 @@
+use Test;
+
+
+BEGIN { plan tests => 9; }
+
+use XML::LibXML;
+use XML::LibXML::NodeList::Iterator;
+
+my $xmlstr = "<A><B/><B/>T<C><D/></C></A>";
+
+sub t01_list_first_element {
+ my $doc = XML::LibXML->new->parse_string( '<test><n1/><n2/></test>');
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '/test/*' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $node = $iterator->next();
+
+ unless ( defined $node ) {
+ print "# next did not return a node\n";
+ return 0;
+ }
+
+ unless ( $node->nodeName() eq 'n1' ) {
+ print "# expected 'n1' received '" . $node->nodeName() . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+ok(t01_list_first_element());
+
+sub t06_set_first {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//A | //B | //C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ $iterator->first();
+
+ unless ( defined $iterator->current() ) {
+ print "# there is no first node\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "A" ) {
+ print "# expected nodeName 'A' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t06_set_first());
+
+sub t07_set_last {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//A | //B | //C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ $iterator->last();
+
+ unless ( defined $iterator->current() ) {
+ print "# there is no last node\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "C" ) {
+ print "# expected nodeName 'C' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+ok(t07_set_last());
+
+sub t02_loop_forward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+ my $nodelist = $doc->findnodes( '//A|//B|//C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 4 ) {
+ print "# expected 4 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# wen out of scope\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "C" ) {
+ print "# expected nodeName 'C' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ $iterator->first();
+ $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 3 ) {
+ print "# expected 3 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# wen out of scope\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "C" ) {
+ print "# expected nodeName 'C' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t02_loop_forward());
+
+sub t03_loop_backward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//A | //B | //C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $i = 0;
+
+ $iterator->last();
+ while ( $iterator->previousNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 3 ) {
+ print "# expected 3 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# went out of scope!\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "A" ) {
+ print "# expected nodeName 'A' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+ return 1;
+}
+ok(t03_loop_backward());
+
+sub t04_loop_forward_backward {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//A | //B | //C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $i = 0;
+
+ while ( $iterator->nextNode() ) {
+ $i++;
+ }
+ while ( $iterator->previousNode() ) {
+ $i++;
+ }
+
+ unless ( $i == 7 ) {
+ print "# expected 7 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ unless ( defined $iterator->current() ) {
+ print "# went out of scope!\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "A" ) {
+ print "# expected nodeName 'A' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t04_loop_forward_backward());
+
+sub t05_run_iterate {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//A | //B | //C' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $i = 0;
+ $iterator->iterate( sub { $i++; } );
+
+ unless ( $i == 4 ) {
+ print "# expected 4 iterations done " . $i . "\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t05_run_iterate());
+
+# RT#28688
+package MyFilter;
+
+use base qw(XML::NodeFilter);
+use XML::NodeFilter qw(:results);
+use UNIVERSAL;
+
+sub accept_node {
+ my $self = shift;
+ my $node = shift;
+ if (!UNIVERSAL::isa($node, 'XML::LibXML::Element')) {
+ die "invalid node in MyFilter::accept_node()";
+ }
+ return FILTER_DECLINED;
+}
+
+package main;
+
+sub t08_last_with_filter {
+ my $doc = XML::LibXML->new->parse_string( $xmlstr );
+
+ unless ( defined $doc ) {
+ print "# XML string was not parsed properly\n";
+ return 0;
+ }
+
+ my $nodelist = $doc->findnodes( '//*' );
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+ $iterator->add_filter( MyFilter->new() );
+
+ $iterator->last();
+
+ unless ( defined $iterator->current() ) {
+ print "# there is no last node\n";
+ return 0;
+ }
+
+ unless ( $iterator->current()->nodeName() eq "D" ) {
+ print "# expected nodeName 'D' received '"
+ . $iterator->current()->nodeName()
+ . "'\n";
+ return 0;
+ }
+
+ return 1;
+}
+ok(t08_last_with_filter());
+
+# END RT#28688
+
+# RT#29262
+
+sub t09_pass_nodes {
+ my $doc = XML::LibXML->new->parse_string( '<a><b/><c/></a>' );
+
+ my $nodelist = $doc->findnodes('/a/*');
+ my $iterator = XML::LibXML::NodeList::Iterator->new( $nodelist );
+
+ my $i = 0;
+ my $cstr = '';
+ $iterator->iterate( sub { my($s, $n) = @_;
+ if ( defined $n && $n->can('nodeName') ) {
+ $i++;
+ $cstr.=$n->nodeName();
+ }
+ });
+
+ unless ( $i == 2 ) {
+ print "# wrong number of nodes has been processed! $i\n";
+ return 0;
+ }
+ unless ( $cstr eq 'bc' ) {
+ print "# wrong nodes have been processed! '$cstr'\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+ok(t09_pass_nodes());
+
+# END RT#29262
More information about the Pkg-perl-cvs-commits
mailing list