Commits

Anonymous committed 5d2aba9

Modified Files:
Tag: mm_fix
06elements.t
+ normalization tests

Added Files:
Tag: mm_fix
09xpath.t
new file to test the find capabilities of XML::LibXML
(includes the test for the problem reported today on perl-xml)

Removed Files:
Tag: mm_fix
09append.t
not required anymore

Comments (0)

Files changed (3)

 use strict;
 use warnings;
 
-BEGIN { plan tests => 41 };
+BEGIN { plan tests => 45 };
 use XML::LibXML;
 
 my $foo       = "foo";
     ok( $nsAttr->ownerDocument, undef);
     # warn $elem->toString() , "\n";
 } 
+
+print "# 4. Text Append and Normalization\n";
+
+{
+    my $doc = XML::LibXML::Document->new();
+    my $t1 = $doc->createTextNode( "bar1" );
+    my $t2 = $doc->createTextNode( "bar2" );
+    my $t3 = $doc->createTextNode( "bar3" );
+    my $e  = $doc->createElement("foo");
+    $e->appendChild( $t1 );
+    $e->appendChild( $t2 );
+    $e->appendChild( $t3 );
+
+    my @cn = $e->childNodes;
+
+    # this is the correct behaviour for DOM. the nodes are still
+    # refered
+    ok( scalar( @cn ), 3 );
+    
+    $e->normalize;
+    
+    @cn = $e->childNodes;
+    ok( scalar( @cn ), 1 );
+
+    ok( $t2->parentNode, undef);
+    ok( $t3->parentNode, undef);
+}

t/09append.t

-use Test;
-BEGIN { plan tests=>11; }
-END {ok(0) unless $loaded;}
-use XML::LibXML;
-$loaded = 1;
-ok($loaded);
-
-# this script tests if insertBefore and insertAfter functions work properly
-
-my ( $dom, $root, $node1, $node2, $node3 );
-
-$dom = XML::LibXML::Document->new( "1.0", "iso-8859-1" );
-
-$root  = $dom->createElement( "R" );
-$node1 = $dom->createElement( "a" );
-$node2 = $dom->createElement( "b" );
-$node3 = $dom->createElement( "c" );
-
-$dom->setDocumentElement( $root ); 
-$root->appendChild( $node1 );
-
-my @children = $root->getChildnodes();
-ok( scalar( @children ), 1 );
-ok( ( $children[0]->getName() eq "a" ) );
-
-$root->insertBefore( $node2 , $node1 );
-@children = $root->getChildnodes();
-ok( scalar( @children ),2 ) ;
-ok( ( $children[0]->getName() eq "b" ) &&
-    ( $children[1]->getName() eq "a" ) );
-
-$root->insertAfter( $node3 , $node1 );
- @children = $root->getChildnodes();
-ok( scalar( @children ), 3 );
-ok( ( $children[0]->getName() eq "b" ) &&
-    ( $children[2]->getName() eq "c" ) &&
-    ( $children[1]->getName() eq "a" ) );
- 
-$root->removeChild( $node3 );
-@children = $root->getChildnodes();
-ok( scalar( @children ),  2  );
-ok( ( $children[0]->getName() eq "b" ) &&
-    ( $children[1]->getName() eq "a" ) );
-
-# lets switch two nodes :)
-$root->insertAfter( $node2, $node1 ); 
-@children = $root->getChildnodes();
-ok( scalar( @children ) , 2 );
-ok( ( $children[1]->getName() eq "b" ) &&
-    ( $children[0]->getName() eq "a" ) );
-
+use Test;
+use Devel::Peek;
+
+use strict;
+use warnings;
+
+BEGIN { plan tests => 20 };
+use XML::LibXML;
+
+my $xmlstring = <<EOSTR;
+<foo>
+    <bar>
+        test 1
+    </bar>
+    <bar>
+        test 2
+    </bar>
+</foo>
+EOSTR
+
+my $parser = XML::LibXML->new();
+
+my $doc = $parser->parse_string( $xmlstring );
+ok($doc);
+
+{
+    my @nodes = $doc->findnodes( "/foo/bar" );
+    ok( @nodes );
+    ok( scalar( @nodes ), 2 );
+
+    ok( $doc->isSameNode($nodes[0]->ownerDocument) );
+    my $n = $doc->createElement( "foobar" );
+
+    my $p = $nodes[1]->parentNode;
+    $p->insertBefore( $n, $nodes[1] );
+
+    ok( $p->isSameNode( $doc->documentElement ) );
+    @nodes = $p->childNodes;
+    ok( scalar( @nodes ), 6 );
+}
+
+{
+    my $result = $doc->find( "/foo/bar" );
+    ok( $result );
+    ok( $result->isa( "XML::LibXML::NodeList" ) );
+    ok( $result->size, 2 );
+
+    ok( $doc->isSameNode($$result[0]->ownerDocument) );
+
+    $result = $doc->find( "string(/foo/bar)" );
+    ok( $result );
+    ok( $result->isa( "XML::LibXML::Literal" ) );
+    ok( $result->string_value =~ /test 1/ );
+
+    $result = $doc->find( "count(/foo/bar)" );
+    ok( $result );
+    ok( $result->isa( "XML::LibXML::Number" ) );
+    ok( $result->value, 2 );
+
+    $result = $doc->find( "contains(/foo/bar[1], 'test 1')" );
+    ok( $result );
+    ok( $result->isa( "XML::LibXML::Boolean" ) );
+    ok( $result->string_value, "true" );
+
+    $result = $doc->find( "contains(/foo/bar[3], 'test 1')" );
+    ok( !$result );
+}
+
+